diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/io/resource_format_binary.cpp | 50 | ||||
-rw-r--r-- | core/io/resource_format_binary.h | 2 | ||||
-rw-r--r-- | core/io/resource_format_xml.cpp | 46 | ||||
-rw-r--r-- | core/io/resource_format_xml.h | 2 | ||||
-rw-r--r-- | core/object.cpp | 58 | ||||
-rw-r--r-- | core/object.h | 7 | ||||
-rw-r--r-- | core/os/memory.cpp | 7 | ||||
-rw-r--r-- | core/os/memory.h | 17 | ||||
-rw-r--r-- | core/reference.h | 3 | ||||
-rw-r--r-- | core/resource.cpp | 12 | ||||
-rw-r--r-- | core/resource.h | 4 | ||||
-rw-r--r-- | core/undo_redo.cpp | 128 | ||||
-rw-r--r-- | core/undo_redo.h | 7 |
13 files changed, 313 insertions, 30 deletions
diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 9fb17bcffb..0f6f8a74b1 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -663,14 +663,18 @@ Error ResourceInteractiveLoaderBinary::poll(){ //maybe it is loaded already String path; + int subindex=0; if (!main) { path=internal_resources[s].path; - if (path.begins_with("local://")) - path=path.replace("local://",res_path+"::"); + if (path.begins_with("local://")) { + path=path.replace_first("local://",""); + subindex = path.to_int(); + path=res_path+"::"+path; + } @@ -709,6 +713,7 @@ Error ResourceInteractiveLoaderBinary::poll(){ RES res = RES( r ); r->set_path(path); + r->set_subindex(subindex); int pc = f->get_32(); @@ -1434,14 +1439,14 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant& p_property, save_unicode_string(path); } else { - if (!resource_map.has(res)) { + if (!resource_set.has(res)) { f->store_32(OBJECT_EMPTY); ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?"); ERR_FAIL(); } f->store_32(OBJECT_INTERNAL_RESOURCE); - f->store_32(resource_map[res]); + f->store_32(res->get_subindex()); //internal resource } @@ -1598,7 +1603,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant& p_variant } - if (resource_map.has(res)) + if (resource_set.has(res)) return; List<PropertyInfo> property_list; @@ -1613,7 +1618,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant& p_variant } } - resource_map[ res ] = saved_resources.size(); + resource_set.insert(res); saved_resources.push_back(res); } break; @@ -1846,11 +1851,42 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path,const RES& p_ // save internal resource table f->store_32(saved_resources.size()); //amount of internal resources Vector<uint64_t> ofs_pos; + Set<int> used_indices; + + 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->get_subindex()!=0) { + if (used_indices.has(r->get_subindex())) { + r->set_subindex(0); //repeated + } else { + used_indices.insert(r->get_subindex()); + } + } + } + + } + + 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) { - save_unicode_string("local://"+itos(ofs_pos.size())); + if (r->get_subindex()==0) { + int new_subindex=1; + if (used_indices.size()) { + new_subindex=used_indices.back()->get()+1; + } + + r->set_subindex(new_subindex); + used_indices.insert(new_subindex); + + } + + save_unicode_string("local://"+itos(r->get_subindex())); if (takeover_paths) { r->set_path(p_path+"::"+itos(ofs_pos.size()),true); } diff --git a/core/io/resource_format_binary.h b/core/io/resource_format_binary.h index ab2e640a86..da415d97a5 100644 --- a/core/io/resource_format_binary.h +++ b/core/io/resource_format_binary.h @@ -129,7 +129,7 @@ class ResourceFormatSaverBinaryInstance { int bin_meta_idx; FileAccess *f; String magic; - Map<RES,int> resource_map; + Set<RES> resource_set; Map<StringName,int> string_map; Vector<StringName> strings; diff --git a/core/io/resource_format_xml.cpp b/core/io/resource_format_xml.cpp index 3c100d375a..36746a4111 100644 --- a/core/io/resource_format_xml.cpp +++ b/core/io/resource_format_xml.cpp @@ -1466,6 +1466,7 @@ Error ResourceInteractiveLoaderXML::poll() { String type; String path; + int subres=0; if (!main) { //loading resource @@ -1476,11 +1477,15 @@ Error ResourceInteractiveLoaderXML::poll() { ERR_EXPLAIN(local_path+":"+itos(get_current_line())+": <resource> missing 'type' field."); ERR_FAIL_COND_V(!tag->args.has("type"),ERR_FILE_CORRUPT); path=tag->args["path"]; + error=OK; if (path.begins_with("local://")) { //built-in resource (but really external) - path=path.replace("local://",local_path+"::"); + + path=path.replace("local://",""); + subres=path.to_int(); + path=local_path+"::"+path; } @@ -1519,6 +1524,7 @@ Error ResourceInteractiveLoaderXML::poll() { res = RES( r ); if (path!="") r->set_path(path); + r->set_subindex(subres); //load properties @@ -2029,9 +2035,9 @@ void ResourceFormatSaverXMLInstance::write_property(const String& p_name,const V //internal resource ERR_EXPLAIN("Resource was not pre cached for the resource section, bug?"); - ERR_FAIL_COND(!resource_map.has(res)); + ERR_FAIL_COND(!resource_set.has(res)); - params+=" path=\"local://"+itos(resource_map[res])+"\""; + params+=" path=\"local://"+itos(res->get_subindex())+"\""; } } break; @@ -2443,7 +2449,7 @@ void ResourceFormatSaverXMLInstance::_find_resources(const Variant& p_variant,bo return; } - if (resource_map.has(res)) + if (resource_set.has(res)) return; List<PropertyInfo> property_list; @@ -2466,7 +2472,7 @@ void ResourceFormatSaverXMLInstance::_find_resources(const Variant& p_variant,bo I=I->next(); } - resource_map[ res ] = resource_map.size(); //saved after, so the childs it needs are available when loaded + resource_set.insert( res ); //saved after, so the childs it needs are available when loaded saved_resources.push_back(res); } break; @@ -2537,12 +2543,27 @@ Error ResourceFormatSaverXMLInstance::save(const String &p_path,const RES& p_res write_string("\n",false); } + Set<int> used_indices; + + 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 (res->get_subindex()!=0) { + if (used_indices.has(res->get_subindex())) { + res->set_subindex(0); //repeated + } else { + used_indices.insert(res->get_subindex()); + } + } + } + } for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) { RES res = E->get(); - ERR_CONTINUE(!resource_map.has(res)); + ERR_CONTINUE(!resource_set.has(res)); bool main = (E->next()==NULL); write_tabs(); @@ -2552,7 +2573,18 @@ Error ResourceFormatSaverXMLInstance::save(const String &p_path,const RES& p_res else if (res->get_path().length() && res->get_path().find("::") == -1 ) enter_tag("resource","type=\""+res->get_type()+"\" path=\""+res->get_path()+"\""); //bundled else { - int idx = resource_map[res]; + + if (res->get_subindex()==0) { + int new_subindex=1; + if (used_indices.size()) { + new_subindex=used_indices.back()->get()+1; + } + + res->set_subindex(new_subindex); + used_indices.insert(new_subindex); + } + + int idx = res->get_subindex(); enter_tag("resource","type=\""+res->get_type()+"\" path=\"local://"+itos(idx)+"\""); if (takeover_paths) { res->set_path(p_path+"::"+itos(idx),true); diff --git a/core/io/resource_format_xml.h b/core/io/resource_format_xml.h index 711b607668..d5ba9eb800 100644 --- a/core/io/resource_format_xml.h +++ b/core/io/resource_format_xml.h @@ -123,7 +123,7 @@ class ResourceFormatSaverXMLInstance { bool skip_editor; FileAccess *f; int depth; - Map<RES,int> resource_map; + Set<RES> resource_set; List<RES> saved_resources; List<RES> external_resources; diff --git a/core/object.cpp b/core/object.cpp index 83a6dada80..cdb2c6517a 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -34,6 +34,7 @@ #include "core_string_names.h" #include "translation.h" #include "os/os.h" +#include "resource.h" #ifdef DEBUG_ENABLED @@ -1464,6 +1465,63 @@ StringName Object::tr(const StringName& p_message) const { } + +void Object::_clear_internal_resource_paths(const Variant &p_var) { + + switch(p_var.get_type()) { + + case Variant::OBJECT: { + + RES r = p_var; + if (!r.is_valid()) + return; + + if (!r->get_path().begins_with("res://") || r->get_path().find("::")==-1) + return; //not an internal resource + + Object *object=p_var; + if (!object) + return; + + r->set_path(""); + r->clear_internal_resource_paths(); + } break; + case Variant::ARRAY: { + + Array a=p_var; + for(int i=0;i<a.size();i++) { + _clear_internal_resource_paths(a[i]); + } + + } break; + case Variant::DICTIONARY: { + + Dictionary d=p_var; + List<Variant> keys; + d.get_key_list(&keys); + + for (List<Variant>::Element *E=keys.front();E;E=E->next()) { + + _clear_internal_resource_paths(E->get()); + _clear_internal_resource_paths(d[E->get()]); + } + } break; + } + +} + +void Object::clear_internal_resource_paths() { + + List<PropertyInfo> pinfo; + + get_property_list(&pinfo); + + for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) { + + _clear_internal_resource_paths(get(E->get().name)); + } +} + void Object::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_type"),&Object::get_type); diff --git a/core/object.h b/core/object.h index 8d1f8ebc5a..9680af924a 100644 --- a/core/object.h +++ b/core/object.h @@ -451,6 +451,8 @@ protected: Array _get_property_list_bind() const; Array _get_method_list_bind() const; + void _clear_internal_resource_paths(const Variant &p_var); + public: //should be protected, but bug in clang++ static void initialize_type(); _FORCE_INLINE_ static void register_custom_data_to_otdb() {}; @@ -599,6 +601,9 @@ public: _FORCE_INLINE_ void set_message_translation(bool p_enable) { _can_translate=p_enable; } _FORCE_INLINE_ bool can_translate_messages() const { return _can_translate; } + + void clear_internal_resource_paths(); + Object(); virtual ~Object(); @@ -651,6 +656,8 @@ public: #endif + + }; //needed by macros diff --git a/core/os/memory.cpp b/core/os/memory.cpp index 617e40e92a..1d9ac4e302 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -112,13 +112,6 @@ size_t Memory::get_dynamic_mem_usage() { return MemoryPoolDynamic::get_singleton()->get_total_usage(); } -void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description) { - - void *failptr=0; - ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */ - - return p_pointer; -} diff --git a/core/os/memory.h b/core/os/memory.h index 4497fcc200..0a35c93fdb 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -236,11 +236,11 @@ void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_de #endif -_FORCE_INLINE_ void postinitialize_handler(void *) {} +_ALWAYS_INLINE_ void postinitialize_handler(void *) {} template<class T> -_FORCE_INLINE_ T *_post_initialize(T *p_obj) { +_ALWAYS_INLINE_ T *_post_initialize(T *p_obj) { postinitialize_handler(p_obj); return p_obj; @@ -249,19 +249,26 @@ _FORCE_INLINE_ T *_post_initialize(T *p_obj) { #ifdef DEBUG_MEMORY_ENABLED #define memnew(m_class) _post_initialize(new(__FILE__":"__STR(__LINE__)", memnew type: "__STR(m_class)) m_class) -#define memnew_placement(m_placement,m_class) _post_initialize(new(m_placement,sizeof(m_class),__FILE__":"__STR(__LINE__)", type: "__STR(m_class)) m_class) #else #define memnew(m_class) _post_initialize(new("") m_class) -#define memnew_placement(m_placement,m_class) _post_initialize(new(m_placement,sizeof(m_class),"") m_class) #endif +_ALWAYS_INLINE_ void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description) { +// void *failptr=0; +// ERR_FAIL_COND_V( check < p_size , failptr); /** bug, or strange compiler, most likely */ + + return p_pointer; +} + + #define memnew_allocator(m_class,m_allocator) _post_initialize(new(m_allocator::alloc) m_class) +#define memnew_placement(m_placement,m_class) _post_initialize(new(m_placement,sizeof(m_class),"") m_class) -_FORCE_INLINE_ bool predelete_handler(void *) { return true; } +_ALWAYS_INLINE_ bool predelete_handler(void *) { return true; } template<class T> void memdelete(T *p_class) { diff --git a/core/reference.h b/core/reference.h index 544da41044..65f31f78f3 100644 --- a/core/reference.h +++ b/core/reference.h @@ -294,6 +294,9 @@ public: reference=NULL; } + void instance() { + ref( memnew( T )); + } Ref() { diff --git a/core/resource.cpp b/core/resource.cpp index 6e65693350..6967599f96 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -195,6 +195,17 @@ String Resource::get_path() const { return path_cache; } +void Resource::set_subindex(int p_sub_index) { + + subindex=p_sub_index; +} + +int Resource::get_subindex() const{ + + return subindex; +} + + void Resource::set_name(const String& p_name) { name=p_name; @@ -326,6 +337,7 @@ Resource::Resource() { last_modified_time=0; #endif + subindex=0; } diff --git a/core/resource.h b/core/resource.h index cf7ffcbd2c..9d9c445e1d 100644 --- a/core/resource.h +++ b/core/resource.h @@ -99,6 +99,7 @@ friend class ResourceCache; String name; String path_cache; + int subindex; virtual bool _use_builtin_script() const { return true; } @@ -132,6 +133,9 @@ public: void set_path(const String& p_path,bool p_take_over=false); String get_path() const; + void set_subindex(int p_sub_index); + int get_subindex() const; + Ref<Resource> duplicate(bool p_subresources=false); void set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata); diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index 5e7df3be7e..f565070216 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -282,6 +282,7 @@ void UndoRedo::undo() { return; //nothing to redo _process_operation_list(actions[current_action].undo_ops.front()); current_action--; + version--; } void UndoRedo::clear_history() { @@ -292,7 +293,7 @@ void UndoRedo::clear_history() { while(actions.size()) _pop_history_tail(); - version++; + //version++; } String UndoRedo::get_current_action_name() const { @@ -326,7 +327,7 @@ void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback,void* UndoRedo::UndoRedo() { - version=0; + version=1; action_level=0; current_action=-1; max_steps=-1; @@ -339,3 +340,126 @@ UndoRedo::~UndoRedo() { clear_history(); } + +Variant UndoRedo::_add_do_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error) { + + if (p_argcount<2) { + r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument=0; + return Variant(); + } + + if (p_args[0]->get_type()!=Variant::OBJECT) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=0; + r_error.expected=Variant::OBJECT; + return Variant(); + } + + if (p_args[1]->get_type()!=Variant::STRING) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=1; + r_error.expected=Variant::STRING; + return Variant(); + } + + r_error.error=Variant::CallError::CALL_OK; + + Object* object = *p_args[0]; + String method = *p_args[1]; + + Variant v[VARIANT_ARG_MAX]; + + + for(int i=0;i<MIN(VARIANT_ARG_MAX,p_argcount-2);++i) { + + v[i]=*p_args[i+2]; + } + + add_do_method(object,method,v[0],v[1],v[2],v[3],v[4]); + return Variant(); +} + +Variant UndoRedo::_add_undo_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error) { + + if (p_argcount<2) { + r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument=0; + return Variant(); + } + + if (p_args[0]->get_type()!=Variant::OBJECT) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=0; + r_error.expected=Variant::OBJECT; + return Variant(); + } + + if (p_args[1]->get_type()!=Variant::STRING) { + r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument=1; + r_error.expected=Variant::STRING; + return Variant(); + } + + r_error.error=Variant::CallError::CALL_OK; + + Object* object = *p_args[0]; + String method = *p_args[1]; + + Variant v[VARIANT_ARG_MAX]; + + + for(int i=0;i<MIN(VARIANT_ARG_MAX,p_argcount-2);++i) { + + v[i]=*p_args[i+2]; + } + + add_undo_method(object,method,v[0],v[1],v[2],v[3],v[4]); + return Variant(); +} + +void UndoRedo::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("create_action","name","mergeable"),&UndoRedo::create_action, DEFVAL(false) ); + ObjectTypeDB::bind_method(_MD("commit_action"),&UndoRedo::commit_action); + + //ObjectTypeDB::bind_method(_MD("add_do_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_do_method); + //ObjectTypeDB::bind_method(_MD("add_undo_method","p_object", "p_method", "VARIANT_ARG_LIST"),&UndoRedo::add_undo_method); + + { + MethodInfo mi; + mi.name="add_do_method"; + mi.arguments.push_back( PropertyInfo( Variant::OBJECT, "object")); + mi.arguments.push_back( PropertyInfo( Variant::STRING, "method")); + Vector<Variant> defargs; + for(int i=0;i<VARIANT_ARG_MAX;++i) { + mi.arguments.push_back( PropertyInfo( Variant::NIL, "arg"+itos(i))); + defargs.push_back(Variant()); + } + + ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"add_do_method",&UndoRedo::_add_do_method,mi,defargs); + } + + { + MethodInfo mi; + mi.name="add_undo_method"; + mi.arguments.push_back( PropertyInfo( Variant::OBJECT, "object")); + mi.arguments.push_back( PropertyInfo( Variant::STRING, "method")); + Vector<Variant> defargs; + for(int i=0;i<VARIANT_ARG_MAX;++i) { + mi.arguments.push_back( PropertyInfo( Variant::NIL, "arg"+itos(i))); + defargs.push_back(Variant()); + } + + ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"add_undo_method",&UndoRedo::_add_undo_method,mi,defargs); + } + + ObjectTypeDB::bind_method(_MD("add_do_property","object", "property", "value:var"),&UndoRedo::add_do_property); + ObjectTypeDB::bind_method(_MD("add_undo_property","object", "property", "value:var"),&UndoRedo::add_undo_property); + ObjectTypeDB::bind_method(_MD("add_do_reference","object"),&UndoRedo::add_do_reference); + ObjectTypeDB::bind_method(_MD("add_undo_reference","object"),&UndoRedo::add_undo_reference); + ObjectTypeDB::bind_method(_MD("clear_history"),&UndoRedo::clear_history); + ObjectTypeDB::bind_method(_MD("get_current_action_name"),&UndoRedo::get_current_action_name); + ObjectTypeDB::bind_method(_MD("get_version"),&UndoRedo::get_version); +} diff --git a/core/undo_redo.h b/core/undo_redo.h index d1b2d3de9e..a9187534c1 100644 --- a/core/undo_redo.h +++ b/core/undo_redo.h @@ -38,9 +38,12 @@ class UndoRedo : public Object { OBJ_TYPE(UndoRedo,Object); + OBJ_SAVE_TYPE( UndoRedo ); public: typedef void (*CommitNotifyCallback)(void *p_ud,const String& p_name); + Variant _add_do_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error); + Variant _add_undo_method(const Variant** p_args, int p_argcount, Variant::CallError& r_error); private: struct Operation { @@ -81,6 +84,10 @@ private: CommitNotifyCallback callback; void* callback_ud; + +protected: + static void _bind_methods(); + public: void create_action(const String& p_name="",bool p_mergeable=false); |