-
Notifications
You must be signed in to change notification settings - Fork 0
/
twewy.mjs
285 lines (234 loc) · 11.1 KB
/
twewy.mjs
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
import * as byteops from './byteops.mjs'
import {PIN_DATA} from './twewy-pins.mjs'
export * from './twewy-pins.mjs';
const TWEWY_SAVE_FILE_MAGIC_HEADER = '\x19SubarAsikiKonosEkai000000'
const TWEWY_SAVE_FILE_MAGIC_HEADER_SIZE = TWEWY_SAVE_FILE_MAGIC_HEADER.length
const TWEWY_SAVE_FILE_DATA_INTEGRITY_HASH_SIZE = 32
const TWEWY_SAVE_FILE_BODY_OFFSET = (TWEWY_SAVE_FILE_MAGIC_HEADER_SIZE +
TWEWY_SAVE_FILE_DATA_INTEGRITY_HASH_SIZE)
export function readSaveFile(saveFileBuffer) {
if (!validateSaveFile(saveFileBuffer)) return false;
const saveFileData = {}
saveFileData.playerCharacterStats = parsePlayerCharacterStats(saveFileBuffer);
saveFileData.pinStockpile = parsePinStockpile(saveFileBuffer);
saveFileData.pinMastered = parsePinMastered(saveFileBuffer);
return saveFileData
}
export async function writeSaveFile(saveFileBuffer, saveFileData) {
packPlayerCharacterStats(saveFileBuffer, saveFileData.playerCharacterStats);
packPinStockpile(saveFileBuffer, saveFileData.pinStockpile);
packPinMastered(saveFileBuffer, saveFileData.pinMastered);
await packSaveDataIntegrityHash(saveFileBuffer);
return saveFileBuffer
}
async function computeSaveDataIntegrityHash(saveFileBuffer) {
const saveFileBody = new Uint8Array(saveFileBuffer, TWEWY_SAVE_FILE_BODY_OFFSET)
const hashBuffer = await crypto.subtle.digest('SHA-256', saveFileBody);
// TWEWY specific bit-mangling and stored little-endian
const hashBytes = (new Uint8Array(hashBuffer)).reverse().map(e=>e^0xff)
return hashBytes
}
async function packSaveDataIntegrityHash(saveFileBuffer) {
const dataIntegrityHash = await computeSaveDataIntegrityHash(saveFileBuffer);
const hashSectionSubarray = new Uint8Array(saveFileBuffer,
TWEWY_SAVE_FILE_MAGIC_HEADER_SIZE,
TWEWY_SAVE_FILE_DATA_INTEGRITY_HASH_SIZE)
dataIntegrityHash.forEach((e,i) => {hashSectionSubarray[i] = e})
return
}
function validateSaveFile(saveFileBuffer) {
const magicHeader = (new TextDecoder('utf-8')
.decode(new Uint8Array(saveFileBuffer,
0, TWEWY_SAVE_FILE_MAGIC_HEADER_SIZE)));
if (magicHeader === TWEWY_SAVE_FILE_MAGIC_HEADER) {
return true
}
else {
return false
}
// const saveFileDataHash = new Uint8Array(saveFileBuffer,
// TWEWY_SAVE_FILE_MAGIC_HEADER_SIZE,
// TWEWY_SAVE_FILE_DATA_INTEGRITY_HASH_SIZE)
// const computedDataHash = await computeTwewySaveDataIntegrityHash(saveFileBuffer);
// if (byteops.byteArraysAreEqual(saveFileDataHash, computedDataHash)) {
// console.log("TWEWY save data has matching data integrity hash.")
// }
// else return false
}
/**************************************** Character Stats ****************************************/
const SAVE_FILE_PLAYER_STATS_STRUCT = {
/* magic header goes here, 26 bytes */
/* data hash goes here, 32 bytes */
nekuCurrentLevel: {type: 'Uint16'}, // max=100
/* skip over unknown 4 bytes here */
nekuMaxLevel: {type: 'Uint16', offset:'+4'}, // max=100
nekuExp: {type: 'Uint32'},
money: {type: 'Uint32'},
nekuBaseAttack: {type: 'Uint16'},
nekuBaseDefense: {type: 'Uint16'},
nekuLuck: {type: 'Uint16'},
nekuBravery: {type: 'Uint16'}, // max = 999
/* ... */
shikiBaseAttack: {type: 'Uint16', offset: 0x3b},
shikiBaseDefense: {type: 'Uint16'},
shikiSync: {type: 'Uint16'}, // percentage * 10, i.e. 100.0% -> 1000 here
shikiBravery: {type: 'Uint16'}, // base is 110
/* ... */
joshuaBaseAttack: {type: 'Uint16', offset: 0x55},
joshuaBaseDefense:{type: 'Uint16'},
joshuaSync: {type: 'Uint16'},
joshuaBravery: {type: 'Uint16'}, // base is 40
/* ... */
beatBaseAttack: {type: 'Uint16', offset: 0x6f},
beatBaseDefense: {type: 'Uint16'},
beatSync: {type: 'Uint16'},
beatBravery: {type: 'Uint16'}, // base is 5
}
function parsePlayerCharacterStats(saveFileBuffer) {
const dataView = new DataView(saveFileBuffer, TWEWY_SAVE_FILE_BODY_OFFSET);
const playerCharacterStats = {}
let bufferOffset = 0;
for (const [attribute, attributeData] of Object.entries(SAVE_FILE_PLAYER_STATS_STRUCT)) {
const attributeType = attributeData.type
if (attributeData.offset) {
if (typeof attributeData.offset === 'string' && attributeData.offset[0] == '+') {
bufferOffset += (+attributeData.offset)
}
else bufferOffset = (+attributeData.offset)
}
const attributeTypeLength = attributeData.length ?? byteops.getTypeByteLength(attributeType)
const attributeValue = dataView['get'+attributeType](bufferOffset, /*little_endian=*/true)
playerCharacterStats[attribute] = attributeValue
bufferOffset += attributeTypeLength
}
return playerCharacterStats
}
function packPlayerCharacterStats(saveFileBuffer, playerCharacterStats) {
const dataView = new DataView(saveFileBuffer, TWEWY_SAVE_FILE_BODY_OFFSET);
let bufferOffset = 0;
for (const [attribute, attributeData] of Object.entries(SAVE_FILE_PLAYER_STATS_STRUCT)) {
const attributeType = attributeData.type
if (attributeData.offset) {
if (typeof attributeData.offset === 'string' && attributeData.offset[0] == '+') {
bufferOffset += (+attributeData.offset)
}
else bufferOffset = (+attributeData.offset)
}
const attributeTypeLength = attributeData.length ?? byteops.getTypeByteLength(attributeType)
const attributeValue = playerCharacterStats[attribute]
dataView['set'+attributeType](bufferOffset, attributeValue, /*little_endian=*/true)
bufferOffset += attributeTypeLength
}
return playerCharacterStats
}
/***************************************** Pin Stockpile *****************************************/
const SAVE_FILE_PIN_STOCKPILE_STRUCT = {
type: {type: 'Uint16'},
level: {type: 'Uint16'},
quantity: {type: 'Uint8'},
battles: {type: 'Uint32'}, // Goes up 1 for every battle session, does not count chain battles
bpp: {type: 'Uint16'},
mpp: {type: 'Uint16'}, // MPP is not used in Solo Remix but not sure what else this could be.
spp: {type: 'Uint16'},
}
const SAVE_FILE_PIN_STOCKPILE_STRUCT_SIZE = (
Object
.values(SAVE_FILE_PIN_STOCKPILE_STRUCT)
.reduce((sum, atts) => sum+byteops.getTypeByteLength(atts.type), 0)
)
export const STOCKPILE_PIN_MAX_COUNT = 255
const SAVE_FILE_PIN_STOCKPILE_START = 0x131
const SAVE_FILE_PIN_STOCKPILE_END = (SAVE_FILE_PIN_STOCKPILE_START+
(SAVE_FILE_PIN_STOCKPILE_STRUCT_SIZE*STOCKPILE_PIN_MAX_COUNT))
const SAVE_FILE_PIN_STOCKPILE_SIZE = SAVE_FILE_PIN_STOCKPILE_END - SAVE_FILE_PIN_STOCKPILE_START
function parsePinStockpile(saveFileBuffer) {
const dataView = new DataView(saveFileBuffer, SAVE_FILE_PIN_STOCKPILE_START);
let bufferOffset = 0;
const pinStockpile = new Array(STOCKPILE_PIN_MAX_COUNT)
let pinIndex = 0
while (bufferOffset < SAVE_FILE_PIN_STOCKPILE_SIZE) {
const pin = {}
for (const [attribute, attributeData] of Object.entries(SAVE_FILE_PIN_STOCKPILE_STRUCT)) {
const attributeType = attributeData.type
const attributeTypeLength = byteops.getTypeByteLength(attributeType)
pin[attribute] = dataView['get'+attributeType](bufferOffset, /*little_endian=*/true)
bufferOffset += attributeTypeLength
}
if (pin.quantity) ++pin.type
pin.name = PIN_DATA[pin.type] ?? "NULL"
pinStockpile[pinIndex] = pin
++pinIndex;
}
return pinStockpile
}
function packPinStockpile(saveFileBuffer, pinStockpile) {
const dataView = new DataView(saveFileBuffer, SAVE_FILE_PIN_STOCKPILE_START);
let bufferOffset = 0;
for (const pin of pinStockpile) {
for (const [attribute, attributeData] of Object.entries(SAVE_FILE_PIN_STOCKPILE_STRUCT)) {
const attributeType = attributeData.type
const attributeTypeLength = byteops.getTypeByteLength(attributeType)
const attributeValue = pin[attribute]
if (attributeValue != null) {
dataView['set'+attributeType](bufferOffset, attributeValue, /*little_endian=*/true)
}
bufferOffset += attributeTypeLength
}
}
}
/***************************************** Mastered Pins *****************************************/
const SAVE_FILE_PIN_MASTERED_STRUCT = {
type: {type: 'Uint16'},
level: {type: 'Uint16'},
isAllocated: {type: 'Uint8'}, // Technically a boolean
unknown: {type: 'Uint32'}, // Unknown what data this is
quantity: {type: 'Uint16'},
}
const SAVE_FILE_PIN_MASTERED_STRUCT_SIZE = (
Object
.values(SAVE_FILE_PIN_MASTERED_STRUCT)
.reduce((sum, atts) => sum+byteops.getTypeByteLength(atts.type), 0)
)
const SAVE_FILE_PIN_MASTERED_START = 0x1031
export const MASTERED_PIN_MAX_COUNT = 320
// const SAVE_FILE_PIN_MASTERED_END = 0x1e10
// const SAVE_FILE_PIN_MASTERED_SIZE = SAVE_FILE_PIN_MASTERED_END - SAVE_FILE_PIN_MASTERED_START
const SAVE_FILE_PIN_MASTERED_SIZE = MASTERED_PIN_MAX_COUNT * SAVE_FILE_PIN_MASTERED_STRUCT_SIZE
// export const MASTERED_PIN_MAX_COUNT = parseInt(SAVE_FILE_PIN_MASTERED_SIZE/
// SAVE_FILE_PIN_MASTERED_STRUCT_SIZE)
function parsePinMastered(saveFileBuffer) {
const dataView = new DataView(saveFileBuffer, SAVE_FILE_PIN_MASTERED_START);
let bufferOffset = 0;
const pinMastered = new Array(MASTERED_PIN_MAX_COUNT)
let pinIndex = 0
while (bufferOffset < SAVE_FILE_PIN_MASTERED_SIZE) {
const pin = {}
for (const [attribute, attributeData] of Object.entries(SAVE_FILE_PIN_MASTERED_STRUCT)) {
const attributeType = attributeData.type
const attributeTypeLength = byteops.getTypeByteLength(attributeType)
pin[attribute] = dataView['get'+attributeType](bufferOffset, /*little_endian=*/true)
bufferOffset += attributeTypeLength
}
if (pin.type != 0xffff) ++pin.type
pin.name = PIN_DATA[pin.type] ?? "NULL"
pinMastered[pinIndex] = pin
++pinIndex;
}
return pinMastered
}
function packPinMastered(saveFileBuffer, pinMastered) {
const dataView = new DataView(saveFileBuffer, SAVE_FILE_PIN_MASTERED_START);
let bufferOffset = 0;
for (const pin of pinMastered) {
for (const [attribute, attributeData] of Object.entries(SAVE_FILE_PIN_MASTERED_STRUCT)) {
const attributeType = attributeData.type
const attributeTypeLength = byteops.getTypeByteLength(attributeType)
const attributeValue = pin[attribute]
if (attributeValue != null) {
dataView['set'+attributeType](bufferOffset, attributeValue, /*little_endian=*/true)
}
bufferOffset += attributeTypeLength
}
}
return pinMastered
}