-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpacketToTs.js
85 lines (71 loc) · 2.58 KB
/
packetToTs.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
const fs = require("fs");
const path = require("path");
function convertFile(fileName) {
const jsFile = fs.readFileSync(path.join(__dirname, `models/actorControl/${fileName}.js`), "utf8");
const packetType = `${fileName.slice(0, 1).toUpperCase()}${fileName.slice(1)}`
const implementation = /{([\s\S]+)}.?$/gm.exec(jsFile)[1];
const tsImplementation = implementation
.trim()
.replace(/struct\./g, '')
.replace(/=/g, ':')
.replace(/MachinaModels\./g, 'reader.')
.replace(/\.get/g, '.next')
.replace(/Uint/g, 'UInt')
.replace(/\(.*\)/g, '()')
.replace(/;/g, ',')
.replace(/ {4}/g, ' ');
const tsFile = `import { BufferReader } from "../../buffer-reader";
import { ${packetType} } from "../../definitions/${packetType}";
export function ${fileName}(reader: BufferReader): ${packetType} {
return {
${tsImplementation}
};
}`;
const fields = tsImplementation.split('\n').reduce((res, line) => {
const split = line.split(':');
if (split.length === 0) {
return res;
}
const fieldName = split[0].trim();
const dataAccess = split[1] ? split[1].trim() : '';
let type = 'number';
if (dataAccess.includes('getString')) {
type = 'string';
} else if (dataAccess.includes('Position3')) {
type = 'Position3';
}
return `${res}
${fieldName}: ${type};`
}, '').trim();
const position3Import = `import { Position3 } from "./Position3";
`;
const interfaceStr = `${fields.includes('Position3') ? position3Import : ''}export interface ${packetType} {
${fields}
}`
const outputDir = path.join(__dirname, 'ts');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
fs.writeFileSync(path.join(outputDir, `${fileName}.ts`), tsFile);
const typesOutputDir = path.join(outputDir, 'types');
if (!fs.existsSync(typesOutputDir)) {
fs.mkdirSync(typesOutputDir);
}
fs.writeFileSync(path.join(typesOutputDir, `${packetType}.ts`), interfaceStr);
}
function convertFiles() {
const models = path.join(__dirname, `models/actorControl/`);
const files = fs.readdirSync(models);
files.forEach(file => {
const stat = fs.lstatSync(path.join(models, file));
if (stat.isFile() && file.endsWith('.js') && !file.startsWith('_')) {
console.log(file);
convertFile(file.replace('.js', ''));
}
})
}
if (process.argv[2]) {
convertFile(process.argv[2]);
} else {
convertFiles()
}