forked from emohx/KoikatuSaveDataEdit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgui.py
458 lines (369 loc) · 16 KB
/
gui.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import io
import shutil
import sys
import traceback
from pathlib import Path
import winreg
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
from scframe import VerticalScrolledFrame
from character import KoikatuCharacter
from save_data import KoikatuSaveData
from resource import Resource as RM
from status import StatusPanel
class PropertyPanel(ttk.Frame):
def __init__(self, parent, character, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.character = character
row = 0
label1 = ttk.Label(self, text=RM.res('lastname'))
self._lastname = tk.StringVar(value=character.lastname)
entry1 = ttk.Entry(self, textvariable=self._lastname)
label1.grid(row=row, column=0, sticky='E', columnspan=1)
entry1.grid(row=row, column=1, sticky='W', columnspan=1)
label2 = ttk.Label(self, text=RM.res('firstname'))
self._firstname = tk.StringVar(value=character.firstname)
entry2 = ttk.Entry(self, textvariable=self._firstname)
label2.grid(row=row, column=2, sticky='E', columnspan=1)
entry2.grid(row=row, column=3, sticky='W', columnspan=1)
row = row + 1
label3 = ttk.Label(self, text=RM.res('nickname'))
self._nickname = tk.StringVar(value=character.nickname)
entry3 = ttk.Entry(self, textvariable=self._nickname)
label3.grid(row=row, column=0, sticky='E', columnspan=1)
entry3.grid(row=row, column=1, sticky='W', columnspan=1)
if character.sex == 0:
label4 = ttk.Label(self, text=RM.res('intellect'))
self._intellect = tk.StringVar(value=character.intelligence)
entry4 = ttk.Entry(self, textvariable=self._intellect)
label4.grid(row=row, column=2, sticky='E', columnspan=1)
entry4.grid(row=row, column=3, sticky='W', columnspan=1)
row = row + 1
label5 = ttk.Label(self, text=RM.res('physical'))
self._physical = tk.StringVar(value=character.strength)
entry5 = ttk.Entry(self, textvariable=self._physical)
label5.grid(row=row, column=0, sticky='E', columnspan=1)
entry5.grid(row=row, column=1, sticky='W', columnspan=1)
label6 = ttk.Label(self, text=RM.res('hentai'))
self._hentai = tk.StringVar(value=character.ero)
entry6 = ttk.Entry(self, textvariable=self._hentai)
label6.grid(row=row, column=2, sticky='E', columnspan=1)
entry6.grid(row=row, column=3, sticky='W', columnspan=1)
elif character.sex == 1:
values = (RM.res('male'), RM.res('female'))
label4 = ttk.Label(self, text=RM.res('sex'))
self._sex = tk.StringVar(value=values[character.sex])
entry4 = ttk.Combobox(self, values=values,
textvariable=self._sex, state='readonly')
label4.grid(row=row, column=2, sticky="E", columnspan=1)
entry4.grid(row=row, column=3, sticky="W", columnspan=1)
row = row + 1
values = RM.res('personalities')
label5 = ttk.Label(self, text=RM.res('personality'))
self._personality = tk.StringVar(value=values[character.personality])
entry5 = ttk.Combobox(self, values=values,
textvariable=self._personality, state='readonly')
label5.grid(row=row, column=0, sticky="E", columnspan=1)
entry5.grid(row=row, column=1, sticky="W", columnspan=1)
values = RM.res('weak_points')
label6 = ttk.Label(self, text=RM.res('weak_point'))
self._weak_point = tk.StringVar(value=values[character.weak_point])
entry6 = ttk.Combobox(self, values=values,
textvariable=self._weak_point, state='readonly')
label6.grid(row=row, column=2, sticky="E", columnspan=1)
entry6.grid(row=row, column=3, sticky="W", columnspan=1)
# answer
row = row + 1
self._answers = {}
frame = ttk.LabelFrame(self, text=RM.res('answer'))
for i, name in enumerate(character.answers.keys()):
self._answers[name] = self._make_boolean_prop(frame,
RM.res(name),
character.answers[name], i, 5)
frame.grid(row=row, column=0, columnspan=4, sticky='W')
# denail
row = row + 1
self._denials = {}
frame = ttk.LabelFrame(self, text=RM.res('denial'))
for i, name in enumerate(character.denials.keys()):
self._denials[name] = self._make_boolean_prop(frame,
RM.res(name),
character.denials[name], i, 5)
frame.grid(row=row, column=0, columnspan=4, sticky='W')
# attribute
row = row + 1
self._attributes = {}
frame = ttk.LabelFrame(self, text=RM.res('attribute'))
for i, name in enumerate(character.attributes.keys()):
self._attributes[name] = self._make_boolean_prop(frame,
RM.res(name),
character.attributes[name], i, 5)
frame.grid(row=row, column=0, columnspan=4, sticky='W')
@property
def firstname(self):
return self._firstname.get()
@property
def lastname(self):
return self._lastname.get()
@property
def nickname(self):
return self._nickname.get()
@property
def intellect(self):
return self._intellect.get()
@property
def physical(self):
return self._physical.get()
@property
def hentai(self):
return self._hentai.get()
@property
def sex(self):
return [RM.res('male'), RM.res('female')].index(self._sex.get())
@property
def personality(self):
values = RM.res('personalities')
return values.index(self._personality.get())
@personality.setter
def personality(self, value):
values = RM.res('personalities')
return self._personality.set(values[value])
@property
def weak_point(self):
values = RM.res('weak_points')
return values.index(self._weak_point.get())
@weak_point.setter
def weak_point(self, value):
values = RM.res('weak_points')
self._weak_point.set(values[value])
@property
def answers(self):
return {key:self._answers[key].get() for key in self._answers}
@property
def denials(self):
return {key:self._denials[key].get() for key in self._denials}
@property
def attributes(self):
return {key:self._attributes[key].get() for key in self._attributes}
def update_character(self, character):
self._firstname.set(character.firstname)
self._lastname.set(character.lastname)
self._nickname.set(character.nickname)
if character.sex == 0:
self._intellect.set(character.intelligence)
self._physical.set(character.strength)
self._hentai.set(character.ero)
self.personality = character.personality
self.weak_point = character.weak_point
for key in self._answers:
self._answers[key].set(character.answers[key])
for key in self._denials:
self._denials[key].set(character.denials[key])
for key in self._attributes:
self._attributes[key].set(character.attributes.setdefault(key,False))
def _make_boolean_prop(self, frame, name, value, i, cols):
var = tk.BooleanVar()
checkbox = ttk.Checkbutton(frame, text=name, variable=var)
var.set(value)
row = i // cols
col = i % cols
checkbox.grid(row=row, column=col, sticky='W')
return var
class CharacterPanel(ttk.Frame):
def __init__(self, app, parent, character, *args, **kwargs):
super().__init__(parent,
relief='ridge',
*args, **kwargs)
self.app = app
self.parent = parent
self._character = character
self.dirty = False
png = Image.open(io.BytesIO(character.png))
self.image = ImageTk.PhotoImage(png)
self.photo = tk.Label(self,
image=self.image,
width=self.image.width(),
height=self.image.height())
self.photo.grid(row=0, column=0, rowspan=3, padx=2, pady=2)
self.property_panel = PropertyPanel(self, character)
if character.sex == 1:
self.property_panel.grid(row=0, column=1, rowspan=1, padx=2, pady=2)
self.status_panel = StatusPanel(self, character)
self.status_panel.grid(row=1, column=1, rowspan=1, padx=4, pady=2, sticky='N')
else:
self.property_panel.grid(row=0, column=1, rowspan=1, columnspan=2, padx=2, pady=2, sticky='W')
self._load_btn = ttk.Button(self,
text='Load Character Card',
command=self._open_dialog)
self._load_btn.grid(row=2, column=1, sticky='W', pady=4)
@property
def character(self):
chara = self._character
panel = self.property_panel
chara.firstname = panel.firstname
chara.lastname = panel.lastname
chara.nickname = panel.nickname
if chara.sex == 0:
chara.intelligence = panel.intellect
chara.strength = panel.physical
chara.ero = panel.hentai
elif chara.sex == 1:
chara.personality = panel.personality
chara.weak_point = panel.weak_point
chara.answers = self.property_panel.answers
chara.denials = self.property_panel.denials
chara.attributes = self.property_panel.attributes
panel = self.status_panel
chara.feeling = panel.feeling
chara.m_love = panel.m_love
chara.h_count = panel.h_count
chara.koikatu = panel.koikatu
chara.lover = panel.relation
chara.date = panel.date
chara.intimacy = panel.intimacy
ac = [
'mune', 'kokan', 'anal', 'siri', 'tikubi',
'kokan_piston', 'anal_piston', 'houshi'
]
for name in ac:
chara.set_ac(name, panel.ac(name))
self.dirty = True
return chara
def _update_character(self, character):
self._character.custom = character.custom
self._character.coordinates = character.coordinates
self._character.parameter = character.parameter
self._character.status = character.status
self._character.png_length = character.png_length
self._character.png = character.png
self.dirty = True
png = Image.open(io.BytesIO(character.png))
self.image = ImageTk.PhotoImage(png)
self.photo.config(image=self.image)
self.property_panel.update_character(self._character)
def _open_dialog(self):
name = askopenfilename(filetype=[("koikatu card", "*.png")],
initialdir=self.app.card_dir)
if name is not None:
with open(name, 'rb') as infile:
chara = KoikatuCharacter(infile, True)
self._update_character(chara)
else:
self.quit()
class App:
def __init__(self, root, filename, out_filename):
self.root = root
self.filename = filename
self.out_filename = out_filename
self.save_data = KoikatuSaveData(filename)
try:
self.card_dir=get_default_chara_folder()
except:
self.card_dir=Path.cwd()
style = ttk.Style()
style.configure('.', padding='2 4 2 4')
frame = VerticalScrolledFrame(self.root)
self.panels = []
row_count = 0
for chara in self.save_data.characters:
panel = CharacterPanel(self, frame.interior, chara)
panel.grid(row=int(row_count/2),column = int(row_count%2))
self.panels.append(panel)
row_count+=1
btn_frame = ttk.Frame(self.root)
save_btn = ttk.Button(btn_frame, text='Save & Quit', command=self.save_and_quit)
quit_btn = ttk.Button(btn_frame, text='Quit', command=self.quit)
quit_btn.pack(side='right', pady=2)
save_btn.pack(side='right', pady=2)
frame.grid(row=0, column=0)
btn_frame.grid(row=1, column=0, pady=2, sticky='E')
y_padding = 4
width = 1440
height = 355 * 3 + btn_frame.winfo_height() + y_padding * 2
WindowsTaskbarHeight=48
if self.root.winfo_screenheight() - WindowsTaskbarHeight < height:
height = int(355 * 2) + btn_frame.winfo_height() + y_padding * 2
self.root.geometry(f'{width}x{height}')
def _configure(event):
fh = self.root.winfo_height() - btn_frame.winfo_height() - y_padding
frame.canvas.config(height=fh)
self.root.bind('<Configure>', _configure)
def save(self):
for i, panel in enumerate(self.panels):
chara = panel.character
if i > 0 and panel.dirty:
self.save_data.replace(i, chara)
if self.filename == self.out_filename:
# create backup
path = Path(self.filename).resolve()
backup = path.parent / (path.stem + '.old.dat')
shutil.copy(self.filename, backup)
self.save_data.save(self.out_filename)
def save_and_quit(self, *args):
self.save()
self.root.destroy()
def quit(self, *args):
self.root.destroy()
def run(self):
self.root.mainloop()
def main():
default_resource = Path(sys.argv[0]).parent / 'resources_ja.json'
parser = argparse.ArgumentParser()
parser.add_argument('save_data',
help='koikatu save data')
parser.add_argument('-o', dest='output',
help='output file')
parser.add_argument('-r',
dest='resources',
default=default_resource,
help='resource file name')
root = tk.Tk()
root.title('Koikatu Save data editor')
if len(sys.argv) > 1:
args = parser.parse_args()
save_data = args.save_data
if args.output is not None:
out_filename = args.output
else:
out_filename = save_data
resources = args.resources
else:
try:
save_folder=get_default_save_folder()
except:
save_folder=Path.cwd()
save_data = askopenfilename(filetype=[("koikatu save data", "*.dat")],
initialdir=save_folder)
print(save_data)
if save_data is None or len(save_data) == 0:
root.destroy()
sys.exit(-1)
out_filename = save_data
resources = default_resource
RM.load(resources)
print('out:', out_filename)
App(root, save_data, out_filename).run()
def get_default_save_folder():
u"""get default save folder from windows registry """
path = r'Software\illusion\Koikatu\koikatu'
key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, path)
data, regtype = winreg.QueryValueEx(key, 'INSTALLDIR')
return data+'UserData\save\game'
def get_default_chara_folder():
u"""get default save folder from windows registry """
path = r'Software\illusion\Koikatu\koikatu'
key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, path)
data, regtype = winreg.QueryValueEx(key, 'INSTALLDIR')
return data+'UserData\chara'
if __name__ == '__main__':
try:
main()
except Exception as e:
traceback.print_exc()
print('push Enter key')
input()