Skip to content

Commit 6fdc54d

Browse files
committed
improve error handling for loading .info files
1 parent de14d8c commit 6fdc54d

2 files changed

Lines changed: 40 additions & 24 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Improved:
66
* It is now possible to have a worker zombie of any efficiency
77
* At the same time you can now decide how many skulls the bodies in the graves should have
88
* Utility functions can now be collapsed and expanded
9+
* Loading of .info files (more robust display of the save files, should an error with a single file happen, only that save is missing instead of all)
910

1011
Fixed:
1112

main.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,38 @@ def get_savefiles():
132132
for file in os.listdir(options["path"]):
133133
# .info contains the information about the save, although it's content doesn't matter considering the save file
134134
if file.endswith(".info"):
135-
with open(os.path.join(options["path"], file)) as f:
136-
data = json.load(f)
137-
138-
# The save file has an id which is from the file name, but also an iterator which is displayed in the
139-
# application as the number of the save file, this number also represents the position in the array of
140-
# save file info
141-
out.append({
142-
"version": round(data["version"], 3),
143-
"savetime": data["real_time"],
144-
"days": int(data["game_time"]-1.5),
145-
"church": data["stats"].split("ss)")[1].strip(),
146-
"graveyard": data["stats"].split("ll)")[1].split("(cr")[0].strip(),
147-
"id": file.split(".info")[0],
148-
"num": i
149-
})
150-
saveslots[i] = file.split(".info")[0]
151-
i += 1
135+
try:
136+
with open(os.path.join(options["path"], file)) as f:
137+
data = json.load(f)
138+
139+
# Sometimes saves don't contain a rating, so we have ??? as placeholder and explicitly avoid errors
140+
church_rating = "???"
141+
graveyard_rating = "???"
142+
143+
try:
144+
church_rating = data["stats"].split("ss)")[1].strip()
145+
graveyard_rating = data["stats"].split("ll)")[1].split("(cr")[0].strip()
146+
except IndexError:
147+
pass
148+
149+
# The save file has an id which is from the file name, but also an iterator which is displayed in
150+
# the application as the number of the save file, this number also represents the position in the
151+
# array of save file info
152+
out.append({
153+
"version": round(data["version"], 3),
154+
"savetime": data["real_time"],
155+
"days": int(data["game_time"]-1.5),
156+
"church": church_rating,
157+
"graveyard": graveyard_rating,
158+
"id": file.split(".info")[0],
159+
"num": i
160+
})
161+
saveslots[i] = file.split(".info")[0]
162+
i += 1
163+
164+
print("Found and loaded informatin for save: " + file)
165+
except:
166+
print("Failed to load information for save: " + file)
152167

153168
return out
154169

@@ -933,7 +948,7 @@ def get_parameter_value(inventory, parameter):
933948
i = 0
934949
# Get it in the list
935950
for current_type in params["_res_type"]["v"]:
936-
951+
937952
# Break when found
938953
if current_type["v"] == parameter:
939954
index = i
@@ -944,22 +959,22 @@ def get_parameter_value(inventory, parameter):
944959
# Return empty value if non existent
945960
if index == -1:
946961
return None
947-
962+
948963
# Otherwise return result
949964
return params["_res_v"]["v"][index]
950965

951966

952967
# Set a specific parameter of an inventory
953968
def set_parameter_value(shash, inventory, parameter, value):
954-
969+
955970
params = inventory["v"]["_params"]["v"]
956971

957972
# The position of our value
958973
index = -1
959974
i = 0
960975
# Get it in the list
961976
for current_type in params["_res_type"]["v"]:
962-
977+
963978
# Break when found
964979
if current_type["v"] == parameter:
965980
index = i
@@ -1016,10 +1031,10 @@ def get_default_path():
10161031
def set_settings(settings):
10171032
global options
10181033
options = settings
1019-
1034+
10201035
if "path" in options:
10211036
options["path"] = os.path.expandvars(options["path"])
1022-
1037+
10231038
with open("./data/settings", "w") as f:
10241039
json.dump(settings, f)
10251040
return True
@@ -1061,7 +1076,7 @@ def get_settings():
10611076
def tkinter_gain_focus():
10621077
root.deiconify()
10631078
root.lift()
1064-
root.focus_force()
1079+
root.focus_force()
10651080

10661081

10671082
def run():

0 commit comments

Comments
 (0)