Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Mantella crash when loading a faulty character override file #397

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions src/games/gameable.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,46 +232,50 @@ def __apply_character_overrides(self, overrides_folder: str, character_df_column
os.makedirs(overrides_folder)
override_files: list[str] = os.listdir(overrides_folder)
for file in override_files:
filename, extension = os.path.splitext(file)
full_path_file = os.path.join(overrides_folder,file)
if extension == ".json":
with open(full_path_file) as fp:
json_object = json.load(fp)
if isinstance(json_object, dict):#Otherwise it is already a list
json_object = [json_object]
for json_content in json_object:
content: dict[str, str] = json_content
name = content.get("name", "")
base_id = content.get("base_id", "")
race = content.get("race", "")
try:
filename, extension = os.path.splitext(file)
full_path_file = os.path.join(overrides_folder,file)
if extension == ".json":
with open(full_path_file) as fp:
json_object = json.load(fp)
if isinstance(json_object, dict):#Otherwise it is already a list
json_object = [json_object]
for json_content in json_object:
content: dict[str, str] = json_content
name = content.get("name", "")
base_id = content.get("base_id", "")
race = content.get("race", "")
matcher = self._get_matching_df_rows_matcher(base_id, name, race)
if isinstance(matcher, type(None)): #character not in csv, add as new row
row = []
for entry in character_df_column_headers:
value = content.get(entry, "")
row.append(value)
self.character_df.loc[len(self.character_df.index)] = row
else: #character is in csv, update row
for entry in character_df_column_headers:
value = content.get(entry, None)
if value and value != "":
self.character_df.loc[matcher, entry] = value
elif extension == ".csv":
extra_df = self.__get_character_df(full_path_file)
for i in range(extra_df.shape[0]):#for each row in df
name = extra_df.iloc[i].get("name", "")
base_id = extra_df.iloc[i].get("base_id", "")
race = extra_df.iloc[i].get("race", "")
matcher = self._get_matching_df_rows_matcher(base_id, name, race)
if isinstance(matcher, type(None)): #character not in csv, add as new row
row = []
for entry in character_df_column_headers:
value = content.get(entry, "")
value = extra_df.iloc[i].get(entry, "")
row.append(value)
self.character_df.loc[len(self.character_df.index)] = row
else: #character is in csv, update row
for entry in character_df_column_headers:
value = content.get(entry, None)
if value and value != "":
value = extra_df.iloc[i].get(entry, None)
if value and not pd.isna(value) and value != "":
self.character_df.loc[matcher, entry] = value
elif extension == ".csv":
extra_df = self.__get_character_df(full_path_file)
for i in range(extra_df.shape[0]):#for each row in df
name = extra_df.iloc[i].get("name", "")
base_id = extra_df.iloc[i].get("base_id", "")
race = extra_df.iloc[i].get("race", "")
matcher = self._get_matching_df_rows_matcher(base_id, name, race)
if isinstance(matcher, type(None)): #character not in csv, add as new row
row = []
for entry in character_df_column_headers:
value = extra_df.iloc[i].get(entry, "")
row.append(value)
self.character_df.loc[len(self.character_df.index)] = row
else: #character is in csv, update row
for entry in character_df_column_headers:
value = extra_df.iloc[i].get(entry, None)
if value and not pd.isna(value) and value != "":
self.character_df.loc[matcher, entry] = value
except Exception as e:
logging.log(logging.WARNING, f"Could not load character override file '{file}' in '{overrides_folder}'. Most likely there is an error in the formating of the file. Error: {e}")