resource.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 <stdio.h>
  32. #include "os/file_access.h"
  33. #include "io/resource_loader.h"
  34. #include "script_language.h"
  35. void ResourceImportMetadata::set_editor(const String& p_editor) {
  36. editor=p_editor;
  37. }
  38. String ResourceImportMetadata::get_editor() const{
  39. return editor;
  40. }
  41. void ResourceImportMetadata::add_source(const String& p_path,const String& p_md5) {
  42. Source s;
  43. s.md5=p_md5;
  44. s.path=p_path;
  45. sources.push_back(s);
  46. }
  47. String ResourceImportMetadata::get_source_path(int p_idx) const{
  48. ERR_FAIL_INDEX_V(p_idx,sources.size(),String());
  49. return sources[p_idx].path;
  50. }
  51. String ResourceImportMetadata::get_source_md5(int p_idx) const{
  52. ERR_FAIL_INDEX_V(p_idx,sources.size(),String());
  53. return sources[p_idx].md5;
  54. }
  55. void ResourceImportMetadata::set_source_md5(int p_idx,const String& p_md5) {
  56. ERR_FAIL_INDEX(p_idx,sources.size());
  57. sources[p_idx].md5=p_md5;
  58. }
  59. void ResourceImportMetadata::remove_source(int p_idx){
  60. ERR_FAIL_INDEX(p_idx,sources.size());
  61. sources.remove(p_idx);
  62. }
  63. int ResourceImportMetadata::get_source_count() const {
  64. return sources.size();
  65. }
  66. void ResourceImportMetadata::set_option(const String& p_key, const Variant& p_value) {
  67. if (p_value.get_type()==Variant::NIL) {
  68. options.erase(p_key);
  69. return;
  70. }
  71. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  72. ERR_FAIL_COND(p_value.get_type() == Variant::_RID);
  73. options[p_key]=p_value;
  74. }
  75. bool ResourceImportMetadata::has_option(const String& p_key) const {
  76. return options.has(p_key);
  77. }
  78. Variant ResourceImportMetadata::get_option(const String& p_key) const {
  79. ERR_FAIL_COND_V(!options.has(p_key),Variant());
  80. return options[p_key];
  81. }
  82. void ResourceImportMetadata::get_options(List<String> *r_options) const {
  83. for(Map<String,Variant>::Element *E=options.front();E;E=E->next()) {
  84. r_options->push_back(E->key());
  85. }
  86. }
  87. StringArray ResourceImportMetadata::_get_options() const {
  88. StringArray option_names;
  89. option_names.resize(options.size());
  90. int i=0;
  91. for(Map<String,Variant>::Element *E=options.front();E;E=E->next()) {
  92. option_names.set(i++,E->key());
  93. }
  94. return option_names;
  95. }
  96. void ResourceImportMetadata::_bind_methods() {
  97. ClassDB::bind_method(_MD("set_editor","name"),&ResourceImportMetadata::set_editor);
  98. ClassDB::bind_method(_MD("get_editor"),&ResourceImportMetadata::get_editor);
  99. ClassDB::bind_method(_MD("add_source","path","md5"),&ResourceImportMetadata::add_source, "");
  100. ClassDB::bind_method(_MD("get_source_path","idx"),&ResourceImportMetadata::get_source_path);
  101. ClassDB::bind_method(_MD("get_source_md5","idx"),&ResourceImportMetadata::get_source_md5);
  102. ClassDB::bind_method(_MD("set_source_md5","idx", "md5"),&ResourceImportMetadata::set_source_md5);
  103. ClassDB::bind_method(_MD("remove_source","idx"),&ResourceImportMetadata::remove_source);
  104. ClassDB::bind_method(_MD("get_source_count"),&ResourceImportMetadata::get_source_count);
  105. ClassDB::bind_method(_MD("set_option","key","value"),&ResourceImportMetadata::set_option);
  106. ClassDB::bind_method(_MD("get_option","key"),&ResourceImportMetadata::get_option);
  107. ClassDB::bind_method(_MD("get_options"),&ResourceImportMetadata::_get_options);
  108. }
  109. ResourceImportMetadata::ResourceImportMetadata() {
  110. }
  111. void Resource::emit_changed() {
  112. emit_signal(CoreStringNames::get_singleton()->changed);
  113. }
  114. void Resource::_resource_path_changed() {
  115. }
  116. void Resource::set_path(const String& p_path, bool p_take_over) {
  117. if (path_cache==p_path)
  118. return;
  119. if (path_cache!="") {
  120. ResourceCache::lock->write_lock();
  121. ResourceCache::resources.erase(path_cache);
  122. ResourceCache::lock->write_unlock();
  123. }
  124. path_cache="";
  125. ResourceCache::lock->read_lock();
  126. bool has_path = ResourceCache::resources.has( p_path );
  127. ResourceCache::lock->read_unlock();
  128. if (has_path) {
  129. if (p_take_over) {
  130. ResourceCache::lock->write_lock();
  131. ResourceCache::resources.get(p_path)->set_name("");
  132. ResourceCache::lock->write_unlock();
  133. } else {
  134. ERR_EXPLAIN("Another resource is loaded from path: "+p_path);
  135. ResourceCache::lock->read_lock();
  136. bool exists = ResourceCache::resources.has( p_path );
  137. ResourceCache::lock->read_unlock();
  138. ERR_FAIL_COND( exists );
  139. }
  140. }
  141. path_cache=p_path;
  142. if (path_cache!="") {
  143. ResourceCache::lock->write_lock();
  144. ResourceCache::resources[path_cache]=this;;
  145. ResourceCache::lock->write_unlock();
  146. }
  147. _change_notify("resource_path");
  148. _resource_path_changed();
  149. }
  150. String Resource::get_path() const {
  151. return path_cache;
  152. }
  153. void Resource::set_subindex(int p_sub_index) {
  154. subindex=p_sub_index;
  155. }
  156. int Resource::get_subindex() const{
  157. return subindex;
  158. }
  159. void Resource::set_name(const String& p_name) {
  160. name=p_name;
  161. _change_notify("resource_name");
  162. }
  163. String Resource::get_name() const {
  164. return name;
  165. }
  166. bool Resource::editor_can_reload_from_file() {
  167. return true; //by default yes
  168. }
  169. void Resource::reload_from_file() {
  170. String path=get_path();
  171. if (!path.is_resource_file())
  172. return;
  173. Ref<Resource> s = ResourceLoader::load(path,get_class(),true);
  174. if (!s.is_valid())
  175. return;
  176. List<PropertyInfo> pi;
  177. s->get_property_list(&pi);
  178. for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
  179. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  180. continue;
  181. if (E->get().name=="resource_path")
  182. continue; //do not change path
  183. set(E->get().name,s->get(E->get().name));
  184. }
  185. }
  186. Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {
  187. List<PropertyInfo> plist;
  188. get_property_list(&plist);
  189. Resource *r = (Resource*)ClassDB::instance(get_class());
  190. ERR_FAIL_COND_V(!r,Ref<Resource>());
  191. r->local_scene=p_for_scene;
  192. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  193. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  194. continue;
  195. Variant p = get(E->get().name);
  196. if (p.get_type()==Variant::OBJECT) {
  197. RES sr = p;
  198. if (sr.is_valid()) {
  199. if (sr->is_local_to_scene()) {
  200. if (remap_cache.has(sr)) {
  201. p=remap_cache[sr];
  202. } else {
  203. RES dupe = sr->duplicate_for_local_scene(p_for_scene,remap_cache);
  204. p=dupe;
  205. remap_cache[sr]=dupe;
  206. }
  207. }
  208. }
  209. }
  210. r->set(E->get().name,p);
  211. }
  212. return Ref<Resource>(r);
  213. }
  214. Ref<Resource> Resource::duplicate(bool p_subresources) {
  215. List<PropertyInfo> plist;
  216. get_property_list(&plist);
  217. Resource *r = (Resource*)ClassDB::instance(get_class());
  218. ERR_FAIL_COND_V(!r,Ref<Resource>());
  219. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  220. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  221. continue;
  222. Variant p = get(E->get().name);
  223. if (p.get_type()==Variant::OBJECT && p_subresources) {
  224. RES sr = p;
  225. if (sr.is_valid())
  226. p=sr->duplicate(true);
  227. }
  228. r->set(E->get().name,p);
  229. }
  230. return Ref<Resource>(r);
  231. }
  232. void Resource::_set_path(const String& p_path) {
  233. set_path(p_path,false);
  234. }
  235. void Resource::_take_over_path(const String& p_path) {
  236. set_path(p_path,true);
  237. }
  238. RID Resource::get_rid() const {
  239. return RID();
  240. }
  241. void Resource::register_owner(Object *p_owner) {
  242. owners.insert(p_owner->get_instance_ID());
  243. }
  244. void Resource::unregister_owner(Object *p_owner) {
  245. owners.erase(p_owner->get_instance_ID());
  246. }
  247. void Resource::notify_change_to_owners() {
  248. for(Set<ObjectID>::Element *E=owners.front();E;E=E->next()) {
  249. Object *obj = ObjectDB::get_instance(E->get());
  250. ERR_EXPLAIN("Object was deleted, while still owning a resource");
  251. ERR_CONTINUE(!obj); //wtf
  252. //TODO store string
  253. obj->call("resource_changed",RES(this));
  254. }
  255. }
  256. void Resource::set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata) {
  257. #ifdef TOOLS_ENABLED
  258. import_metadata=p_metadata;
  259. #endif
  260. }
  261. Ref<ResourceImportMetadata> Resource::get_import_metadata() const {
  262. #ifdef TOOLS_ENABLED
  263. return import_metadata;
  264. #else
  265. return Ref<ResourceImportMetadata>();
  266. #endif
  267. }
  268. #ifdef TOOLS_ENABLED
  269. uint32_t Resource::hash_edited_version() const {
  270. uint32_t hash = hash_djb2_one_32(get_edited_version());
  271. List<PropertyInfo> plist;
  272. get_property_list(&plist);
  273. for (List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  274. if (E->get().type==Variant::OBJECT && E->get().hint==PROPERTY_HINT_RESOURCE_TYPE) {
  275. RES res = get(E->get().name);
  276. if (res.is_valid()) {
  277. hash = hash_djb2_one_32(res->hash_edited_version(),hash);
  278. }
  279. }
  280. }
  281. return hash;
  282. }
  283. #endif
  284. void Resource::set_local_to_scene(bool p_enable) {
  285. local_to_scene=p_enable;
  286. }
  287. bool Resource::is_local_to_scene() const {
  288. return local_to_scene;
  289. }
  290. Node* Resource::get_local_scene() const {
  291. if (local_scene)
  292. return local_scene;
  293. if (_get_local_scene_func) {
  294. return _get_local_scene_func();
  295. }
  296. return NULL;
  297. }
  298. void Resource::setup_local_to_scene() {
  299. if (get_script_instance())
  300. get_script_instance()->call("_setup_local_to_scene");
  301. }
  302. Node* (*Resource::_get_local_scene_func)()=NULL;
  303. void Resource::_bind_methods() {
  304. ClassDB::bind_method(_MD("set_path","path"),&Resource::_set_path);
  305. ClassDB::bind_method(_MD("take_over_path","path"),&Resource::_take_over_path);
  306. ClassDB::bind_method(_MD("get_path"),&Resource::get_path);
  307. ClassDB::bind_method(_MD("set_name","name"),&Resource::set_name);
  308. ClassDB::bind_method(_MD("get_name"),&Resource::get_name);
  309. ClassDB::bind_method(_MD("get_rid"),&Resource::get_rid);
  310. ClassDB::bind_method(_MD("set_import_metadata","metadata"),&Resource::set_import_metadata);
  311. ClassDB::bind_method(_MD("get_import_metadata"),&Resource::get_import_metadata);
  312. ClassDB::bind_method(_MD("set_local_to_scene","enable"),&Resource::set_local_to_scene);
  313. ClassDB::bind_method(_MD("is_local_to_scene"),&Resource::is_local_to_scene);
  314. ClassDB::bind_method(_MD("get_local_scene:Node"),&Resource::get_local_scene);
  315. ClassDB::bind_method(_MD("setup_local_to_scene"),&Resource::setup_local_to_scene);
  316. ClassDB::bind_method(_MD("duplicate","subresources"),&Resource::duplicate,DEFVAL(false));
  317. ADD_SIGNAL( MethodInfo("changed") );
  318. ADD_GROUP("Resource","resource_");
  319. ADD_PROPERTYNZ( PropertyInfo(Variant::BOOL,"resource_local_to_scene" ), _SCS("set_local_to_scene"),_SCS("is_local_to_scene"));
  320. ADD_PROPERTY( PropertyInfo(Variant::STRING,"resource_path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ), _SCS("set_path"),_SCS("get_path"));
  321. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"resource_name"), _SCS("set_name"),_SCS("get_name"));
  322. BIND_VMETHOD( MethodInfo("_setup_local_to_scene") );
  323. }
  324. Resource::Resource() {
  325. #ifdef TOOLS_ENABLED
  326. last_modified_time=0;
  327. #endif
  328. subindex=0;
  329. local_scene=NULL;
  330. }
  331. Resource::~Resource() {
  332. if (path_cache!="") {
  333. ResourceCache::lock->write_lock();
  334. ResourceCache::resources.erase(path_cache);
  335. ResourceCache::lock->write_unlock();
  336. }
  337. if (owners.size()) {
  338. WARN_PRINT("Resource is still owned");
  339. }
  340. }
  341. HashMap<String,Resource*> ResourceCache::resources;
  342. RWLock *ResourceCache::lock=NULL;
  343. void ResourceCache::setup() {
  344. lock = RWLock::create();
  345. }
  346. void ResourceCache::clear() {
  347. if (resources.size())
  348. ERR_PRINT("Resources Still in use at Exit!");
  349. resources.clear();
  350. memdelete(lock);
  351. }
  352. void ResourceCache::reload_externals() {
  353. //const String *K=NULL;
  354. //while ((K=resources.next(K))) {
  355. // resources[*K]->reload_external_data();
  356. // }
  357. }
  358. bool ResourceCache::has(const String& p_path) {
  359. lock->read_lock();;
  360. bool b = resources.has(p_path);
  361. lock->read_unlock();;
  362. return b;
  363. }
  364. Resource *ResourceCache::get(const String& p_path) {
  365. lock->read_lock();
  366. Resource **res = resources.getptr(p_path);
  367. lock->read_unlock();
  368. if (!res) {
  369. return NULL;
  370. }
  371. return *res;
  372. }
  373. void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
  374. lock->read_lock();
  375. const String* K=NULL;
  376. while((K=resources.next(K))) {
  377. Resource *r = resources[*K];
  378. p_resources->push_back( Ref<Resource>( r ));
  379. }
  380. lock->read_unlock();
  381. }
  382. int ResourceCache::get_cached_resource_count() {
  383. lock->read_lock();
  384. int rc = resources.size();
  385. lock->read_unlock();
  386. return rc;
  387. }
  388. void ResourceCache::dump(const char* p_file,bool p_short) {
  389. #ifdef DEBUG_ENABLED
  390. lock->read_lock();
  391. Map<String,int> type_count;
  392. FileAccess *f=NULL;
  393. if (p_file) {
  394. f = FileAccess::open(p_file,FileAccess::WRITE);
  395. ERR_FAIL_COND(!f);
  396. }
  397. const String* K=NULL;
  398. while((K=resources.next(K))) {
  399. Resource *r = resources[*K];
  400. if (!type_count.has(r->get_class())) {
  401. type_count[r->get_class()]=0;
  402. }
  403. type_count[r->get_class()]++;
  404. if (!p_short) {
  405. if (f)
  406. f->store_line(r->get_class()+": "+r->get_path());
  407. }
  408. }
  409. for(Map<String,int>::Element *E=type_count.front();E;E=E->next()) {
  410. if (f)
  411. f->store_line(E->key()+" count: "+itos(E->get()));
  412. }
  413. if (f) {
  414. f->close();
  415. memdelete(f);
  416. }
  417. lock->read_unlock();
  418. #endif
  419. }