From d344841a1a2ef41465100b13f3cef34722b9a36f Mon Sep 17 00:00:00 2001 From: cecilkorik Date: Sun, 20 Apr 2025 21:30:48 -0400 Subject: [PATCH] improve configs --- .gitignore | 1 + conf/README.md | 7 ++++++- conf/default.json | 3 +++ conf/init.json | 2 -- engine/config.py | 22 +++++++++++++++++++--- engine/data.py | 14 +++++++++++--- engine/screen.py | 8 +++++--- 7 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 conf/default.json delete mode 100644 conf/init.json diff --git a/.gitignore b/.gitignore index 5d381cc..2b85710 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +conf/user*.json \ No newline at end of file diff --git a/conf/README.md b/conf/README.md index fb857fe..cdeec8f 100644 --- a/conf/README.md +++ b/conf/README.md @@ -1 +1,6 @@ -game configuration goes here \ No newline at end of file +Various game configuration files go here + +* `default.json` contains default settings for the game +* `user.json` contains modifications of those defaults made through the UI + +`engine/config.py` is designed to read, write, manage and otherwise handle these files. diff --git a/conf/default.json b/conf/default.json new file mode 100644 index 0000000..9f1bf59 --- /dev/null +++ b/conf/default.json @@ -0,0 +1,3 @@ +{ + "fullscreen": true +} diff --git a/conf/init.json b/conf/init.json deleted file mode 100644 index 2c63c08..0000000 --- a/conf/init.json +++ /dev/null @@ -1,2 +0,0 @@ -{ -} diff --git a/engine/config.py b/engine/config.py index 4c3ed3f..90fd3b3 100644 --- a/engine/config.py +++ b/engine/config.py @@ -11,8 +11,10 @@ class SettingsLoader(UserDict): self.reload() def load_settings_file(self: object, file: str) -> None: + if not data.exists('conf', file): + return ext = os.path.splitext(file)[1] - with data.data_open('conf', file) as fd: + with data.fd_open('conf', file) as fd: if ext == '.json': self.data.update(json.load(fd)) else: @@ -30,6 +32,20 @@ class SettingsMutable(SettingsLoader): pass class SettingsWritable(SettingsMutable): - pass + def __init__(self: object, files: list, outfile: str): + super().__init__(files) + self.outfile = outfile + + def save(self: object): + defaults = SettingsReadOnly([x for x in self.files if x != self.outfile]) + outdata = {} + for k in self.data.keys(): + if not k in defaults: + outdata[k] = self.data[k] + if outdata or data.exists('conf', self.outfile): + with data.fd_write('conf', self.outfile) as fd: + json.dump(outdata, fd, indent=4) + + +init = SettingsWritable(['default.json', 'user.json'], 'user.json') -init = SettingsWritable(['init.json']) diff --git a/engine/data.py b/engine/data.py index 8994fd3..1dd1135 100644 --- a/engine/data.py +++ b/engine/data.py @@ -10,7 +10,15 @@ def program_path() -> str: return g_pp -def data_open(vault: str, path: str) -> object: +def exists(vault: str, path: str) -> bool: fp = os.path.join(program_path(), vault, path) - return open(fp, 'r') - + return os.path.exists(fp) + +def fd_open(vault: str, path: str) -> object: + fp = os.path.join(program_path(), vault, path) + return open(fp, 'r') + +def fd_write(vault: str, path: str) -> object: + fp = os.path.join(program_path(), vault, path) + os.makedirs(os.path.join(program_path(), vault), exist_ok=True) + return open(fp, 'w') diff --git a/engine/screen.py b/engine/screen.py index fc72927..2def9be 100644 --- a/engine/screen.py +++ b/engine/screen.py @@ -47,6 +47,8 @@ def init_window() -> None: display = pyglet.display.get_display() screens = display.get_screens() windows = [] - for screen in screens: - windows.append(pyglet.window.Window(fullscreen=True, screen=screen)) - window = pyglet.window.Window() \ No newline at end of file + # for screen in screens: + # print(f"creating window on screen {screen}") + # windows.append(pyglet.window.Window(fullscreen=True, screen=screen)) + window = pyglet.window.Window(fullscreen=fs, screen=screens[0]) + cfg.save() \ No newline at end of file