Skip to content

Commit 97d64a0

Browse files
authored
Change .ini format slightly
This change allows mods with the same name, but different parent folders (games) to coexist and avoids problems with naming a mod "options" by adding forbidden characters to the options section.
1 parent b90cf3c commit 97d64a0

1 file changed

Lines changed: 40 additions & 26 deletions

File tree

manager.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def copymod(src, dst, filelist=None, primary=True): # making this recursive is
6060
if filelist is None: # dang mutable defaults
6161
filelist = []
6262
# collect all files first so we can show progress
63-
# todo: display something while searching for files
63+
# todo: maybe display something while searching for files
6464
# noticeable blank screen with >~50 files
6565
for item in os.listdir(src):
6666
s = os.path.join(src, item)
@@ -78,7 +78,8 @@ def copymod(src, dst, filelist=None, primary=True): # making this recursive is
7878
if config.get(section, option) == d:
7979
fileowner = section
8080
if fileowner:
81-
print("The mod \"" + fileowner + "\" is already using \"" + d + "\"\nReplace the file?\n")
81+
print("The mod \"" + fileowner.split("|")[1] + "\" from \"" + fileowner.split("|")[0] +
82+
"\" is already using \"" + d + "\"\nReplace the file?\n")
8283
else:
8384
print(d + " already exists.\nReplace it?\n")
8485
sys.stdout.flush()
@@ -127,30 +128,34 @@ def copymod(src, dst, filelist=None, primary=True): # making this recursive is
127128

128129
def delmod(mod):
129130
# printb(b"Removing files\n Do not exit") # I don't think this is needed, deletes too fast to be visible
130-
for option in config.options(mod):
131-
file = config.get(mod, option)
131+
sectionName = activeGame + "|" + mod
132+
for option in config.options(sectionName):
133+
file = config.get(sectionName, option)
132134
if os.path.exists(file):
133135
os.remove(file)
134136
try:
135137
os.removedirs(os.path.dirname(file)) # remove empty folders
136138
except OSError: # throws OSError if there are still files in the folder
137139
pass # do nothing if there are other files
138-
config.remove_section(mod)
140+
config.remove_section(sectionName)
139141
config.write(open(configFile, 'w'))
140142

141143

142144
def savemodinfo(mod, file, length):
143-
if not config.has_section(mod):
144-
config.add_section(mod)
145-
option = len(config.options(mod))
146-
config.set(mod, str(length) + "," + str(option), file)
145+
# global activeGame
146+
sectionName = activeGame + "|" + mod
147+
if not config.has_section(sectionName):
148+
config.add_section(sectionName)
149+
option = len(config.options(sectionName)) # to make options not identical
150+
config.set(sectionName, str(length) + "," + str(option), file)
147151
config.write(open(configFile, 'w'))
148152

149153

