From d91d7e10babeb68cd96b9d740bee3d37bf894074 Mon Sep 17 00:00:00 2001 From: Tanasart Date: Wed, 28 Aug 2024 09:42:25 +0700 Subject: [PATCH] pref folder --- objects/o_main/Other_2.gml | 1 + scripts/hotkey_data/hotkey_data.gml | 4 +- scripts/node_collection/node_collection.gml | 4 +- scripts/preferences/preferences.gml | 59 ++++++++++++++++++--- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/objects/o_main/Other_2.gml b/objects/o_main/Other_2.gml index 10da096a1..391b1da32 100644 --- a/objects/o_main/Other_2.gml +++ b/objects/o_main/Other_2.gml @@ -42,6 +42,7 @@ } else DIRECTORY = APP_DIRECTORY; + PREFERENCES_DIR = $"{DIRECTORY}Preferences\\{PREF_VERSION}\\"; directory_verify(DIRECTORY); APP_LOCATION = program_directory; diff --git a/scripts/hotkey_data/hotkey_data.gml b/scripts/hotkey_data/hotkey_data.gml index 60347fb5c..96946aabd 100644 --- a/scripts/hotkey_data/hotkey_data.gml +++ b/scripts/hotkey_data/hotkey_data.gml @@ -264,12 +264,12 @@ function hotkey_serialize() { } } - json_save_struct(DIRECTORY + "hotkeys.json", { context: _context, node: _node }); + json_save_struct(PREFERENCES_DIR + "hotkeys.json", { context: _context, node: _node }); } function hotkey_deserialize() { HOTKEYS_DATA = {}; - var path = DIRECTORY + "hotkeys.json"; + var path = PREFERENCES_DIR + "hotkeys.json"; if(!file_exists(path)) return; var map = json_load_struct(path); diff --git a/scripts/node_collection/node_collection.gml b/scripts/node_collection/node_collection.gml index 7917a5552..f22e9914c 100644 --- a/scripts/node_collection/node_collection.gml +++ b/scripts/node_collection/node_collection.gml @@ -583,8 +583,8 @@ function Node_Collection(_x, _y, _group = noone) : Node(_x, _y, _group) construc } function getPreviewingNode() { - var _oj = outputs[preview_channel]; - if(is_undefined(_oj)) return self; + var _oj = array_safe_get(outputs, preview_channel, noone); + if(_oj == noone) return self; switch(_oj.type) { case VALUE_TYPE.d3Mesh : diff --git a/scripts/preferences/preferences.gml b/scripts/preferences/preferences.gml index d593089e2..9778698ee 100644 --- a/scripts/preferences/preferences.gml +++ b/scripts/preferences/preferences.gml @@ -1,5 +1,5 @@ #region preference - globalvar PREFERENCES, PREFERENCES_DEF, HOTKEYS_DATA; + globalvar PREFERENCES, PREFERENCES_DEF, HOTKEYS_DATA, PREFERENCES_DIR; PREFERENCES = {}; HOTKEYS_DATA = {}; @@ -241,24 +241,67 @@ #endregion #region save load + globalvar PREF_VERSION, PREF_UPDATES; + PREF_VERSION = 1_17_1; + PREF_UPDATES = [ + { + version: 0, + exists: function() /*=>*/ {return file_exists(DIRECTORY + "keys.json")}, + update: function() /*=>*/ { + var _pth_k = DIRECTORY + "keys.json"; + var _pth_h = DIRECTORY + "hotkeys.json"; + var _pth_d = DIRECTORY + "default_project.json"; + + if(file_exists(_pth_k)) file_copy(_pth_k, PREFERENCES_DIR + "keys.json"); + if(file_exists(_pth_h)) file_copy(_pth_h, PREFERENCES_DIR + "hotkeys.json"); + if(file_exists(_pth_d)) file_copy(_pth_d, PREFERENCES_DIR + "default_project.json"); + } + } + ]; + + function PREF_UPDATE() { + directory_verify(PREFERENCES_DIR); + var _oldest = -1; + + for (var i = 0, n = array_length(PREF_UPDATES); i < n; i++) { + var _pf = PREF_UPDATES[i]; + if(_pf.exists()) { + _oldest = i; + break; + } + } + + if(_oldest == -1) return; + for(var i = _oldest; i >= 0; i--) { + var _pf = PREF_UPDATES[i]; + _pf.update(); + } + } + function PREF_SAVE() { if(IS_CMD) return; + directory_verify($"{DIRECTORY}Preferences"); + directory_verify($"{DIRECTORY}Preferences\\{PREF_VERSION}"); + PREFERENCES.window_maximize = window_is_maximized; PREFERENCES.window_width = max(960, window_minimize_size[0]); PREFERENCES.window_height = max(600, window_minimize_size[1]); PREFERENCES.window_monitor = window_monitor; - json_save_struct(DIRECTORY + "keys.json", PREFERENCES); - json_save_struct(DIRECTORY + "Nodes/fav.json", global.FAV_NODES); - json_save_struct(DIRECTORY + "Nodes/recent.json", global.RECENT_NODES); - json_save_struct(DIRECTORY + "default_project.json", PROJECT_ATTRIBUTES); + json_save_struct(PREFERENCES_DIR + "keys.json", PREFERENCES); + json_save_struct(PREFERENCES_DIR + "default_project.json", PROJECT_ATTRIBUTES); + json_save_struct(DIRECTORY + "Nodes/fav.json", global.FAV_NODES); + json_save_struct(DIRECTORY + "Nodes/recent.json", global.RECENT_NODES); hotkey_serialize(); } function PREF_LOAD() { - var path = DIRECTORY + "keys.json"; + directory_verify($"{DIRECTORY}Preferences"); + if(!directory_exists(PREFERENCES_DIR)) PREF_UPDATE(); + + var path = PREFERENCES_DIR + "keys.json"; if(file_exists(path)) { var map = json_load_struct(path); if(struct_has(map, "preferences")) struct_override(PREFERENCES, map.preferences); @@ -276,7 +319,7 @@ if(PREFERENCES.move_directory) directory_set_current_working(DIRECTORY); - var f = json_load_struct(DIRECTORY + "default_project.json"); + var f = json_load_struct(PREFERENCES_DIR + "default_project.json"); struct_override(PROJECT_ATTRIBUTES, f); hotkey_deserialize(); @@ -329,4 +372,4 @@ var grav = struct_try_get(PREFERENCES, "physics_gravity", [ 0, 10 ]); physics_world_gravity(array_safe_get_fast(grav, 0, 0), array_safe_get_fast(grav, 1, 10)); } -#endregion \ No newline at end of file +#endregion