Pixel-Composer/scripts/load_function/load_function.gml

169 lines
3.8 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);
2024-12-04 12:20:29 +01:00
Render();
closeProject(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-11-24 11:39:17 +01:00
static log = 0;
2024-03-31 05:36:11 +02:00
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();
2024-11-24 11:39:17 +01:00
var 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-11-24 02:33:57 +01:00
var bf = buffer_load(path);
var bc = buffer_decompress(bf);
if(bc == -1) {
s = buffer_read(bf, buffer_string);
2024-04-01 13:32:13 +02:00
2024-11-24 02:33:57 +01:00
} else {
s = buffer_read(bc, buffer_string);
buffer_delete(bc);
2024-04-01 13:32:13 +02:00
}
2024-03-31 05:36:11 +02:00
2024-11-24 02:33:57 +01:00
buffer_delete(bf);
2024-11-24 11:39:17 +01:00
content = json_try_parse(s);
printIf(log, $" > Load struct : {(get_timer() - t1) / 1000} ms");
2024-03-31 05:36:11 +02:00
2024-11-24 11:39:17 +01:00
return instance_create(0, 0, project_loader, { path, content, log, params, t0, t1 });
}
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
}