kitao / pyxel

A retro game engine for Python
MIT License
15.38k stars 848 forks source link

Ensure parent directory when saving a resource file #414

Closed musjj closed 2 years ago

musjj commented 2 years ago

When creating a new resource file with pyxel edit D:/dir/test.pyxres, if the parent directory (dir in this case) doesn't exist, the resource editor will crash if you try to save:

thread '<unnamed>' panicked at 'Unable to open file 'D:\dir\test.pyxres'', D:\a\pyxel\pyxel\crates\pyxel-engine\src\resource.rs:117:29
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This can potentially make a user lose all their progress so far (like I just did 😞).

Morgandri1 commented 2 years ago

what's your code? currently trying to recreate this issue without making an entire game lol

DiddiLeija commented 2 years ago

what's your code? currently trying to recreate this issue without making an entire game lol

As I understand it, you should be able to reproduce this by running pyxel edit unexistent/folder/resource.pyxres (like @musjj said above), and click on the save button (or press Ctrl(Command)+S).

I tried it, using the latest Pyxel version, and found the following error:

``` thread '' panicked at 'Unable to open file 'C:\Users\Diego Ramirez\unexistent\folder\resource.pyxres'', D:\a\pyxel\pyxel\crates\pyxel-core\src\resource.rs:117:29 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace --- PyO3 is resuming a panic after fetching a PanicException from Python. --- Python stack trace below: Traceback (most recent call last): File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\widget.py", line 120, in update_all self._update() File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\widget.py", line 198, in _update self.trigger_event("update") File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\widget.py", line 112, in trigger_event listener(*args) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\app.py", line 191, in __on_update self._save_button.is_pressed_var = True File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\widget.py", line 230, in setter getattr(self, member_name).set(value) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\widget_var.py", line 21, in set value = listener(value) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\button.py", line 51, in __on_is_pressed_set self.trigger_event("press") File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\widgets\widget.py", line 112, in trigger_event listener(*args) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\app.py", line 148, in __on_save_button_press pyxel.save(self._resource_file) pyo3_runtime.PanicException: Unable to open file 'C:\Users\Diego Ramirez\unexistent\folder\resource.pyxres' Traceback (most recent call last): File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\Scripts\pyxel.exe\__main__.py", line 7, in File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\cli.py", line 23, in cli edit_pyxel_resource(sys.argv[2] if num_args == 3 else None) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\cli.py", line 134, in edit_pyxel_resource pyxel.editor.App(pyxel_resource_file) File "C:\Users\Diego Ramirez\AppData\Local\Programs\Python\Python39\lib\site-packages\pyxel\editor\app.py", line 118, in __init__ pyxel.run(self.update_all, self.draw_all) pyo3_runtime.PanicException: Unwrapped panic from Python code ```
Morgandri1 commented 2 years ago

ah, yes that did it. thanks

Morgandri1 commented 2 years ago

im noticing i cant find a good way to create the file if its not found, tried using WB and just W:

(sorry this is shitcode, im new to more complicated on this)

def __on_save_button_press(self):
        print("Saving...")
        if not os.path.exists(self._resource_file):
            parts = self._resource_file.split("/")
            new = parts[-1].removesuffix(".pyxres") 
            new = new+".pyxres" # make sure the file has .pyxres
            open(new, "wb").close() # save to CWD
            pyxel.save(new)
        else:
            pyxel.save(self._resource_file)
kitao commented 2 years ago

In Pyxel 1.8.6. The directory of the specified file is checked when the file is opened.