Pixel-Composer/scripts/load_function/load_function.gml

342 lines
9.7 KiB
Text
Raw Normal View History

2023-11-23 02:32:26 +01:00
function LOAD(safe = false) { #region
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;
if(filename_ext(path) != ".json" && filename_ext(path) != ".pxc") 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);
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-18 05:44:05 +01:00
2023-11-23 02:32:26 +01:00
function TEST_PATH(path) { #region
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();
PANEL_GRAPH.setProject(PROJECT);
2024-02-04 07:33:42 +01:00
LOAD_AT(path);
2023-11-23 02:32:26 +01:00
} #endregion
2023-06-17 18:59:20 +02:00
2023-11-23 02:32:26 +01:00
function LOAD_PATH(path, readonly = false, safe_mode = false) { #region
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(PROJECTS); i < n; i++ )
2024-05-16 03:06:18 +02:00
if(PROJECTS[i].path == path) closeProject(PROJECTS[i]);
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(_PROJECT.path == "" && !_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-02-04 07:33:42 +01:00
var res = LOAD_AT(path, readonly);
2023-07-23 20:21:35 +02:00
if(!res) return false;
2023-07-06 19:49:16 +02:00
2023-11-01 08:10:25 +01:00
PROJECT.safeMode = safe_mode;
2024-02-12 10:25:23 +01:00
if(!IS_CMD)
setFocus(PANEL_GRAPH.panel);
2023-07-23 20:21:35 +02:00
return PROJECT;
2023-11-23 02:32:26 +01:00
} #endregion
2023-02-23 07:02:19 +01:00
2024-02-04 07:33:42 +01:00
function LOAD_AT(path, readonly = false, override = false) { #region
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-04-01 13:32:13 +02:00
if(filename_ext(path) != ".json" && filename_ext(path) != ".pxc" && filename_ext(path) != ".cpxc") {
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
2023-07-06 19:49:16 +02:00
if(override) {
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);
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
2023-07-06 19:49:16 +02:00
PROJECT.readonly = readonly;
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-04-01 13:32:13 +02:00
var _ext = filename_ext(path);
var s;
if(_ext == ".pxc") {
var f = file_text_open_read(path);
s = file_text_read_all(f);
file_text_close(f);
} else if(_ext == ".cpxc") {
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();
2022-01-18 05:44:05 +01:00
var create_list = ds_list_create();
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-03-31 05:36:11 +02:00
for(var i = 0, n = array_length(_node_list); i < n; i++) {
// printIf(log, $" >> Loading nodes : {_node_list[i].type}");
2023-06-13 14:42:06 +02:00
var _node = nodeLoad(_node_list[i]);
if(_node) ds_list_add(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-03-31 05:36:11 +02:00
printIf(log, $" > Load nodes : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
try {
2023-06-13 14:42:06 +02:00
if(struct_has(_load_content, "animator")) {
2023-07-06 19:49:16 +02:00
var _anim_map = _load_content.animator;
PROJECT.animator.frames_total = struct_try_get(_anim_map, "frames_total", 30);
PROJECT.animator.framerate = struct_try_get(_anim_map, "framerate", 30);
PROJECT.animator.frame_range = struct_try_get(_anim_map, "frame_range", noone);
}
} catch(e) {
2022-12-22 03:09:55 +01:00
log_warning("LOAD, animator", exception_print(e));
2022-01-18 05:44:05 +01:00
}
2023-08-01 19:21:51 +02:00
if(struct_has(_load_content, "onion_skin"))
2023-10-18 14:58:55 +02:00
struct_override(PROJECT.onion_skin, _load_content.onion_skin);
2023-08-01 19:21:51 +02:00
if(struct_has(_load_content, "previewGrid"))
2023-10-18 14:58:55 +02:00
struct_override(PROJECT.previewGrid, _load_content.previewGrid);
2023-08-01 19:21:51 +02:00
if(struct_has(_load_content, "graphGrid"))
2023-10-18 14:58:55 +02:00
struct_override(PROJECT.graphGrid, _load_content.graphGrid);
2023-07-08 20:29:23 +02:00
2024-05-16 15:28:45 +02:00
if(struct_has(_load_content, "attributes")) {
2023-10-06 11:51:11 +02:00
struct_override(PROJECT.attributes, _load_content.attributes);
2024-05-16 15:28:45 +02:00
}
PROJECT.setPalette();
2023-10-06 11:51:11 +02:00
2023-10-15 15:04:42 +02:00
if(struct_has(_load_content, "notes")) {
PROJECT.notes = array_create(array_length(_load_content.notes));
for( var i = 0, n = array_length(_load_content.notes); i < n; i++ )
PROJECT.notes[i] = new Note.deserialize(_load_content.notes[i]);
}
2022-12-10 05:06:01 +01:00
try {
2023-06-13 14:42:06 +02:00
if(struct_has(_load_content, "metadata"))
2023-11-27 14:50:20 +01:00
PROJECT.meta.deserialize(_load_content.metadata);
2022-12-10 05:06:01 +01:00
} catch(e) {
2023-02-14 02:51:14 +01:00
log_warning("LOAD, metadata", exception_print(e));
2022-12-10 05:06:01 +01:00
}
2023-07-06 19:49:16 +02:00
PROJECT.globalNode = new Node_Global();
2023-03-07 14:29:47 +01:00
try {
2023-06-13 14:42:06 +02:00
if(struct_has(_load_content, "global"))
2023-07-06 19:49:16 +02:00
PROJECT.globalNode.deserialize(_load_content.global);
2023-07-05 15:09:52 +02:00
else if(struct_has(_load_content, "global_node"))
2023-07-06 19:49:16 +02:00
PROJECT.globalNode.deserialize(_load_content.global_node);
2023-03-07 14:29:47 +01:00
} catch(e) {
log_warning("LOAD, global", exception_print(e));
}
2023-05-16 21:28:16 +02:00
try {
2023-06-13 14:42:06 +02:00
if(struct_has(_load_content, "addon")) {
var _addon = _load_content.addon;
2023-07-06 19:49:16 +02:00
PROJECT.addons = _addon;
2023-06-17 14:30:49 +02:00
struct_foreach(_addon, function(_name, _value) { addonLoad(_name, false); });
} else
2023-07-06 19:49:16 +02:00
PROJECT.addons = {};
2023-05-16 21:28:16 +02:00
} catch(e) {
log_warning("LOAD, addon", exception_print(e));
}
2024-03-31 05:36:11 +02:00
printIf(log, $" > Load data : {(get_timer() - t1) / 1000} ms"); t1 = get_timer();
2022-01-18 05:44:05 +01:00
ds_queue_clear(CONNECTION_CONFLICT);
try {
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].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 {
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].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 {
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].applyDeserialize();
} 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 {
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].preConnect();
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].connect();
2023-10-07 16:23:40 +02:00
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].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 {
for(var i = 0; i < ds_list_size(create_list); i++)
2023-10-07 16:23:40 +02:00
create_list[| i].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 {
for(var i = 0; i < ds_list_size(create_list); i++)
create_list[| i].clearInputCache();
} 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);
2024-03-31 05:36:11 +02:00
printIf(log, $"========== Load {ds_map_size(PROJECT.nodeMap)} nodes completed in {(get_timer() - t0) / 1000} ms ==========");
2024-01-26 14:38:50 +01:00
2022-01-18 05:44:05 +01:00
return true;
2023-11-23 02:32:26 +01:00
} #endregion
function __IMPORT_ZIP() { #region
2024-05-16 15:28:45 +02:00
var path = get_open_filename_pxc("Pixel Composer portable project (.zip)|*.zip", "");
2023-11-23 02:32:26 +01:00
var _fname = filename_name_only(path);
var _fext = filename_ext(path);
if(_fext != ".zip") return false;
directory_verify(TEMPDIR + "proj/");
var _dir = TEMPDIR + "proj/" + _fname;
directory_create(_dir);
zip_unzip(path, _dir);
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);
} #endregion