resource.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*************************************************************************/
  2. /* resource.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "resource.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/os.h"
  37. #include "scene/main/node.h" //only so casting works
  38. #include <stdio.h>
  39. void Resource::emit_changed() {
  40. emit_signal(CoreStringNames::get_singleton()->changed);
  41. }
  42. void Resource::_resource_path_changed() {
  43. }
  44. void Resource::set_path(const String &p_path, bool p_take_over) {
  45. if (path_cache == p_path) {
  46. return;
  47. }
  48. if (!path_cache.is_empty()) {
  49. ResourceCache::lock.write_lock();
  50. ResourceCache::resources.erase(path_cache);
  51. ResourceCache::lock.write_unlock();
  52. }
  53. path_cache = "";
  54. ResourceCache::lock.read_lock();
  55. bool has_path = ResourceCache::resources.has(p_path);
  56. ResourceCache::lock.read_unlock();
  57. if (has_path) {
  58. if (p_take_over) {
  59. ResourceCache::lock.write_lock();
  60. Resource **res = ResourceCache::resources.getptr(p_path);
  61. if (res) {
  62. (*res)->set_name("");
  63. }
  64. ResourceCache::lock.write_unlock();
  65. } else {
  66. ResourceCache::lock.read_lock();
  67. bool exists = ResourceCache::resources.has(p_path);
  68. ResourceCache::lock.read_unlock();
  69. ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion).");
  70. }
  71. }
  72. path_cache = p_path;
  73. if (!path_cache.is_empty()) {
  74. ResourceCache::lock.write_lock();
  75. ResourceCache::resources[path_cache] = this;
  76. ResourceCache::lock.write_unlock();
  77. }
  78. _resource_path_changed();
  79. }
  80. String Resource::get_path() const {
  81. return path_cache;
  82. }
  83. String Resource::generate_scene_unique_id() {
  84. // Generate a unique enough hash, but still user-readable.
  85. // If it's not unique it does not matter because the saver will try again.
  86. OS::Date date = OS::get_singleton()->get_date();
  87. OS::Time time = OS::get_singleton()->get_time();
  88. uint32_t hash = hash_djb2_one_32(OS::get_singleton()->get_ticks_usec());
  89. hash = hash_djb2_one_32(date.year, hash);
  90. hash = hash_djb2_one_32(date.month, hash);
  91. hash = hash_djb2_one_32(date.day, hash);
  92. hash = hash_djb2_one_32(time.hour, hash);
  93. hash = hash_djb2_one_32(time.minute, hash);
  94. hash = hash_djb2_one_32(time.second, hash);
  95. hash = hash_djb2_one_32(Math::rand(), hash);
  96. static constexpr uint32_t characters = 5;
  97. static constexpr uint32_t char_count = ('z' - 'a');
  98. static constexpr uint32_t base = char_count + ('9' - '0');
  99. String id;
  100. for (uint32_t i = 0; i < characters; i++) {
  101. uint32_t c = hash % base;
  102. if (c < char_count) {
  103. id += String::chr('a' + c);
  104. } else {
  105. id += String::chr('0' + (c - char_count));
  106. }
  107. hash /= base;
  108. }
  109. return id;
  110. }
  111. void Resource::set_scene_unique_id(const String &p_id) {
  112. scene_unique_id = p_id;
  113. }
  114. String Resource::get_scene_unique_id() const {
  115. return scene_unique_id;
  116. }
  117. void Resource::set_name(const String &p_name) {
  118. name = p_name;
  119. emit_changed();
  120. }
  121. String Resource::get_name() const {
  122. return name;
  123. }
  124. void Resource::update_configuration_warning() {
  125. if (_update_configuration_warning) {
  126. _update_configuration_warning();
  127. }
  128. }
  129. bool Resource::editor_can_reload_from_file() {
  130. return true; //by default yes
  131. }
  132. void Resource::reset_state() {
  133. }
  134. Error Resource::copy_from(const Ref<Resource> &p_resource) {
  135. ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
  136. if (get_class() != p_resource->get_class()) {
  137. return ERR_INVALID_PARAMETER;
  138. }
  139. reset_state(); //may want to reset state
  140. List<PropertyInfo> pi;
  141. p_resource->get_property_list(&pi);
  142. for (const PropertyInfo &E : pi) {
  143. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  144. continue;
  145. }
  146. if (E.name == "resource_path") {
  147. continue; //do not change path
  148. }
  149. set(E.name, p_resource->get(E.name));
  150. }
  151. return OK;
  152. }
  153. void Resource::reload_from_file() {
  154. String path = get_path();
  155. if (!path.is_resource_file()) {
  156. return;
  157. }
  158. Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  159. if (!s.is_valid()) {
  160. return;
  161. }
  162. copy_from(s);
  163. }
  164. Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache) {
  165. List<PropertyInfo> plist;
  166. get_property_list(&plist);
  167. Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instantiate(get_class()));
  168. ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
  169. r->local_scene = p_for_scene;
  170. for (const PropertyInfo &E : plist) {
  171. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  172. continue;
  173. }
  174. Variant p = get(E.name);
  175. if (p.get_type() == Variant::OBJECT) {
  176. Ref<Resource> sr = p;
  177. if (sr.is_valid()) {
  178. if (sr->is_local_to_scene()) {
  179. if (remap_cache.has(sr)) {
  180. p = remap_cache[sr];
  181. } else {
  182. Ref<Resource> dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
  183. p = dupe;
  184. remap_cache[sr] = dupe;
  185. }
  186. }
  187. }
  188. }
  189. r->set(E.name, p);
  190. }
  191. return r;
  192. }
  193. void Resource::configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache) {
  194. List<PropertyInfo> plist;
  195. get_property_list(&plist);
  196. local_scene = p_for_scene;
  197. for (const PropertyInfo &E : plist) {
  198. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  199. continue;
  200. }
  201. Variant p = get(E.name);
  202. if (p.get_type() == Variant::OBJECT) {
  203. Ref<Resource> sr = p;
  204. if (sr.is_valid()) {
  205. if (sr->is_local_to_scene()) {
  206. if (!remap_cache.has(sr)) {
  207. sr->configure_for_local_scene(p_for_scene, remap_cache);
  208. remap_cache[sr] = sr;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. Ref<Resource> Resource::duplicate(bool p_subresources) const {
  216. List<PropertyInfo> plist;
  217. get_property_list(&plist);
  218. Ref<Resource> r = static_cast<Resource *>(ClassDB::instantiate(get_class()));
  219. ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
  220. for (const PropertyInfo &E : plist) {
  221. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  222. continue;
  223. }
  224. Variant p = get(E.name);
  225. if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
  226. r->set(E.name, p.duplicate(p_subresources));
  227. } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E.usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
  228. Ref<Resource> sr = p;
  229. if (sr.is_valid()) {
  230. r->set(E.name, sr->duplicate(p_subresources));
  231. }
  232. } else {
  233. r->set(E.name, p);
  234. }
  235. }
  236. return r;
  237. }
  238. void Resource::_set_path(const String &p_path) {
  239. set_path(p_path, false);
  240. }
  241. void Resource::_take_over_path(const String &p_path) {
  242. set_path(p_path, true);
  243. }
  244. RID Resource::get_rid() const {
  245. if (get_script_instance()) {
  246. Callable::CallError ce;
  247. RID ret = get_script_instance()->callp(SNAME("_get_rid"), nullptr, 0, ce);
  248. if (ce.error == Callable::CallError::CALL_OK && ret.is_valid()) {
  249. return ret;
  250. }
  251. }
  252. if (_get_extension() && _get_extension()->get_rid) {
  253. RID ret;
  254. ret.from_uint64(_get_extension()->get_rid(_get_extension_instance()));
  255. if (ret.is_valid()) {
  256. return ret;
  257. }
  258. }
  259. return RID();
  260. }
  261. void Resource::register_owner(Object *p_owner) {
  262. owners.insert(p_owner->get_instance_id());
  263. }
  264. void Resource::unregister_owner(Object *p_owner) {
  265. owners.erase(p_owner->get_instance_id());
  266. }
  267. void Resource::notify_change_to_owners() {
  268. for (RBSet<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
  269. Object *obj = ObjectDB::get_instance(E->get());
  270. ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
  271. //TODO store string
  272. obj->call("resource_changed", Ref<Resource>(this));
  273. }
  274. }
  275. #ifdef TOOLS_ENABLED
  276. uint32_t Resource::hash_edited_version() const {
  277. uint32_t hash = hash_djb2_one_32(get_edited_version());
  278. List<PropertyInfo> plist;
  279. get_property_list(&plist);
  280. for (const PropertyInfo &E : plist) {
  281. if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  282. Ref<Resource> res = get(E.name);
  283. if (res.is_valid()) {
  284. hash = hash_djb2_one_32(res->hash_edited_version(), hash);
  285. }
  286. }
  287. }
  288. return hash;
  289. }
  290. #endif
  291. void Resource::set_local_to_scene(bool p_enable) {
  292. local_to_scene = p_enable;
  293. }
  294. bool Resource::is_local_to_scene() const {
  295. return local_to_scene;
  296. }
  297. Node *Resource::get_local_scene() const {
  298. if (local_scene) {
  299. return local_scene;
  300. }
  301. if (_get_local_scene_func) {
  302. return _get_local_scene_func();
  303. }
  304. return nullptr;
  305. }
  306. void Resource::setup_local_to_scene() {
  307. // Can't use GDVIRTUAL in Resource, so this will have to be done with a signal
  308. emit_signal(SNAME("setup_local_to_scene_requested"));
  309. }
  310. Node *(*Resource::_get_local_scene_func)() = nullptr;
  311. void (*Resource::_update_configuration_warning)() = nullptr;
  312. void Resource::set_as_translation_remapped(bool p_remapped) {
  313. if (remapped_list.in_list() == p_remapped) {
  314. return;
  315. }
  316. ResourceCache::lock.write_lock();
  317. if (p_remapped) {
  318. ResourceLoader::remapped_list.add(&remapped_list);
  319. } else {
  320. ResourceLoader::remapped_list.remove(&remapped_list);
  321. }
  322. ResourceCache::lock.write_unlock();
  323. }
  324. bool Resource::is_translation_remapped() const {
  325. return remapped_list.in_list();
  326. }
  327. #ifdef TOOLS_ENABLED
  328. //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
  329. void Resource::set_id_for_path(const String &p_path, const String &p_id) {
  330. if (p_id.is_empty()) {
  331. ResourceCache::path_cache_lock.write_lock();
  332. ResourceCache::resource_path_cache[p_path].erase(get_path());
  333. ResourceCache::path_cache_lock.write_unlock();
  334. } else {
  335. ResourceCache::path_cache_lock.write_lock();
  336. ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
  337. ResourceCache::path_cache_lock.write_unlock();
  338. }
  339. }
  340. String Resource::get_id_for_path(const String &p_path) const {
  341. ResourceCache::path_cache_lock.read_lock();
  342. if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
  343. String result = ResourceCache::resource_path_cache[p_path][get_path()];
  344. ResourceCache::path_cache_lock.read_unlock();
  345. return result;
  346. } else {
  347. ResourceCache::path_cache_lock.read_unlock();
  348. return "";
  349. }
  350. }
  351. #endif
  352. void Resource::_bind_methods() {
  353. ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
  354. ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
  355. ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
  356. ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
  357. ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
  358. ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
  359. ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
  360. ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
  361. ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
  362. ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
  363. ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
  364. ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
  365. ADD_SIGNAL(MethodInfo("changed"));
  366. ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));
  367. ADD_GROUP("Resource", "resource_");
  368. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
  369. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
  370. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name");
  371. MethodInfo get_rid_bind("_get_rid");
  372. get_rid_bind.return_val.type = Variant::RID;
  373. ::ClassDB::add_virtual_method(get_class_static(), get_rid_bind, true, Vector<String>(), true);
  374. }
  375. Resource::Resource() :
  376. remapped_list(this) {}
  377. Resource::~Resource() {
  378. if (!path_cache.is_empty()) {
  379. ResourceCache::lock.write_lock();
  380. ResourceCache::resources.erase(path_cache);
  381. ResourceCache::lock.write_unlock();
  382. }
  383. if (owners.size()) {
  384. WARN_PRINT("Resource is still owned.");
  385. }
  386. }
  387. HashMap<String, Resource *> ResourceCache::resources;
  388. #ifdef TOOLS_ENABLED
  389. HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache;
  390. #endif
  391. RWLock ResourceCache::lock;
  392. #ifdef TOOLS_ENABLED
  393. RWLock ResourceCache::path_cache_lock;
  394. #endif
  395. void ResourceCache::clear() {
  396. if (resources.size()) {
  397. ERR_PRINT("Resources still in use at exit (run with --verbose for details).");
  398. if (OS::get_singleton()->is_stdout_verbose()) {
  399. for (const KeyValue<String, Resource *> &E : resources) {
  400. print_line(vformat("Resource still in use: %s (%s)", E.key, E.value->get_class()));
  401. }
  402. }
  403. }
  404. resources.clear();
  405. }
  406. void ResourceCache::reload_externals() {
  407. }
  408. bool ResourceCache::has(const String &p_path) {
  409. lock.read_lock();
  410. bool b = resources.has(p_path);
  411. lock.read_unlock();
  412. return b;
  413. }
  414. Resource *ResourceCache::get(const String &p_path) {
  415. lock.read_lock();
  416. Resource **res = resources.getptr(p_path);
  417. lock.read_unlock();
  418. if (!res) {
  419. return nullptr;
  420. }
  421. return *res;
  422. }
  423. void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
  424. lock.read_lock();
  425. for (KeyValue<String, Resource *> &E : resources) {
  426. p_resources->push_back(Ref<Resource>(E.value));
  427. }
  428. lock.read_unlock();
  429. }
  430. int ResourceCache::get_cached_resource_count() {
  431. lock.read_lock();
  432. int rc = resources.size();
  433. lock.read_unlock();
  434. return rc;
  435. }
  436. void ResourceCache::dump(const char *p_file, bool p_short) {
  437. #ifdef DEBUG_ENABLED
  438. lock.read_lock();
  439. HashMap<String, int> type_count;
  440. Ref<FileAccess> f;
  441. if (p_file) {
  442. f = FileAccess::open(String::utf8(p_file), FileAccess::WRITE);
  443. ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file at path '" + String::utf8(p_file) + "'.");
  444. }
  445. for (KeyValue<String, Resource *> &E : resources) {
  446. Resource *r = E.value;
  447. if (!type_count.has(r->get_class())) {
  448. type_count[r->get_class()] = 0;
  449. }
  450. type_count[r->get_class()]++;
  451. if (!p_short) {
  452. if (f.is_valid()) {
  453. f->store_line(r->get_class() + ": " + r->get_path());
  454. }
  455. }
  456. }
  457. for (const KeyValue<String, int> &E : type_count) {
  458. if (f.is_valid()) {
  459. f->store_line(E.key + " count: " + itos(E.value));
  460. }
  461. }
  462. lock.read_unlock();
  463. #else
  464. WARN_PRINT("ResourceCache::dump only with in debug builds.");
  465. #endif
  466. }