Add is_built_in() method to Resource

This commit is contained in:
kobewi 2021-07-10 21:17:41 +02:00
parent 78931aa040
commit e393c2a734
12 changed files with 28 additions and 34 deletions

View file

@ -103,6 +103,7 @@ public:
virtual void set_path(const String &p_path, bool p_take_over = false); virtual void set_path(const String &p_path, bool p_take_over = false);
String get_path() const; String get_path() const;
_FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.find("::") != -1 || path_cache.begins_with("local://"); }
static String generate_scene_unique_id(); static String generate_scene_unique_id();
void set_scene_unique_id(const String &p_id); void set_scene_unique_id(const String &p_id);

View file

@ -1572,7 +1572,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
return; // don't save it 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(OBJECT_EXTERNAL_RESOURCE_INDEX);
f->store_32(external_resources[res]); f->store_32(external_resources[res]);
} else { } else {
@ -1743,7 +1743,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
return; 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) { if (res->get_path() == path) {
ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded."); ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
return; return;
@ -1978,7 +1978,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
Set<String> used_unique_ids; Set<String> used_unique_ids;
for (RES &r : saved_resources) { for (RES &r : saved_resources) {
if (r->get_path() == "" || r->get_path().find("::") != -1) { if (r->is_built_in()) {
if (r->get_scene_unique_id() != "") { if (r->get_scene_unique_id() != "") {
if (used_unique_ids.has(r->get_scene_unique_id())) { if (used_unique_ids.has(r->get_scene_unique_id())) {
r->set_scene_unique_id(""); r->set_scene_unique_id("");
@ -1992,7 +1992,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
Map<RES, int> resource_map; Map<RES, int> resource_map;
int res_index = 0; int res_index = 0;
for (RES &r : saved_resources) { for (RES &r : saved_resources) {
if (r->get_path() == "" || r->get_path().find("::") != -1) { if (r->is_built_in()) {
if (r->get_scene_unique_id() == "") { if (r->get_scene_unique_id() == "") {
String new_id; String new_id;

View file

@ -1475,7 +1475,7 @@ void Object::_clear_internal_resource_paths(const Variant &p_var) {
return; return;
} }
if (!r->get_path().begins_with("res://") || r->get_path().find("::") == -1) { if (!r->is_built_in()) {
return; //not an internal resource return; //not an internal resource
} }

View file

@ -3490,7 +3490,7 @@ void EditorInspector::_update_script_class_properties(const Object &p_object, Li
String path = s->get_path(); String path = s->get_path();
String name = EditorNode::get_editor_data().script_class_get_name(path); String name = EditorNode::get_editor_data().script_class_get_name(path);
if (name.is_empty()) { if (name.is_empty()) {
if (!path.is_empty() && path.find("::") == -1) { if (!s->is_built_in()) {
name = path.get_file(); name = path.get_file();
} else { } else {
name = TTR("Built-in script"); name = TTR("Built-in script");

View file

@ -227,12 +227,6 @@ void ScriptEditorBase::_bind_methods() {
ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text"))); ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
} }
static bool _is_built_in_script(Script *p_script) {
String path = p_script->get_path();
return path.find("::") != -1;
}
class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache { class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
struct Cache { struct Cache {
uint64_t time_loaded = 0; uint64_t time_loaded = 0;
@ -764,7 +758,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
if (p_save && file.is_valid()) { if (p_save && file.is_valid()) {
// Do not try to save internal scripts, but prompt to save in-memory // Do not try to save internal scripts, but prompt to save in-memory
// scripts which are not saved to disk yet (have empty path). // scripts which are not saved to disk yet (have empty path).
if (file->get_path().find("local://") == -1 && file->get_path().find("::") == -1) { if (file->is_built_in()) {
save_current_script(); save_current_script();
} }
} }
@ -910,7 +904,7 @@ void ScriptEditor::_resave_scripts(const String &p_str) {
RES script = se->get_edited_resource(); 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 continue; //internal script, who cares
} }
@ -951,7 +945,7 @@ void ScriptEditor::_reload_scripts() {
RES edited_res = se->get_edited_resource(); 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 continue; //internal script, who cares
} }
@ -995,7 +989,7 @@ void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
RES script = se->get_edited_resource(); 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 continue; //internal script, who cares
} }
@ -1037,7 +1031,7 @@ bool ScriptEditor::_test_script_times_on_disk(RES p_for_script) {
continue; 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 continue; //internal script, who cares
} }
@ -1624,7 +1618,7 @@ void ScriptEditor::close_builtin_scripts_from_scene(const String &p_scene) {
continue; 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); _close_tab(i);
i--; i--;
} }
@ -2450,7 +2444,7 @@ void ScriptEditor::save_all_scripts() {
se->apply_code(); 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; Ref<TextFile> text_file = edited_res;
Ref<Script> script = edited_res; Ref<Script> script = edited_res;
@ -2570,7 +2564,7 @@ void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const
script_list->select(script_list->find_metadata(i)); script_list->select(script_list->find_metadata(i));
// Save the current script so the changes can be picked up by an external editor. // 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(); save_current_script();
} }
@ -3858,7 +3852,7 @@ void ScriptEditorPlugin::edit(Object *p_object) {
Script *p_script = 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); 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 (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
if (!EditorNode::get_singleton()->is_scene_open(res_path)) { if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
EditorNode::get_singleton()->load_scene(res_path); EditorNode::get_singleton()->load_scene(res_path);

View file

@ -375,7 +375,7 @@ void ScriptTextEditor::ensure_focus() {
String ScriptTextEditor::get_name() { String ScriptTextEditor::get_name() {
String name; String name;
if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) { if (!script->is_built_in()) {
name = script->get_path().get_file(); name = script->get_path().get_file();
if (is_unsaved()) { if (is_unsaved()) {
if (script->get_path().is_empty()) { if (script->get_path().is_empty()) {
@ -658,7 +658,7 @@ void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_fo
continue; 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 continue; //internal script, who cares, though weird
} }

View file

@ -482,8 +482,7 @@ void ShaderEditor::_check_for_external_edit() {
return; return;
} }
// internal shader. if (shader->is_built_in()) {
if (shader->get_path() == "" || shader->get_path().find("local://") != -1 || shader->get_path().find("::") != -1) {
return; return;
} }
@ -530,7 +529,7 @@ void ShaderEditor::save_external_data(const String &p_str) {
} }
apply_shaders(); 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 //external shader, save it
ResourceSaver::save(shader->get_path(), shader); ResourceSaver::save(shader->get_path(), shader);
} }

View file

@ -65,7 +65,7 @@ void TextEditor::_load_theme_settings() {
String TextEditor::get_name() { String TextEditor::get_name() {
String name; String name;
if (text_file->get_path().find("local://") == -1 && text_file->get_path().find("::") == -1) { if (!text_file->is_built_in()) {
name = text_file->get_path().get_file(); name = text_file->get_path().get_file();
if (is_unsaved()) { if (is_unsaved()) {
if (text_file->get_path().is_empty()) { if (text_file->get_path().is_empty()) {

View file

@ -3152,7 +3152,7 @@ void SceneTreeDock::_create_remap_for_node(Node *p_node, Map<RES, RES> &r_remap)
} }
} }
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); _create_remap_for_resource(res, r_remap);
} }
} }
@ -3179,7 +3179,7 @@ void SceneTreeDock::_create_remap_for_resource(RES p_resource, Map<RES, RES> &r_
if (v.is_ref()) { if (v.is_ref()) {
RES res = v; RES res = v;
if (res.is_valid()) { 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); _create_remap_for_resource(res, r_remap);
} }
} }

View file

@ -2582,7 +2582,7 @@ void VisualScriptEditor::reload_text() {
String VisualScriptEditor::get_name() { String VisualScriptEditor::get_name() {
String name; String name;
if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) { if (!script->is_built_in()) {
name = script->get_path().get_file(); name = script->get_path().get_file();
if (is_unsaved()) { if (is_unsaved()) {
if (script->get_path().is_empty()) { if (script->get_path().is_empty()) {

View file

@ -1651,7 +1651,7 @@ Node *PackedScene::instantiate(GenEditState p_edit_state) const {
s->set_scene_instance_state(state); s->set_scene_instance_state(state);
} }
if (get_path() != "" && get_path().find("::") == -1) { if (!is_built_in()) {
s->set_scene_file_path(get_path()); s->set_scene_file_path(get_path());
} }

View file

@ -1492,7 +1492,7 @@ String ResourceFormatSaverTextInstance::_write_resource(const RES &res) {
} else { } else {
if (internal_resources.has(res)) { if (internal_resources.has(res)) {
return "SubResource( \"" + internal_resources[res] + "\" )"; return "SubResource( \"" + 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 if (res->get_path() == local_path) { //circular reference attempt
return "null"; return "null";
} }
@ -1515,7 +1515,7 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant,
return; 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) { 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."); ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded.");
return; return;
@ -1728,7 +1728,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const RES &p_r
for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) { for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
RES res = E->get(); 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_scene_unique_id() != "") { if (res->get_scene_unique_id() != "") {
if (used_unique_ids.has(res->get_scene_unique_id())) { if (used_unique_ids.has(res->get_scene_unique_id())) {
res->set_scene_unique_id(""); // Repeated. res->set_scene_unique_id(""); // Repeated.