-
Notifications
You must be signed in to change notification settings - Fork 0
/
P5TextSwapper.py
854 lines (715 loc) · 36.5 KB
/
P5TextSwapper.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
import os
import subprocess
import re
import shutil
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import customtkinter
import sv_ttk
import configparser
import logging
from tkinter import filedialog
# Make a config parser
config = configparser.ConfigParser()
# Read the config file if it exists
if os.path.isfile('config.ini'):
config.read('config.ini')
else:
# If it doesn't exist, create it with default values
config['Folders'] = {'mod_folder': '',
'language_folder': '', 'output_folder': '', 'game': 'Persona 5 Royal'}
with open('config.ini', 'w') as configfile:
config.write(configfile)
# Get the saved values or set default values if they don't exist
mod_folder = config.get('Folders', 'mod_folder', fallback='')
language_folder = config.get('Folders', 'language_folder', fallback='')
output_folder = config.get('Folders', 'output_folder', fallback='')
game = config.get('Folders', 'game', fallback='Persona 5 Royal')
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
root = customtkinter.CTk()
def disable_button():
run_button.config(state="disabled")
dropdown.config(state="disable")
mod_folder_button.config(state="disable")
language_folder_button.config(state="disable")
output_folder_button.config(state="disable")
mod_folder_entry.config(state="disable")
language_folder_entry.config(state="disable")
output_folder_entry.config(state="disable")
def enable_button():
run_button.config(state="normal")
dropdown.config(state="normal")
mod_folder_button.config(state="normal")
language_folder_button.config(state="normal")
output_folder_button.config(state="normal")
mod_folder_entry.config(state="normal")
language_folder_entry.config(state="normal")
output_folder_entry.config(state="normal")
# Log box with scrollbar and no text editing
log_box = tk.Text(root, height=20, width=50)
log_box.grid(row=5, columnspan=3, column=0, sticky='ew')
# use columnspan to make the widget span multiple columns
# log_box.grid(columnspan=2)
# Create a scrollbar and associate it with log_box
scrollbar = ttk.Scrollbar(root, command=log_box.yview)
scrollbar.grid(row=5, column=4, sticky='ns')
# Disable editing
log_box.config(state=tk.DISABLED, yscrollcommand=scrollbar.set)
def on_select(event):
# Make the variable global so it can be used
global game
game = dropdown.get()
# Clear the log box
log_box.config(state=tk.NORMAL)
log_box.delete('1.0', tk.END)
log_box.config(state=tk.DISABLED)
logging.info(f"Game: {game}")\
def log_to_box(message):
# Escribir en el Text widget
log_box.config(state=tk.NORMAL)
log_box.insert(tk.END, message + '\n', ('stdout',))
log_box.see(tk.END)
log_box.config(state=tk.DISABLED)
root.update()
logging.info = log_to_box
# Options for the dropdown
options = ["Persona 5 Royal", "Persona 5", "Persona 4 Golden"]
# Make the dropdown
dropdown = ttk.Combobox(root, values=options)
# Check if the saved value in the config file exists in the options list
if game in options:
# Set the saved value in the config file as the default option
dropdown.current(options.index(game))
else:
dropdown.current(0)
# Disable write in the dropdown
dropdown.configure(state="readonly")
# Add the on_select function to the dropdown
dropdown.bind("<<ComboboxSelected>>", on_select)
# Run the on_select function when the program starts
on_select(None)
# Set the position of the dropdown in the window
dropdown.grid(row=3, column=0, padx=10, pady=10)
# specify the size of the window
root.geometry("900x600")
# disable resizing the GUI
root.resizable(False, False)
def browse_mod_folder():
mod_folder = filedialog.askdirectory()
mod_folder_entry.delete(0, tk.END)
mod_folder_entry.insert(0, mod_folder)
def browse_language_folder():
language_folder = filedialog.askdirectory()
language_folder_entry.delete(0, tk.END)
language_folder_entry.insert(0, language_folder)
def browse_output_folder():
output_folder = filedialog.askdirectory()
output_folder_entry.delete(0, tk.END)
output_folder_entry.insert(0, output_folder)
def run_program():
if not mod_folder_entry.get() or not language_folder_entry.get() or not output_folder_entry.get():
messagebox.showerror(
"Error", "Please fill in all fields.")
return
disable_button()
# Clear the log box
log_box.config(state=tk.NORMAL)
log_box.delete('1.0', tk.END)
log_box.config(state=tk.DISABLED)
mod_folder = mod_folder_entry.get()
language_folder = language_folder_entry.get()
output_folder = output_folder_entry.get()
# Get the name of the last folder in the paths
mod_folder_name = os.path.basename(os.path.normpath(mod_folder))
language_folder_name = os.path.basename(
os.path.normpath(language_folder))
output_folder_name = os.path.basename(os.path.normpath(output_folder))
# List files and folders in mod_folder including those in subfolders and full path
mod_files_list = []
for root, dirs, files in os.walk(mod_folder):
for file in files:
path = os.path.join(root, file)
mod_files_list.append(path)
# Delete the value of mod_folder from the path
mod_files_list = [x.replace(mod_folder, '') for x in mod_files_list]
# List files and folders in language_folder including those in subfolders and full path
language_files_list = []
for root, dirs, files in os.walk(language_folder):
for file in files:
path = os.path.join(root, file)
language_files_list.append(path)
# Delete the value of language_folder from the path
language_files_list = [x.replace(language_folder, '')
for x in language_files_list]
logging.info(
"Deleting files from language_folder that don't exist in mod_folder")
# If the file exists in language_folder and not in mod_folder, delete it from language_folder
for file in language_files_list:
full_path = language_folder + file
if file not in mod_files_list:
os.remove(full_path)
logging.info("Copying files missing in language_folder from mod_folder")
# If the file exists in mod_folder and not in language_folder, copy it from mod_folder to language_folder to avoid errors
for file in mod_files_list:
full_path = mod_folder + file
if file not in language_files_list:
# New path for the file
new_path = language_folder + file
logging.info("Copying " + file + " to " + new_path)
shutil.copy(full_path, new_path)
logging.info("Deleting files with wrong extension")
language_files_list_updated = []
for root, dirs, files in os.walk(language_folder):
for file in files:
path = os.path.join(root, file)
language_files_list_updated.append(path)
# Delete the path of language_folder from the list
language_files_list_updated = [x.replace(language_folder, '')
for x in language_files_list_updated]
mod_files_list_updated = []
for root, dirs, files in os.walk(mod_folder):
for file in files:
path = os.path.join(root, file)
mod_files_list_updated.append(path)
# Delete the path of mod_folder from the list
mod_files_list_updated = [x.replace(mod_folder, '')
for x in mod_files_list_updated]
# Delete files from language_folder that don't end with .bf or .bmd
for file in language_files_list_updated:
full_path = language_folder + file
if not file.lower().endswith((".bf", ".bmd")):
# print(f"El archivo {file} no es .bf o .bmd, se eliminará.")
os.remove(full_path)
for file in mod_files_list_updated:
full_path = mod_folder + file
if not file.lower().endswith((".bf", ".bmd")):
# print(f"El archivo {file} no es .bf o .bmd, se eliminará.")
os.remove(full_path)
# Path to the dependencies
personaeditor_path = os.path.join(
'dependencies', 'personaeditor', 'personaeditorcmd.exe')
atlus_script_tools_path = os.path.join(
"dependencies", "atlusscripttools", "AtlusScriptCompiler.exe")
# Current directory
current_dir = os.path.abspath(os.path.dirname(__file__))
# Define the functions that will be executed in each case
# AtlusScriptCompiler functions
def ASCDecompile(input_file_path):
input_file_name = os.path.basename(input_file_path)
if game == "Persona 5 Royal":
logging.info(
f"Decompiling BMD file: {input_file_name} with P5R library")
subprocess.run([atlus_script_tools_path, input_file_path,
"-Decompiled", "-Library", "P5R", "-Encoding", "P5"])
elif game == "Persona 5":
logging.info(
f"Decompiling BMD file: {input_file_name} with P5 library")
subprocess.run([atlus_script_tools_path, input_file_path,
"-Decompiled", "-Library", "P5", "-Encoding", "P5"])
elif game == "Persona 4 Golden":
logging.info(
f"Decompiling BMD file: {input_file_name} with P4G library")
subprocess.run([atlus_script_tools_path, input_file_path,
"-Decompiled", "-Library", "P4G", "-Encoding", "P5"])
def ASCCompile(input_file_path):
input_file_name = os.path.basename(input_file_path)
if game == "Persona 5 Royal":
logging.info(
f"Compiling BMD file: {input_file_name} with P5R library")
output_file_path = os.path.splitext(input_file_path)[0] + '.bmd'
subprocess.run([atlus_script_tools_path, input_file_path, "-Out",
output_file_path, "-Compile", "-OutFormat", "V1BE", "-Library", "P5R", "-Encoding", "P5"])
elif game == "Persona 5":
logging.info(
f"Compiling BMD file: {input_file_name} with P5 library")
output_file_path = os.path.splitext(input_file_path)[0] + '.bmd'
subprocess.run([atlus_script_tools_path, input_file_path, "-Out",
output_file_path, "-Compile", "-OutFormat", "V1BE", "-Library", "P5", "-Encoding", "P5"])
elif game == "Persona 4 Golden":
logging.info(
f"Compiling BMD file: {input_file_name} with P4G library")
output_file_path = os.path.splitext(input_file_path)[0] + '.bmd'
subprocess.run([atlus_script_tools_path, input_file_path, "-Out",
output_file_path, "-Compile", "-OutFormat", "V1", "-Library", "P4G", "-Encoding", "P5"])
# PersonaEditor functions
def PEExport(input_file_path):
subprocess.run([personaeditor_path, input_file_path, '-expall'])
def PEImport(input_file_path):
subprocess.run([personaeditor_path, input_file_path,
'-impall', '-save', input_file_path])
def des_case_bmd(name_file):
ASCDecompile(name_file)
def des_case_bf(path_file):
PEExport(path_file)
# the name_file is a path, so we need to get the name of the file
name_file = os.path.basename(path_file)
logging.info(f"Extracting all files from bf file: {path_file}")
# change the extension of the file to .bmd
name_file_bmd = os.path.splitext(path_file)[0] + '.bmd'
# the name of the file without the extension
name_file_only = os.path.splitext(name_file)[0]
# search for the bf file on the mod_folder
for root, dirs, files in os.walk(mod_folder):
for file in files:
if file.lower() == name_file.lower() and file.lower().endswith('.bf'):
logging.info(
f"Searching for the bf file: {name_file} in the mod folder")
name_file_bf_mod = os.path.join(root, file)
rel_path = os.path.relpath(name_file_bf_mod, mod_folder)
output_file = os.path.join(output_folder, rel_path)
output_dir = os.path.dirname(output_file)
# create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
shutil.copy(name_file_bf_mod, output_file)
break
# check if the file exists
if os.path.isfile(name_file_bmd):
des_case_bmd(name_file_bmd)
# dirname
name_file_dirname = os.path.dirname(path_file)
# get all the .dat files in the folder
# get all the .dat files in the folder
dat_files = []
for root, dirs, files in os.walk(name_file_dirname):
for file in files:
if file.endswith('.DAT'):
dat_files.append(os.path.join(root, file))
# if the files name start with name_file.name, copy the .dat files to the output folder
for dat_file in dat_files:
if dat_file.startswith(name_file_dirname):
# replace the mod_folder or language_folder with the output_folder
name_file_dirname_output = name_file_dirname.replace(
mod_folder, output_folder).replace(language_folder, output_folder)
# create the output directory if it doesn't exist
os.makedirs(os.path.dirname(
name_file_dirname_output), exist_ok=True)
# copy the file to the output directory
shutil.copy(dat_file, name_file_dirname_output)
def com_case_bmd(name_file):
ASCCompile(name_file)
# replace
def process_msg(name_file):
# get the name of the file without the extension and add .msg
name_file_msg = name_file
# replace the mod_folder with language_folder
name_file_msg_editlang = name_file_msg.replace(
mod_folder, language_folder)
# replace the language_folder with mod_folder
name_file_msg_editmod = name_file_msg.replace(
language_folder, mod_folder)
# name_file_msg get the name_file_msg, and change the Mod folder for the Language folder
name_file_msg_lang = os.path.join(
name_file_msg_editlang)
logging.info(f"Processing {name_file_msg_lang}")
# name_file_msg from the mod folder
name_file_msg_mod = os.path.join(
name_file_msg_editmod)
name_file_msg_mod_msg = name_file_msg
# get the route but on the output folder
name_file_with_file = os.path.join(
output_folder, os.path.basename(name_file_msg_mod_msg))
mod_msg_names = [] # list with the names of the msg
lang_msg_names = [] # list with the names of the msg
excluded_keyslist = ['Ryuji', 'Morgana', 'Yusuke', 'Ann', 'Makoto', 'Futaba', 'Haru', 'Goro', 'Akechi', 'Kasumi', 'Yoshisawa', 'Kamoshida', 'Madarame', 'Kaneshiro', 'Okumura', 'Shido', 'Yaldabaoth', 'Sojiro', 'Sae', 'Nijima', 'Igor', 'Caroline', 'Justine', 'Tae', 'Takemi',
'Munehisa', 'Chihaya', 'Mifune', 'Yuki', 'Mishima', 'Ohya', 'Kawakami', 'Toranosuke', 'Shinya', 'Oya', 'Ichiko', 'Sadayo', 'Hifumi', 'Togo', 'Sugimura', 'Kawanabe', 'Iwai', 'Shiho', 'Wakaba', 'Maruki', 'Jose', 'Takuto', 'Joker', 'Kitagawa', 'Niijima', 'Sakamoto', 'Takamaki', 'Sakura', 'Sumire']
with open(name_file_msg_lang, 'r', encoding='utf-8-sig', errors='ignore') as f:
for line in f:
if line.startswith('[msg'):
# Remove everything before the second space and the [ and ]
fields = line.strip().split(' ', 2)
if len(fields) >= 3:
key = fields[2]
key = key[1:-1]
if key not in excluded_keyslist:
# Save the key in the msg_names list
lang_msg_names.append(key)
else:
logging.warning(
f"Warning: Line '{line.strip()}' in {name_file_msg_lang} is not in the expected format.")
# Read the lines of the mod .msg file and save the lines that start with '[msg'
with open(name_file_msg_mod, 'r', encoding='utf-8-sig', errors='ignore') as f:
for line in f:
if line.startswith('[msg'):
# Remove everything before the second space and the [ and ]
fields = line.strip().split(' ', 2)
if len(fields) >= 3:
key = fields[2]
key = key[1:-1]
if key not in excluded_keyslist:
# Save the line in the msg_names list
mod_msg_names.append(key)
else:
logging.warning(
f"Warning: Line '{line.strip()}' in {name_file_msg_mod} is not in the expected format.")
# Get the two lists and join them by index
msg_names = zip(mod_msg_names, lang_msg_names)
# Make the dictionary from the joined lists
dic_names = dict(msg_names)
lang_msg_lines = {}
match = r"\[[^\[\]]*\]|\[[^\[]*[^\s\[\]]\]"
# Read the lines of the language .msg file and save the lines that start with '[msg'
with open(name_file_msg_lang, 'r', encoding='utf-8-sig', errors='ignore') as f:
for line in f:
if line.startswith('[msg'):
value = []
while True:
next_line = next(f).strip()
if next_line:
# Apply regular expression to remove text between brackets
result = []
position = 0
while True:
coincidencia = re.search(
match, next_line[position:])
if coincidencia:
result.append(coincidencia.group(0))
position += coincidencia.end()
else:
break
# Check if the next character is a space or a bracket
if len(next_line[position:].strip()) == 0 or next_line[position:position+5] == "[var " or next_line[position:position+7] == "[f 4 1]" or next_line[position:position+7] == "[f 4 2]" or next_line[position:position+5] == "[clr " or next_line[position] != "[":
break
# Remove the text between brackets and the characters "[w]" and "[e]"
for item in result:
next_line = next_line.replace(item, "")
next_line = next_line.replace("[w]", "").replace(
"[e]", "")
# Fix compilation error, replace "/" with "l"
value.append(next_line)
else:
break
lang_msg_lines[line.strip()] = "$f$".join(value)
if line.startswith('[sel'):
value = []
while True:
next_line = next(f).strip()
if next_line:
value.append(next_line)
else:
break
lang_msg_lines[line.strip()] = "$f$".join(value)
mod_msg_lines = {}
# Open mod file and process each line
with open(name_file_msg_mod, 'r', encoding='utf-8-sig', errors='ignore') as f:
for line in f:
if line.startswith('[msg'):
value = []
while True:
next_line = next(f).strip()
if next_line:
# Apply regular expression to remove text between brackets
result = []
position = 0
while True:
coincidencia = re.search(
match, next_line[position:])
if coincidencia:
result.append(coincidencia.group(0))
position += coincidencia.end()
else:
break
# Check if the next character is a space or a bracket
if len(next_line[position:].strip()) == 0 or next_line[position:position+5] == "[var " or next_line[position:position+7] == "[f 4 1]" or next_line[position:position+7] == "[f 4 2]" or next_line[position:position+5] == "[clr " or next_line[position] != "[":
break
# Remove the text between brackets and the characters "[w]" and "[e]"
for item in result:
next_line = next_line.replace(item, "")
next_line = next_line.replace("[w]", "").replace(
"[e]", "")
# Fix compilation error, replace "/" with "l"
value.append(next_line)
else:
break
mod_msg_lines[line.strip()] = "$f$".join(value)
if line.startswith('[sel'):
value = []
while True:
next_line = next(f).strip()
if next_line:
value.append(next_line)
else:
break
mod_msg_lines[line.strip()] = "$f$".join(value)
# Delete [name] from key
for key in mod_msg_lines.copy().keys():
# delete all next from the second space
new_key = key.split(' ', 2)[0] + ' ' + key.split(' ', 2)[1]
# remove the last character (']') from the new key
new_key = new_key.rstrip(']')
# replace the key with the new key
mod_msg_lines[new_key] = mod_msg_lines.pop(key)
for key in lang_msg_lines.copy().keys():
# delete all next from the second space
new_key = key.split(' ', 2)[0] + ' ' + key.split(' ', 2)[1]
# remove the last character (']') from the new key
new_key = new_key.rstrip(']')
# replace the key with the new key
lang_msg_lines[new_key] = lang_msg_lines.pop(key)
# Get the common keys
common_keys = set(mod_msg_lines.keys()) & set(lang_msg_lines.keys())
# Make a new dictionary with the corresponding keys and values of both dictionaries
msg_matches = {}
# Add the keys and values of the common keys
for key in common_keys:
mod_msg_text = mod_msg_lines[key].strip()
lang_msg_text = lang_msg_lines[key].strip()
msg_matches[mod_msg_text] = lang_msg_text
keys_to_delete = []
keys_to_add = []
for key, value in msg_matches.items():
if "$f$" in key:
original_key = key
key = key.split("$f$")
value = value.split("$f$")
for i in range(len(key)):
if i < len(key) and i < len(value):
keys_to_add.append((key[i], value[i]))
keys_to_delete.append(original_key)
if "$f$" in value:
original_key = key
key = key.split("$f$")
value = value.split("$f$")
for i in range(len(key)):
if i < len(key) and i < len(value):
keys_to_add.append((key[i], value[i]))
keys_to_delete.append(original_key)
# Delete the keys from the list
for key in keys_to_delete:
del msg_matches[key]
# Add the keys from the list
for key, value in keys_to_add:
msg_matches[key] = value
# Check if the file has the .msg extension
if os.path.splitext(name_file_msg)[1].lower() != '.msg':
return
# Change language_folder or mod_folder to output_folder
name_file_msg_mod_msg2 = name_file_msg_mod_msg.replace(
language_folder, output_folder).replace(mod_folder, output_folder)
# delete the file_name from the path
file_name = os.path.basename(name_file_msg_mod_msg2)
# delete the file_name from the path
output_folder_msg_withoutroute = name_file_msg_mod_msg2.replace(
file_name, '')
logging.info(f"Coping {name_file_msg_mod} to {name_file_msg_mod_msg2}")
# Check if the output folder exists, otherwise create it
if not os.path.exists(output_folder_msg_withoutroute):
os.makedirs(output_folder_msg_withoutroute)
# Copy the file to the output folder
shutil.copy(name_file_msg_mod_msg, name_file_msg_mod_msg2)
num_lines_replaced = 0
# Replace the lines of the name_file.msg file in the output_folder folder that match key with the value of msg_matches[key]
if os.path.isfile(name_file_msg_mod_msg2) and name_file_msg_mod_msg2.lower().endswith('.msg'):
with open(name_file_msg_mod_msg2, 'r', encoding='utf-8-sig', errors='ignore') as f:
content = f.read()
for key, value in msg_matches.items():
content, num_replaced = content.replace(
key, value), content.count(key)
num_lines_replaced += num_replaced
with open(name_file_msg_mod_msg2, 'w', encoding='utf-8-sig', errors='ignore') as f:
f.write(content)
# Replace "/" with "l"
if os.path.isfile(name_file_msg_mod_msg2) and name_file_msg_mod_msg2.lower().endswith('.msg'):
with open(name_file_msg_mod_msg2, 'r', encoding='utf-8-sig', errors='ignore') as f:
content = f.readlines()
with open(name_file_msg_mod_msg2, 'w', encoding='utf-8-sig', errors='ignore') as f:
for line in content:
# check if the line not start with [msg, [sel, // or is empty
if not line.startswith('[msg') and not line.startswith('//') and not line.startswith('[sel') and line.strip():
line = line.replace('/', 'l')
f.write(line)
# Add [f 0 5 -258] at the start of all lines that do not start with [msg, [sel or are empty
with open(name_file_msg_mod_msg2, 'r', encoding='utf-8-sig', errors='ignore') as f:
content = f.readlines()
with open(name_file_msg_mod_msg2, 'w', encoding='utf-8-sig', errors='ignore') as f:
for line in content:
if line.startswith('[msg') or line.startswith('//') or line.startswith('[sel') or not line.strip():
f.write(line)
else:
f.write('[f 0 5 -258]' + line)
# Replace the lines with the dic_names dictionary, search the keys and replace them with their values in the name_file_with_file file
with open(name_file_msg_mod_msg2, 'r', encoding='utf-8-sig', errors='ignore') as f:
content = f.readlines()
with open(name_file_msg_mod_msg2, 'w', encoding='utf-8-sig', errors='ignore') as f:
for line in content:
if line.startswith("[msg"):
for key, value in dic_names.items():
line, num_replaced = line.replace(
key, value), line.count(key)
num_lines_replaced += num_replaced
f.write(line)
if num_lines_replaced == 0:
logging.info(f"No lines replaced in {name_file_msg_mod_msg2}")
elif num_lines_replaced == 1:
logging.info(f"1 line replaced in {name_file_msg_mod_msg2}")
else:
logging.info(
f"{num_lines_replaced} lines replaced in {name_file_msg_mod_msg2}")
# Delete all files that are not .bin, .bmd, .pak or .bf
for folder in [mod_folder, language_folder]:
for file in os.scandir(folder):
if file.is_file() and not file.name.lower().endswith((".bin", ".bmd", ".pak", ".bf")):
os.remove(file.path)
# Check all types of files in the "Mod" folder
# Read all files in the root and its subfolders
for folder_name, subfolders, files in os.walk(mod_folder):
# Read all files in the current folder
for name_file in files:
logging.info(f"Processing file: {name_file}")
file_extension = name_file.split('.')[-1]
switcher = {
'bmd': des_case_bmd,
'bf': des_case_bf,
'BMD': des_case_bmd,
'BF': des_case_bf
}
if file_extension in switcher:
logging.info(f"Processing switcher file: {name_file}")
switcher[file_extension](os.path.join(folder_name, name_file))
# Check all types of files in the "Language" folder
# Read all files in the root and its subfolders
for folder_name, subfolders, files in os.walk(language_folder):
# Read all files in the current folder
for name_file in files:
logging.info(f"Processing file: {name_file}")
file_extension = name_file.split('.')[-1]
switcher = {
'bmd': des_case_bmd,
'bf': des_case_bf,
'BMD': des_case_bmd,
'BF': des_case_bf
}
if file_extension in switcher:
logging.info(f"Processing switcher file: {name_file}")
switcher[file_extension](os.path.join(folder_name, name_file))
# Proccess all .msg files in the "Mod" folder and make the changes in the "Output" folder
for root, dirs, files in os.walk(mod_folder):
for name_file in files:
if name_file.lower().endswith('.msg'):
# print(f"Processing file: {root}/{name_file}")
process_msg(os.path.join(root, name_file))
for root, dirs, files in os.walk(output_folder):
for msg_file in files:
if msg_file.lower().endswith('.msg'):
logging.info(f"Compiling {msg_file}")
ASCCompile(os.path.join(root, msg_file))
# Change all the endwith .bmd.bmd to .bmd
for root, dirs, files in os.walk(output_folder):
for name in files:
if name.lower().endswith('.bmd.bmd'):
old_path = os.path.join(root, name)
new_path = os.path.join(root, name[:-8] + ".bmd")
logging.info(f"Renaming {old_path} to {new_path}")
try:
os.rename(old_path, new_path)
except FileExistsError:
logging.info(
f"Skipping {old_path} as {new_path} already exists")
continue
# Import all .bf files in the "Output" folder with PEImport
for root, dirs, files in os.walk(output_folder):
for mod_file in files:
if mod_file.lower().endswith('.bf'):
mod_file_path = os.path.join(root, mod_file)
logging.info(f"Importing {mod_file} in {root}")
PEImport(mod_file_path)
mod_file_name_new = mod_file.split('.')[0]
# print(f"Deleting extra files from {output_folder}...")
def delete_files_not_in_list(folder_path, files_list):
# Make a list with all the files in the folder including the ones in the subfolders
Del_files = []
for root, dirs, files in os.walk(folder_path):
for name in files:
Del_files.append(os.path.join(root, name))
Mod_List_Keep = []
for i in range(len(files_list)):
Mod_List_Keep.append(
folder_path + files_list[i])
# Delete all lines from Del_files that are in Mod_List_Keep
for i in range(len(Mod_List_Keep)):
for j in range(len(Del_files)):
if Mod_List_Keep[i].lower() == Del_files[j].lower():
Del_files[j] = ""
# Delete all the empty lines
while "" in Del_files:
Del_files.remove("")
# Delete the files that are left in the keep_files list
for file in Del_files:
try:
logging.info(f"Deleting {file}")
os.remove(file)
except FileNotFoundError:
logging.info(f"Skipping {file} as it doesn't exist")
continue
delete_files_not_in_list(output_folder, mod_files_list)
delete_files_not_in_list(mod_folder, mod_files_list)
delete_files_not_in_list(language_folder, language_files_list)
def delete_empty_folders(path):
for root, dirs, files in os.walk(path, topdown=False):
for name in dirs:
full_path = os.path.join(root, name)
if not os.listdir(full_path): # check if directory is empty
os.rmdir(full_path)
else:
# recursively delete all files and subfolders
delete_empty_folders(full_path)
delete_empty_folders(output_folder)
delete_empty_folders(mod_folder)
delete_empty_folders(language_folder)
logging.info("Done!")
enable_button()
tk.messagebox.showinfo("Finished", "Replaced all the names in the mod!")
# Mod folder
mod_folder_label = ttk.Label(root, text="Mod folder:", background="#242424")
mod_folder_entry = ttk.Entry(root, width=60)
mod_folder_button = ttk.Button(root, text="Browse", command=browse_mod_folder)
mod_folder_entry.insert(0, mod_folder) # Insert the saved value
mod_folder_label.grid(row=0, column=0, padx=5, pady=10)
mod_folder_entry.grid(row=0, column=1, padx=5, pady=10)
mod_folder_button.grid(row=0, column=2, padx=5, pady=10)
# Language folder
language_folder_label = ttk.Label(
root, text="Language folder:", background="#242424")
language_folder_entry = ttk.Entry(root, width=60)
language_folder_button = ttk.Button(
root, text="Browse", command=browse_language_folder)
language_folder_entry.insert(0, language_folder) # Insert the saved value
language_folder_label.grid(row=1, column=0, padx=5, pady=10)
language_folder_entry.grid(row=1, column=1, padx=5, pady=10)
language_folder_button.grid(row=1, column=2, padx=5, pady=10)
# Output folder
output_folder_label = ttk.Label(
root, text="Output folder:", background="#242424")
output_folder_entry = ttk.Entry(root, width=60)
output_folder_button = ttk.Button(
root, text="Browse", command=browse_output_folder)
output_folder_entry.insert(0, output_folder) # Insert the saved value
output_folder_label.grid(row=2, column=0, padx=5, pady=10)
output_folder_entry.grid(row=2, column=1, padx=5, pady=10)
output_folder_button.grid(row=2, column=2, padx=5, pady=10)
# Run button
run_button = ttk.Button(root, text="Replace", command=run_program)
run_button.grid(row=4, column=1, padx=0, pady=15)
def save_config():
# Save the values in the config file
config['Folders'] = {
'mod_folder': mod_folder_entry.get(),
'language_folder': language_folder_entry.get(),
'output_folder': output_folder_entry.get(),
'game': game
}
with open('config.ini', 'w') as configfile:
config.write(configfile)
def on_closing():
if tk.messagebox.askokcancel("Quit", "Do you want to quit?"):
save_config()
root.destroy()
# Run the on_closing function when the window is closed
root.protocol("WM_DELETE_WINDOW", on_closing)
sv_ttk.set_theme("dark")
# set theme dark in cttk
customtkinter.set_appearance_mode("dark")
root.iconbitmap("dependencies/test2.ico")
root.title("Persona 5 Text Swapper")
root.mainloop()