-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (79 loc) · 3.41 KB
/
index.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
// output a typescript file using the Slice.json file
const fs = require('fs');
const path = require('path');
// loop throguh the slice and create a typescript file
// read all json files in the folder toTS
fs.readdir(path.join(__dirname, "toTS"), (err, files) => {
files.forEach(file => {
if (!file.endsWith(".json")) return;
let json = require(path.join(__dirname, "toTS", file));
convert(file.split('.')[0], json);
});
});
// keep a list of serialized functions
function convert(name, json) {
const functions = [];
// json replacer - returns a placeholder for functions
const jsonReplacer = function (key, val) {
if (typeof val === 'function') {
functions.push(val.toString());
return "{func_" + (functions.length - 1) + "}";
}
return val;
};
// regex replacer - replaces placeholders with functions
const funcReplacer = function (match, id) {
return functions[id];
};
const Packages = {};
let NonStaticClasses = [];
for (const [key, value] of Object.entries(json)) {
let splitClassName = key.split('.');
// loop through the splitClassName and create the packages
if (!value.modifiers.includes("static")) {
NonStaticClasses.push('"' + key + '"')
}
let currentClassName = "";
for (let i = 0; i < splitClassName.length; i++) {
if (i == 0) {
currentClassName = splitClassName[i];
}
else {
currentClassName += '.' + splitClassName[i];
}
eval(`if (Packages.${currentClassName} == undefined) Packages.${currentClassName} = {};`);
console.log(currentClassName)
}
}
for (const [key, value] of Object.entries(json)) {
// add the package to the packages object example: Packages.net.minecraft.block.Block = { ... }
for (const [outputType, outputObject] of Object.entries(value)) {
switch (outputType) {
case 'methods':
for (const [methodName, methodObject] of Object.entries(outputObject)) {
if (methodObject.modifiers.includes('static')) {
// write a placeholder function at Packages.${key}.${methodName}
eval(`Packages.${key}.${methodName} = function() {};`);
} else {
NonStaticClasses.push('"' + key + "." + methodName + '"');
}
}
break;
case 'fields':
for (const [fieldName, fieldObject] of Object.entries(outputObject)) {
eval(`Packages.${key}.${fieldName} = {};`);
}
break;
}
}
}
// make Packages a string without removing the functions
let PackagesString = "export const Packages = " + JSON.stringify(Packages, jsonReplacer, 4).replace(/"\{func_(\d+)\}"/g, funcReplacer) + ";\n";
NonStaticClasses = NonStaticClasses.join(' | \n ');
const javaType =
"export const Java = {\n" +
` type: (className: (${NonStaticClasses}\n )\n ) => {}\n` +
"};"
PackagesString += javaType;
fs.writeFileSync(path.join(__dirname, "toTS", name + ".ts"), PackagesString);
}