Last active
December 1, 2023 20:10
-
-
Save blazej222/4d53a6fa7c05eede4432b9a6f067ce9a to your computer and use it in GitHub Desktop.
Barony game quicksave app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a simple script that enables quicksaving/quickloading save files for Barony game. | |
# Place this script inside barony_folder/savegames. | |
# Create a directory named backup inside barony_folder/savegames. | |
# Start game & adjust save_file variable with a name of savefile you want to backup. | |
# You must do this after game autosaves at least once | |
# In case of multiplayer games, all players must create backup independently - otherwise there will be troubles with loading game. | |
# After changing save_file and game autosaving at least once, start the app. | |
# Quicksave on F5, Quickload on F7, exit app on = | |
import os | |
import shutil | |
import keyboard | |
import time | |
save_file = "savegame6_mp.baronysave" | |
backup_dir = "backup" | |
def make_backup(src_file, dest_dir): | |
# Create backup in backup directory | |
timestamp = time.strftime("%Y%m%d%H%M%S") | |
dest_file = os.path.join(dest_dir, f"backup_{timestamp}_savegame.baronysave") | |
shutil.copy(src_file, dest_file) | |
print(f"Created backup: {dest_file}") | |
def replace_with_latest_backup(src_file, backup_dir): | |
# Replace savefile with newest copy from backup dir | |
backup_files = sorted(os.listdir(backup_dir), reverse=True) | |
if len(backup_files) > 0: | |
latest_backup = os.path.join(backup_dir, backup_files[0]) | |
shutil.copy(latest_backup, src_file) | |
print(f"Replaced {src_file} with newest backup file") | |
else: | |
print("No backups available") | |
def cleanup_backup_dir(backup_dir, max_backups=6): | |
# Remove redundant backups | |
backup_files = sorted(os.listdir(backup_dir), reverse=True) | |
excess_backups = backup_files[max_backups:] | |
for file in excess_backups: | |
file_path = os.path.join(backup_dir, file) | |
os.remove(file_path) | |
print(f"Removed redundant copy: {file_path}") | |
def on_key_event(e): | |
if e.event_type == keyboard.KEY_DOWN: | |
if e.name == 'f5': | |
make_backup(save_file, backup_dir) | |
cleanup_backup_dir(backup_dir) | |
elif e.name == 'f7': | |
replace_with_latest_backup(save_file, backup_dir) | |
if not os.path.exists(backup_dir): | |
os.makedirs(backup_dir) | |
print("Press F5 to backup savefile.\nPress F7 to load savefile.\nPress = to exit program.") | |
keyboard.hook(on_key_event) | |
keyboard.wait('=') # End app on = |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment