Recover easier from save shenanigans

This commit is contained in:
somebody
2022-12-09 16:49:25 -06:00
parent f4c51b13e8
commit 59864705f0

View File

@@ -929,16 +929,22 @@ class story_settings(settings):
if self.story_id == j["story_id"]: if self.story_id == j["story_id"]:
break break
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError(f"Malformed save file: Missing story.json in {self.save_paths.base}") logger.error(f"Malformed save file: Missing story.json in {self.save_paths.base}. Populating it with new data.")
break
disambiguator += 1 disambiguator += 1
self.save_paths.base = os.path.join("stories", save_name + (f" ({disambiguator})" if disambiguator else "")) self.save_paths.base = os.path.join("stories", save_name + (f" ({disambiguator})" if disambiguator else ""))
if not os.path.exists(self.save_paths.base): # Setup the directory structure.
# We are making the story for the first time. Setup the directory structure. for path in [
os.mkdir(self.save_paths.base) self.save_paths.base,
os.mkdir(self.save_paths.generated_audio) self.save_paths.generated_audio,
os.mkdir(self.save_paths.generated_images) self.save_paths.generated_images,
]:
try:
os.mkdir(path)
except FileExistsError:
pass
# Convert v2 if applicable # Convert v2 if applicable
v2_path = os.path.join("stories", f"{self.story_name}_v2.json") v2_path = os.path.join("stories", f"{self.story_name}_v2.json")
@@ -947,13 +953,10 @@ class story_settings(settings):
with open(v2_path, "r") as file: with open(v2_path, "r") as file:
v2j = json.load(file) v2j = json.load(file)
try: if v2j["story_id"] == self.story_id:
assert v2j["story_id"] == self.story_id
except AssertionError:
logger.error(f"Story mismatch in v2 migration! Existing file had story id {v2j['story_id']} but we have {self.story_id}")
raise
shutil.move(v2_path, os.path.join(self.save_paths.base, ".v2_old.json")) shutil.move(v2_path, os.path.join(self.save_paths.base, ".v2_old.json"))
else:
logger.warning(f"Story mismatch in v2 migration. Existing file had story id {v2j['story_id']} but we have {self.story_id}")
with open(self.save_paths.story, "w") as file: with open(self.save_paths.story, "w") as file:
file.write(self.to_json()) file.write(self.to_json())