Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic save backup #117

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion PalEdit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, webbrowser, json, time, uuid, math
import os, webbrowser, json, time, uuid, math, shutil

# pyperclip
# docs: https://pypi.org/project/pyperclip/#description
Expand Down Expand Up @@ -565,9 +565,31 @@ def loadfile(self):
file = askopenfilename(initialdir=os.path.expanduser('~') + "\\AppData\\Local\\Pal\\Saved\\SaveGames",
filetypes=[("Level.sav", "Level.sav")])
self.logger.WriteLog(f"Opening file {file}")
self.logger.WriteLog(f"Backing up save")

if file:
self.filename = file

now = datetime.now()
current_date = ".".join((str(now.date().year),
"{:02d}".format(now.date().month),
"{:02d}".format(now.date().day)))
current_time = ".".join(("{:02d}".format(now.time().hour),
"{:02d}".format(now.time().minute),
"{:02d}".format(now.time().second)))
current_datetime = "-".join((current_date, current_time))
backup_path = file.replace("Level.sav", os.path.join("backup", "world", current_datetime))
if not os.path.exists(backup_path):
os.makedirs(backup_path)
os.makedirs(os.path.join(backup_path, "Players"))
if not os.path.exists(backup_path.replace('world', 'local')):
os.makedirs(backup_path.replace('world', 'local'))
shutil.copy(self.filename, os.path.join(backup_path, "Level.sav"))
shutil.copy(self.filename.replace("Level.sav", "LevelMeta.sav"), os.path.join(backup_path, "LevelMeta.sav"))
shutil.copy(self.filename.replace("Level.sav", "LocalData.sav"), os.path.join(backup_path.replace('world', 'local'), 'LocalData.sav'))
for player_file in os.scandir(self.filename.replace("Level.sav", "Players")):
shutil.copy(player_file.path, os.path.join(backup_path, "players", player_file.name))

self.gui.title(f"PalEdit v{PalEditConfig.version} - {file}")
self.skilllabel.config(text=self.i18n['msg_decompressing'])
with open(file, "rb") as f:
Expand Down