-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatchmanager.py
572 lines (459 loc) · 19.5 KB
/
patchmanager.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# This program allows merging of JD-08/JX-08 patches from one
# .svd file to another.
# Copyright (C) 2023 Nils Kronert
#
# This program 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.
#
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
#
# https://github.com/NilsKr/JD08PatchManager
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
from tkinter import simpledialog
from preferences import Preferences
from patchdiffviewer import PatchDiffViewer
import sys
import os
import struct
VERSION = "1.1"
JD_SIG = "JD PA11"
JX_SIG = "JX PA11"
ctrlPressed = False
def keyup(e):
global ctrlPressed
if e.keysym == 'Control_L':
ctrlPressed = False
def keydown(e):
global ctrlPressed
if e.keysym == 'Control_L':
ctrlPressed = True
def getSaveAsName(fileName):
saveAsName = filedialog.asksaveasfile(initialfile = os.path.basename(fileName),
initialdir = os.path.dirname(fileName),
defaultextension=".svd",filetypes = (("JD-08/JX-08 backup files", "*.svd"),
("all files", "*.*")))
return saveAsName
def getFileSize(fileName):
try:
if not os.path.exists(fileName):
return -1;
file_size = os.path.getsize(fileName)
print(f"File size of {fileName} in Bytes is {file_size}")
return file_size
except FileNotFoundError:
print("File not found.")
except OSError:
print("OS error occurred.")
def debug(msg):
print(msg)
def all_children(wid) :
_list = wid.winfo_children()
for item in _list :
if item.winfo_children() :
_list.extend(item.winfo_children())
return _list
def childrenByType(wid, typeName) :
_list = all_children(wid)
result = []
for item in _list:
className = type(item).__name__
#print(className)
if className == typeName:
result.append(item)
return result
CONFIGFILE = "patchmanager.cfg"
prefs = None
buttonBar = None
class App(Tk):
def onBeforeExit(self):
if self.leftFile.canClose():
if self.rightFile.canClose():
self.destroy()
def __init__(self, title, size, *args):
global prefs
global buttonBar
prefs = Preferences(CONFIGFILE)
print(f"prefs={prefs}")
path1 = None
path2 = None
if len(args) > 1:
path1 = args[1]
if len(args) > 2:
path2 = args[2]
# main setup
super().__init__()
self.title(title)
self.geometry(f'{size[0]}x{size[1]}')
self.minsize(size[0],size[1])
self.bind("<KeyPress>", keydown)
self.bind("<KeyRelease>", keyup)
self.style = ttk.Style(self)
# widgets
self.leftFile = PatchFile(self, path1)
self.rightFile = PatchFile(self, path2)
self.rightFile.isLeftList = False
self.leftFile.otherFile = self.rightFile
self.rightFile.otherFile = self.leftFile
buttonBar = self.buttons = ButtonBar(self, self.leftFile, self.rightFile)
self.columnconfigure((0,2), weight = 10, uniform = 'a')
self.columnconfigure((1), weight = 2, uniform = 'a')
self.rowconfigure((0), weight = 1, uniform = 'a')
self.leftFile.grid(row = 0, column = 0, sticky = 'nswe', padx = 4, pady = 4)
self.buttons.grid(row = 0, column = 1, sticky = 'nswe', padx = 4, pady = 4)
self.rightFile.grid(row = 0, column = 2, sticky = 'nswe', padx = 4, pady = 4)
self.protocol("WM_DELETE_WINDOW", self.onBeforeExit)
# run
self.mainloop()
class PatchFile(Frame):
isLeftList = True
def __init__(self, parent, preloadFileName):
super().__init__(parent)
self.data = None
self.orig = None
self.cursel = None
self.preloadFileName = preloadFileName
self.create_widgets()
def canClose(self):
if self.data == None or self.data == self.orig:
return True
answer = messagebox.askyesnocancel("WARNING", f"The following file is not saved. Save?\n\n{self.fileName}", default='cancel')
if answer == None:
return False
if answer:
self.onSave()
return True
def setFileName(self, fileName):
self.fileName = fileName
curdir = os.path.dirname(fileName)
self.fileNameVar.set(os.path.basename(fileName) + " (" + curdir + ")")
def tryFile(self, fileName):
data = self.loadFile(fileName)
if data == None:
return;
self.setFileName(fileName)
self.data = data
self.orig = data[:]
self.populateList()
self.updateButtons()
def onBrowse(self):
global prefs
if not self.canClose():
return
defaultDir = prefs.getValue("defaultdir", ".")
fileName = filedialog.askopenfilename(initialdir = defaultDir,
title = "Select a JD-08/JX-08 backup File",
filetypes = (("JD-08/JX-08 backup files", "*.svd"),
("all files", "*.*")))
if fileName == "":
return
curdir = os.path.dirname(fileName)
if curdir != defaultDir:
prefs.setValue("defaultdir", curdir)
self.tryFile(fileName)
def itemSelected(self):
if self.cursel != None:
return len(self.cursel) > 0
return False
def updateButtons(self):
renameState = "disabled"
if self.data == None:
revertState = btnState = "disabled"
else:
if self.itemSelected():
renameState = "normal"
if self.data == self.orig:
btnState = "disabled"
else:
btnState = "normal"
if not self.itemSelected():
revertState = "disabled"
else:
if self.getPatch(self.cursel[0]) == self.getOriginalPatch(self.cursel[0]):
revertState = "disabled"
else:
revertState = "normal"
self.btnSave.configure(state=btnState)
self.btnRevert.configure(state=revertState)
self.btnRename.configure(state=renameState)
self.btnRevertAll.configure(state=btnState)
def onSave(self):
global ctrlPressed
if self.data != None:
if ctrlPressed:
ctrlPressed = False # This gets stuck sometimes due to the save as dialog, so we reset it here
saveAsName = getSaveAsName(self.fileName)
if saveAsName != None:
saveAsName = saveAsName.name
else:
saveAsName = self.fileName
if saveAsName == None:
return
f = open(saveAsName, mode="wb")
f.write(self.data)
f.close()
if saveAsName == self.fileName:
self.orig = self.data[:]
else:
self.setFileName(saveAsName)
self.onRevertAll()
def onRevert(self):
if self.data != None:
ndx = self.cursel[0]
self.updatePatch(ndx, self.getOriginalPatch(ndx))
def onRename(self):
if self.cursel[0] != None:
newName = simpledialog.askstring(title="Rename",
prompt="Rename patch to:",
initialvalue=self.getPatchName(self.data, self.cursel[0]))
if newName != None:
self.setPatchName(self.data, self.cursel[0], newName)
def onRevertAll(self):
if self.data != None:
self.data = self.orig[:]
temp = self.cursel
for i in range(256):
self.updatePatch(i, self.getOriginalPatch(i))
self.setSelection(temp[0])
def onPatchDblclick(self, event):
if not (self.data == None or self.otherFile.data == None):
self.otherFile.copyPatchFrom(self)
def onPatchClick(self, event):
if not (self.data == None or self.otherFile.data == None):
self.cursel = self.patchList.curselection()
self.updateButtons()
def onKeyUp(self, e):
global buttonBar
if e.keysym == 'F2':
self.onRename()
elif e.keysym == 'Up' or e.keysym == 'Down': # Cursor up/down
self.onPatchClick(e)
elif e.keysym == 'space': # Space bar
self.onPatchDblclick(e)
elif e.keysym == 'd': # Diff
if diffWindow == None:
buttonBar.openDiffWindow(prefs)
if self.isLeftList:
leftList = self
rightList = self.otherFile
else:
leftList = self.otherFile
rightList = self
if leftList.cursel == None:
patchL = None
else:
patchL = leftList.getPatch(leftList.cursel[0])
if rightList.cursel == None:
patchR = None
else:
patchR = rightList.getPatch(rightList.cursel[0])
if not diffWindow == None:
lst = childrenByType(diffWindow, "PatchDiffViewer")[0]
lst.setPatches(patchL, patchR)
def create_widgets(self):
# create the widgets
fileFrame = Frame(self)
label = Label(fileFrame, text = "JD-08/JX-08 backup file", anchor="w")
self.fileNameVar = StringVar()
self.fileName = fileName = Entry(fileFrame, text=self.fileNameVar, state="readonly")
btnBrowse = Button(fileFrame, text = '...', command = self.onBrowse)
listFrame = Frame(self)
scrollFrame = LabelFrame(listFrame)
scrollBar = Scrollbar(scrollFrame)#, orientation = 'vertical', width = 20)
# exportselection=False below allows the listbox to show the selected item when the focus is elsewhere
self.patchList = patchList = Listbox(listFrame, yscrollcommand = scrollBar.set, exportselection=False)
patchList.insert(1, "Browse for .svd file to display")
patchList.bind('<Double-1>', self.onPatchDblclick)
patchList.bind('<ButtonRelease-1>', self.onPatchClick)
patchList.bind('<KeyRelease>', self.onKeyUp)
scrollBar.config(command = patchList.yview)
self.buttonFrame = buttonFrame = Frame(self)
buttonFrame.columnconfigure((0,1,2,3,4), weight = 1, uniform = 'buttonFrame ')
self.btnSave = btnSave = Button(buttonFrame, text = 'Save', command= self.onSave)
self.btnRevert = btnRevert = Button(buttonFrame, text = 'Revert patch', command= self.onRevert)
self.btnRename = btnRename = Button(buttonFrame, text = 'Rename patch', command= self.onRename)
self.btnRevertAll = btnRevertAll = Button(buttonFrame, text = 'Revert all', command= self.onRevertAll)
fileFrame.place(relx=0, x=0, relwidth=1, width = 0, height=40, rely=0, y=10, anchor='nw')
label.place(x=10, y=10, relwidth=1, height = 20, anchor='w')
fileName.place(relx=0, x=10, relwidth=1, width = -44, height = 20, rely=0, y=20, anchor='nw')
btnBrowse.place(relx=1, x=-4, width=20, height = 20, rely=0, y=20, anchor='ne')
listFrame.place(relx=0, x=0, relwidth=1, width=0, relheight=1, height=-100, rely=0, y=56, anchor='nw')
patchList.place(relx=0, x=10, relwidth=1, width=-44, relheight=1, rely=0, y=0, anchor='nw')
scrollFrame.place(relx=1, x=-4, width=20, relheight=1, rely=0, y=0, anchor='ne')
scrollBar.place(relwidth = 1, relheight = 1)
buttonFrame.place(relx=0, x=0, relwidth=1, width = 0, height=40, rely=1, y=-40, anchor='nw')
btnRevertAll.pack(side = 'right', padx = 3, pady = 1)
btnRevert.pack(side = 'right', padx = 3, pady = 1)
btnRename.pack(side = 'right', padx = 3, pady = 1)
btnSave.pack(side = 'right', padx = 3, pady = 1)
if self.preloadFileName != None:
self.tryFile(self.preloadFileName)
self.updateButtons()
def getPatchSectionOffset(self, data):
for i in range(5):
section = data[i * 16: (i + 1) * 16]
if section[0:4].decode() == "PATa":
offs = struct.unpack("i", section[8:12])[0]
#print(f"Found! {offs}")
return offs
print("Section PAT not found!") # TODO: throw exception?
return 0x168260
def getPatchOffset(self, data, index):
return self.getPatchSectionOffset(data) + 16 + index * 0x800 # 16 is the size of the patch section metadata
def getPatch(self, index):
offs = self.getPatchOffset(self.data, index)
return self.data[offs:offs + 0x0800]
def getOriginalPatch(self, index):
offs = self.getPatchOffset(self.orig, index)
return self.orig[offs:offs + 0x0800]
def setPatch(self, index, data):
offs = self.getPatchOffset(self.data, index)
for i in range(len(data)):
self.data[offs + i] = data[i]
def getPatchNumber(self, ndx):
bank = chr((ndx >> 6) + 65)
subbank = ((ndx >> 3) & 7) + 1
patch = (ndx & 7) + 1
return f"{bank}{subbank}:{patch}"
def updatePatch(self, toIndex, newPatch):
self.setPatch(toIndex, newPatch)
self.patchList.delete(toIndex)
newLabel = self.getPatchNumber(toIndex) + " " + self.getPatchName(self.data, toIndex)
origPatch = self.getOriginalPatch(toIndex)
if origPatch != newPatch:
newLabel += " (was : " + self.getPatchName(self.orig, toIndex) + ")"
self.patchList.insert(toIndex, newLabel)
toIndex += 1
if toIndex == 256:
toIndex = 0
self.setSelection(toIndex)
self.updateButtons()
def copyPatchFrom(self, src):
if src.cursel == None or self.cursel == None:
return
fromIndex = src.cursel[0]
toIndex = self.cursel[0]
newPatch = src.getPatch(fromIndex)
self.updatePatch(toIndex, newPatch)
def setSelection(self, toIndex):
self.patchList.selection_clear(0, END)
self.patchList.select_set(toIndex)
self.patchList.see(toIndex)
self.cursel = self.patchList.curselection()
self.updateButtons()
def getSynthName(self, data):
patchName = data[96:96 + 16]
return patchName.decode("UTF-8").strip()
def getPatchName(self, data, index):
offs = self.getPatchOffset(data, index)
patchName = data[offs + 16:offs + 32]
return patchName.decode("UTF-8").strip()
def setPatchName(self, data, index, newName):
if len(newName) == 0:
messagebox.showerror("Value required", "The patch name cannot be empty")
return
if len(newName) > 16:
messagebox.showwarning("Value truncated", "The patch name will be truncated to 16 characters")
newName = newName[0:16]
if len(newName) < 16:
newName = newName.ljust(16, ' ')
offs = self.getPatchOffset(data, index)
arr = bytearray()
arr.extend(newName.encode())
data[offs + 16:offs + 32] = arr
self.updatePatch(index, self.getPatch(index))
def populateList(self):
data = self.data
self.patchList.delete(0,END)
for i in range(256):
self.patchList.insert('end', self.getPatchNumber(i) + " " + self.getPatchName(data, i))
self.cursel = None
self.updateButtons()
def loadFile(self, fileName):
n = getFileSize(fileName)
if n == -1:
messagebox.showerror("Invalid file", f"The file '{fileName}' does not exist")
return None;
if n < 0x001E8800:
messagebox.showerror("Invalid file", f"The file '{fileName}' is invalid")
return None;
f = open(fileName, mode="rb")
data = f.read()
f.close()
data = bytearray(data) # Convert from bytes() to bytearray() because we want to be able to change the data
#print("&" + self.getSynthName(data) + "&")
if b"N\0SVD5" != data[0:6]:
messagebox.showerror("Invalid file", f"The file '{fileName}' is invalid (header should be 'N.SVD5' but is {data[0:6]})")
return None;
return data;
diffWindow = None
# class DiffWindow(Frame):
# def __init__(self, parent):
# super().__init__(parent)
# self.parent = self
# self.create_widgets()
# def onBeforeExit(self):
# global diffWindow
# diffWindow.destroy()
# diffWindow = None
# diffWindow = newWindow = Toplevel(self)
# newWindow.protocol("WM_DELETE_WINDOW", self.onBeforeExit)
# newWindow.title("Patch comparison")
# newWindow.geometry("1200x600")
# f = tk.Frame(newWindow)
# f.config(bg ="pink")
# f.pack(expand = True, fill = BOTH)
class ButtonBar(Frame):
def __init__(self, parent, leftList1, rightList1):
super().__init__(parent)
self.leftList = leftList1
self.rightList = rightList1
self.parent = self
self.create_widgets()
def copyLeftToRight(button):
button.rightList.copyPatchFrom(button.leftList)
def copyRightToLeft(button):
button.leftList.copyPatchFrom(button.rightList)
def onBeforeExit(self):
global diffWindow
if not diffWindow == None:
diffWindow.destroy()
diffWindow = None
def openDiffWindow(self, prefs):
global diffWindow
if not diffWindow == None:
self.onBeforeExit()
return
# Toplevel object which will
# be treated as a new window
diffWindow = newWindow = Toplevel(self)
newWindow.protocol("WM_DELETE_WINDOW", self.onBeforeExit)
newWindow.title("Patch comparison")
newWindow.geometry("500x600")
f = tk.Frame(newWindow)
f.config(bg ="pink")
f.pack(expand = True, fill = BOTH)
self.lst = PatchDiffViewer(f, prefs)
self.lst.pack(expand = True, fill = BOTH) #.grid(row = 0, column = 0, sticky = 'nswe', padx = 4, pady = 4)
def create_widgets(self):
# create the widgets
btnCopyRight = Button(self, text = '>>', command=self.copyLeftToRight)
btnCopyLeft = Button(self, text = '<<', command=self.copyRightToLeft)
# btnDiff = Button(self, text = 'Diff', command=self.openDiff)
# place the widgets
btnCopyRight.place(relx=.5, rely=.5, y=-20, height=30, anchor=CENTER)
btnCopyLeft.place(relx=.5, rely=.5, y=20, height=30, anchor=CENTER)
# btnDiff.place(relx=.5, rely=.5, y=60, height=30, anchor=CENTER)
# It is possible to pass one or two file names from the command line that will be opened
App('JD-08/JX-08 Patch Manager v' + VERSION, (650,350), *sys.argv)