From 3d1a5bea6778a7975586885b5b841e824488232e Mon Sep 17 00:00:00 2001 From: Raffson Date: Sun, 28 Jul 2024 18:59:07 +0200 Subject: [PATCH] Adjust pre-pretense backup strategy --- game/persistency.py | 4 ++++ game/pretense/pretensemissiongenerator.py | 14 ++++++-------- qt_ui/windows/QLiberationWindow.py | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/game/persistency.py b/game/persistency.py index f196bf33b..d32d2aa65 100644 --- a/game/persistency.py +++ b/game/persistency.py @@ -154,6 +154,10 @@ def save_dir() -> Path: return base_path() / "Retribution" / "Saves" +def pre_pretense_backups_dir() -> Path: + return save_dir() / "PrePretenseBackups" + + def server_port() -> int: global _server_port return _server_port diff --git a/game/pretense/pretensemissiongenerator.py b/game/pretense/pretensemissiongenerator.py index a39221546..fc427debb 100644 --- a/game/pretense/pretensemissiongenerator.py +++ b/game/pretense/pretensemissiongenerator.py @@ -1,6 +1,5 @@ from __future__ import annotations -import copy import logging import pickle from datetime import datetime @@ -20,7 +19,6 @@ from game.lasercodes.lasercoderegistry import LaserCodeRegistry from game.missiongenerator.convoygenerator import ConvoyGenerator from game.missiongenerator.environmentgenerator import EnvironmentGenerator -from game.missiongenerator.flotgenerator import FlotGenerator from game.missiongenerator.forcedoptionsgenerator import ForcedOptionsGenerator from game.missiongenerator.frontlineconflictdescription import ( FrontLineConflictDescription, @@ -29,6 +27,7 @@ from game.missiongenerator.tgogenerator import TgoGenerator from game.missiongenerator.visualsgenerator import VisualsGenerator from game.naming import namegen +from game.persistency import pre_pretense_backups_dir from game.pretense.pretenseaircraftgenerator import PretenseAircraftGenerator from game.radio.radios import RadioRegistry from game.radio.tacan import TacanRegistry @@ -74,17 +73,16 @@ def __init__(self, game: Game, time: datetime) -> None: self.mission.options.load_from_dict(options) def generate_miz(self, output: Path) -> UnitMap: - now = datetime.now() - date_time = now.strftime("%Y-%d-%mT%H_%M_%S") game_backup_pickle = pickle.dumps(self.game) + path = pre_pretense_backups_dir() + path.mkdir(parents=True, exist_ok=True) + path /= f".pre-pretense-backup.retribution" try: - with open( - self.game.savepath + ".pre-pretense-backup." + date_time, "wb" - ) as f: + with open(path, "wb") as f: pickle.dump(self.game, f) except: logging.error( - f"Unable to save Pretense pre-generation backup to {self.game.savepath}.pre-pretense-backup.{date_time}" + f"Unable to save Pretense pre-generation backup to {path}" ) if self.generation_started: diff --git a/qt_ui/windows/QLiberationWindow.py b/qt_ui/windows/QLiberationWindow.py index a4b8fbe16..dc8ac0f20 100644 --- a/qt_ui/windows/QLiberationWindow.py +++ b/qt_ui/windows/QLiberationWindow.py @@ -1,6 +1,7 @@ import logging import traceback import webbrowser +from datetime import datetime from pathlib import Path from typing import Optional @@ -21,6 +22,7 @@ from game.debriefing import Debriefing from game.game import TurnState from game.layout import LAYOUTS +from game.persistency import pre_pretense_backups_dir from game.pretense.pretensemissiongenerator import PretenseMissionGenerator from game.server import EventStream, GameContext from game.server.dependencies import QtCallbacks, QtContext @@ -322,9 +324,23 @@ def newGame(self): def newPretenseCampaign(self): output = persistency.mission_path_for("pretense_campaign.miz") - PretenseMissionGenerator( - self.game, self.game.conditions.start_time - ).generate_miz(output) + try: + PretenseMissionGenerator( + self.game, self.game.conditions.start_time + ).generate_miz(output) + except Exception as e: + now = datetime.now() + date_time = now.strftime("%Y-%d-%mT%H_%M_%S") + path = pre_pretense_backups_dir() + path.mkdir(parents=True, exist_ok=True) + tgt = path / f"pre-pretense-backup_{date_time}.retribution" + path /= f".pre-pretense-backup.retribution" + if path.exists(): + with open(path, "rb") as source: + with open(tgt, "wb") as target: + target.write(source.read()) + raise e + title = "Pretense campaign generated" msg = f"A Pretense campaign mission has been successfully generated in {output}" QMessageBox.information(QApplication.focusWidget(), title, msg, QMessageBox.Ok)