diff --git a/engine/saveload.py b/engine/saveload.py index c567b39..abc3fec 100644 --- a/engine/saveload.py +++ b/engine/saveload.py @@ -1,25 +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 -from pathlib import Path - -from .logger import logger, Status as LoggerStatus - -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. @@ -27,85 +19,65 @@ 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[str]: - """ - Save the project as an absp file + """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 """ - - dir = filedialog.askdirectory() - - if dir and os.path.exists(dir): - gamefile = str( - Path(dir) / "game.absp", - ) - - with open(gamefile, "w") as f: - dump( - { - "name": engine.project_name, - "game": { - "dimensions": engine.game_dimensions, - "cursor_visible": engine.cursor_visible, - "fullscreen": engine.fullscreen, - }, - "entities": engine.entities, + file_path = filedialog.asksaveasfilename( + defaultextension=".absp", + filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], + title="Save ABS Project", + initialfile="game.absp", + ) + + if not file_path: + return None + + with open(file_path, "w", encoding="utf-8") as 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.") - - return gamefile + "entities": engine.entities, + }, + file, + indent=4, + ) - return None + messagebox.showinfo("Success", "Project saved successfully.") + return file_path -def load_project() -> Optional[list]: - """ - 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_path = filedialog.askopenfilename( + defaultextension=".absp", + filetypes=[("ABS Project Files", "*.absp"), ("JSON Files", "*.json")], + title="Load ABS Project", + ) - dir = filedialog.askdirectory() - - if dir and os.path.exists(dir): - gamefile = str(Path(dir) / "game.absp") - - if not os.path.exists(gamefile): - logger( - "game.absp file not found in seleted directory. Creating.", - status=LoggerStatus.WARNING, - ) - - with open(gamefile, "w") as f: - f.write("{}") - f.close() - - return [{}, gamefile] - - if os.path.isdir(gamefile): - messagebox.showerror("Error", "game.absp project file is a directory.") - return None - - with open(gamefile, "r") as f: - data: dict = load(f) - f.close() + if not file_path: + return None - return [data, gamefile] + with open(file_path, "r", encoding="utf-8") as file: + data: dict = load(file) - return None + return data, file_path \ No newline at end of file