-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlot2_save_editor.py
370 lines (323 loc) · 13 KB
/
lot2_save_editor.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
'''
東方記偽器 ~ Artificial Record
By Doctus ([email protected])
A LoT2 save editor.
東方記偽器 ~ Artificial Record 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.
RandomGameGenerator 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 東方記偽器 ~ Artificial Record. If not, see
<http://www.gnu.org/licenses/>.
'''
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
EDITOR_TITLE = u"東方記偽器 ~ Artificial Record v1.00"
CHARACTER_NAMES = ["Reimu", "Marisa", "Rinnosuke", "Keine",
"Momiji", "Youmu", "Kogasa", "Rumia",
"Cirno", "Minoriko", "Komachi", "Chen",
"Nitori", "Parsee", "Wriggle", "Kaguya",
"Mokou", "Aya", "Mystia", "Kasen",
"Nazrin", "Hina", "Rin", "Okuu",
"Satori", "Yuugi", "Meiling", "Alice",
"Patchouli", "Eirin", "Reisen", "Sanae",
"Iku", "Suika", "Ran", "Remilia",
"Sakuya", "Kanako", "Suwako", "Tenshi",
"Flandre", "Yuyuko", "Yuuka", "Yukari",
"Byakuren", "Eiki", "Renko", "Maribel"]
SUBCLASSES = ["Guardian", "Monk", "Warrior", "Sorcerer",
"Healer", "Enhancer", "Hexer", "Toxicologist",
"Magician", "Herbalist", "Strategist",
"Gambler", "Diva", "Transcendant"]
STAT_ORDER = ["LEVEL", "EXPERIENCE", "SKILLPOINTS",
"LEVELPOINTS", "BATTLEPOINTS"]
LEVEL_BONUS_ORDER = ["HP", "ATTACK", "DEFENSE",
"MAGIC", "MIND", "SPEED"]
LIBRARY_STAT_ORDER = ["HP", "ATTACK", "DEFENSE", "MAGIC",
"MIND", "SPEED", "FIRE", "COLD",
"WIND", "NATURE", "MYSTIC", "SPIRIT",
"DARK", "PHYSICAL"]
GENERAL_STUFF_ORDER = ["TIME", "MONEY", "BATTLES"]
GENERAL_MEM_STRUCT = {"TIME": [4, 2],
"MONEY": [13, 3],
"BATTLES": [17, 1]}
CHAR_MEM_STRUCT = {"LEVEL": 3,
"EXPERIENCE": 11,
"LIBRARY":
{"HP": 15,
"ATTACK": 19,
"DEFENSE": 23,
"MAGIC": 27,
"MIND": 31,
"SPEED": 35,
"FIRE": 39,
"COLD": 43,
"WIND": 47,
"NATURE": 51,
"MYSTIC": 55,
"SPIRIT": 59,
"DARK": 63,
"PHYSICAL": 67},
"LEVELUP":
{"HP": 71,
"ATTACK": 75,
"DEFENSE": 79,
"MAGIC": 83,
"MIND": 87,
"SPEED": 91},
"SUBCLASS":95,
#"SKILLS": 0x60 ~ 0xEA
"SKILLPOINTS": 238,
"LEVELPOINTS": 243,
"BATTLEPOINTS": 264
}
class HexDataContainer(object):
def __init__(self, hexData):
self._hexData = [ord(byte) for byte in hexData]
@property
def hexData(self):
return "".join(["%c" % byte for byte in self._hexData])
def _value(self, address, limit):
return sum([self._hexData[address-i] * (0x100**i) for i in range(limit)])
def bigValue(self, address):
return self._value(address, 6)
def value(self, address, limit=4):
return self._value(address, limit)
def singleValue(self, address):
return self._hexData[address]
def bigPoke(self, address, newValue):
if newValue <= 0xFFFFFFFFFFFF:
self._hexData[address] = "%c" % (newValue % 0x100)
if newValue > 0xFF:
self._hexData[address-1] = "%c" % ((newValue / 0x100) % 0x100)
if newValue > 0xFFFF:
self._hexData[address-2] = "%c" % ((newValue / 0x10000) % 0x100)
if newValue > 0xFFFFFF:
self._hexData[address-3] = "%c" % ((newValue / 0x1000000) % 0x100)
if newValue > 0xFFFFFFFF:
self._hexData[address-4] = "%c" % ((newValue / 0x100000000) % 0x100)
if newValue > 0xFFFFFFFFFF:
self._hexData[address-5] = "%c" % (newValue / 0x10000000000)
else:
self.errorDialog = QErrorMessage(self)
self.errorDialog.showMessage("Value too large!", "overflow")
def poke(self, address, newValue, limit=3):
if newValue <= 0xFF:
self._hexData[address] = "%c" % newValue
elif newValue <= 0xFFFFFFFF:
self._hexData[address] = "%c" % (newValue % 0x100)
if newValue > 0xFF and limit >= 1:
self._hexData[address-1] = "%c" % ((newValue / 0x100) % 0x100)
if newValue > 0xFFFF and limit >= 2:
self._hexData[address-2] = "%c" % ((newValue / 0x10000) % 0x100)
if newValue > 0xFFFFFF and limit >= 3:
self._hexData[address-3] = "%c" % (newValue / 0x1000000)
else:
self.errorDialog = QErrorMessage(self)
self.errorDialog.showMessage("Value too large!", "overflow")
class SubclassPicker(QComboBox):
def __init__(self, *args, **kwargs):
super(SubclassPicker, self).__init__(*args, **kwargs)
self.addItem("(None)")
for subclass in SUBCLASSES:
self.addItem(subclass)
def setValue(self, value):
if value > 0:
self.setCurrentIndex(value-99)
else:
self.setCurrentIndex(0)
class CharacterEditWidget(QWidget):
def __init__(self, characterName, characterData, recruited, *args, **kwargs):
super(CharacterEditWidget, self).__init__(*args, **kwargs)
self.data = characterData
self.layout = QGridLayout()
recruitedLabel = QCheckBox("Recruited")
recruitedLabel.setChecked(recruited)
recruitedLabel.setEnabled(False)
charNameLabel = QLabel(characterName)
self.layout.addWidget(charNameLabel, 0, 0, 1, 3)
self.layout.addWidget(recruitedLabel, 0, 3)
bigFont = QFont()
bigFont.setPointSize(16)
bigFont.setBold(True)
underlineFont = QFont()
underlineFont.setUnderline(True)
charNameLabel.setFont(bigFont)
baseLabel = QLabel("Base")
libraryLabel = QLabel("Library")
levelupLabel = QLabel("Level Bonus")
baseLabel.setFont(underlineFont)
libraryLabel.setFont(underlineFont)
levelupLabel.setFont(underlineFont)
self.layout.addWidget(baseLabel, 1, 0, 1, 2)
self.layout.addWidget(libraryLabel, 1, 2, 1, 2)
self.layout.addWidget(levelupLabel, 1, 4, 1, 2)
for i, stat in enumerate(STAT_ORDER):
self.layout.addWidget(QLabel(stat + ":"), i+2, 0)
spinbox = QSpinBox()
spinbox.setMaximum(2147483647)
if stat != "EXPERIENCE":
spinbox.setValue(self.data.value(CHAR_MEM_STRUCT[stat]))
spinbox.valueChanged.connect(self.pokeValue(self.data, CHAR_MEM_STRUCT[stat]))
else:
spinbox.setValue(self.data.bigValue(CHAR_MEM_STRUCT[stat]))
spinbox.valueChanged.connect(self.pokeValue(self.data, CHAR_MEM_STRUCT[stat], True))
self.layout.addWidget(spinbox, i+2, 1)
self.layout.addWidget(QLabel("SUBCLASS:"), len(STAT_ORDER)+2, 0)
subclass = SubclassPicker()
subclass.setValue(self.data.value(CHAR_MEM_STRUCT["SUBCLASS"]))
subclass.currentIndexChanged.connect(self.pokeSubclass)
self.layout.addWidget(subclass, len(STAT_ORDER)+2, 1)
for i, libraryUp in enumerate(LIBRARY_STAT_ORDER):
self.layout.addWidget(QLabel(libraryUp + ":"), i+2, 2)
spinbox = QSpinBox()
spinbox.setMaximum(2147483647)
spinbox.setValue(self.data.value(CHAR_MEM_STRUCT["LIBRARY"][libraryUp]))
spinbox.valueChanged.connect(self.pokeValue(self.data, CHAR_MEM_STRUCT["LIBRARY"][libraryUp]))
self.layout.addWidget(spinbox, i+2, 3)
for i, stat in enumerate(LEVEL_BONUS_ORDER):
self.layout.addWidget(QLabel(stat + ":"), i+2, 4)
spinbox = QSpinBox()
spinbox.setMaximum(2147483647)
spinbox.setValue(self.data.value(CHAR_MEM_STRUCT["LEVELUP"][stat]))
spinbox.valueChanged.connect(self.pokeValue(self.data, CHAR_MEM_STRUCT["LEVELUP"][stat]))
self.layout.addWidget(spinbox, i+2, 5)
self.setLayout(self.layout)
def pokeSubclass(self, value):
if value > 0:
value = value + 99
self.data.poke(CHAR_MEM_STRUCT["SUBCLASS"], value)
def pokeValue(self, dataContainer, address, big=False):
if not big:
def pokeValueFunc(value):
dataContainer.poke(address, value)
return pokeValueFunc
else:
def pokeValueFunc(value):
dataContainer.bigPoke(address, value)
return pokeValueFunc
@property
def hexData(self):
return self.data.hexData
class CharacterDataTab(QWidget):
def __init__(self, basePath, *args, **kwargs):
super(CharacterDataTab, self).__init__(*args, **kwargs)
self.basePath = basePath
self.characterData = {}
self.layout = QGridLayout()
self.layout.setColumnStretch(2, 10)
self.liste = QListWidget()
self.stack = QStackedWidget()
recruitmentData = self.loadRecruitmentData(basePath)
for index, charName in enumerate(CHARACTER_NAMES):
data = self.loadCharacterData(index+1, charName, basePath)
if data is not None:
self.liste.addItem(charName)
self.stack.addWidget(CharacterEditWidget(charName, data, recruitmentData.singleValue(index+1)))
self.layout.addWidget(self.liste, 0, 0, 2, 1)
self.layout.addWidget(self.stack, 0, 1)
self.stack.setSizePolicy(QSizePolicy(QSizePolicy.Maximum))
self.setLayout(self.layout)
self.liste.currentRowChanged.connect(self.stack.setCurrentIndex)
def loadRecruitmentData(self, basePath):
try:
with open(os.path.join(basePath, "PCF01.ngd"), "rb") as f:
data = f.read()
except:
self.errorDialog = QErrorMessage(self)
self.errorDialog.showMessage("Error reading character recruitment file. SAVING NOT RECOMMENDED. YOUR SAVE MAY BE (FURTHER) CORRUPTED.", "recruitmentFileRead")
else:
return HexDataContainer(data)
def loadCharacterData(self, index, characterName, basePath):
try:
with open(os.path.join(basePath, "C%02i.ngd" % index), "rb") as f:
data = f.read()
except:
self.errorDialog = QErrorMessage(self)
self.errorDialog.showMessage("Error reading character file: %s. SAVING NOT RECOMMENDED. YOUR SAVE MAY BE (FURTHER) CORRUPTED." % characterName, "charFileRead")
else:
return HexDataContainer(data)
def _saveCharacterData(self, index, data, basePath):
with open(os.path.join(basePath, "C%02i.ngd" % index), "wb") as f:
f.write(data)
def saveCharacterData(self):
for index, charName in enumerate(CHARACTER_NAMES):
self._saveCharacterData(index+1, self.stack.widget(index).hexData, self.basePath)
class MiscDataTab(QWidget):
def __init__(self, basePath, *args, **kwargs):
super(MiscDataTab, self).__init__(*args, **kwargs)
self.basePath = basePath
self.data = self.loadGeneralData(basePath)
self.layout = QGridLayout()
self.layout.setColumnStretch(2, 10)
self.layout.setRowStretch(len(GENERAL_STUFF_ORDER), 10)
for i, stat in enumerate(GENERAL_STUFF_ORDER):
self.layout.addWidget(QLabel(stat + ":"), i, 0)
spinbox = QSpinBox()
spinbox.setMaximum(2147483647)
memdata = GENERAL_MEM_STRUCT[stat]
spinbox.setValue(self.data.value(memdata[0], memdata[1]+1))
spinbox.valueChanged.connect(self.pokeValue(self.data, *memdata))
self.layout.addWidget(spinbox, i, 1)
self.setLayout(self.layout)
def pokeValue(self, dataContainer, address, limit):
def pokeValueFunc(value):
dataContainer.poke(address, value, limit)
return pokeValueFunc
def loadGeneralData(self, basePath):
try:
with open(os.path.join(basePath, "SHD01.ngd"), "rb") as f:
data = f.read()
except:
self.errorDialog = QErrorMessage(self)
self.errorDialog.showMessage("Error reading general data file. SAVING NOT RECOMMENDED. YOUR SAVE MAY BE (FURTHER) CORRUPTED.", "recruitmentFileRead")
else:
return HexDataContainer(data)
def saveGeneralData(self):
with open(os.path.join(self.basePath, "SHD01.ngd"), "wb") as f:
f.write(self.data.hexData)
class MainWidget(QWidget):
def __init__(self, *args, **kwargs):
super(MainWidget, self).__init__(*args, **kwargs)
self.tabWidget = QTabWidget()
self.characterTab = CharacterDataTab(basePath="save1")
self.miscTab = MiscDataTab(basePath="save1")
self.tabWidget.addTab(self.characterTab, "Characters")
#self.tabWidget.addTab(QLabel("Inventory!"), "Inventory")
self.tabWidget.addTab(self.miscTab, "Misc")
self.tabWidget.addTab(QTextEdit("Fields currently limited to 2147483647 due to a Qt limitation I'm too lazy to circumvent. (Experience points above that limit won't be lost as long as you don't touch that character's experience field.)<br><br>Many addresses taken from Wymar's research after I noticed it existed. Thanks for the saved time and effort."), "About")
bigFont = QFont()
bigFont.setPointSize(16)
self.saveButton = QPushButton("Save All Changes")
self.saveButton.setFont(bigFont)
self.saveButton.pressed.connect(self.saveEverything)
self.layout = QGridLayout()
self.layout.addWidget(self.tabWidget, 0, 0)
self.layout.addWidget(self.saveButton, 1, 0)
self.setLayout(self.layout)
def saveEverything(self):
self.characterTab.saveCharacterData()
self.miscTab.saveGeneralData()
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setObjectName("MainWindow")
self.setWindowIcon(QIcon("logo.png"))
self.setWindowTitle(EDITOR_TITLE)
if os.path.exists("save1"):
self.mainWidget = MainWidget()
self.setCentralWidget(self.mainWidget)
else:
QMessageBox.critical(self, "ERROR: No save folder", "You must copy your chosen save folder (the folder, not just its contents) to this directory as 'save1'. After editing, delete the original folder and replace it with the edited one. Be sure to keep backups!")
import sys
sys.exit()
app = QApplication([EDITOR_TITLE])
main = MainWindow()
main.show()
app.exec_()