From 5aed3fbb87fb82f9df95534e6f5495a6bf80081c Mon Sep 17 00:00:00 2001 From: Raffson Date: Sat, 19 Nov 2022 18:10:49 +0100 Subject: [PATCH] Add migrator Attempts to fix incompatibilities in save files across builds. The idea is to try and handle the incompatibility as graceful as possible, and adding more "incompatibility fixes" in the future. --- game/__init__.py | 1 + game/migrator.py | 44 ++++++++++++++++++++++++++++++ qt_ui/windows/QLiberationWindow.py | 4 ++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 game/migrator.py diff --git a/game/__init__.py b/game/__init__.py index 250f4a35..59976db3 100644 --- a/game/__init__.py +++ b/game/__init__.py @@ -1,2 +1,3 @@ from .game import Game from .version import VERSION +from .migrator import Migrator diff --git a/game/migrator.py b/game/migrator.py new file mode 100644 index 00000000..186499fe --- /dev/null +++ b/game/migrator.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +from game.ato.packagewaypoints import PackageWaypoints +from game.data.doctrine import MODERN_DOCTRINE, COLDWAR_DOCTRINE, WWII_DOCTRINE +from game.utils import knots, feet, nautical_miles + +if TYPE_CHECKING: + from game import Game + + +class Migrator: + def __init__(self, game: Game): + self.game = game + self._migrate_game() + + def _migrate_game(self) -> None: + self._update_doctrine() + self._update_packagewaypoints() + + def _update_doctrine(self) -> None: + doctrines = [ + MODERN_DOCTRINE, + COLDWAR_DOCTRINE, + WWII_DOCTRINE, + ] + for c in self.game.coalitions: + if c.faction.doctrine.__dict__ in [d.__dict__ for d in doctrines]: + continue + found = False + for d in doctrines: + if c.faction.doctrine.rendezvous_altitude == d.rendezvous_altitude: + c.faction.doctrine = d + found = True + break + if not found: + c.faction.doctrine = MODERN_DOCTRINE + + def _update_packagewaypoints(self) -> None: + for c in self.game.coalitions: + for p in c.ato.packages: + if not hasattr(p.waypoints, "initial"): + p.waypoints = PackageWaypoints.create(p, c) diff --git a/qt_ui/windows/QLiberationWindow.py b/qt_ui/windows/QLiberationWindow.py index 75d5e674..9766444f 100644 --- a/qt_ui/windows/QLiberationWindow.py +++ b/qt_ui/windows/QLiberationWindow.py @@ -19,7 +19,7 @@ from PySide2.QtWidgets import ( ) import qt_ui.uiconstants as CONST -from game import Game, VERSION, persistency +from game import Game, VERSION, persistency, Migrator from game.debriefing import Debriefing from game.game import TurnState from game.layout import LAYOUTS @@ -108,6 +108,7 @@ class QLiberationWindow(QMainWindow): try: logging.info("Loading last saved game : " + str(last_save_file)) game = persistency.load_game(last_save_file) + Migrator(game) self.onGameGenerated(game) self.updateWindowTitle(last_save_file if game else None) except: @@ -317,6 +318,7 @@ class QLiberationWindow(QMainWindow): ) if file is not None and file[0] != "": game = persistency.load_game(file[0]) + Migrator(game) GameUpdateSignal.get_instance().game_loaded.emit(game) self.updateWindowTitle(file[0])