From 7d8ceeaac0f5a3967025478bcb295312b33bbdf1 Mon Sep 17 00:00:00 2001 From: Pacsfury Date: Sun, 21 Jun 2026 20:04:21 +0200 Subject: [PATCH 1/3] Refactor save and load functions for file handling Signed-off-by: Pacsfury --- engine/saveload.py | 51 ++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/engine/saveload.py b/engine/saveload.py index fdd9294..fcfa2dc 100644 --- a/engine/saveload.py +++ b/engine/saveload.py @@ -39,7 +39,6 @@ def save_project(engine: Any) -> Optional[Any]: Returns: Optional[Any]: IO object of the file or None """ - file = filedialog.asksaveasfile( defaultextension=".absp", filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], @@ -47,27 +46,26 @@ def save_project(engine: Any) -> Optional[Any]: initialfile="game.absp", ) - if file: - with open(file.name, "w") as f: - dump( - { - "name": engine.project_name, - "game": { - "dimensions": engine.game_dimensions, - "cursor_visible": engine.cursor_visible, - "fullscreen": engine.fullscreen, - }, - "entities": engine.entities, + if not file: + return None + + with file: + dump( + { + "name": engine.project_name, + "game": { + "dimensions": engine.game_dimensions, + "cursor_visible": engine.cursor_visible, + "fullscreen": engine.fullscreen, }, - f, - ) - f.close() - - messagebox.showinfo("Success", "Project saved successfully.") + "entities": engine.entities, + }, + file, + indent=4, # Optional: Makes the saved JSON human-readable + ) - return file - - return None + messagebox.showinfo("Success", "Project saved successfully.") + return file def load_project() -> Optional[list]: @@ -77,18 +75,17 @@ def load_project() -> Optional[list]: Returns: Optional[list]: file content """ - + file = filedialog.askopenfile( defaultextension=".absp", filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], title="Load ABS Project", ) - if file: - with open(file.name, "r") as f: - data: dict = load(f) - f.close() + if not file: + return None - return [data, file] + with file: + data: dict = load(file) - return None + return data, file From 2ba7b5ad0e09935c6b6474cb9b386586c8997e12 Mon Sep 17 00:00:00 2001 From: Pacsfury Date: Sun, 21 Jun 2026 20:09:10 +0200 Subject: [PATCH 2/3] Refactor save and load project functions Updated load_project function to return a tuple instead of a list. Modified save_project function to include mode and encoding parameters. Signed-off-by: Pacsfury --- engine/saveload.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/engine/saveload.py b/engine/saveload.py index fcfa2dc..894558a 100644 --- a/engine/saveload.py +++ b/engine/saveload.py @@ -8,7 +8,7 @@ from tkinter import messagebox as messagebox from tkinter import filedialog as filedialog from json import dump, load -from typing import Optional, Any +from typing import Optional, Any, Tuple import sys import os @@ -44,6 +44,8 @@ def save_project(engine: Any) -> Optional[Any]: filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], title="Save ABS Project", initialfile="game.absp", + mode="w", + encoding="utf-8", ) if not file: @@ -68,7 +70,7 @@ def save_project(engine: Any) -> Optional[Any]: return file -def load_project() -> Optional[list]: +def load_project() -> Optional[Tuple[dict, Any]]: """ Ask the user to open an absp file, then return the contents @@ -80,6 +82,8 @@ def load_project() -> Optional[list]: defaultextension=".absp", filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], title="Load ABS Project", + mode="r", + encoding="utf-8", ) if not file: From 41e3055ebd133e2d4a272d2ead7f704a13d33c55 Mon Sep 17 00:00:00 2001 From: Pacsfury Date: Sun, 21 Jun 2026 20:22:24 +0200 Subject: [PATCH 3/3] Refactor save/load functions to use file paths Signed-off-by: Pacsfury --- engine/saveload.py | 54 ++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/engine/saveload.py b/engine/saveload.py index 894558a..12ff86e 100644 --- a/engine/saveload.py +++ b/engine/saveload.py @@ -1,21 +1,17 @@ # Copyright (C) Natuworkguy # See the LICENSE file for GPLv3 -""" -Handles saving and loading of engine projects and data. -""" +"""Handles saving and loading of engine projects and data.""" -from tkinter import messagebox as messagebox -from tkinter import filedialog as filedialog -from json import dump, load -from typing import Optional, Any, Tuple -import sys import os +import sys +from json import dump, load +from tkinter import filedialog, messagebox +from typing import Any, Optional, Tuple def resource_path(relative: str) -> str: - """ - Convert a relative resource path into an absolute path. + """Convert a relative resource path into an absolute path. Args: relative (str): Relative path to a resource. @@ -23,35 +19,31 @@ def resource_path(relative: str) -> str: Returns: str: Absolute path to the resource. """ - if hasattr(sys, "_MEIPASS"): return os.path.join(sys._MEIPASS, relative) # pyright: ignore[reportAttributeAccessIssue] return os.path.join(os.path.abspath("."), relative) -def save_project(engine: Any) -> Optional[Any]: - """ - Save the project as an absp file +def save_project(engine: Any) -> Optional[str]: + """Save the project as an absp file. Args: engine (Any): engine instance to extract the project information from Returns: - Optional[Any]: IO object of the file or None + Optional[str]: Path to the saved file or None """ - file = filedialog.asksaveasfile( + file_path = filedialog.asksaveasfilename( defaultextension=".absp", filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], title="Save ABS Project", initialfile="game.absp", - mode="w", - encoding="utf-8", ) - if not file: + if not file_path: return None - with file: + with open(file_path, "w", encoding="utf-8") as file: dump( { "name": engine.project_name, @@ -63,33 +55,29 @@ def save_project(engine: Any) -> Optional[Any]: "entities": engine.entities, }, file, - indent=4, # Optional: Makes the saved JSON human-readable + indent=4, ) messagebox.showinfo("Success", "Project saved successfully.") - return file + return file_path -def load_project() -> Optional[Tuple[dict, Any]]: - """ - Ask the user to open an absp file, then return the contents +def load_project() -> Optional[Tuple[dict, str]]: + """Ask the user to open an absp file, then return the contents. Returns: - Optional[list]: file content + Optional[Tuple[dict, str]]: A tuple containing the data dict and the file path, or None. """ - - file = filedialog.askopenfile( + file_path = filedialog.askopenfilename( defaultextension=".absp", filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], title="Load ABS Project", - mode="r", - encoding="utf-8", ) - if not file: + if not file_path: return None - with file: + with open(file_path, "r", encoding="utf-8") as file: data: dict = load(file) - return data, file + return data, file_path