-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetroarchCheatFileReducer.py
99 lines (84 loc) · 3.3 KB
/
RetroarchCheatFileReducer.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
"""
Retroarch Cheat File Reducer
Scripted by: RB
AKA: Git-Forked
Source Code: https://github.com/Git-Forked/RetroarchCheatFileReducer/
License: GPL-3.0
Requisites:
pip install easygui
or set a file manually.
"""
"""
SIMPLE CONFIGURATION
"""
# Print extra data to screen. True or False.
verbose = False
# Use EasyGUI. True or False.
use_easygui = True
"""
ADVANCED CONFIGURATION
"""
# Use EasyGUI to choose a file or set one manually @ file_path
if use_easygui == True:
import easygui
file_path = easygui.fileopenbox()
else:
# Alternately set a file manually to work with.
# Do this if your choice to use EasyGUI is False.
file_path = "/home/user/Desktop/Test.cht"
"""
PROCESS
"""
print("Opened file: " + file_path + "\n")
# n for cheat incrementation, always starts at 0
n = 0
# substring_previous always set to "" to delcare empty string variable
substring_previous = ""
# replaced_content always set to "" to delcare empty string variable
replaced_content = ""
# Open the file for reading and make edits to the file content.
with open(file_path, 'r') as file:
for line in file:
# Strip line breaks
line = line.strip()
# Ignore empty lines
if not line.isspace():
# Ignore the "cheats = ??" line.
if not "cheats" in line:
# Make sure line contains "cheats".
if "cheat" in line:
# Get the substring number. (String in string technique.)
start = line.index('cheat') # From "cheat"
end = line.index('_',start+1) # To "_"
substring = line[start+5:end] # The substring number
# Check whether or not to increment n.
if substring_previous != "" and substring_previous != substring:
# Another cheat was found.
if verbose == True: print("\n-----Next cheat found.")
# increment n by 1
n+=1
# Replacement text as new_line.
new_line = "\n" + line.replace(substring, str(n), 1)
else:
# Replacement text as new_line.
new_line = line.replace(substring, str(n), 1)
# Verbose printing to console.
if verbose == True: print("[Replace " + substring + " with " + str(n) + "]")
if verbose == True: print("Original : " + line)
if verbose == True: print("Replaced : " + new_line)
# Concatenate the new string and add an end-line break
replaced_content = replaced_content + new_line + "\n"
# Save a variable for substring as substring_previous
substring_previous = substring
# Write cheat total to top of file.
replaced_content = "cheats = " + str(n+1) + "\n\n" + replaced_content
# Print new content to the console.
if verbose == True: print("\nNew content: \n\n" + replaced_content)
# Print total cheats to console.
print("\nTotal cheats processed: " + str(n+1) + "\n")
# Write new content to file.
if use_easygui == True:
file_path = easygui.filesavebox()
with open(file_path, 'w') as file:
file.write(replaced_content)
print("Filed saved as: " + file_path + "\n")