1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import { Field, FieldId } from "./Field";
import { Types } from "../server/Message";
import { CompileScript, ScriptOptions, CompiledScript } from "../client/util/Scripting";
import { Server } from "../client/Server";
export interface ScriptData {
script: string;
options: ScriptOptions;
}
export class ScriptField extends Field {
readonly script: CompiledScript;
constructor(script: CompiledScript, id?: FieldId, save: boolean = true) {
super(id);
this.script = script;
if (save) {
Server.UpdateField(this);
}
}
static FromJson(id: string, data: ScriptData): ScriptField {
const script = CompileScript(data.script, data.options);
if (!script.compiled) {
throw new Error("Can't compile script");
}
return new ScriptField(script, id, false);
}
ToScriptString() {
return "new ScriptField(...)";
}
GetValue() {
return this.script;
}
TrySetValue(): boolean {
throw new Error("Script fields currently can't be modified");
}
UpdateFromServer() {
throw new Error("Script fields currently can't be updated");
}
ToJson(): { _id: string, type: Types, data: ScriptData } {
const { options, originalScript } = this.script;
return {
_id: this.Id,
type: Types.Script,
data: {
script: originalScript,
options
},
};
}
Copy(): Field {
//Script fields are currently immutable, so we can fake copy them
return this;
}
}
|