-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumgen.ts
137 lines (118 loc) · 3.88 KB
/
enumgen.ts
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
import fs = require('fs');
import path = require('path');
import { ScriptWriter } from './bdsx/bdsx/writer/scriptwriter';
const targetDir = path.join(__dirname, './bdsx/bdsx/minecraft_impl');
let comment:string|null = null;
function writeComment():void {
if (comment === null) return;
switch (comment.charAt(0)) {
case '#':
case ';':
dts.writeln(`//${comment.substr(1)}`);
break;
default:
dts.writeln(`/**${comment} */`);
break;
}
comment = null;
}
let nsLevel = 0;
function closeNamespace():void {
if (nsLevel === 0) return;
do {
dts.tab(-4);
dts.writeln(`}`);
} while (--nsLevel !== 0);
}
const dts = new ScriptWriter;
const js = new ScriptWriter;
dts.generateWarningComment('the enum generator', 'bdsx/minecraft_impl/enums_ini/*.ini');
js.generateWarningComment('the enum generator', 'bdsx/minecraft_impl/enums_ini/*.ini');
js.writeln('const minecraft=require("../minecraft");');
js.writeln(`let v;`);
dts.writeln('declare module "../minecraft" {');
dts.tab(4);
const enumsDir = path.join(targetDir, 'enums_ini');
const readExp = /^[ \t]*([^\s]*)[ \t]*=[ \t]*([^\s]*)[ \t]*(?:[#;][^\r\n]*)?$/gm;
const firstIsNumber = /^[0-9]/;
let nsCheck = '';
for (const filename of fs.readdirSync(enumsDir)) {
if (!filename.endsWith('.ini')) continue;
const nsList = filename.split('.');
nsList.pop();
const ns = nsList.join('.');
const content = fs.readFileSync(path.join(enumsDir, filename), 'utf8');
readExp.lastIndex = 0;
const lines = content.split(/\r?\n/g);
js.writeln(`(minecraft.${ns}=v={}).__proto__=null;`);
if (nsCheck !== filename) {
closeNamespace();
nsCheck = ns;
nsLevel = nsList.length-1;
for (let i=0;i<nsLevel;i++) {
dts.writeln(`namespace ${nsList[i]} {`);
dts.tab(4);
}
}
const enumName = nsList[nsLevel];
dts.writeln(`enum ${enumName} {`);
dts.tab(4);
let next:number|null = 0;
for (let lineNumber=0;lineNumber<lines.length;lineNumber++) {
try {
writeComment();
let line = lines[lineNumber];
let idx = line.indexOf(';');
if (idx !== -1) {
comment = line.substr(idx+1);
line = line.substr(0, idx);
}
let value:string|number|null = null;
idx = line.indexOf('=');
if (idx !== -1) {
value = line.substr(idx+1).trim();
line = line.substr(0, idx).trim();
} else {
line = line.trim();
if (line === '') {
continue;
}
}
if (!value) {
if (next === null) {
throw Error(`needs value`);
}
dts.writeln(`${line},`);
value = next+'';
next++;
} else if (firstIsNumber.test(value)) {
dts.writeln(`${line} = ${value},`);
value = +value;
next = value + 1;
} else {
const v = JSON.parse(value);
if (typeof v !== 'number' && v !== 'string') {
throw Error(`Unexpected value`);
}
dts.writeln(`${line}=${value},`);
next = typeof value === 'number' ? value : null;
}
js.writeln(`v[v[${value}]=${JSON.stringify(line)}]=${value};`);
} catch (err) {
console.error(`${filename}:${lineNumber+1} ${err.message}`);
}
}
writeComment();
dts.tab(-4);
dts.writeln(`}`);
}
closeNamespace();
dts.tab(-4);
dts.writeln('}');
dts.writeln('export {};');
try {
fs.writeFileSync(path.join(targetDir, 'enums.d.ts'), dts.script, 'utf8');
fs.writeFileSync(path.join(targetDir, 'enums.js'), js.script, 'utf8');
} catch (err) {
console.error(err.stack);
}