-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetLuaObject.js
175 lines (164 loc) · 5.42 KB
/
getLuaObject.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* wfApi,
* Copyright (C) 2017 Ilkka Kuosmanen
*
* This file is part of wfApi.
*
* wfApi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wfApi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wfApi. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const spawn = require("child_process").spawn;
const CustomError = require("../error").CustomError;
function getData(input, dependencies) {
return new Promise((resolve, reject) => {
let luaCmd = "lua5.3";
let luaParams = ["./luaObjectToJson.lua"];
if (process.platform === "win32") {
luaCmd = "./lua53.exe";
}
let luaToJson = spawn(luaCmd, luaParams, { cwd: __dirname });
// Change the sparse arrays that can contain a negative index to string keys.
let r = /\[([-\d]{1,2})\]/g;
input = input.replace(r, '["$1"]');
let removeNull = /local null = {}.*$/gim;
input = input.replace(removeNull, "");
let removeNull2 = /null = null.*$/gim;
input = input.replace(removeNull2, "");
let removeWikiCommentBlock = /--\s?\[=+\[(?:.|\n)*?--]=+]/gim;
input = input.replace(removeWikiCommentBlock, "");
let removeComments = /--.*$/gim;
input = input.replace(removeComments, "");
let replaceModuleShared = /require\(\'Module:Shared\'\)/gim;
input = input.replace(replaceModuleShared, "require('shared')");
let replaceModuleTable = /require\(\'Module:Table\'\)/gim;
input = input.replace(replaceModuleTable, "require('module_table')");
// Special fixes to weapon data table... I expect these to break rather soon.....
input = input.replace(
`local attack = weaponEntry['Attack'..i] or weaponEntry.attack[i]`,
`local attack = weaponEntry['Attack'..i]`
);
input = input.replace(
`weaponEntry['Attack'..i], weaponEntry.attack[i] = attack, attack`,
`weaponEntry['Attack'..i] = attack`
);
// dependency to "Warframes" cache from "WarframesConclave" cache
if (dependencies) {
let replaceDependency = /mw\.loadData\([\'\"](.*)[\'\"]\)/gim;
let result;
while ((result = replaceDependency.exec(input)) !== null) {
const dependency = dependencies.find((v) => v.module === result[1]);
input = input.replace(
`mw.loadData('${dependency.module}')`,
dependency.cache.src.replace("return", "")
);
}
}
let replaceVersion2 = /return VersionData/gim;
input = input.replace(
replaceVersion2,
`
for i, up in ipairs(VersionData.Versions) do
if VersionData.Versions[i][1] then
VersionData.Versions[i][1] = nil
end
end
return VersionData`
);
let replaceMission2 = /return MissionData/gim;
input = input.replace(
replaceMission2,
`
MissionData.by = nil
MissionData.dictionary = nil
MissionData.vars = nil
return MissionData`
);
input = input.replace(
/MissionData.vars\n\s*{\n\s*'Node',\n\s*'Enemy',\n\s*'Planet',\n\s*'Type',\n\s*'Tileset',\n\s*'Tier',\n\s*'LinkName',\n\s*'Drops',\n\s*'Pic',\n\s*'ObjectiveDetails',\n\s*}/gim,
"MissionData.vars"
);
luaToJson.stderr.pipe(process.stderr);
luaToJson.stdin.setEncoding("utf-8");
luaToJson.stdin.write(input);
luaToJson.stdin.end();
let data = "";
luaToJson.stdout.on("data", (d) => {
data += d;
});
let err = "";
luaToJson.stderr.on("data", (d) => {
err += d;
});
luaToJson.stderr.on("end", (d) => {
if (err.length > 0) {
return reject(err);
}
});
luaToJson.stdout.on("end", (c) => {
let d = "";
if (data.length === 0) {
return reject(new CustomError("Lua parsing failed."));
}
try {
d = JSON.parse(data);
return resolve(d);
} catch (e) {
return reject(e);
}
});
});
}
function getDataNoClean(input) {
return new Promise((resolve, reject) => {
let luaCmd = "lua5.3";
let luaParams = ["./luaObjectToJson.lua"];
if (process.platform === "win32") {
luaCmd = "./lua53.exe";
}
let luaToJson = spawn(luaCmd, luaParams, { cwd: __dirname });
luaToJson.stderr.pipe(process.stderr);
luaToJson.stdin.setEncoding("utf-8");
luaToJson.stdin.write(input);
luaToJson.stdin.end();
let data = "";
luaToJson.stdout.on("data", (d) => {
data += d;
});
let err = "";
luaToJson.stderr.on("data", (d) => {
err += d;
});
luaToJson.stderr.on("end", (d) => {
if (err.length > 0) {
return reject(err);
}
});
luaToJson.stdout.on("end", (c) => {
let d = "";
if (data.length === 0) {
return reject(new CustomError("Lua parsing failed."));
}
try {
d = JSON.parse(data);
return resolve(d);
} catch (e) {
return reject(e);
}
});
});
}
module.exports = {
getData,
getDataNoClean,
};