Skip to content

Commit

Permalink
Support Add&Update project assets at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Nov 6, 2024
1 parent b647264 commit d8fb8e2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
1 change: 0 additions & 1 deletion editor/dependency_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ void DependencyRemoveDialog::ok_pressed() {
if (new_favorites.size() < previous_favorites.size()) {
EditorSettings::get_singleton()->set_favorites(new_favorites);
}
OS::get_singleton()->refresh_fs();
}

void DependencyRemoveDialog::_bind_methods() {
Expand Down
2 changes: 2 additions & 0 deletions platform/web/display_server_web.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class DisplayServerWeb : public DisplayServer {
static void _drop_files_js_callback(const Vector<String> &p_files);
WASM_EXPORT static void delete_files_js_callback(const char **p_filev, int p_filec);
static void _delete_files_js_callback(const Vector<String> &p_files);
WASM_EXPORT static void update_files_js_callback(const char **p_filev, int p_filec);
static void _update_files_js_callback(const Vector<String> &p_files);

void process_joypads();

Expand Down
58 changes: 55 additions & 3 deletions platform/web/js/libs/library_godot_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,50 @@ const GodotInputDragDrop = {
return function (ev) {
GodotInputDragDrop._process_event(ev, callback);
};
},


_process_add_files_event: function (ev, callback) {
infos = ev.detail
const drops = [];
const files = [];
const DROP = `/tmp/add-file-${parseInt(Math.random() * (1 << 30), 10)}/`;
FS.mkdir(DROP.slice(0, -1)); // Without trailing slash
for (let i = 0; i < infos.length; i++) {
const elem = infos[i];
const path = elem['path'];
console.log("_process_add_files_event ", path)
GodotFS.copy_to_fs(DROP + path, new Uint8Array(elem['data']));
let idx = path.indexOf('/');
if (idx === -1) {
// Root file
drops.push(DROP + path);
} else {
// Subdir
const sub = path.substr(0, idx);
idx = sub.indexOf('/');
if (idx < 0 && drops.indexOf(DROP + sub) === -1) {
drops.push(DROP + sub);
}
}
files.push(DROP + path);
}
callback(drops);
if (GodotConfig.persistent_drops) {
// Delay removal at exit.
GodotOS.atexit(function (resolve, reject) {
GodotInputDragDrop.remove_drop(files, DROP);
resolve();
});
} else {
GodotInputDragDrop.remove_drop(files, DROP);
}
},

handler_add_files: function (callback) {
return function (ev) {
GodotInputDragDrop._process_add_files_event(ev, callback);
};
},
},
};
Expand Down Expand Up @@ -507,14 +551,14 @@ const GodotInput = {
GodotRuntime.setHeapValue(r_standard, is_standard, 'i32');
return 0;
},


godot_js_update_files_cb__proxy: 'sync',
godot_js_update_files_cb__sig: 'vi',
godot_js_update_files_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
const update_files = function (ev) {
const files = ev.detail;
const files = ev.detail;
const args = files || [];
if (!args.length) {
return;
Expand All @@ -526,14 +570,20 @@ const GodotInput = {
};
const canvas = GodotConfig.canvas;
GodotEventListeners.add(canvas, "update_files", update_files);

const refresh_fs = function (evt) {
console.log("refresh_fs called");
GodotFS.refresh_fs()
}
GodotEventListeners.add(canvas, 'refresh_fs', refresh_fs);
},

godot_js_delete_files_cb__proxy: 'sync',
godot_js_delete_files_cb__sig: 'vi',
godot_js_delete_files_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
const delete_files = function (ev) {
const files = ev.detail;
const files = ev.detail;
const args = files || [];
if (!args.length) {
return;
Expand Down Expand Up @@ -568,7 +618,9 @@ const GodotInput = {
// Prevent default behavior (which would try to open the file(s))
ev.preventDefault();
}, false);

GodotEventListeners.add(canvas, 'drop', GodotInputDragDrop.handler(dropFiles));
GodotEventListeners.add(canvas, 'add_files', GodotInputDragDrop.handler_add_files(dropFiles));
},

/* Paste API */
Expand Down
4 changes: 3 additions & 1 deletion platform/web/js/libs/library_godot_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ const GodotFS = {
},

refresh_fs: function () {
GodotFS.sync()
if ( !GodotFS._syncing ) {
GodotFS.sync()
}
},
},
};
Expand Down

0 comments on commit d8fb8e2

Please sign in to comment.