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

Ensure parent directory when saving a resource file #414

Closed
musjj opened this issue Sep 9, 2022 · 5 comments
Closed

Ensure parent directory when saving a resource file #414

musjj opened this issue Sep 9, 2022 · 5 comments

Comments

@musjj
Copy link

musjj commented Sep 9, 2022

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
Copy link

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

@DiddiLeija
Copy link
Contributor

DiddiLeija commented Sep 18, 2022

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 '<unnamed>' 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 <module>
  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
Copy link

ah, yes that did it. thanks

@Morgandri1
Copy link

Morgandri1 commented Sep 18, 2022

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
Copy link
Owner

kitao commented Sep 23, 2022

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

@kitao kitao closed this as completed Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants