diff options
-rw-r--r-- | core/ustring.cpp | 40 | ||||
-rw-r--r-- | core/ustring.h | 1 | ||||
-rw-r--r-- | core/variant_call.cpp | 2 | ||||
-rw-r--r-- | doc/classes/String.xml | 14 | ||||
-rw-r--r-- | editor/editor_autoload_settings.cpp | 193 | ||||
-rw-r--r-- | editor/editor_autoload_settings.h | 10 | ||||
-rw-r--r-- | editor/import/editor_import_collada.cpp | 2 | ||||
-rw-r--r-- | editor/plugins/asset_library_editor_plugin.cpp | 17 | ||||
-rw-r--r-- | main/main.cpp | 25 |
9 files changed, 200 insertions, 104 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 85b7a16e6a..51f05468e2 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -753,6 +753,46 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p return ret; } +Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const { + + Vector<String> ret; + const int len = length(); + int from = len; + + while (true) { + + int end = rfind(p_splitter, from); + if (end < 0) + end = 0; + + if (p_allow_empty || (end < from)) { + const String str = substr(end > 0 ? end + p_splitter.length() : end, end > 0 ? from - end : from + 2); + + if (p_maxsplit <= 0) { + ret.push_back(str); + } else if (p_maxsplit > 0) { + + // Put rest of the string and leave cycle. + if (p_maxsplit == ret.size()) { + ret.push_back(substr(0, from + 2)); + break; + } + + // Otherwise, push items until positive limit is reached. + ret.push_back(str); + } + } + + if (end == 0) + break; + + from = end - p_splitter.length(); + } + + ret.invert(); + return ret; +} + Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const { Vector<float> ret; diff --git a/core/ustring.h b/core/ustring.h index 1ed694bb80..b57e9629d9 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -172,6 +172,7 @@ public: String get_slicec(CharType p_splitter, int p_slice) const; Vector<String> split(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const; + Vector<String> rsplit(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const; Vector<String> split_spaces() const; Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const; Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 4e883d496f..4158c2a60e 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -257,6 +257,7 @@ struct _VariantCall { VCALL_LOCALMEM2R(String, insert); VCALL_LOCALMEM0R(String, capitalize); VCALL_LOCALMEM3R(String, split); + VCALL_LOCALMEM3R(String, rsplit); VCALL_LOCALMEM2R(String, split_floats); VCALL_LOCALMEM0R(String, to_upper); VCALL_LOCALMEM0R(String, to_lower); @@ -1469,6 +1470,7 @@ void register_variant_methods() { ADDFUNC2R(STRING, STRING, String, insert, INT, "position", STRING, "what", varray()); ADDFUNC0R(STRING, STRING, String, capitalize, varray()); ADDFUNC3R(STRING, POOL_STRING_ARRAY, String, split, STRING, "divisor", BOOL, "allow_empty", INT, "maxsplit", varray(true, 0)); + ADDFUNC3R(STRING, POOL_STRING_ARRAY, String, rsplit, STRING, "divisor", BOOL, "allow_empty", INT, "maxsplit", varray(true, 0)); ADDFUNC2R(STRING, POOL_REAL_ARRAY, String, split_floats, STRING, "divisor", BOOL, "allow_empty", varray(true)); ADDFUNC0R(STRING, STRING, String, to_upper, varray()); diff --git a/doc/classes/String.xml b/doc/classes/String.xml index 83fb76f287..a55e184474 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -688,6 +688,20 @@ If [code]maxsplit[/code] is given, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements) </description> </method> + <method name="rsplit"> + <return type="PoolStringArray"> + </return> + <argument index="0" name="divisor" type="String"> + </argument> + <argument index="1" name="allow_empty" type="bool" default="True"> + </argument> + <argument index="2" name="maxsplit" type="int" default="0"> + </argument> + <description> + Splits the string by a [code]divisor[/code] string and returns an array of the substrings, starting from right. Example "One,Two,Three" will return ["One","Two","Three"] if split by ",". + If [code]maxsplit[/code] is specified, then it is number of splits to do, default is 0 which splits all the items. + </description> + </method> <method name="split_floats"> <return type="PoolRealArray"> </return> diff --git a/editor/editor_autoload_settings.cpp b/editor/editor_autoload_settings.cpp index 708bff252a..de9203232c 100644 --- a/editor/editor_autoload_settings.cpp +++ b/editor/editor_autoload_settings.cpp @@ -52,6 +52,13 @@ void EditorAutoloadSettings::_notification(int p_what) { file_dialog->add_filter("*." + E->get()); } + + for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) { + AutoLoadInfo &info = E->get(); + if (info.node && info.in_editor) { + get_tree()->get_root()->call_deferred("add_child", info.node); + } + } } } @@ -291,6 +298,36 @@ void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) { autoload_add_name->set_text(p_path.get_file().get_basename()); } +Node *EditorAutoloadSettings::_create_autoload(const String &p_path) { + RES res = ResourceLoader::load(p_path); + ERR_EXPLAIN("Can't autoload: " + p_path); + ERR_FAIL_COND_V(res.is_null(), NULL); + Node *n = NULL; + if (res->is_class("PackedScene")) { + Ref<PackedScene> ps = res; + n = ps->instance(); + } else if (res->is_class("Script")) { + Ref<Script> s = res; + StringName ibt = s->get_instance_base_type(); + bool valid_type = ClassDB::is_parent_class(ibt, "Node"); + ERR_EXPLAIN("Script does not inherit a Node: " + p_path); + ERR_FAIL_COND_V(!valid_type, NULL); + + Object *obj = ClassDB::instance(ibt); + + ERR_EXPLAIN("Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt)); + ERR_FAIL_COND_V(obj == NULL, NULL); + + n = Object::cast_to<Node>(obj); + n->set_script(s.get_ref_ptr()); + } + + ERR_EXPLAIN("Path in autoload not a node or script: " + p_path); + ERR_FAIL_COND_V(!n, NULL); + + return n; +} + void EditorAutoloadSettings::update_autoload() { if (updating_autoload) @@ -299,15 +336,11 @@ void EditorAutoloadSettings::update_autoload() { updating_autoload = true; Map<String, AutoLoadInfo> to_remove; - Map<String, AutoLoadInfo> to_remove_singleton; - List<AutoLoadInfo> to_add; - List<String> to_add_singleton; // Only for when the node is still the same + List<AutoLoadInfo *> to_add; for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) { - to_remove.insert(E->get().name, E->get()); - if (E->get().is_singleton) { - to_remove_singleton.insert(E->get().name, E->get()); - } + AutoLoadInfo &info = E->get(); + to_remove.insert(info.name, info); } autoload_cache.clear(); @@ -331,11 +364,6 @@ void EditorAutoloadSettings::update_autoload() { if (name.empty()) continue; - AutoLoadInfo old_info; - if (to_remove.has(name)) { - old_info = to_remove[name]; - } - AutoLoadInfo info; info.is_singleton = path.begins_with("*"); @@ -347,28 +375,31 @@ void EditorAutoloadSettings::update_autoload() { info.path = path; info.order = ProjectSettings::get_singleton()->get_order(pi.name); - if (old_info.name == info.name) { + bool need_to_add = true; + if (to_remove.has(name)) { + AutoLoadInfo &old_info = to_remove[name]; if (old_info.path == info.path) { - // Still the same resource, check singleton status - to_remove.erase(name); - if (info.is_singleton) { - if (old_info.is_singleton) { - to_remove_singleton.erase(name); + // Still the same resource, check status + info.node = old_info.node; + if (info.node) { + Ref<Script> scr = info.node->get_script(); + info.in_editor = scr.is_valid() && scr->is_tool(); + if (info.is_singleton == old_info.is_singleton && info.in_editor == old_info.in_editor) { + to_remove.erase(name); + need_to_add = false; } else { - to_add_singleton.push_back(name); + info.node = NULL; } } - } else { - // Resource changed - to_add.push_back(info); } - } else { - // New autoload - to_add.push_back(info); } autoload_cache.push_back(info); + if (need_to_add) { + to_add.push_back(&(autoload_cache.back()->get())); + } + TreeItem *item = tree->create_item(root); item->set_text(0, name); item->set_editable(0, true); @@ -387,71 +418,54 @@ void EditorAutoloadSettings::update_autoload() { item->set_selectable(3, false); } - // Remove autoload constants - for (Map<String, AutoLoadInfo>::Element *E = to_remove_singleton.front(); E; E = E->next()) { - for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->remove_named_global_constant(E->get().name); - } - } - - // Remove obsolete nodes from the tree + // Remove deleted/changed autoloads for (Map<String, AutoLoadInfo>::Element *E = to_remove.front(); E; E = E->next()) { AutoLoadInfo &info = E->get(); - Node *al = get_node("/root/" + info.name); - ERR_CONTINUE(!al); - get_tree()->get_root()->remove_child(al); - memdelete(al); - } + if (info.is_singleton) { + for (int i = 0; i < ScriptServer::get_language_count(); i++) { + ScriptServer::get_language(i)->remove_named_global_constant(info.name); + } + } + if (info.in_editor) { + ERR_CONTINUE(!info.node); + get_tree()->get_root()->remove_child(info.node); + } - // Register new singletons already in the tree - for (List<String>::Element *E = to_add_singleton.front(); E; E = E->next()) { - Node *al = get_node("/root/" + E->get()); - ERR_CONTINUE(!al); - for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->add_named_global_constant(E->get(), al); + if (info.node) { + memdelete(info.node); + info.node = NULL; } } - // Add new nodes to the tree + // Load new/changed autoloads List<Node *> nodes_to_add; - for (List<AutoLoadInfo>::Element *E = to_add.front(); E; E = E->next()) { - AutoLoadInfo &info = E->get(); + for (List<AutoLoadInfo *>::Element *E = to_add.front(); E; E = E->next()) { + AutoLoadInfo *info = E->get(); - RES res = ResourceLoader::load(info.path); - ERR_EXPLAIN("Can't autoload: " + info.path); - ERR_CONTINUE(res.is_null()); - Node *n = NULL; - if (res->is_class("PackedScene")) { - Ref<PackedScene> ps = res; - n = ps->instance(); - } else if (res->is_class("Script")) { - Ref<Script> s = res; - StringName ibt = s->get_instance_base_type(); - bool valid_type = ClassDB::is_parent_class(ibt, "Node"); - ERR_EXPLAIN("Script does not inherit a Node: " + info.path); - ERR_CONTINUE(!valid_type); - - Object *obj = ClassDB::instance(ibt); - - ERR_EXPLAIN("Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt)); - ERR_CONTINUE(obj == NULL); - - n = Object::cast_to<Node>(obj); - n->set_script(s.get_ref_ptr()); - } + info->node = _create_autoload(info->path); - ERR_EXPLAIN("Path in autoload not a node or script: " + info.path); - ERR_CONTINUE(!n); - n->set_name(info.name); + ERR_CONTINUE(!info->node); + info->node->set_name(info->name); - //defer so references are all valid on _ready() - nodes_to_add.push_back(n); + Ref<Script> scr = info->node->get_script(); + info->in_editor = scr.is_valid() && scr->is_tool(); - if (info.is_singleton) { + if (info->in_editor) { + //defer so references are all valid on _ready() + nodes_to_add.push_back(info->node); + } + + if (info->is_singleton) { for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->add_named_global_constant(info.name, n); + ScriptServer::get_language(i)->add_named_global_constant(info->name, info->node); } } + + if (!info->in_editor && !info->is_singleton) { + // No reason to keep this node + memdelete(info->node); + info->node = NULL; + } } for (List<Node *>::Element *E = nodes_to_add.front(); E; E = E->next()) { @@ -728,6 +742,24 @@ EditorAutoloadSettings::EditorAutoloadSettings() { info.name = name; info.path = path; info.order = ProjectSettings::get_singleton()->get_order(pi.name); + info.node = _create_autoload(path); + + if (info.node) { + Ref<Script> scr = info.node->get_script(); + info.in_editor = scr.is_valid() && scr->is_tool(); + info.node->set_name(info.name); + } + + if (info.is_singleton) { + for (int i = 0; i < ScriptServer::get_language_count(); i++) { + ScriptServer::get_language(i)->add_named_global_constant(info.name, info.node); + } + } + + if (!info.is_singleton && !info.in_editor) { + memdelete(info.node); + info.node = NULL; + } autoload_cache.push_back(info); } @@ -796,3 +828,12 @@ EditorAutoloadSettings::EditorAutoloadSettings() { add_child(tree, true); } + +EditorAutoloadSettings::~EditorAutoloadSettings() { + for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) { + AutoLoadInfo &info = E->get(); + if (info.node && !info.in_editor) { + memdelete(info.node); + } + } +} diff --git a/editor/editor_autoload_settings.h b/editor/editor_autoload_settings.h index 1797c10e61..0b75faa009 100644 --- a/editor/editor_autoload_settings.h +++ b/editor/editor_autoload_settings.h @@ -52,11 +52,19 @@ class EditorAutoloadSettings : public VBoxContainer { String name; String path; bool is_singleton; + bool in_editor; int order; + Node *node; bool operator==(const AutoLoadInfo &p_info) { return order == p_info.order; } + + AutoLoadInfo() { + is_singleton = false; + in_editor = false; + node = NULL; + } }; List<AutoLoadInfo> autoload_cache; @@ -78,6 +86,7 @@ class EditorAutoloadSettings : public VBoxContainer { void _autoload_activated(); void _autoload_open(const String &fpath); void _autoload_file_callback(const String &p_path); + Node *_create_autoload(const String &p_path); Variant get_drag_data_fw(const Point2 &p_point, Control *p_control); bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const; @@ -93,6 +102,7 @@ public: void autoload_remove(const String &p_name); EditorAutoloadSettings(); + ~EditorAutoloadSettings(); }; #endif diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp index 1d7545f182..2fb3bf7b1e 100644 --- a/editor/import/editor_import_collada.cpp +++ b/editor/import/editor_import_collada.cpp @@ -339,7 +339,7 @@ Error ColladaImport::_create_scene(Collada::Node *p_node, Spatial *p_parent) { NodeMap nm; nm.node = node; node_map[p_node->id] = nm; - node_name_map[p_node->name] = p_node->id; + node_name_map[node->get_name()] = p_node->id; Transform xf = p_node->default_transform; xf = collada.fix_transform(xf) * p_node->post_transform; diff --git a/editor/plugins/asset_library_editor_plugin.cpp b/editor/plugins/asset_library_editor_plugin.cpp index d21c84eb61..d595d4dd98 100644 --- a/editor/plugins/asset_library_editor_plugin.cpp +++ b/editor/plugins/asset_library_editor_plugin.cpp @@ -705,7 +705,18 @@ void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PoolByt int len = image_data.size(); PoolByteArray::Read r = image_data.read(); - Ref<Image> image = Ref<Image>(memnew(Image(r.ptr(), len))); + Ref<Image> image = Ref<Image>(memnew(Image)); + + uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; + uint8_t jpg_signature[3] = { 255, 216, 255 }; + + if (r.ptr()) { + if (memcmp(&r[0], &png_signature[0], 8) == 0) { + image->copy_internals_from(Image::_png_mem_loader_func(r.ptr(), len)); + } else if (memcmp(&r[0], &jpg_signature[0], 3) == 0) { + image->copy_internals_from(Image::_jpg_mem_loader_func(r.ptr(), len)); + } + } if (!image->empty()) { switch (image_queue[p_queue_id].image_type) { @@ -750,7 +761,7 @@ void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, cons ERR_FAIL_COND(!image_queue.has(p_queue_id)); - if (p_status == HTTPRequest::RESULT_SUCCESS) { + if (p_status == HTTPRequest::RESULT_SUCCESS && p_code < HTTPClient::RESPONSE_BAD_REQUEST) { if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) { for (int i = 0; i < headers.size(); i++) { @@ -781,7 +792,7 @@ void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, cons _image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id); } else { - WARN_PRINTS("Error getting PNG file from URL: " + image_queue[p_queue_id].image_url); + // WARN_PRINTS("Error getting image file from URL: " + image_queue[p_queue_id].image_url); Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target); if (obj) { obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("DefaultProjectIcon", "EditorIcons")); diff --git a/main/main.cpp b/main/main.cpp index c287bc81cb..70713e2dd8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1444,7 +1444,7 @@ bool Main::start() { } #endif - if (!project_manager) { // game or editor + if (!project_manager && !editor) { // game if (game_path != "" || script != "") { //autoload List<PropertyInfo> props; @@ -1465,24 +1465,13 @@ bool Main::start() { if (global_var) { for (int i = 0; i < ScriptServer::get_language_count(); i++) { -#ifdef TOOLS_ENABLED - if (editor) { - ScriptServer::get_language(i)->add_named_global_constant(name, Variant()); - } else { - ScriptServer::get_language(i)->add_global_constant(name, Variant()); - } -#else ScriptServer::get_language(i)->add_global_constant(name, Variant()); -#endif } } } //second pass, load into global constants List<Node *> to_add; -#ifdef TOOLS_ENABLED - ResourceLoader::set_timestamp_on_load(editor); // Avoid problems when editing -#endif for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { String s = E->get().name; @@ -1528,23 +1517,11 @@ bool Main::start() { if (global_var) { for (int i = 0; i < ScriptServer::get_language_count(); i++) { -#ifdef TOOLS_ENABLED - if (editor) { - ScriptServer::get_language(i)->add_named_global_constant(name, n); - } else { - ScriptServer::get_language(i)->add_global_constant(name, n); - } -#else ScriptServer::get_language(i)->add_global_constant(name, n); -#endif } } } -#ifdef TOOLS_ENABLED - ResourceLoader::set_timestamp_on_load(false); -#endif - for (List<Node *>::Element *E = to_add.front(); E; E = E->next()) { sml->get_root()->add_child(E->get()); |