Skip to content

Commit 26738ea

Browse files
committed
Merge pull request godotengine#91379 from KoBeWi/annihilate_them_duplications
Remove code duplication for adding global script class
2 parents 4e30bc7 + 0904378 commit 26738ea

File tree

3 files changed

+31
-53
lines changed

3 files changed

+31
-53
lines changed

editor/editor_file_system.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,31 +1578,7 @@ void EditorFileSystem::_update_script_classes() {
15781578
update_script_mutex.lock();
15791579

15801580
for (const String &path : update_script_paths) {
1581-
ScriptServer::remove_global_class_by_path(path); // First remove, just in case it changed
1582-
1583-
int index = -1;
1584-
EditorFileSystemDirectory *efd = find_file(path, &index);
1585-
1586-
if (!efd || index < 0) {
1587-
// The file was removed
1588-
continue;
1589-
}
1590-
1591-
if (!efd->files[index]->script_class_name.is_empty()) {
1592-
String lang;
1593-
for (int j = 0; j < ScriptServer::get_language_count(); j++) {
1594-
if (ScriptServer::get_language(j)->handles_global_class_type(efd->files[index]->type)) {
1595-
lang = ScriptServer::get_language(j)->get_name();
1596-
}
1597-
}
1598-
if (lang.is_empty()) {
1599-
continue; // No lang found that can handle this global class
1600-
}
1601-
1602-
ScriptServer::add_global_class(efd->files[index]->script_class_name, efd->files[index]->script_class_extends, lang, path);
1603-
EditorNode::get_editor_data().script_class_set_icon_path(efd->files[index]->script_class_name, efd->files[index]->script_class_icon_path);
1604-
EditorNode::get_editor_data().script_class_set_name(path, efd->files[index]->script_class_name);
1605-
}
1581+
EditorFileSystem::get_singleton()->register_global_class_script(path, path);
16061582
}
16071583

16081584
// Parse documentation second, as it requires the class names to be correct and registered
@@ -1844,6 +1820,34 @@ HashSet<String> EditorFileSystem::get_valid_extensions() const {
18441820
return valid_extensions;
18451821
}
18461822

1823+
void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) {
1824+
ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed
1825+
1826+
int index = -1;
1827+
EditorFileSystemDirectory *efd = find_file(p_search_path, &index);
1828+
1829+
if (!efd || index < 0) {
1830+
// The file was removed
1831+
return;
1832+
}
1833+
1834+
if (!efd->files[index]->script_class_name.is_empty()) {
1835+
String lang;
1836+
for (int j = 0; j < ScriptServer::get_language_count(); j++) {
1837+
if (ScriptServer::get_language(j)->handles_global_class_type(efd->files[index]->type)) {
1838+
lang = ScriptServer::get_language(j)->get_name();
1839+
}
1840+
}
1841+
if (lang.is_empty()) {
1842+
return; // No lang found that can handle this global class
1843+
}
1844+
1845+
ScriptServer::add_global_class(efd->files[index]->script_class_name, efd->files[index]->script_class_extends, lang, p_target_path);
1846+
EditorNode::get_editor_data().script_class_set_icon_path(efd->files[index]->script_class_name, efd->files[index]->script_class_icon_path);
1847+
EditorNode::get_editor_data().script_class_set_name(p_target_path, efd->files[index]->script_class_name);
1848+
}
1849+
}
1850+
18471851
Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
18481852
String importer_name;
18491853

editor/editor_file_system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ class EditorFileSystem : public Node {
310310
void scan_changes();
311311
void update_file(const String &p_file);
312312
HashSet<String> get_valid_extensions() const;
313+
void register_global_class_script(const String &p_search_path, const String &p_target_path);
313314

314315
EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
315316
String get_file_type(const String &p_file) const;

editor/filesystem_dock.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,34 +1568,7 @@ void FileSystemDock::_update_resource_paths_after_move(const HashMap<String, Str
15681568
if (I) {
15691569
ResourceUID::get_singleton()->set_id(I->value, new_path);
15701570
}
1571-
1572-
ScriptServer::remove_global_class_by_path(old_path);
1573-
1574-
int index = -1;
1575-
EditorFileSystemDirectory *efd = EditorFileSystem::get_singleton()->find_file(old_path, &index);
1576-
1577-
if (!efd || index < 0) {
1578-
// The file was removed.
1579-
continue;
1580-
}
1581-
1582-
// Update paths for global classes.
1583-
if (!efd->get_file_script_class_name(index).is_empty()) {
1584-
String lang;
1585-
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
1586-
if (ScriptServer::get_language(i)->handles_global_class_type(efd->get_file_type(index))) {
1587-
lang = ScriptServer::get_language(i)->get_name();
1588-
break;
1589-
}
1590-
}
1591-
if (lang.is_empty()) {
1592-
continue; // No language found that can handle this global class.
1593-
}
1594-
1595-
ScriptServer::add_global_class(efd->get_file_script_class_name(index), efd->get_file_script_class_extends(index), lang, new_path);
1596-
EditorNode::get_editor_data().script_class_set_icon_path(efd->get_file_script_class_name(index), efd->get_file_script_class_icon_path(index));
1597-
EditorNode::get_editor_data().script_class_set_name(new_path, efd->get_file_script_class_name(index));
1598-
}
1571+
EditorFileSystem::get_singleton()->register_global_class_script(old_path, new_path);
15991572
}
16001573

16011574
// Rename all resources loaded, be it subresources or actual resources.

0 commit comments

Comments
 (0)