-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_idmap.js
237 lines (216 loc) · 6.21 KB
/
gen_idmap.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* SPDX-License-Identifier: MIT
* Copyright(c) 2022 Darek Stojaczyk for pwmirage.com
*/
'use strict';
class Loading {
static show_tag() {};
static hide_tag() {};
static show_error_tag() {};
static try_cancel_tag() {};
};
const fs = require('fs');
const ROOT_URL = 'ROOT_URL/';
const DB = require('./script/db.js');
eval(fs.readFileSync('./script/util.js'));
const PWDB = eval('(' + fs.readFileSync('./script/pwdb.js') + ')');
const read_file = async (path) => {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
const save_file = async (path, content) => {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, 'utf8', (err) => {
if (err) {
return reject(err);
}
return resolve();
});
});
};
const get = async (url, { is_json, headers } = {}) => {
const ret = { ok: false, data: is_json ? {} : '' };
if (url.startsWith(ROOT_URL)) {
try {
url = __dirname + '/' + url.replace(ROOT_URL, '').split('?v=')[0];
const data = await read_file(url);
if (is_json) {
ret.data = JSON.parse(data);
} else {
ret.data = data;
}
} catch (e) { console.error(e); }
}
return ret;
}
(async () => {
PWDB.init();
const map_id = {
gs01: 1,
is01: 2,
is02: 3,
is05: 4,
is06: 5,
is07: 6,
is08: 7,
is09: 8,
is10: 9,
is11: 10,
is12: 11,
is13: 12,
is14: 13,
is15: 14,
is16: 15,
is17: 16,
is18: 17,
is19: 18,
is20: 19,
is21: 20,
is22: 21,
is23: 22,
is24: 23,
is25: 24,
is26: 25,
is27: 26,
is28: 27,
is29: 28,
is31: 29,
is32: 30,
is33: 31,
a26b: 32,
};
const usage = () => {
console.log('Usage: ./gen_idmap.js idmap triggers|spawners|recipes|tasks');
console.log(' ./gen_idmap.js db');
};
const args = process.argv.slice(2);
const op_type = args[0];
const arr_type = args[1];
if (op_type === 'idmap') {
const db = await PWDB.new_db({ pid: 0 });
if (arr_type === 'triggers') {
for (const t in db.type_info) {
if (!t.startsWith('triggers_')) {
continue;
}
const type_id = map_id[t.substring('triggers_'.length)];
const type = db.type_info[t];
const to_export = db[t].filter(o => o.id && o.id < DB.parse_id('2:0'));
const lid_entries = to_export.map(o => ({ lid: DB.serialize_id(o.id), id: o.id - 0x80100000 - type.alias_offset, type: type_id }));
for (const e of lid_entries) {
console.log(JSON.stringify(e) + ',');
}
};
} else if (arr_type === 'spawners') {
for (const t in db.type_info) {
if (!t.startsWith('spawners_')) {
continue;
}
const type_id = map_id[t.substring('spawners_'.length)];
const type = db.type_info[t];
const to_export = db[t].filter(o => o.id && o.id < DB.parse_id('2:0'));
const lid_entries = to_export.map(o => ({ lid: DB.serialize_id(o.id), id: o.id, type: type_id }));
for (const o of lid_entries) {
if (type.resource_offset && o.id >= 0x80100000 + type.alias_offset + type.resource_offset) {
o.id = o.id - 0x80100000 - type.alias_offset - type.resource_offset + 100000;
} else {
o.id = o.id - 0x80100000 - type.alias_offset;
}
console.log(JSON.stringify(o) + ',');
}
};
} else if (arr_type === 'recipes') {
const type = db.type_info.recipes;
const to_export = db.recipes.filter(r => r.id && r.id <= DB.parse_id('2:0'));
const lid_entries = to_export.map(o => ({ lid: DB.serialize_id(o.id), id: o.id - 0x80100000 - type.alias_offset, type: 69 }));
for (const e of lid_entries) {
console.log(JSON.stringify(e) + ',');
}
} else if (arr_type === 'tasks') {
const type = db.type_info.tasks;
const to_export = db.tasks.filter(r => r.id && r.id <= DB.parse_id('2:0'));
const lid_entries = to_export.map(o => ({ lid: DB.serialize_id(o.id), id: o.id - 0x80100000 - type.alias_offset, type: 69 }));
for (const e of lid_entries) {
console.log(JSON.stringify(e) + ',');
}
} else {
usage();
}
} else if (op_type === 'db') {
/* "move" all recipes into a different id space, so that all IDs in the editor
* can be unique. The recipes are still accessible at their original IDs for
* backward compatibility. */
const recipes = JSON.parse(await read_file('data/base/recipes.json'));
const alias_offsets = {};
const resource_offsets = {};
let highest_off = 0;
let current_off = 0;
for (const r of recipes) {
if (r.id >= current_off) {
current_off = r.id + 1;
}
r.id += 0x80100000;
}
alias_offsets.recipes = highest_off;
highest_off += current_off;
current_off = 0;
await save_file('data/base/recipes.json', JSON.stringify(recipes));
const tasks = JSON.parse(await read_file('data/base/tasks.json'));
for (const r of tasks) {
if (r.id >= current_off) {
current_off = r.id + 1;
}
r.id += 0x80100000 + highest_off;
}
alias_offsets.tasks = highest_off;
highest_off += current_off;
current_off = 0;
await save_file('data/base/tasks.json', JSON.stringify(tasks));
const triggers = JSON.parse(await read_file('data/base/triggers.json'));
for (const arr of triggers) {
for (const r of arr.entries) {
if (r.id >= current_off) {
current_off = r.id + 1;
}
r.id += 0x80100000 + highest_off;
}
alias_offsets['triggers_' + arr.tag] = highest_off;
highest_off += current_off;
current_off = 0;
}
await save_file('data/base/triggers.json', JSON.stringify(triggers));
const spawners = JSON.parse(await read_file('data/base/spawners.json'));
for (const arr of spawners) {
let mob_count = undefined;
for (const r of arr.entries) {
/* resources always start at id 100k+ and we can't afford to store
* them as usual */
if (r.id >= 100000) {
if (mob_count === undefined) {
mob_count = current_off;
}
/* we handle resources separately - they always come after mobs
* so mob_count stays constant at this point */
r.id -= 100000 - mob_count - 1;
}
if (r.id >= current_off) {
current_off = r.id + 1;
}
r.id += 0x80100000 + highest_off;
}
alias_offsets['spawners_' + arr.tag] = highest_off;
resource_offsets['spawners_' + arr.tag] = mob_count + 1;
highest_off += current_off;
current_off = 0;
}
await save_file('data/base/spawners.json', JSON.stringify(spawners));
await save_file('data/base/idmap_meta.json', JSON.stringify({ alias_offsets, resource_offsets }));
} else {
usage();
}
})();