Skip to content

Commit

Permalink
Fix reloading scripts already in use
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilderin committed Sep 19, 2024
1 parent 694d3c2 commit 0522bd7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
9 changes: 8 additions & 1 deletion core/object/script_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,14 @@ void Script::reload_from_file() {
set_source_code(rel->get_source_code());
set_last_modified_time(rel->get_last_modified_time());

reload();
if (Engine::get_singleton()->is_editor_hint() && is_tool()) {
get_language()->reload_tool_script(this, true);
} else {
// It's important to set p_keep_state to true in order to manage reloading scripts
// that are currently instantiated.
reload(true);
}

#else
Resource::reload_from_file();
#endif
Expand Down
44 changes: 36 additions & 8 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,18 +1973,16 @@ void EditorFileSystem::_update_script_documentation() {
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
ScriptLanguage *lang = ScriptServer::get_language(i);
if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
// Reloading the script from disk if resource already in memory. Otherwise, the
// ResourceLoader::load will return the last loaded version of the script (without the modifications).
// The only have the script already loaded here is to edit the script outside the
// editor without being connected to the LSP server.
Ref<Resource> res = ResourceCache::get_ref(path);
if (res.is_valid()) {
res->reload_from_file();
}
bool should_reload_script = _should_reload_script(path);
Ref<Script> scr = ResourceLoader::load(path);
if (scr.is_null()) {
continue;
}
if (should_reload_script) {
// Reloading the script from disk. Otherwise, the ResourceLoader::load will
// return the last loaded version of the script (without the modifications).
scr->reload_from_file();
}
Vector<DocData::ClassDoc> docs = scr->get_documentation();
for (int j = 0; j < docs.size(); j++) {
EditorHelp::get_doc_data()->add_doc(docs[j]);
Expand All @@ -2006,6 +2004,36 @@ void EditorFileSystem::_update_script_documentation() {
update_script_paths_documentation.clear();
}

bool EditorFileSystem::_should_reload_script(const String &p_path) {
if (first_scan) {
return false;
}

Ref<Resource> res = ResourceCache::get_ref(p_path);
if (res.is_null()) {
// Script not already loaded.
return false;
}

Ref<Script> scr = res;
if (scr.is_null()) {
// Not a script.
return false;
}

// Scripts are reloaded via the script editor if they are currently opened.
if (ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
return false;
}

if (res->get_last_modified_time() == FileAccess::get_modified_time(p_path)) {
// Already up to date.
return false;
}

return true;
}

void EditorFileSystem::_process_update_pending() {
_update_script_classes();
// Parse documentation second, as it requires the class names to be loaded
Expand Down
1 change: 1 addition & 0 deletions editor/editor_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class EditorFileSystem : public Node {
void _update_script_documentation();
void _process_update_pending();
void _process_removed_files(const HashSet<String> &p_processed_files);
bool _should_reload_script(const String &p_path);

Mutex update_scene_mutex;
HashSet<String> update_scene_paths;
Expand Down
6 changes: 6 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@ void EditorNode::_resources_changed(const Vector<String> &p_resources) {
continue;
}

if (res->get_last_modified_time() == FileAccess::get_modified_time(res->get_path())) {
// Already up to date. That prevents reloading a script already reloaded for documentation
// in EditorFileSystem::_update_script_documentation.
continue;
}

changed.push_back(res);
}

Expand Down

0 comments on commit 0522bd7

Please sign in to comment.