mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
QOL : On launch, auto load the latest saved game.
This commit is contained in:
parent
8c64867918
commit
d6e4a50064
@ -7,45 +7,31 @@ from typing import Optional
|
||||
_dcs_saved_game_folder: Optional[str] = None
|
||||
_file_abs_path = None
|
||||
|
||||
|
||||
def setup(user_folder: str):
|
||||
global _dcs_saved_game_folder
|
||||
_dcs_saved_game_folder = user_folder
|
||||
_file_abs_path = os.path.join(base_path(), "default.liberation")
|
||||
|
||||
|
||||
def base_path() -> str:
|
||||
global _dcs_saved_game_folder
|
||||
assert _dcs_saved_game_folder
|
||||
return _dcs_saved_game_folder
|
||||
|
||||
def _save_file() -> str:
|
||||
return os.path.join(base_path(), "default.liberation")
|
||||
|
||||
def _temporary_save_file() -> str:
|
||||
return os.path.join(base_path(), "tmpsave.liberation")
|
||||
|
||||
|
||||
def _autosave_path() -> str:
|
||||
return os.path.join(base_path(), "autosave.liberation")
|
||||
|
||||
def _save_file_exists() -> bool:
|
||||
return os.path.exists(_save_file())
|
||||
|
||||
def mission_path_for(name: str) -> str:
|
||||
return os.path.join(base_path(), "Missions", "{}".format(name))
|
||||
|
||||
|
||||
def restore_game():
|
||||
if not _save_file_exists():
|
||||
return None
|
||||
|
||||
with open(_save_file(), "rb") as f:
|
||||
try:
|
||||
save = pickle.load(f)
|
||||
return save
|
||||
except Exception:
|
||||
logging.exception("Invalid Save game")
|
||||
return None
|
||||
|
||||
|
||||
def load_game(path):
|
||||
with open(path, "rb") as f:
|
||||
try:
|
||||
|
||||
@ -8,6 +8,7 @@ from game import persistency
|
||||
|
||||
global __dcs_saved_game_directory
|
||||
global __dcs_installation_directory
|
||||
global __last_save_file
|
||||
|
||||
PREFERENCES_FILE_PATH = "liberation_preferences.json"
|
||||
|
||||
@ -15,6 +16,7 @@ PREFERENCES_FILE_PATH = "liberation_preferences.json"
|
||||
def init():
|
||||
global __dcs_saved_game_directory
|
||||
global __dcs_installation_directory
|
||||
global __last_save_file
|
||||
|
||||
if os.path.isfile(PREFERENCES_FILE_PATH):
|
||||
try:
|
||||
@ -22,10 +24,15 @@ def init():
|
||||
pref_data = json.loads(prefs.read())
|
||||
__dcs_saved_game_directory = pref_data["saved_game_dir"]
|
||||
__dcs_installation_directory = pref_data["dcs_install_dir"]
|
||||
if "last_save_file" in pref_data:
|
||||
__last_save_file = pref_data["last_save_file"]
|
||||
else:
|
||||
__last_save_file = ""
|
||||
is_first_start = False
|
||||
except:
|
||||
__dcs_saved_game_directory = ""
|
||||
__dcs_installation_directory = ""
|
||||
__last_save_file = ""
|
||||
is_first_start = True
|
||||
else:
|
||||
try:
|
||||
@ -52,11 +59,18 @@ def setup(saved_game_dir, install_dir):
|
||||
persistency.setup(__dcs_saved_game_directory)
|
||||
|
||||
|
||||
def setup_last_save_file(last_save_file):
|
||||
global __last_save_file
|
||||
__last_save_file = last_save_file
|
||||
|
||||
|
||||
def save_config():
|
||||
global __dcs_saved_game_directory
|
||||
global __dcs_installation_directory
|
||||
global __last_save_file
|
||||
pref_data = {"saved_game_dir": __dcs_saved_game_directory,
|
||||
"dcs_install_dir": __dcs_installation_directory}
|
||||
"dcs_install_dir": __dcs_installation_directory,
|
||||
"last_save_file": __last_save_file}
|
||||
with(open(PREFERENCES_FILE_PATH, "w")) as prefs:
|
||||
prefs.write(json.dumps(pref_data))
|
||||
|
||||
@ -71,6 +85,15 @@ def get_saved_game_dir():
|
||||
return __dcs_saved_game_directory
|
||||
|
||||
|
||||
def get_last_save_file():
|
||||
global __last_save_file
|
||||
print(__last_save_file)
|
||||
if os.path.exists(__last_save_file):
|
||||
return __last_save_file
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def replace_mission_scripting_file():
|
||||
install_dir = get_dcs_install_directory()
|
||||
mission_scripting_path = os.path.join(install_dir, "Scripts", "MissionScripting.lua")
|
||||
|
||||
@ -19,6 +19,7 @@ from PySide2.QtWidgets import (
|
||||
import qt_ui.uiconstants as CONST
|
||||
from game import Game, VERSION, persistency
|
||||
from game.debriefing import Debriefing
|
||||
from qt_ui import liberation_install
|
||||
from qt_ui.dialogs import Dialog
|
||||
from qt_ui.displayoptions import DisplayGroup, DisplayOptions, DisplayRule
|
||||
from qt_ui.models import GameModel
|
||||
@ -62,7 +63,16 @@ class QLiberationWindow(QMainWindow):
|
||||
self.setWindowState(Qt.WindowMaximized)
|
||||
|
||||
if self.game is None:
|
||||
self.onGameGenerated(persistency.restore_game())
|
||||
last_save_file = liberation_install.get_last_save_file()
|
||||
if last_save_file:
|
||||
try:
|
||||
logging.info("Loading last saved game : {0}".format(str(last_save_file)))
|
||||
game = persistency.load_game(last_save_file)
|
||||
self.onGameGenerated(game)
|
||||
except:
|
||||
logging.info("Error loading latest save game")
|
||||
else:
|
||||
logging.info("No existing save game")
|
||||
else:
|
||||
self.onGameGenerated(self.game)
|
||||
|
||||
@ -227,6 +237,8 @@ class QLiberationWindow(QMainWindow):
|
||||
if self.game.savepath:
|
||||
persistency.save_game(self.game)
|
||||
GameUpdateSignal.get_instance().updateGame(self.game)
|
||||
liberation_install.setup_last_save_file(self.game.savepath)
|
||||
liberation_install.save_config()
|
||||
else:
|
||||
self.saveGameAs()
|
||||
|
||||
@ -235,6 +247,8 @@ class QLiberationWindow(QMainWindow):
|
||||
if file is not None:
|
||||
self.game.savepath = file[0]
|
||||
persistency.save_game(self.game)
|
||||
liberation_install.setup_last_save_file(self.game.savepath)
|
||||
liberation_install.save_config()
|
||||
|
||||
def onGameGenerated(self, game: Game):
|
||||
logging.info("On Game generated")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user