150154
# This feels like a mess. Maybe I ought to do a re-write of some of this
151155
def makemenu(menulist, mainmenu=False): # todo: rename variables to make more sense
152156
global pageNum
153157
global modFolder
158+
global originalModFolder
154159
global selected_mod
155160
global activeGame
156161
listLen = 38
@@ -165,9 +170,10 @@ def makemenu(menulist, mainmenu=False): # todo: rename variables to make more s
165170
if len(modsPrint[i]) > 68:
166171
modsPrint[i] = modsPrint[i][:65] + "..."
167172
if not mainmenu:
168-
if config.has_section(mod):
169-
totalfiles = int(config.options(mod)[0].split(",")[0])
170-
active = len(config.options(mod))
173+
section = activeGame + "|" + mod
174+
if config.has_section(section):
175+
totalfiles = int(config.options(section)[0].split(",")[0])
176+
active = len(config.options(section))
171177
fractionenabled = str(active) + "/" + str(totalfiles)
172178
if totalfiles > active:
173179
modsPrint[i] += (width - (len(modsPrint[i]) + len(fractionenabled))) * " " + fractionenabled
@@ -204,7 +210,7 @@ def makemenu(menulist, mainmenu=False): # todo: rename variables to make more s
204210
AnsiMenu.selected_idx = 0
205211
# Main Menu
206212
elif (pageNum == 0) & (selected_index == 0) & (not mainmenu):
207-
modFolder = config.get("options", "modFolder")
213+
modFolder = originalModFolder
208214
AnsiMenu.selected_idx = 0
209215
elif not mainmenu:
210216
AnsiMenu.selected_idx = selected_index
@@ -213,7 +219,7 @@ def makemenu(menulist, mainmenu=False): # todo: rename variables to make more s
213219
selected_mod = mods[selected_index]
214220
nx.utils.clear_terminal()
215221
sys.stdout.flush()
216-
if config.has_section(selected_mod):
222+
if config.has_section(activeGame + "|" + selected_mod):
217223
delmod(selected_mod)
218224
else:
219225
copymod(modFolder + "/" + selected_mod, layeredFSFolder)
@@ -232,35 +238,43 @@ def natural_key(string_): # for natural sorting
232238
config = configparser.RawConfigParser()
233239
config.read(configFile)
234240
# if config does't exit, add some default values
235-
if not config.has_section("options"):
236-
config.add_section("options")
237-
if not config.has_option("options", "modFolder"):
238-
config.set("options", "modFolder", "/mods")
239-
if not config.has_option("options", "layeredFSFolder"):
240-
config.set("options", "layeredFSFolder", "/atmosphere/titles")
241+
if not config.has_section("|options|"):
242+
config.add_section("|options|")
243+
if not config.has_option("|options|", "modFolder"):
244+
config.set("|options|", "modFolder", "/mods")
245+
if not config.has_option("|options|", "layeredFSFolder"):
246+
config.set("|options|", "layeredFSFolder", "/atmosphere/titles")
241247
config.write(open(configFile, 'w'))
242248

243-
modFolder = config.get("options", "modFolder")
244-
layeredFSFolder = config.get("options", "layeredFSFolder")
249+
modFolder = config.get("|options|", "modFolder")
250+
originalModFolder = config.get("|options|", "modFolder") # for comparisons
251+
layeredFSFolder = config.get("|options|", "layeredFSFolder")
245252

246253
while True: # todo: maybe add a way to exit to hbmenu
247254
promptSkip = 0
248255
filecount = 0
249256

250257
if not os.path.isdir(modFolder):
258+
nx.utils.clear_terminal()
259+
print("Your mods folder \"" + modFolder + "\" doesn't exit\n")
260+
sys.stdout.flush()
261+
AnsiMenu(["Create it?"]).query()
251262
os.mkdir(modFolder)
252-
if (modFolder == config.get("options", "modFolder")) & bool(os.listdir(modFolder)):
263+
if (modFolder == originalModFolder) & bool(os.listdir(modFolder)):
253264
gameList = os.listdir(modFolder)
254265
gameList = sorted(gameList, key=natural_key)
255266
nx.utils.clear_terminal()
256267
sys.stdout.flush()
257268
printb(b"Generic Mod Manager" + bytes(" " * 53, "UTF-8") + b"By Seth\n\n") # Main menu
258269
makemenu(gameList, True)
259270
AnsiMenu.selected_idx = 0
260-
elif modFolder == config.get("options", "modFolder"):
271+
elif modFolder == originalModFolder:
261272
nx.utils.clear_terminal()
262273
print("Your mods folder \"" + modFolder + "\" looks empty\n"
263-
"Add some mods to it or change the folder location in " + configFile)
274+
"Add some mods to it or change the folder location in " + configFile +
275+
"\n\nThe recommended folder format for mods is:\n"
276+
"\"/ModsFolder/GameName/ModName/TitleID/ModFiles\"\n\n"
277+
"For Example:\n\"/mods/Legend of Zelda/Bowser Hinox/01007EF00011E000/romfs/...\"\n")
264278
sys.stdout.flush()
265279
AnsiMenu(["try again?"]).query()
266280
elif os.listdir(modFolder):
@@ -280,4 +294,4 @@ def natural_key(string_): # for natural sorting
280294
sys.stdout.flush()
281295
selected_index = AnsiMenu(["[Main Menu]","try again?"]).query()
282296
if selected_index == 0:
283-
modFolder = config.get("options", "modFolder")
297+
modFolder = originalModFolder

0 commit comments

Comments
 (0)