Pixel-Composer/scripts/load_function/load_function.gml

301 lines
8.1 KiB
Text
Raw Normal View History

function __loadParams(readonly = false, override = false, apply_layout = false) constructor {
self.readonly = readonly;
self.override = override;
self.apply_layout = apply_layout;
}
2024-08-09 13:30:09 +02:00
function LOAD_SAFE() { LOAD(true); }
function LOAD(safe = false) {
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2024-05-16 15:28:45 +02:00
var path = get_open_filename_pxc("Pixel Composer project (.pxc)|*.pxc;*.cpxc", "");
2023-02-28 09:43:01 +01:00
key_release();
2022-01-18 05:44:05 +01:00
if(path == "") return;
2024-06-03 09:34:59 +02:00
if(!path_is_project(path)) return;
2022-11-01 03:06:03 +01:00
gc_collect();
2023-11-01 08:10:25 +01:00
var proj = LOAD_PATH(path, false, safe);
}
2022-01-18 05:44:05 +01:00
function TEST_PATH(path) {
2023-10-18 14:58:55 +02:00
TESTING = true;
2023-06-17 18:59:20 +02:00
TEST_ERROR = true;
2023-07-23 20:21:35 +02:00
PROJECT.cleanup();
PROJECT = new Project();
2024-02-04 07:33:42 +01:00
LOAD_AT(path);
PANEL_GRAPH.setProject(PROJECT);
}
2023-06-17 18:59:20 +02:00
function LOAD_PATH(path, readonly = false, safe_mode = false) {
var _rep = false;
for( var i = array_length(PROJECTS) - 1; i >= 0; i-- ) {
var _p = array_safe_get_fast(PROJECTS, i);
if(!is_instanceof(_p, Project)) continue;
if(_p.path == path) {
_rep = true;
closeProject(_p);
}
}
2023-07-23 20:21:35 +02:00
2023-07-06 19:49:16 +02:00
var _PROJECT = PROJECT;
PROJECT = new Project();
2024-02-12 10:25:23 +01:00
2024-02-15 14:23:26 +01:00
if(_PROJECT == noone) {
PROJECTS = [ PROJECT ];
} else if(!_rep && ((_PROJECT.path == "" || _PROJECT.readonly) && !_PROJECT.modified)) {
2024-02-12 10:25:23 +01:00
var ind = array_find(PROJECTS, _PROJECT);
2023-07-30 13:56:22 +02:00
if(ind == -1) ind = 0;
2023-07-25 20:12:40 +02:00
PROJECTS[ind] = PROJECT;
2024-02-12 10:25:23 +01:00
if(!IS_CMD) PANEL_GRAPH.setProject(PROJECT);
2024-02-15 14:23:26 +01:00
2023-07-06 19:49:16 +02:00
} else {
2024-02-12 10:25:23 +01:00
if(!IS_CMD) {
var graph = new Panel_Graph(PROJECT);
PANEL_GRAPH.panel.setContent(graph, true);
PANEL_GRAPH = graph;
}
2023-07-25 20:12:40 +02:00
array_push(PROJECTS, PROJECT);
2023-07-06 19:49:16 +02:00
}
2024-08-03 12:43:09 +02:00
var res = LOAD_AT(path, new __loadParams(readonly));
if(!res) return false;
2023-11-01 08:10:25 +01:00
PROJECT.safeMode = safe_mode;
if(!IS_CMD) setFocus(PANEL_GRAPH.panel);
2023-07-23 20:21:35 +02:00
2024-08-12 13:42:05 +02:00
if(PROJECT.meta.author_steam_id) PROJECT.meta.steam = FILE_STEAM_TYPE.steamOpen;
2023-07-23 20:21:35 +02:00
return PROJECT;
}
2023-02-23 07:02:19 +01:00
function LOAD_AT(path, params = new __loadParams()) {
2024-03-31 05:36:11 +02:00
static log = false;
2024-02-06 13:53:08 +01:00
CALL("load");
2024-02-04 07:33:42 +01:00
2024-03-31 05:36:11 +02:00
printIf(log, $"========== Loading {path} =========="); var t0 = get_timer(), t1 = get_timer();
2024-01-26 14:38:50 +01:00
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(path)) {
2024-02-11 14:53:33 +01:00
log_warning("LOAD", $"File not found: {path}");
2022-01-18 05:44:05 +01:00
return false;
}
2024-06-03 09:34:59 +02:00
if(!path_is_project(path)) {
2023-07-06 19:49:16 +02:00
log_warning("LOAD", "File not a valid PROJECT");
2022-01-18 05:44:05 +01:00
return false;
}
2023-02-23 07:02:19 +01:00
LOADING = true;
2022-12-10 05:06:01 +01:00
if(params.override) {
2023-07-06 19:49:16 +02:00
nodeCleanUp();
clearPanel();
setPanel();
if(!TESTING)
instance_destroy(_p_dialog);
ds_list_clear(ERRORS);
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Check file : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2023-11-01 08:10:25 +01:00
var temp_path = TEMPDIR;
directory_verify(temp_path);
ds_map_clear(APPEND_MAP);
2023-07-06 19:49:16 +02:00
2023-11-01 08:10:25 +01:00
var temp_file_path = TEMPDIR + string(UUID_generate(6));
2023-12-08 03:50:09 +01:00
if(file_exists_empty(temp_file_path)) file_delete(temp_file_path);
2023-07-06 19:49:16 +02:00
file_copy(path, temp_file_path);
2022-01-18 05:44:05 +01:00
PROJECT.readonly = params.readonly;
2023-07-06 19:49:16 +02:00
SET_PATH(PROJECT, path);
2022-01-18 05:44:05 +01:00
2024-03-31 05:36:11 +02:00
printIf(log, $" > Create temp : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
var _load_content;
2024-06-03 09:34:59 +02:00
var _ext = filename_ext_raw(path);
2024-04-01 13:32:13 +02:00
var s;
2024-06-03 09:34:59 +02:00
if(_ext == "pxc") {
2024-04-01 13:32:13 +02:00
var f = file_text_open_read(path);
s = file_text_read_all(f);
file_text_close(f);
2024-06-03 09:34:59 +02:00
} else if(_ext == "cpxc") {
2024-04-01 13:32:13 +02:00
var b = buffer_decompress(buffer_load(path));
s = buffer_read(b, buffer_string);
}
2024-03-31 05:36:11 +02:00
2024-04-18 07:12:31 +02:00
_load_content = json_try_parse(s);
2024-03-31 05:36:11 +02:00
printIf(log, $" > Load struct : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2022-01-18 05:44:05 +01:00
2023-06-13 14:42:06 +02:00
if(struct_has(_load_content, "version")) {
var _v = _load_content.version;
2024-03-27 11:51:14 +01:00
2023-07-06 19:49:16 +02:00
PROJECT.version = _v;
2024-03-27 11:51:14 +01:00
LOADING_VERSION = _v;
2024-02-04 07:33:42 +01:00
if(PREFERENCES.notify_load_version && floor(_v) != floor(SAVE_VERSION)) {
2023-08-02 19:11:57 +02:00
var warn = $"File version mismatch : loading file version {_v} to Pixel Composer {SAVE_VERSION}";
2022-11-14 03:16:15 +01:00
log_warning("LOAD", warn);
2022-01-18 05:44:05 +01:00
}
} else {
2023-07-25 20:12:40 +02:00
var warn = $"File version mismatch : loading old format to Pixel Composer {SAVE_VERSION}";
2022-11-14 03:16:15 +01:00
log_warning("LOAD", warn);
2022-01-18 05:44:05 +01:00
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Load meta : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
var create_list = [];
2023-06-13 14:42:06 +02:00
if(struct_has(_load_content, "nodes")) {
try {
2023-06-13 14:42:06 +02:00
var _node_list = _load_content.nodes;
2024-08-13 13:17:45 +02:00
2024-03-31 05:36:11 +02:00
for(var i = 0, n = array_length(_node_list); i < n; i++) {
2024-08-13 13:17:45 +02:00
2023-06-13 14:42:06 +02:00
var _node = nodeLoad(_node_list[i]);
if(_node) array_push(create_list, _node);
}
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD", exception_print(e));
2022-01-18 05:44:05 +01:00
}
}
2024-06-09 06:27:50 +02:00
PROJECT.deserialize(_load_content);
2024-03-31 05:36:11 +02:00
2022-01-18 05:44:05 +01:00
ds_queue_clear(CONNECTION_CONFLICT);
try {
array_foreach(create_list, function(node) /*=>*/ {return node.loadGroup()} );
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD, group", exception_print(e));
2023-07-21 12:40:20 +02:00
return false;
2022-01-18 05:44:05 +01:00
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Load group : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
try {
array_foreach(create_list, function(node) /*=>*/ {return node.postDeserialize()} );
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD, deserialize", exception_print(e));
2022-01-18 05:44:05 +01:00
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Deserialize: {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2022-12-21 02:30:23 +01:00
try {
array_foreach(create_list, function(node) /*=>*/ {return node.applyDeserialize()} );
2022-12-21 02:30:23 +01:00
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD, apply deserialize", exception_print(e));
2022-12-21 02:30:23 +01:00
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Apply deserialize : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
try {
array_foreach(create_list, function(node) /*=>*/ {return node.preConnect()} );
array_foreach(create_list, function(node) /*=>*/ {return node.connect()} );
array_foreach(create_list, function(node) /*=>*/ {return node.postConnect()} );
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD, connect", exception_print(e));
2022-01-18 05:44:05 +01:00
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Connect : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2022-01-18 05:44:05 +01:00
if(!ds_queue_empty(CONNECTION_CONFLICT)) {
var pass = 0;
try {
while(++pass < 4 && !ds_queue_empty(CONNECTION_CONFLICT)) {
var size = ds_queue_size(CONNECTION_CONFLICT);
2023-06-13 14:42:06 +02:00
log_message("LOAD", $"[Connect] {size} Connection conflict(s) detected (pass: {pass})");
2023-10-12 07:07:24 +02:00
repeat(size) ds_queue_dequeue(CONNECTION_CONFLICT).connect();
repeat(size) ds_queue_dequeue(CONNECTION_CONFLICT).postConnect();
2022-12-10 05:06:01 +01:00
Render();
2022-01-18 05:44:05 +01:00
}
2022-12-23 04:45:52 +01:00
if(!ds_queue_empty(CONNECTION_CONFLICT))
2022-11-14 03:16:15 +01:00
log_warning("LOAD", "Some connection(s) is unsolved. This may caused by render node not being update properly, or image path is broken.");
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD, connect solver", exception_print(e));
}
2022-01-18 05:44:05 +01:00
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Conflict : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2023-01-09 03:14:20 +01:00
try {
array_foreach(create_list, function(node) { node.postLoad(); } );
2023-01-09 03:14:20 +01:00
} catch(e) {
log_warning("LOAD, connect", exception_print(e));
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Post load : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2023-07-25 20:12:40 +02:00
try {
array_foreach(create_list, function(node) { node.clearInputCache(); } );
2023-07-25 20:12:40 +02:00
} catch(e) {
log_warning("LOAD, connect", exception_print(e));
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Clear cache : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2023-10-07 16:23:40 +02:00
RENDER_ALL_REORDER
2022-12-27 13:30:02 +01:00
2022-01-18 05:44:05 +01:00
LOADING = false;
2023-07-06 19:49:16 +02:00
PROJECT.modified = false;
2022-01-18 05:44:05 +01:00
2022-11-18 03:20:31 +01:00
log_message("FILE", "load " + path, THEME.noti_icon_file_load);
2024-02-15 14:23:26 +01:00
log_console("Loaded project: " + path);
2024-02-12 10:25:23 +01:00
if(!IS_CMD) PANEL_MENU.setNotiIcon(THEME.noti_icon_file_load);
2022-01-29 14:25:18 +01:00
2023-04-15 14:48:29 +02:00
refreshNodeMap();
2024-03-31 05:36:11 +02:00
printIf(log, $" > Refresh map : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2023-10-14 08:00:35 +02:00
if(struct_has(_load_content, "timelines") && !array_empty(_load_content.timelines.contents))
2023-10-12 07:07:24 +02:00
PROJECT.timelines.deserialize(_load_content.timelines);
2024-03-31 05:36:11 +02:00
printIf(log, $" > Timeline : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2024-02-12 10:25:23 +01:00
if(!IS_CMD) run_in(1, PANEL_GRAPH.toCenterNode);
printIf(log, $"========== Load {array_length(PROJECT.allNodes)} nodes completed in {(get_timer() - t0) / 1000} ms ==========");
2024-01-26 14:38:50 +01:00
if((PROJECT.load_layout || PREFERENCES.save_layout) && struct_has(_load_content, "layout"))
LoadPanelStruct(_load_content.layout.panel);
2022-01-18 05:44:05 +01:00
return true;
}
2023-11-23 02:32:26 +01:00
2024-08-09 13:30:09 +02:00
function __EXPORT_ZIP() { exportPortable(PROJECT); }
function __IMPORT_ZIP() {
var _path = get_open_filename_pxc("Pixel Composer portable project (.zip)|*.zip", "");
if(!file_exists_empty(_path)) return;
2023-11-23 02:32:26 +01:00
var _fname = filename_name_only(_path);
var _fext = filename_ext(_path);
2023-11-23 02:32:26 +01:00
if(_fext != ".zip") return false;
directory_verify(TEMPDIR + "proj/");
var _dir = TEMPDIR + "proj/" + _fname;
directory_create(_dir);
zip_unzip(_path, _dir);
2023-11-23 02:32:26 +01:00
var _f = file_find_first(_dir + "/*.pxc", fa_none);
var _proj = $"{_dir}/{_f}";
print(_proj);
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(_proj)) return false;
2023-11-23 02:32:26 +01:00
LOAD_PATH(_proj, true);
2024-08-09 13:30:09 +02:00
}