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.
This commit is contained in:
Raffson 2022-11-19 18:10:49 +01:00
parent 13cb4d321a
commit 5aed3fbb87
No known key found for this signature in database
GPG Key ID: B0402B2C9B764D99
3 changed files with 48 additions and 1 deletions

View File

@ -1,2 +1,3 @@
from .game import Game
from .version import VERSION
from .migrator import Migrator

44
game/migrator.py Normal file
View File

@ -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)

View File

@ -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])