resource.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*************************************************************************/
  2. /* resource.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "resource.h"
  30. #include "core_string_names.h"
  31. #include "os/file_access.h"
  32. #include "io/resource_loader.h"
  33. #include "script_language.h"
  34. #include <stdio.h>
  35. void Resource::emit_changed() {
  36. emit_signal(CoreStringNames::get_singleton()->changed);
  37. }
  38. void Resource::_resource_path_changed() {
  39. }
  40. void Resource::set_path(const String& p_path, bool p_take_over) {
  41. if (path_cache==p_path)
  42. return;
  43. if (path_cache!="") {
  44. ResourceCache::lock->write_lock();
  45. ResourceCache::resources.erase(path_cache);
  46. ResourceCache::lock->write_unlock();
  47. }
  48. path_cache="";
  49. ResourceCache::lock->read_lock();
  50. bool has_path = ResourceCache::resources.has( p_path );
  51. ResourceCache::lock->read_unlock();
  52. if (has_path) {
  53. if (p_take_over) {
  54. ResourceCache::lock->write_lock();
  55. ResourceCache::resources.get(p_path)->set_name("");
  56. ResourceCache::lock->write_unlock();
  57. } else {
  58. ERR_EXPLAIN("Another resource is loaded from path: "+p_path);
  59. ResourceCache::lock->read_lock();
  60. bool exists = ResourceCache::resources.has( p_path );
  61. ResourceCache::lock->read_unlock();
  62. ERR_FAIL_COND( exists );
  63. }
  64. }
  65. path_cache=p_path;
  66. if (path_cache!="") {
  67. ResourceCache::lock->write_lock();
  68. ResourceCache::resources[path_cache]=this;
  69. ResourceCache::lock->write_unlock();
  70. }
  71. _change_notify("resource_path");
  72. _resource_path_changed();
  73. }
  74. String Resource::get_path() const {
  75. return path_cache;
  76. }
  77. void Resource::set_subindex(int p_sub_index) {
  78. subindex=p_sub_index;
  79. }
  80. int Resource::get_subindex() const{
  81. return subindex;
  82. }
  83. void Resource::set_name(const String& p_name) {
  84. name=p_name;
  85. _change_notify("resource_name");
  86. }
  87. String Resource::get_name() const {
  88. return name;
  89. }
  90. bool Resource::editor_can_reload_from_file() {
  91. return true; //by default yes
  92. }
  93. void Resource::reload_from_file() {
  94. String path=get_path();
  95. if (!path.is_resource_file())
  96. return;
  97. Ref<Resource> s = ResourceLoader::load(path,get_class(),true);
  98. if (!s.is_valid())
  99. return;
  100. List<PropertyInfo> pi;
  101. s->get_property_list(&pi);
  102. for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
  103. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  104. continue;
  105. if (E->get().name=="resource_path")
  106. continue; //do not change path
  107. set(E->get().name,s->get(E->get().name));
  108. }
  109. }
  110. Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {
  111. List<PropertyInfo> plist;
  112. get_property_list(&plist);
  113. Resource *r = (Resource*)ClassDB::instance(get_class());
  114. ERR_FAIL_COND_V(!r,Ref<Resource>());
  115. r->local_scene=p_for_scene;
  116. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  117. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  118. continue;
  119. Variant p = get(E->get().name);
  120. if (p.get_type()==Variant::OBJECT) {
  121. RES sr = p;
  122. if (sr.is_valid()) {
  123. if (sr->is_local_to_scene()) {
  124. if (remap_cache.has(sr)) {
  125. p=remap_cache[sr];
  126. } else {
  127. RES dupe = sr->duplicate_for_local_scene(p_for_scene,remap_cache);
  128. p=dupe;
  129. remap_cache[sr]=dupe;
  130. }
  131. }
  132. }
  133. }
  134. r->set(E->get().name,p);
  135. }
  136. return Ref<Resource>(r);
  137. }
  138. Ref<Resource> Resource::duplicate(bool p_subresources) {
  139. List<PropertyInfo> plist;
  140. get_property_list(&plist);
  141. Resource *r = (Resource*)ClassDB::instance(get_class());
  142. ERR_FAIL_COND_V(!r,Ref<Resource>());
  143. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  144. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  145. continue;
  146. Variant p = get(E->get().name);
  147. if (p.get_type()==Variant::OBJECT && p_subresources) {
  148. RES sr = p;
  149. if (sr.is_valid())
  150. p=sr->duplicate(true);
  151. }
  152. r->set(E->get().name,p);
  153. }
  154. return Ref<Resource>(r);
  155. }
  156. void Resource::_set_path(const String& p_path) {
  157. set_path(p_path,false);
  158. }
  159. void Resource::_take_over_path(const String& p_path) {
  160. set_path(p_path,true);
  161. }
  162. RID Resource::get_rid() const {
  163. return RID();
  164. }
  165. void Resource::register_owner(Object *p_owner) {
  166. owners.insert(p_owner->get_instance_ID());
  167. }
  168. void Resource::unregister_owner(Object *p_owner) {
  169. owners.erase(p_owner->get_instance_ID());
  170. }
  171. void Resource::notify_change_to_owners() {
  172. for(Set<ObjectID>::Element *E=owners.front();E;E=E->next()) {
  173. Object *obj = ObjectDB::get_instance(E->get());
  174. ERR_EXPLAIN("Object was deleted, while still owning a resource");
  175. ERR_CONTINUE(!obj); //wtf
  176. //TODO store string
  177. obj->call("resource_changed",RES(this));
  178. }
  179. }
  180. #ifdef TOOLS_ENABLED
  181. uint32_t Resource::hash_edited_version() const {
  182. uint32_t hash = hash_djb2_one_32(get_edited_version());
  183. List<PropertyInfo> plist;
  184. get_property_list(&plist);
  185. for (List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  186. if (E->get().type==Variant::OBJECT && E->get().hint==PROPERTY_HINT_RESOURCE_TYPE) {
  187. RES res = get(E->get().name);
  188. if (res.is_valid()) {
  189. hash = hash_djb2_one_32(res->hash_edited_version(),hash);
  190. }
  191. }
  192. }
  193. return hash;
  194. }
  195. #endif
  196. void Resource::set_local_to_scene(bool p_enable) {
  197. local_to_scene=p_enable;
  198. }
  199. bool Resource::is_local_to_scene() const {
  200. return local_to_scene;
  201. }
  202. Node* Resource::get_local_scene() const {
  203. if (local_scene)
  204. return local_scene;
  205. if (_get_local_scene_func) {
  206. return _get_local_scene_func();
  207. }
  208. return NULL;
  209. }
  210. void Resource::setup_local_to_scene() {
  211. if (get_script_instance())
  212. get_script_instance()->call("_setup_local_to_scene");
  213. }
  214. Node* (*Resource::_get_local_scene_func)()=NULL;
  215. void Resource::_bind_methods() {
  216. ClassDB::bind_method(D_METHOD("set_path","path"),&Resource::_set_path);
  217. ClassDB::bind_method(D_METHOD("take_over_path","path"),&Resource::_take_over_path);
  218. ClassDB::bind_method(D_METHOD("get_path"),&Resource::get_path);
  219. ClassDB::bind_method(D_METHOD("set_name","name"),&Resource::set_name);
  220. ClassDB::bind_method(D_METHOD("get_name"),&Resource::get_name);
  221. ClassDB::bind_method(D_METHOD("get_rid"),&Resource::get_rid);
  222. ClassDB::bind_method(D_METHOD("set_local_to_scene","enable"),&Resource::set_local_to_scene);
  223. ClassDB::bind_method(D_METHOD("is_local_to_scene"),&Resource::is_local_to_scene);
  224. ClassDB::bind_method(D_METHOD("get_local_scene:Node"),&Resource::get_local_scene);
  225. ClassDB::bind_method(D_METHOD("setup_local_to_scene"),&Resource::setup_local_to_scene);
  226. ClassDB::bind_method(D_METHOD("duplicate","subresources"),&Resource::duplicate,DEFVAL(false));
  227. ADD_SIGNAL( MethodInfo("changed") );
  228. ADD_GROUP("Resource","resource_");
  229. ADD_PROPERTYNZ( PropertyInfo(Variant::BOOL,"resource_local_to_scene" ), "set_local_to_scene","is_local_to_scene");
  230. ADD_PROPERTY( PropertyInfo(Variant::STRING,"resource_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ), "set_path","get_path");
  231. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"resource_name"), "set_name","get_name");
  232. BIND_VMETHOD( MethodInfo("_setup_local_to_scene") );
  233. }
  234. Resource::Resource() {
  235. #ifdef TOOLS_ENABLED
  236. last_modified_time=0;
  237. import_last_modified_time=0;
  238. #endif
  239. subindex=0;
  240. local_to_scene=false;
  241. local_scene=NULL;
  242. }
  243. Resource::~Resource() {
  244. if (path_cache!="") {
  245. ResourceCache::lock->write_lock();
  246. ResourceCache::resources.erase(path_cache);
  247. ResourceCache::lock->write_unlock();
  248. }
  249. if (owners.size()) {
  250. WARN_PRINT("Resource is still owned");
  251. }
  252. }
  253. HashMap<String,Resource*> ResourceCache::resources;
  254. RWLock *ResourceCache::lock=NULL;
  255. void ResourceCache::setup() {
  256. lock = RWLock::create();
  257. }
  258. void ResourceCache::clear() {
  259. if (resources.size())
  260. ERR_PRINT("Resources Still in use at Exit!");
  261. resources.clear();
  262. memdelete(lock);
  263. }
  264. void ResourceCache::reload_externals() {
  265. /*
  266. const String *K=NULL;
  267. while ((K=resources.next(K))) {
  268. resources[*K]->reload_external_data();
  269. }
  270. */
  271. }
  272. bool ResourceCache::has(const String& p_path) {
  273. lock->read_lock();
  274. bool b = resources.has(p_path);
  275. lock->read_unlock();
  276. return b;
  277. }
  278. Resource *ResourceCache::get(const String& p_path) {
  279. lock->read_lock();
  280. Resource **res = resources.getptr(p_path);
  281. lock->read_unlock();
  282. if (!res) {
  283. return NULL;
  284. }
  285. return *res;
  286. }
  287. void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
  288. lock->read_lock();
  289. const String* K=NULL;
  290. while((K=resources.next(K))) {
  291. Resource *r = resources[*K];
  292. p_resources->push_back( Ref<Resource>( r ));
  293. }
  294. lock->read_unlock();
  295. }
  296. int ResourceCache::get_cached_resource_count() {
  297. lock->read_lock();
  298. int rc = resources.size();
  299. lock->read_unlock();
  300. return rc;
  301. }
  302. void ResourceCache::dump(const char* p_file,bool p_short) {
  303. #ifdef DEBUG_ENABLED
  304. lock->read_lock();
  305. Map<String,int> type_count;
  306. FileAccess *f=NULL;
  307. if (p_file) {
  308. f = FileAccess::open(p_file,FileAccess::WRITE);
  309. ERR_FAIL_COND(!f);
  310. }
  311. const String* K=NULL;
  312. while((K=resources.next(K))) {
  313. Resource *r = resources[*K];
  314. if (!type_count.has(r->get_class())) {
  315. type_count[r->get_class()]=0;
  316. }
  317. type_count[r->get_class()]++;
  318. if (!p_short) {
  319. if (f)
  320. f->store_line(r->get_class()+": "+r->get_path());
  321. }
  322. }
  323. for(Map<String,int>::Element *E=type_count.front();E;E=E->next()) {
  324. if (f)
  325. f->store_line(E->key()+" count: "+itos(E->get()));
  326. }
  327. if (f) {
  328. f->close();
  329. memdelete(f);
  330. }
  331. lock->read_unlock();
  332. #endif
  333. }