diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /core/resource.cpp | |
parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'core/resource.cpp')
-rw-r--r-- | core/resource.cpp | 221 |
1 files changed, 92 insertions, 129 deletions
diff --git a/core/resource.cpp b/core/resource.cpp index fe3cb2df92..e9ce4038d9 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -29,40 +29,36 @@ #include "resource.h" #include "core_string_names.h" -#include "os/file_access.h" #include "io/resource_loader.h" +#include "os/file_access.h" #include "script_language.h" #include <stdio.h> - void Resource::emit_changed() { emit_signal(CoreStringNames::get_singleton()->changed); } - void Resource::_resource_path_changed() { - - } -void Resource::set_path(const String& p_path, bool p_take_over) { +void Resource::set_path(const String &p_path, bool p_take_over) { - if (path_cache==p_path) + if (path_cache == p_path) return; - if (path_cache!="") { + if (path_cache != "") { ResourceCache::lock->write_lock(); ResourceCache::resources.erase(path_cache); ResourceCache::lock->write_unlock(); } - path_cache=""; + path_cache = ""; ResourceCache::lock->read_lock(); - bool has_path = ResourceCache::resources.has( p_path ); + bool has_path = ResourceCache::resources.has(p_path); ResourceCache::lock->read_unlock(); if (has_path) { @@ -72,28 +68,26 @@ void Resource::set_path(const String& p_path, bool p_take_over) { ResourceCache::resources.get(p_path)->set_name(""); ResourceCache::lock->write_unlock(); } else { - ERR_EXPLAIN("Another resource is loaded from path: "+p_path); + ERR_EXPLAIN("Another resource is loaded from path: " + p_path); ResourceCache::lock->read_lock(); - bool exists = ResourceCache::resources.has( p_path ); + bool exists = ResourceCache::resources.has(p_path); ResourceCache::lock->read_unlock(); - ERR_FAIL_COND( exists ); + ERR_FAIL_COND(exists); } - } - path_cache=p_path; + path_cache = p_path; - if (path_cache!="") { + if (path_cache != "") { ResourceCache::lock->write_lock(); - ResourceCache::resources[path_cache]=this; + ResourceCache::resources[path_cache] = this; ResourceCache::lock->write_unlock(); } _change_notify("resource_path"); _resource_path_changed(); - } String Resource::get_path() const { @@ -103,20 +97,18 @@ String Resource::get_path() const { void Resource::set_subindex(int p_sub_index) { - subindex=p_sub_index; + subindex = p_sub_index; } -int Resource::get_subindex() const{ +int Resource::get_subindex() const { return subindex; } +void Resource::set_name(const String &p_name) { -void Resource::set_name(const String& p_name) { - - name=p_name; + name = p_name; _change_notify("resource_name"); - } String Resource::get_name() const { @@ -130,12 +122,11 @@ bool Resource::editor_can_reload_from_file() { void Resource::reload_from_file() { - - String path=get_path(); + String path = get_path(); if (!path.is_resource_file()) return; - Ref<Resource> s = ResourceLoader::load(path,get_class(),true); + Ref<Resource> s = ResourceLoader::load(path, get_class(), true); if (!s.is_valid()) return; @@ -143,56 +134,51 @@ void Resource::reload_from_file() { List<PropertyInfo> pi; s->get_property_list(&pi); - for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) { + for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) { - if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) continue; - if (E->get().name=="resource_path") + if (E->get().name == "resource_path") continue; //do not change path - set(E->get().name,s->get(E->get().name)); - + set(E->get().name, s->get(E->get().name)); } } - Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) { - List<PropertyInfo> plist; get_property_list(&plist); + Resource *r = (Resource *)ClassDB::instance(get_class()); + ERR_FAIL_COND_V(!r, Ref<Resource>()); - Resource *r = (Resource*)ClassDB::instance(get_class()); - ERR_FAIL_COND_V(!r,Ref<Resource>()); - - r->local_scene=p_for_scene; + r->local_scene = p_for_scene; - for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) { + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) continue; Variant p = get(E->get().name); - if (p.get_type()==Variant::OBJECT) { + if (p.get_type() == Variant::OBJECT) { RES sr = p; if (sr.is_valid()) { if (sr->is_local_to_scene()) { if (remap_cache.has(sr)) { - p=remap_cache[sr]; + p = remap_cache[sr]; } else { - - RES dupe = sr->duplicate_for_local_scene(p_for_scene,remap_cache); - p=dupe; - remap_cache[sr]=dupe; + RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache); + p = dupe; + remap_cache[sr] = dupe; } } } } - r->set(E->get().name,p); + r->set(E->get().name, p); } return Ref<Resource>(r); @@ -200,50 +186,45 @@ Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Res Ref<Resource> Resource::duplicate(bool p_subresources) { - List<PropertyInfo> plist; get_property_list(&plist); + Resource *r = (Resource *)ClassDB::instance(get_class()); + ERR_FAIL_COND_V(!r, Ref<Resource>()); - Resource *r = (Resource*)ClassDB::instance(get_class()); - ERR_FAIL_COND_V(!r,Ref<Resource>()); - - for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) { + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) continue; Variant p = get(E->get().name); - if (p.get_type()==Variant::OBJECT && p_subresources) { + if (p.get_type() == Variant::OBJECT && p_subresources) { RES sr = p; if (sr.is_valid()) - p=sr->duplicate(true); + p = sr->duplicate(true); } - r->set(E->get().name,p); + r->set(E->get().name, p); } return Ref<Resource>(r); } +void Resource::_set_path(const String &p_path) { -void Resource::_set_path(const String& p_path) { - - set_path(p_path,false); + set_path(p_path, false); } -void Resource::_take_over_path(const String& p_path) { +void Resource::_take_over_path(const String &p_path) { - set_path(p_path,true); + set_path(p_path, true); } - RID Resource::get_rid() const { return RID(); } - void Resource::register_owner(Object *p_owner) { owners.insert(p_owner->get_instance_ID()); @@ -252,23 +233,20 @@ void Resource::register_owner(Object *p_owner) { void Resource::unregister_owner(Object *p_owner) { owners.erase(p_owner->get_instance_ID()); - } - void Resource::notify_change_to_owners() { - for(Set<ObjectID>::Element *E=owners.front();E;E=E->next()) { + for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) { Object *obj = ObjectDB::get_instance(E->get()); ERR_EXPLAIN("Object was deleted, while still owning a resource"); ERR_CONTINUE(!obj); //wtf //TODO store string - obj->call("resource_changed",RES(this)); + obj->call("resource_changed", RES(this)); } } - #ifdef TOOLS_ENABLED uint32_t Resource::hash_edited_version() const { @@ -278,25 +256,24 @@ uint32_t Resource::hash_edited_version() const { List<PropertyInfo> plist; get_property_list(&plist); - for (List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) { + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - if (E->get().type==Variant::OBJECT && E->get().hint==PROPERTY_HINT_RESOURCE_TYPE) { + if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) { RES res = get(E->get().name); if (res.is_valid()) { - hash = hash_djb2_one_32(res->hash_edited_version(),hash); + hash = hash_djb2_one_32(res->hash_edited_version(), hash); } } } return hash; - } #endif void Resource::set_local_to_scene(bool p_enable) { - local_to_scene=p_enable; + local_to_scene = p_enable; } bool Resource::is_local_to_scene() const { @@ -304,7 +281,7 @@ bool Resource::is_local_to_scene() const { return local_to_scene; } -Node* Resource::get_local_scene() const { +Node *Resource::get_local_scene() const { if (local_scene) return local_scene; @@ -322,51 +299,46 @@ void Resource::setup_local_to_scene() { get_script_instance()->call("_setup_local_to_scene"); } -Node* (*Resource::_get_local_scene_func)()=NULL; - +Node *(*Resource::_get_local_scene_func)() = NULL; void Resource::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_path","path"),&Resource::_set_path); - ClassDB::bind_method(D_METHOD("take_over_path","path"),&Resource::_take_over_path); - ClassDB::bind_method(D_METHOD("get_path"),&Resource::get_path); - ClassDB::bind_method(D_METHOD("set_name","name"),&Resource::set_name); - ClassDB::bind_method(D_METHOD("get_name"),&Resource::get_name); - ClassDB::bind_method(D_METHOD("get_rid"),&Resource::get_rid); - ClassDB::bind_method(D_METHOD("set_local_to_scene","enable"),&Resource::set_local_to_scene); - ClassDB::bind_method(D_METHOD("is_local_to_scene"),&Resource::is_local_to_scene); - ClassDB::bind_method(D_METHOD("get_local_scene:Node"),&Resource::get_local_scene); - ClassDB::bind_method(D_METHOD("setup_local_to_scene"),&Resource::setup_local_to_scene); - - ClassDB::bind_method(D_METHOD("duplicate","subresources"),&Resource::duplicate,DEFVAL(false)); - ADD_SIGNAL( MethodInfo("changed") ); - ADD_GROUP("Resource","resource_"); - ADD_PROPERTYNZ( PropertyInfo(Variant::BOOL,"resource_local_to_scene" ), "set_local_to_scene","is_local_to_scene"); - ADD_PROPERTY( PropertyInfo(Variant::STRING,"resource_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ), "set_path","get_path"); - ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"resource_name"), "set_name","get_name"); + ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path); + ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path); + ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path); + ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name); + ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name); + ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid); + ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene); + ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene); + ClassDB::bind_method(D_METHOD("get_local_scene:Node"), &Resource::get_local_scene); + ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene); - BIND_VMETHOD( MethodInfo("_setup_local_to_scene") ); + ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false)); + ADD_SIGNAL(MethodInfo("changed")); + ADD_GROUP("Resource", "resource_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path"); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name"); + BIND_VMETHOD(MethodInfo("_setup_local_to_scene")); } Resource::Resource() { #ifdef TOOLS_ENABLED - last_modified_time=0; - import_last_modified_time=0; + last_modified_time = 0; + import_last_modified_time = 0; #endif - subindex=0; - local_to_scene=false; - local_scene=NULL; + subindex = 0; + local_to_scene = false; + local_scene = NULL; } - - - Resource::~Resource() { - if (path_cache!="") { + if (path_cache != "") { ResourceCache::lock->write_lock(); ResourceCache::resources.erase(path_cache); ResourceCache::lock->write_unlock(); @@ -376,9 +348,9 @@ Resource::~Resource() { } } -HashMap<String,Resource*> ResourceCache::resources; +HashMap<String, Resource *> ResourceCache::resources; -RWLock *ResourceCache::lock=NULL; +RWLock *ResourceCache::lock = NULL; void ResourceCache::setup() { @@ -393,7 +365,6 @@ void ResourceCache::clear() { memdelete(lock); } - void ResourceCache::reload_externals() { /* @@ -404,16 +375,15 @@ void ResourceCache::reload_externals() { */ } -bool ResourceCache::has(const String& p_path) { +bool ResourceCache::has(const String &p_path) { lock->read_lock(); bool b = resources.has(p_path); lock->read_unlock(); - return b; } -Resource *ResourceCache::get(const String& p_path) { +Resource *ResourceCache::get(const String &p_path) { lock->read_lock(); @@ -428,20 +398,16 @@ Resource *ResourceCache::get(const String& p_path) { return *res; } - void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) { - lock->read_lock(); - const String* K=NULL; - while((K=resources.next(K))) { + const String *K = NULL; + while ((K = resources.next(K))) { Resource *r = resources[*K]; - p_resources->push_back( Ref<Resource>( r )); - + p_resources->push_back(Ref<Resource>(r)); } lock->read_unlock(); - } int ResourceCache::get_cached_resource_count() { @@ -453,42 +419,39 @@ int ResourceCache::get_cached_resource_count() { return rc; } -void ResourceCache::dump(const char* p_file,bool p_short) { +void ResourceCache::dump(const char *p_file, bool p_short) { #ifdef DEBUG_ENABLED lock->read_lock(); - Map<String,int> type_count; - + Map<String, int> type_count; - FileAccess *f=NULL; + FileAccess *f = NULL; if (p_file) { - f = FileAccess::open(p_file,FileAccess::WRITE); + f = FileAccess::open(p_file, FileAccess::WRITE); ERR_FAIL_COND(!f); - } - const String* K=NULL; - while((K=resources.next(K))) { + const String *K = NULL; + while ((K = resources.next(K))) { Resource *r = resources[*K]; if (!type_count.has(r->get_class())) { - type_count[r->get_class()]=0; + type_count[r->get_class()] = 0; } - type_count[r->get_class()]++; if (!p_short) { if (f) - f->store_line(r->get_class()+": "+r->get_path()); + f->store_line(r->get_class() + ": " + r->get_path()); } } - for(Map<String,int>::Element *E=type_count.front();E;E=E->next()) { + for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) { if (f) - f->store_line(E->key()+" count: "+itos(E->get())); + f->store_line(E->key() + " count: " + itos(E->get())); } if (f) { f->close(); |