Skip to content

Commit

Permalink
Adjust pre-pretense backup strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffson committed Jul 28, 2024
1 parent dcc424d commit 3d1a5be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
4 changes: 4 additions & 0 deletions game/persistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions game/pretense/pretensemissiongenerator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import copy
import logging
import pickle
from datetime import datetime
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 19 additions & 3 deletions qt_ui/windows/QLiberationWindow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import traceback
import webbrowser
from datetime import datetime
from pathlib import Path
from typing import Optional

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3d1a5be

Please sign in to comment.