-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCraftopiaServerSetting.ts
More file actions
104 lines (91 loc) · 3.13 KB
/
Copy pathCraftopiaServerSetting.ts
File metadata and controls
104 lines (91 loc) · 3.13 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { path } from "./deps.ts";
export type EnumValues = typeof CraftopiaServerSetting.enumValuesMap;
export type EnumValue<T extends EnumKey> = keyof EnumValues[T];
export type EnumKey = keyof EnumValues;
export class CraftopiaServerSetting {
serverDirectory: string;
serverSetting: string;
static enumValuesMap = {
difficulty: {
Easy: 0,
Normal: 1,
Hard: 2,
VeryHard: 3,
},
gameMode: {
NormalWorld: 1,
CreativeWorld_Build: 2,
CreativeWorld_Play: 3,
},
} as const;
static enumKeys = Object.keys(
CraftopiaServerSetting.enumValuesMap,
) as EnumKey[];
constructor(serverDirectory: string) {
this.serverDirectory = serverDirectory;
this.serverSetting = path.join(this.serverDirectory, "ServerSetting.ini");
}
read() {
const data = new TextDecoder().decode(
Deno.readFileSync(this.serverSetting),
);
const lines = data.split("\n");
const result: { [key: string]: string } = {};
for (const line of lines) {
if (/^\s*;/.test(line)) continue;
if (!/=/.test(line)) continue;
const [key, value] = line.split("=");
result[key] = value;
}
return result;
}
static possibleEnumValues<K extends EnumKey>(key: K): EnumValue<K>[];
static possibleEnumValues(key: string): EnumValue<EnumKey>[];
static possibleEnumValues(key: string) {
return Object.keys(CraftopiaServerSetting.enumValuesMap[key as EnumKey]) ||
[];
}
static getEnumValue<K extends EnumKey>(
key: K,
value: number,
): EnumValue<K> | undefined;
static getEnumValue(
key: string,
value: number,
): EnumValue<EnumKey> | undefined;
static getEnumValue(key: string, value: number) {
const values = (CraftopiaServerSetting.enumValuesMap as any)[key];
if (values) {
return Object.keys(values).find((k) => values[k] === value) || undefined;
}
}
static isEnumKey(key: string) {
return CraftopiaServerSetting.enumKeys.includes(key as EnumKey);
}
static canSetEnum(key: string, value: string) {
return (CraftopiaServerSetting.enumValuesMap as any)[key]?.[value] != null;
}
setEnum<K extends EnumKey>(key: K, value: EnumValue<K>): void;
setEnum(key: string, value: string): void;
setEnum(key: string, value: string) {
if (CraftopiaServerSetting.canSetEnum(key, value)) {
this.write(
"difficulty",
(CraftopiaServerSetting.enumValuesMap as any)[key][value].toString(),
);
}
}
setName(name: string) {
this.write("name", name);
}
write(key: string, value: string) {
const data = new TextDecoder().decode(
Deno.readFileSync(this.serverSetting),
);
const newData = data.replace(
new RegExp(`^${key}=.*$`, "m"),
`${key}=${value}`,
);
Deno.writeFileSync(this.serverSetting, new TextEncoder().encode(newData));
}
}