-
Notifications
You must be signed in to change notification settings - Fork 17
/
prebuild.js
150 lines (123 loc) · 4.66 KB
/
prebuild.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const path = require("path");
const fs = require("fs");
const crypto = require("crypto");
function updateDllHash() {
const buff = fs.readFileSync(path.join(__dirname, "lib/deucalion/deucalion.dll"));
const buff6 = fs.readFileSync(path.join(__dirname, "lib/deucalion/deucalion_6.dll"));
const hash = crypto.createHash("sha256").update(buff).digest("hex");
const hash6 = crypto.createHash("sha256").update(buff6).digest("hex");
fs.writeFileSync(path.join(__dirname, "lib/dll.sum"), hash);
fs.writeFileSync(path.join(__dirname, "lib/dll_6.sum"), hash6);
}
function generateImportsAndProcessors(folder) {
const processorFiles = fs.readdirSync(path.join(__dirname, "./src/packet-processors/", folder));
const imports = [];
const processors = [];
processorFiles.forEach((file) => {
if (!file.endsWith(".ts")) {
return;
}
const processorName = file.replace(".ts", "");
imports.push(`import { ${processorName} } from "./${folder}/${processorName}";`);
processors.push(processorName);
});
return { imports, processors };
}
function createProcessorsLoader(filename, propertyName, folder, processorType, processorTypeName, additionalImports) {
const { imports, processors } = generateImportsAndProcessors(folder);
if (additionalImports) {
imports.push(...additionalImports);
}
const loader = `import { ${processorTypeName} } from "../models";
${imports.join("\n")}
/**
* THIS IS A GENERATED FILE, DO NOT EDIT IT BY HAND.
*
* To update it, restart the build process.
*/
export const ${propertyName}: Record<string, ${processorType}> = {
${processors.join(",\n\t")},
};
`;
fs.writeFileSync(`./src/packet-processors/${filename}.ts`, loader);
}
function generateInterfaces(processors, parentInterfaceName, excludeImports) {
const entries = [];
parentInterfaceName = parentInterfaceName || "";
const parentProcessorName = parentInterfaceName
? parentInterfaceName[0].toLowerCase() + parentInterfaceName.slice(1)
: null;
// Then actorControl packets
processors.forEach((processor) => {
const modelName = processor[0].toUpperCase() + processor.slice(1);
const subTypeField = parentProcessorName ? `\n\tsubType: "${processor}";` : "";
const entry = {
name: `${parentInterfaceName}${modelName}Message`,
importString: `import { ${modelName} } from "../definitions";`,
interfaceString: `export interface ${parentInterfaceName}${modelName}Message extends GenericMessage<${modelName}> {
type: "${parentProcessorName || processor}";${subTypeField}
}`,
};
if (excludeImports) {
delete entry.importString;
}
entries.push(entry);
});
return entries;
}
function createMessageType() {
const { processors } = generateImportsAndProcessors("processors");
const actorControlProcessors = generateImportsAndProcessors("processors/actor-control").processors;
const desynthResultProcessors = generateImportsAndProcessors("processors/desynth-result").processors;
const resultDialogProcessors = generateImportsAndProcessors("processors/result-dialog").processors;
const entries = [
...generateInterfaces(processors),
...generateInterfaces(actorControlProcessors, "ActorControl"),
...generateInterfaces(actorControlProcessors, "ActorControlSelf", true),
...generateInterfaces(actorControlProcessors, "ActorControlTarget", true),
...generateInterfaces(desynthResultProcessors, "DesynthResult"),
...generateInterfaces(resultDialogProcessors, "ResultDialog"),
];
const fileContent = `import { GenericMessage } from "./GenericMessage";
${entries
.filter((e) => e.importString)
.map((e) => e.importString)
.join("\n")}
/**
* THIS IS A GENERATED FILE, DO NOT EDIT IT BY HAND.
*
* To update it, restart the build process.
*/
${entries.map((e) => e.interfaceString).join("\n\n")}
export type Message =
| ${entries.map((e) => e.name).join("\n\t| ")};
`;
fs.writeFileSync(`./src/models/Message.ts`, fileContent);
}
createProcessorsLoader("packet-processors", "packetProcessors", "processors", "PacketProcessor", "PacketProcessor");
createProcessorsLoader(
"actor-control-packet-processors",
"actorControlPacketProcessors",
"processors/actor-control",
"SuperPacketProcessor<ActorControl>",
"SuperPacketProcessor",
[`import { ActorControl } from "../definitions";`],
);
createProcessorsLoader(
"result-dialog-packet-processors",
"resultDialogPacketProcessors",
"processors/result-dialog",
"SuperPacketProcessor<ResultDialog>",
"SuperPacketProcessor",
[`import { ResultDialog } from "../definitions";`],
);
createProcessorsLoader(
"desynth-result-packet-processors",
"desynthResultPacketProcessors",
"processors/desynth-result",
"SuperPacketProcessor<DesynthResult>",
"SuperPacketProcessor",
[`import { DesynthResult } from "../definitions";`],
);
createMessageType();
updateDllHash();