Pixel-Composer/scripts/load_function/load_function.gml

273 lines
7.4 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;
2023-07-06 19:49:16 +02:00
var path = get_open_filename("Pixel Composer PROJECT (.pxc)|*.pxc", "");
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);
2023-11-01 08:10:25 +01:00
__LOAD_PATH(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++ )
2023-07-23 20:21:35 +02:00
if(PROJECTS[i].path == path) return;
2023-07-06 19:49:16 +02:00
var _PROJECT = PROJECT;
PROJECT = new Project();
if(PANEL_GRAPH.project.path == "" && !PANEL_GRAPH.project.modified) {
2023-07-25 20:12:40 +02:00
var ind = array_find(PROJECTS, PANEL_GRAPH.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;
2023-07-06 19:49:16 +02:00
PANEL_GRAPH.setProject(PROJECT);
} else {
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
}
2023-11-01 08:10:25 +01:00
var res = __LOAD_PATH(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;
2023-07-06 19:49:16 +02:00
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
2023-11-23 02:32:26 +01:00
function __LOAD_PATH(path, readonly = false, override = false) { #region
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)) {
2022-11-14 03:16:15 +01:00
log_warning("LOAD", "File not found");
2022-01-18 05:44:05 +01:00
return false;
}
if(filename_ext(path) != ".json" && filename_ext(path) != ".pxc") {
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);
}
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
2023-07-06 19:49:16 +02:00
var _load_content = json_load_struct(temp_file_path);
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;
2023-07-06 19:49:16 +02:00
PROJECT.version = _v;
2023-12-05 04:28:49 +01:00
if(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
}
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;
for(var i = 0; i < array_length(_node_list); i++) {
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
}
}
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;
2023-10-09 16:07:33 +02:00
TOTAL_FRAMES = _anim_map.frames_total;
2023-07-06 19:49:16 +02:00
PROJECT.animator.framerate = _anim_map.framerate;
}
} 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
2023-10-06 11:51:11 +02:00
if(struct_has(_load_content, "attributes"))
struct_override(PROJECT.attributes, _load_content.attributes);
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));
}
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
}
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
}
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
}
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
}
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
}
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));
}
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));
}
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);
2022-12-10 05:06:01 +01:00
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();
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);
2023-11-27 11:40:28 +01:00
run_in(1, PANEL_GRAPH.toCenterNode);
2022-01-18 05:44:05 +01:00
return true;
2023-11-23 02:32:26 +01:00
} #endregion
function __IMPORT_ZIP() { #region
var path = get_open_filename("Pixel Composer portable project (.zip)|*.zip", "");
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