Skip to content

Commit

Permalink
Backpoered from Godot 4: Add is_built_in() method to Resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Mar 8, 2024
1 parent 70bd326 commit 60eb59a
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 34 deletions.
8 changes: 4 additions & 4 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
return; // don't save it
}

if (res->get_path().length() && res->get_path().find("::") == -1) {
if (!res->is_built_in()) {
f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
f->store_32(external_resources[res]);
} else {
Expand Down Expand Up @@ -1798,7 +1798,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
return;
}

if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) {
if (!p_main && (!bundle_resources) && !res->is_built_in()) {
if (res->get_path() == path) {
ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
return;
Expand Down Expand Up @@ -2029,7 +2029,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p

for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
RES r = E->get();
if (r->get_path() == "" || r->get_path().find("::") != -1) {
if (!r->is_built_in()) {
if (r->get_subindex() != 0) {
if (used_indices.has(r->get_subindex())) {
r->set_subindex(0); //repeated
Expand All @@ -2042,7 +2042,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p

for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
RES r = E->get();
if (r->get_path() == "" || r->get_path().find("::") != -1) {
if (!r->is_built_in()) {
if (r->get_subindex() == 0) {
int new_subindex = 1;
if (used_indices.size()) {
Expand Down
2 changes: 1 addition & 1 deletion core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ void Object::_clear_internal_resource_paths(const Variant &p_var) {
return;
}

if (!r->get_path().begins_with("res://") || r->get_path().find("::") == -1) {
if (!r->is_built_in()) {
return; //not an internal resource
}

Expand Down
1 change: 1 addition & 0 deletions core/object/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Resource : public Reference {

virtual void set_path(const String &p_path, bool p_take_over = false);
String get_path() const;
_FORCE_INLINE_ bool is_built_in() const { return path_cache.empty() || path_cache.find("::") != -1 || path_cache.begins_with("local://"); }

void set_subindex(int p_sub_index);
int get_subindex() const;
Expand Down
4 changes: 2 additions & 2 deletions editor/scene_tree_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3377,7 +3377,7 @@ void SceneTreeDock::_create_remap_for_node(Node *p_node, RBMap<RES, RES> &r_rema
continue;
}

if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
if (res->is_built_in() && !r_remap.has(res)) {
_create_remap_for_resource(res, r_remap);
}
}
Expand All @@ -3404,7 +3404,7 @@ void SceneTreeDock::_create_remap_for_resource(RES p_resource, RBMap<RES, RES> &
if (v.is_ref()) {
RES res = v;
if (res.is_valid()) {
if ((res->get_path() == "" || res->get_path().find("::") > -1) && !r_remap.has(res)) {
if (res->is_built_in() && !r_remap.has(res)) {
_create_remap_for_resource(res, r_remap);
}
}
Expand Down
20 changes: 7 additions & 13 deletions editor_modules/editor_code_editor/editor_script_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@
#include "shader_editor/shader_editor_plugin.h"
#endif

static bool _is_built_in_script(Script *p_script) {
String path = p_script->get_path();

return path.find("::") != -1;
}

class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
struct Cache {
uint64_t time_loaded;
Expand Down Expand Up @@ -496,7 +490,7 @@ void EditorScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back)
Ref<Script> script = current->get_edited_resource();
if (p_save) {
// Do not try to save internal scripts
if (!script.is_valid() || !(script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)) {
if (!script.is_valid() || !(script->is_built_in())) {
save_current_script();
}
}
Expand Down Expand Up @@ -633,7 +627,7 @@ void EditorScriptEditor::_resave_scripts(const String &p_str) {

RES script = se->get_edited_resource();

if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
if (script->is_built_in()) {
continue; //internal script, who cares
}

Expand Down Expand Up @@ -674,7 +668,7 @@ void EditorScriptEditor::reload_scripts() {

RES edited_res = se->get_edited_resource();

if (edited_res->get_path() == "" || edited_res->get_path().find("local://") != -1 || edited_res->get_path().find("::") != -1) {
if (edited_res->is_built_in()) {
continue; //internal script, who cares
}

Expand Down Expand Up @@ -781,7 +775,7 @@ bool EditorScriptEditor::_test_script_times_on_disk(RES p_for_script) {
continue;
}

if (edited_res->get_path() == "" || edited_res->get_path().find("local://") != -1 || edited_res->get_path().find("::") != -1) {
if (edited_res->is_built_in()) {
continue; //internal script, who cares
}

Expand Down Expand Up @@ -1439,7 +1433,7 @@ void EditorScriptEditor::close_builtin_scripts_from_scene(const String &p_scene)
continue;
}

if (script->get_path().find("::") != -1 && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
if (script->is_built_in() && script->get_path().begins_with(p_scene)) { //is an internal script and belongs to scene being closed
_close_tab(i, false);
i--;
}
Expand Down Expand Up @@ -2224,7 +2218,7 @@ void EditorScriptEditor::save_all_scripts() {
se->apply_code();
}

if (edited_res->get_path() != "" && edited_res->get_path().find("local://") == -1 && edited_res->get_path().find("::") == -1) {
if (!edited_res->is_built_in()) {
Ref<TextFile> text_file = edited_res;
if (text_file != nullptr) {
_save_text_file(text_file, text_file->get_path());
Expand Down Expand Up @@ -2314,7 +2308,7 @@ void EditorScriptEditor::_add_callback(Object *p_obj, const String &p_function,
script_list->select(script_list->find_metadata(i));

// Save the current script so the changes can be picked up by an external editor.
if (!_is_built_in_script(script.ptr())) { // But only if it's not built-in script.
if (!script.ptr()->is_built_in()) { // But only if it's not built-in script.
save_current_script();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ void EditorScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script
continue;
}

if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
if (script->is_built_in()) {
continue; //internal script, who cares, though weird
}

Expand Down
8 changes: 1 addition & 7 deletions editor_modules/editor_code_editor/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,12 @@

#include "editor_script_editor.h"

static bool _is_built_in_script(Script *p_script) {
String path = p_script->get_path();

return path.find("::") != -1;
}

void EditorScriptEditorPlugin::edit(Object *p_object) {
if (Object::cast_to<Script>(p_object)) {
Script *p_script = Object::cast_to<Script>(p_object);
String res_path = p_script->get_path().get_slice("::", 0);

if (_is_built_in_script(p_script)) {
if (p_script->is_built_in()) {
if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
EditorNode::get_singleton()->load_scene(res_path);
Expand Down
4 changes: 2 additions & 2 deletions editor_modules/shader_editor/shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void ShaderEditor::_check_for_external_edit() {
}

// internal shader.
if (shader->get_path() == "" || shader->get_path().find("local://") != -1 || shader->get_path().find("::") != -1) {
if (shader->is_built_in()) {
return;
}

Expand Down Expand Up @@ -496,7 +496,7 @@ void ShaderEditor::save_external_data(const String &p_str) {
}

apply_shaders();
if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) {
if (!shader->is_built_in()) {
//external shader, save it
ResourceSaver::save(shader->get_path(), shader);
}
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/packed_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ Node *PackedScene::instance(GenEditState p_edit_state) const {
s->set_scene_instance_state(state);
}

if (get_path() != "" && get_path().find("::") == -1) {
if (!is_built_in()) {
s->set_filename(get_path());
}

Expand Down
6 changes: 3 additions & 3 deletions scene/resources/resource_format_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ String ResourceFormatSaverTextInstance::_write_resource(const RES &res) {
} else {
if (internal_resources.has(res)) {
return "SubResource( " + itos(internal_resources[res]) + " )";
} else if (res->get_path().length() && res->get_path().find("::") == -1) {
} else if (!res->is_built_in()) {
if (res->get_path() == local_path) { //circular reference attempt
return "null";
}
Expand All @@ -1330,7 +1330,7 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant,
return;
}

if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) {
if (!p_main && (!bundle_resources) && !res->is_built_in()) {
if (res->get_path() == local_path) {
ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
return;
Expand Down Expand Up @@ -1523,7 +1523,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const RES &p_r

for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
RES res = E->get();
if (E->next() && (res->get_path() == "" || res->get_path().find("::") != -1)) {
if (E->next() && res->is_built_in()) {
if (res->get_subindex() != 0) {
if (used_indices.has(res->get_subindex())) {
res->set_subindex(0); //repeated
Expand Down

0 comments on commit 60eb59a

Please sign in to comment.