-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_game_data.js
More file actions
342 lines (278 loc) · 11 KB
/
update_game_data.js
File metadata and controls
342 lines (278 loc) · 11 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import fs from "fs"
const get_file = (path) => {
return fs.readFileSync(`./shattered-pixel-dungeon/${path}`, "utf-8")
}
if (!fs.existsSync("./shattered-pixel-dungeon")) throw `Please ensure that there's a folder called "shattered-pixel-dungeon" in the same directory as this script.`
/* Metadata */
const SUPPORTED_GAME_VERSION = get_file("build.gradle").match(/appVersionCode = (.*)/)[1]
let ERRORS = []
/* Items */
let ITEMS = []
let GENERIC_ITEMS = [] // ex: Scroll of ISAZ, Amethyst Ring, Crimson Potion, etc
get_file("core/src/main/assets/messages/items/items.properties").matchAll(/items\.(.*).name\=(.*)\n/g).forEach(item => {
const item_id = item[1].replace(/_/g, " ")
const item_name = item[2].toLowerCase()
if (
item_id.includes("curses") ||
item_id.includes("glyphs") ||
item_id.includes("enchantments") ||
item_id.includes("ability") ||
item_id.includes("pasty.") ||
item_id.includes(".staff") ||
item_id.includes("$")
) return
ITEMS.push({
id: item_id,
name: item_name
})
})
get_file("core/src/main/assets/messages/plants/plants.properties").matchAll(/(plants\..*)\$seed\.name=(.*)/g).forEach(plant => {
const item_id = plant[1].replace(/_/g, " ")
const item_name = plant[2]
ITEMS.push({
id: item_id,
name: item_name,
seed: true
})
})
ITEMS.map(item => item.id).forEach(item_id => {
let item_path = item_id.replace(/\./g, "/").split("/").slice(0,-1).join("/")
let item_name = item_id.replace(/'S/g, "s").replace(/'/g, "").replace(/ /g, "").split(".").pop()
if (item_path.includes("plants")) return
fs.readdirSync(`./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/${item_path}`)
.filter(file => file.endsWith(".java"))
.forEach(file => {
// We have to check every file because the item name is lowercase and the file names are capitalized
if (file.toLowerCase().replace(".java", "") == item_name) {
let data = fs.readFileSync(`./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/${item_path}/${file}`, "utf-8")
let spritesheet_id = data.match(/image = ItemSpriteSheet\.(.*);/)
spritesheet_id = spritesheet_id ? spritesheet_id[1] : null
let spritesheet_icon_id = data.match(/icon = ItemSpriteSheet\.Icons\.(.*);/)
spritesheet_icon_id = spritesheet_icon_id ? spritesheet_icon_id[1] : null
let item = ITEMS.find(item => item.id == item_id)
if (!item) throw "Item not found: " + item_id
item.game_id = (
"com.shatteredpixel.shatteredpixeldungeon.items." +
item_id.split(".").slice(0,-1).join(".") + "." + file.replace(".java", "")
).replace("..", ".")
item.sprite = {
id: spritesheet_id,
pos: null,
clip: null,
icon: {
id: spritesheet_icon_id,
pos: null,
clip: null
}
}
}
})
})
fs.readdirSync(`./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/plants`)
.filter(file => file.endsWith(".java"))
.forEach(file => {
if (file == "Plant.java") return
if (file == "BlandfruitBush.java") return
let data = get_file(`core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/plants/${file}`)
let spritesheet_id = data.match(/image = ItemSpriteSheet\.(.*);/)
spritesheet_id = spritesheet_id ? spritesheet_id[1] : null
if (spritesheet_id) {
const item_id = `plants.${spritesheet_id.replace("SEED_", "").toLowerCase()}`
let item = ITEMS.find(item => item.id == item_id)
if (!item) throw "Item not found: " + item_id
item.game_id = (
"com.shatteredpixel.shatteredpixeldungeon." +
item_id.split(".").slice(0,-1).join(".") + "." + file.replace(".java", "") +
"$Seed"
)
item.sprite = {
id: spritesheet_id,
pos: null,
clip: null,
icon: {
id: null,
pos: null,
clip: null
}
}
} else {
ERRORS.push(`Item sprite for plant ${file} not found. This might be a new plant or a bug.`)
}
})
// Map exotic items to their regular counterparts
const exotic_ids = [
...get_file("core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ExoticScroll.java").matchAll(/exoToReg\.put\((.*), (.*)\);/gm),
...get_file("core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/exotic/ExoticPotion.java").matchAll(/exoToReg\.put\((.*), (.*)\);/gm)
]
exotic_ids.forEach(exotic_id => {
const exotic = exotic_id[1].replace(".class", "").trim()
const regular = exotic_id[2].replace(".class", "").trim()
let exotic_item = ITEMS.find(item => item.game_id !== undefined && item.game_id.includes(exotic))
let regular_item = ITEMS.find(item => item.game_id !== undefined && item.game_id.includes(regular))
if (exotic_item && regular_item) {
exotic_item.regular_id = regular_item.id
}
})
/* Sprites */
const xy = (x, y) => (x-1) + 16 * (y-1);
const rxy = (n) => [((n % 16) + 1), (Math.floor(n / 16) + 1)];
const ItemSpriteSheet = get_file("core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/ItemSpriteSheet.java")
let CATEGORIES = {
"SOMETHING": 0,
"DARTS": 160 // Temporary fix: https://github.com/00-Evan/shattered-pixel-dungeon/issues/2043
}
const calculate_sprite_position_num = (pos) => {
let [x, y] = pos
.match(/xy\((.*),(.*)\)/)
.slice(1, 3)
.map(n => parseInt(n.trim()))
return ((x - 1) + 16 * (y - 1))
}
const calculate_position = (pos) => {
let [category, offset] = pos.match(/(.*)\+(.*)/).slice(1,3).map(n => n.trim())
let raw = CATEGORIES[category] + parseInt(offset.trim())
let [x, y] = rxy(raw)
return {
x: x,
y: y,
raw: raw,
}
}
ItemSpriteSheet.matchAll(/(private|public) static final int (.*)\s*=\s*(.*);/g)
.forEach(item => {
const is_category = item[1] == "private"
const item_id = item[2].trim()
const sprite_position = item[3].trim()
if (["WIDTH", "SIZE", "SOMETHING"].includes(item_id)) return
if (is_category) {
// If the category already exists, we're near the end of the file which means it's an icon category
if (CATEGORIES[item_id]) {
CATEGORIES[`${item_id}_ICON`] = calculate_sprite_position_num(sprite_position)
} else {
CATEGORIES[item_id] = calculate_sprite_position_num(sprite_position)
}
} else {
let item = ITEMS.find(item => item.sprite !== undefined && item.sprite.id == item_id)
if (item) {
item.sprite.pos = calculate_position(sprite_position)
} else {
let item_icon = ITEMS.find(item => item.sprite !== undefined && item.sprite.icon.id == item_id)
if (item_icon) {
item_icon.sprite.icon.pos = calculate_position(sprite_position)
} else {
if (item_id.includes("POTION_") || item_id.includes("SCROLL_") || item_id.includes("RING_") || item_id.includes("EXOTIC_")) {
GENERIC_ITEMS.push({
id: item_id,
sprite: {
id: item_id,
pos: calculate_position(sprite_position),
clip: null
}
})
} else {
ERRORS.push(`Item sprite for ${item_id} not found. This might be a new item or a bug.`)
}
}
}
}
})
/* Sprite Clipping */
ItemSpriteSheet.matchAll(/assignItemRect\((.*),(.*),(.*)\);/g)
.forEach(item_data => {
const item_id = item_data[1].trim()
const x = item_data[2].trim()
const y = item_data[3].trim()
if (item_id == "i") return // For loops, handled below
let item = ITEMS.find(item => item.sprite !== undefined && item.sprite.id == item_id)
if (item) {
item.sprite.clip = {
x: parseInt(x),
y: parseInt(y),
}
} else {
if (item_id.includes("POTION_") || item_id.includes("SCROLL_") || item_id.includes("RING_") || item_id.includes("EXOTIC_")) {
let generic_item = GENERIC_ITEMS.find(item => item.sprite.id == item_id)
generic_item.sprite.clip = {
x: parseInt(x),
y: parseInt(y),
}
} else {
ERRORS.push(`Item sprite clipping for ${item_id} not found. This might be a new item or a bug.`)
}
}
})
ItemSpriteSheet.matchAll(/assignIconRect\((.*),(.*),(.*)\);/g)
.forEach(item_data => {
const item_id = item_data[1].trim()
const x = item_data[2].trim()
const y = item_data[3].trim()
if (item_id == "i") return // For loops, handled below
let item = ITEMS.find(item => item.sprite !== undefined && item.sprite.icon.id == item_id)
if (item) {
item.sprite.icon.clip = {
x: parseInt(x),
y: parseInt(y),
}
} else {
ERRORS.push(`Icon sprite clipping for ${item_id} not found. This might be a new item or a bug.`)
}
})
// Handle for loops
ItemSpriteSheet.matchAll(/for \(int i = (.*); i < (.*)\+(.*); i\+\+\)\s*assignItemRect\(i, (.*), (.*)\);/g)
.forEach(item_data => {
const item_id = item_data[1]
const item_id_regex = `public static final int (.*)= (${item_data[1] == "BREWS" ? "ELIXIRS|BREWS" : item_id})\+(.*);` // Fixes a bug with elixirs
const x = item_data[4].trim()
const y = item_data[5].trim()
ItemSpriteSheet.matchAll(item_id_regex)
.forEach(item_fr => {
const item_id = item_fr[1].trim()
let item = ITEMS.find(item => item.sprite !== undefined && item.sprite.id == item_id)
if (item) {
item.sprite.clip = {
x: parseInt(x),
y: parseInt(y),
}
} else {
if (item_id.includes("POTION_") || item_id.includes("SCROLL_") || item_id.includes("RING_") || item_id.includes("EXOTIC_")) {
let generic_item = GENERIC_ITEMS.find(item => item.sprite !== undefined && item.sprite.id == item_id)
if (generic_item) {
generic_item.sprite.clip = {
x: parseInt(x),
y: parseInt(y),
}
} else {
ERRORS.push(`Generic item sprite clipping for ${item_id} not found. This might be a new item or a bug.`)
}
} else {
ERRORS.push(`Item sprite clipping for ${item_id} not found. This might be a new item or a bug.`)
}
}
})
})
/* Enchantments / Glyphs */
let ENCHANTMENTS = [
...fs.readdirSync("./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/enchantments")
.map(file => `weapon.enchantments.${file.replace(".java", "")}`),
...fs.readdirSync("./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/curses")
.map(file => `weapon.curses.${file.replace(".java", "")}`),
]
let GLYPHS = [
...fs.readdirSync("./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/glyphs")
.map(file => `armor.glyphs.${file.replace(".java", "")}`),
...fs.readdirSync("./shattered-pixel-dungeon/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/curses")
.map(file => `armor.curses.${file.replace(".java", "")}`),
]
/* Export */
console.log("// This file was automatically generated by update_game_data.js. Do not edit it manually.")
console.log(`// Generated on ${new Date().toLocaleString()}`)
console.log()
console.log(`const SUPPORTED_GAME_VERSION = ${SUPPORTED_GAME_VERSION};`)
console.log()
console.log(`const GENERIC_ITEMS = ${JSON.stringify(GENERIC_ITEMS, null, 2)};`)
console.log(`const ITEMS = ${JSON.stringify(ITEMS, null, 2)};`)
console.log()
console.log(`const ENCHANTMENTS = ${JSON.stringify(ENCHANTMENTS, null, 2)};`)
console.log(`const GLYPHS = ${JSON.stringify(GLYPHS, null, 2)};`)
console.log()
console.log(`const ERRORS = ${JSON.stringify(ERRORS, null, 2)};`)