Skip to content

Commit c74c6f6

Browse files
committed
移动了程序下载的位置(编译→评测)
除此以外,在Config.ts中增添了Or验证器。
1 parent 243223d commit c74c6f6

File tree

8 files changed

+258
-442
lines changed

8 files changed

+258
-442
lines changed

src/Config.ts

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
11
import * as TOML from "@iarna/toml";
2-
import { Type, plainToClass } from "class-transformer";
2+
import { plainToClass, Type } from "class-transformer";
33
import {
4+
isBoolean,
45
IsBoolean,
56
IsInt,
67
IsNotEmpty,
78
IsNumber,
89
IsOptional,
910
IsPositive,
11+
isString,
1012
IsString,
1113
Min,
14+
ValidateBy,
1215
ValidateNested,
1316
validateSync,
17+
ValidationOptions,
1418
} from "class-validator";
1519
import fs from "fs";
1620
import { getLogger } from "log4js";
21+
22+
function Or(...constraints: ((value: unknown) => boolean)[]): PropertyDecorator;
23+
function Or(
24+
validationOptions?: ValidationOptions,
25+
...constraints: ((value: unknown) => boolean)[]
26+
): PropertyDecorator;
27+
function Or(
28+
validationOptions?: ValidationOptions | ((value: unknown) => boolean),
29+
...constraints: ((value: unknown) => boolean)[]
30+
): PropertyDecorator {
31+
if (typeof validationOptions === "function") {
32+
constraints = [validationOptions, ...constraints];
33+
validationOptions = undefined;
34+
}
35+
return ValidateBy(
36+
{
37+
name: "or",
38+
constraints,
39+
validator: {
40+
validate: (value, validationArguments) => {
41+
if (validationArguments) {
42+
for (const constraint of validationArguments.constraints) {
43+
if (constraint(value)) {
44+
return true;
45+
}
46+
}
47+
}
48+
return false;
49+
},
50+
},
51+
},
52+
validationOptions
53+
);
54+
}
55+
1756
const logger = getLogger("ConfigService");
1857
const configToml = fs.readFileSync("config/config.toml").toString();
1958
export class LanguageConfig {
@@ -53,15 +92,18 @@ export class LanguageConfig {
5392
@IsString()
5493
@IsNotEmpty()
5594
ise!: string;
56-
@IsString()
95+
@Or(isBoolean, isString)
5796
@IsNotEmpty()
58-
shell!: string; //TODO: string or boolean
97+
shell!: string | boolean;
5998
@IsString()
6099
@IsNotEmpty()
61100
verilog!: string;
62101
@IsString()
63102
@IsNotEmpty()
64103
vhdl!: string;
104+
@IsString()
105+
@IsNotEmpty()
106+
impact!: string;
65107
}
66108
export class JailConfig {
67109
@IsString()

src/Spawn/Language/CMP.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class CMP extends Language {
2424
return [getConfig().language.ojcmp];
2525
}
2626

27-
execOptionGenerator(): RunOption {
27+
pragramOptionGenerator(): RunOption {
2828
const binPath = getConfig().language.ojcmp;
2929
return {
3030
skip: false,

src/Spawn/Language/PlainText.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class PlainText extends Language {
2727
return [];
2828
}
2929

30-
execOptionGenerator(): RunOption {
30+
pragramOptionGenerator(): RunOption {
3131
const binPath = path.join(this.compileDir, this.src);
3232
return {
3333
skip: false,

src/Spawn/Language/Verilog.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ export class Verilog extends Language {
1919
};
2020
}
2121

22-
execOptionGenerator(): RunOption {
22+
pragramOptionGenerator(): RunOption {
2323
return {
24-
skip: true,
24+
skip: false,
25+
command: getConfig().language.impact,
26+
args: ["-batch", "main.cmd"],
2527
};
2628
}
2729

src/Spawn/Language/decl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ export abstract class Language {
4343
abstract get srcFileName(): string;
4444
abstract get compiledFiles(): string[];
4545
abstract compileOptionGenerator(): RunOption;
46-
abstract execOptionGenerator(): RunOption;
46+
abstract pragramOptionGenerator(): RunOption;
4747
}

src/Utilities/ExecutableAgent.ts

+18-22
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getConfiguredLanguage } from "../Spawn/Language";
1010
import { getLogger } from "log4js";
1111
import { FileHandle } from "fs/promises";
1212
import { hengSpawn, HengSpawnOption } from "../Spawn";
13-
import { MeteredChildProcess, MeterResult } from "../Spawn/Meter";
13+
import { MeterResult } from "../Spawn/Meter";
1414

1515
const compileCachedJudge = new Map<string, string>();
1616
export const SourceCodeName = "srcCode";
@@ -29,23 +29,23 @@ export class ExecutableAgent {
2929
protected logger = getLogger("ExecutableAgent");
3030

3131
constructor(
32-
private readonly execType: ExecType,
33-
private readonly excutable: Executable
32+
public readonly execType: ExecType,
33+
public readonly executable: Executable
3434
) {
3535
this.judgeHash = crypto
3636
.createHash("sha256")
3737
.update(
3838
JSON.stringify({
3939
execType,
40-
excutable,
40+
excutable: executable,
4141
})
4242
)
4343
.digest("hex");
4444
this.configuredLanguage = getConfiguredLanguage(
45-
this.excutable.environment.language,
45+
this.executable.environment.language,
4646
{
4747
execType: this.execType,
48-
excutable: this.excutable,
48+
excutable: this.executable,
4949
compileDir: "",
5050
}
5151
);
@@ -105,7 +105,7 @@ export class ExecutableAgent {
105105
await this.fileAgent.init(false);
106106
this.fileAgent.add(
107107
SourceCodeName,
108-
this.excutable.source,
108+
this.executable.source,
109109
this.configuredLanguage.srcFileName
110110
);
111111
}
@@ -196,16 +196,16 @@ export class ExecutableAgent {
196196
gid: getConfig().judger.gid,
197197
timeLimit:
198198
languageRunOption.spawnOption?.timeLimit ??
199-
this.excutable.limit.compiler.cpuTime,
199+
this.executable.limit.compiler.cpuTime,
200200
memoryLimit:
201201
languageRunOption.spawnOption?.memoryLimit ??
202-
this.excutable.limit.compiler.memory,
202+
this.executable.limit.compiler.memory,
203203
pidLimit:
204204
languageRunOption.spawnOption?.pidLimit ??
205205
getConfig().judger.defaultPidLimit,
206206
fileLimit:
207207
languageRunOption.spawnOption?.fileLimit ??
208-
this.excutable.limit.compiler.output,
208+
this.executable.limit.compiler.output,
209209
};
210210

211211
const subProc = hengSpawn(command, args, spawnOption);
@@ -253,15 +253,12 @@ export class ExecutableAgent {
253253
* @param cwd
254254
* @returns
255255
*/
256-
async exec(
257-
cwd?: string,
258-
stdio?: CompleteStdioOptions,
259-
args?: string[]
260-
): Promise<MeteredChildProcess> {
256+
async program(cwd?: string, stdio?: CompleteStdioOptions, args?: string[]) {
261257
this.checkInit();
262-
const languageRunOption = this.configuredLanguage.execOptionGenerator();
258+
const languageRunOption =
259+
this.configuredLanguage.pragramOptionGenerator();
263260
if (languageRunOption.skip) {
264-
throw new Error("Can't skip exec");
261+
throw new Error("Can't skip pragram");
265262
}
266263
if (!this.compiled && !this.compileCached) {
267264
throw new Error("Please compile first");
@@ -285,20 +282,19 @@ export class ExecutableAgent {
285282
gid: getConfig().judger.gid,
286283
timeLimit:
287284
languageRunOption.spawnOption?.timeLimit ??
288-
this.excutable.limit.runtime.cpuTime,
285+
this.executable.limit.runtime.cpuTime,
289286
memoryLimit:
290287
languageRunOption.spawnOption?.memoryLimit ??
291-
this.excutable.limit.runtime.memory,
288+
this.executable.limit.runtime.memory,
292289
pidLimit:
293290
languageRunOption.spawnOption?.pidLimit ??
294291
getConfig().judger.defaultPidLimit,
295292
fileLimit:
296293
languageRunOption.spawnOption?.fileLimit ??
297-
this.excutable.limit.runtime.output,
294+
this.executable.limit.runtime.output,
298295
};
299296

300-
const subProc = hengSpawn(command, args, spawnOption);
301-
return subProc;
297+
return hengSpawn(command, args, spawnOption).result;
302298
}
303299
}
304300

0 commit comments

Comments
 (0)