resource.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 (p_path.is_empty()) {
  49. p_take_over = false; // Can't take over an empty path
  50. }
  51. ResourceCache::lock.lock();
  52. if (!path_cache.is_empty()) {
  53. ResourceCache::resources.erase(path_cache);
  54. }
  55. path_cache = "";
  56. Ref<Resource> existing = ResourceCache::get_ref(p_path);
  57. if (existing.is_valid()) {
  58. if (p_take_over) {
  59. existing->path_cache = String();
  60. ResourceCache::resources.erase(p_path);
  61. } else {
  62. ResourceCache::lock.unlock();
  63. ERR_FAIL_MSG("Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion).");
  64. }
  65. }
  66. path_cache = p_path;
  67. if (!path_cache.is_empty()) {
  68. ResourceCache::resources[path_cache] = this;
  69. }
  70. ResourceCache::lock.unlock();
  71. _resource_path_changed();
  72. }
  73. String Resource::get_path() const {
  74. return path_cache;
  75. }
  76. String Resource::generate_scene_unique_id() {
  77. // Generate a unique enough hash, but still user-readable.
  78. // If it's not unique it does not matter because the saver will try again.
  79. OS::Date date = OS::get_singleton()->get_date();
  80. OS::Time time = OS::get_singleton()->get_time();
  81. uint32_t hash = hash_murmur3_one_32(OS::get_singleton()->get_ticks_usec());
  82. hash = hash_murmur3_one_32(date.year, hash);
  83. hash = hash_murmur3_one_32(date.month, hash);
  84. hash = hash_murmur3_one_32(date.day, hash);
  85. hash = hash_murmur3_one_32(time.hour, hash);
  86. hash = hash_murmur3_one_32(time.minute, hash);
  87. hash = hash_murmur3_one_32(time.second, hash);
  88. hash = hash_murmur3_one_32(Math::rand(), hash);
  89. static constexpr uint32_t characters = 5;
  90. static constexpr uint32_t char_count = ('z' - 'a');
  91. static constexpr uint32_t base = char_count + ('9' - '0');
  92. String id;
  93. for (uint32_t i = 0; i < characters; i++) {
  94. uint32_t c = hash % base;
  95. if (c < char_count) {
  96. id += String::chr('a' + c);
  97. } else {
  98. id += String::chr('0' + (c - char_count));
  99. }
  100. hash /= base;
  101. }
  102. return id;
  103. }
  104. void Resource::set_scene_unique_id(const String &p_id) {
  105. scene_unique_id = p_id;
  106. }
  107. String Resource::get_scene_unique_id() const {
  108. return scene_unique_id;
  109. }
  110. void Resource::set_name(const String &p_name) {
  111. name = p_name;
  112. emit_changed();
  113. }
  114. String Resource::get_name() const {
  115. return name;
  116. }
  117. void Resource::update_configuration_warning() {
  118. if (_update_configuration_warning) {
  119. _update_configuration_warning();
  120. }
  121. }
  122. bool Resource::editor_can_reload_from_file() {
  123. return true; //by default yes
  124. }
  125. void Resource::reset_state() {
  126. }
  127. Error Resource::copy_from(const Ref<Resource> &p_resource) {
  128. ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
  129. if (get_class() != p_resource->get_class()) {
  130. return ERR_INVALID_PARAMETER;
  131. }
  132. reset_state(); //may want to reset state
  133. List<PropertyInfo> pi;
  134. p_resource->get_property_list(&pi);
  135. for (const PropertyInfo &E : pi) {
  136. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  137. continue;
  138. }
  139. if (E.name == "resource_path") {
  140. continue; //do not change path
  141. }
  142. set(E.name, p_resource->get(E.name));
  143. }
  144. return OK;
  145. }
  146. void Resource::reload_from_file() {
  147. String path = get_path();
  148. if (!path.is_resource_file()) {
  149. return;
  150. }
  151. Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  152. if (!s.is_valid()) {
  153. return;
  154. }
  155. copy_from(s);
  156. }
  157. Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache) {
  158. List<PropertyInfo> plist;
  159. get_property_list(&plist);
  160. Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instantiate(get_class()));
  161. ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
  162. r->local_scene = p_for_scene;
  163. for (const PropertyInfo &E : plist) {
  164. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  165. continue;
  166. }
  167. Variant p = get(E.name);
  168. if (p.get_type() == Variant::OBJECT) {
  169. Ref<Resource> sr = p;
  170. if (sr.is_valid()) {
  171. if (sr->is_local_to_scene()) {
  172. if (remap_cache.has(sr)) {
  173. p = remap_cache[sr];
  174. } else {
  175. Ref<Resource> dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
  176. p = dupe;
  177. remap_cache[sr] = dupe;
  178. }
  179. }
  180. }
  181. }
  182. r->set(E.name, p);
  183. }
  184. return r;
  185. }
  186. void Resource::configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache) {
  187. List<PropertyInfo> plist;
  188. get_property_list(&plist);
  189. local_scene = p_for_scene;
  190. for (const PropertyInfo &E : plist) {
  191. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  192. continue;
  193. }
  194. Variant p = get(E.name);
  195. if (p.get_type() == Variant::OBJECT) {
  196. Ref<Resource> sr = p;
  197. if (sr.is_valid()) {
  198. if (sr->is_local_to_scene()) {
  199. if (!remap_cache.has(sr)) {
  200. sr->configure_for_local_scene(p_for_scene, remap_cache);
  201. remap_cache[sr] = sr;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. Ref<Resource> Resource::duplicate(bool p_subresources) const {
  209. List<PropertyInfo> plist;
  210. get_property_list(&plist);
  211. Ref<Resource> r = static_cast<Resource *>(ClassDB::instantiate(get_class()));
  212. ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
  213. for (const PropertyInfo &E : plist) {
  214. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  215. continue;
  216. }
  217. Variant p = get(E.name);
  218. if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
  219. r->set(E.name, p.duplicate(p_subresources));
  220. } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E.usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
  221. Ref<Resource> sr = p;
  222. if (sr.is_valid()) {
  223. r->set(E.name, sr->duplicate(p_subresources));
  224. }
  225. } else {
  226. r->set(E.name, p);
  227. }
  228. }
  229. return r;
  230. }
  231. void Resource::_set_path(const String &p_path) {
  232. set_path(p_path, false);
  233. }
  234. void Resource::_take_over_path(const String &p_path) {
  235. set_path(p_path, true);
  236. }
  237. RID Resource::get_rid() const {
  238. if (get_script_instance()) {
  239. Callable::CallError ce;
  240. RID ret = get_script_instance()->callp(SNAME("_get_rid"), nullptr, 0, ce);
  241. if (ce.error == Callable::CallError::CALL_OK && ret.is_valid()) {
  242. return ret;
  243. }
  244. }
  245. if (_get_extension() && _get_extension()->get_rid) {
  246. RID ret;
  247. ret.from_uint64(_get_extension()->get_rid(_get_extension_instance()));
  248. if (ret.is_valid()) {
  249. return ret;
  250. }
  251. }
  252. return RID();
  253. }
  254. void Resource::register_owner(Object *p_owner) {
  255. owners.insert(p_owner->get_instance_id());
  256. }
  257. void Resource::unregister_owner(Object *p_owner) {
  258. owners.erase(p_owner->get_instance_id());
  259. }
  260. void Resource::notify_change_to_owners() {
  261. for (const ObjectID &E : owners) {
  262. Object *obj = ObjectDB::get_instance(E);
  263. ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
  264. //TODO store string
  265. obj->call("resource_changed", Ref<Resource>(this));
  266. }
  267. }
  268. #ifdef TOOLS_ENABLED
  269. uint32_t Resource::hash_edited_version() const {
  270. uint32_t hash = hash_murmur3_one_32(get_edited_version());
  271. List<PropertyInfo> plist;
  272. get_property_list(&plist);
  273. for (const PropertyInfo &E : plist) {
  274. if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  275. Ref<Resource> res = get(E.name);
  276. if (res.is_valid()) {
  277. hash = hash_murmur3_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. }
  294. if (_get_local_scene_func) {
  295. return _get_local_scene_func();
  296. }
  297. return nullptr;
  298. }
  299. void Resource::setup_local_to_scene() {
  300. // Can't use GDVIRTUAL in Resource, so this will have to be done with a signal
  301. emit_signal(SNAME("setup_local_to_scene_requested"));
  302. }
  303. Node *(*Resource::_get_local_scene_func)() = nullptr;
  304. void (*Resource::_update_configuration_warning)() = nullptr;
  305. void Resource::set_as_translation_remapped(bool p_remapped) {
  306. if (remapped_list.in_list() == p_remapped) {
  307. return;
  308. }
  309. ResourceCache::lock.lock();
  310. if (p_remapped) {
  311. ResourceLoader::remapped_list.add(&remapped_list);
  312. } else {
  313. ResourceLoader::remapped_list.remove(&remapped_list);
  314. }
  315. ResourceCache::lock.unlock();
  316. }
  317. bool Resource::is_translation_remapped() const {
  318. return remapped_list.in_list();
  319. }
  320. #ifdef TOOLS_ENABLED
  321. //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
  322. void Resource::set_id_for_path(const String &p_path, const String &p_id) {
  323. if (p_id.is_empty()) {
  324. ResourceCache::path_cache_lock.write_lock();
  325. ResourceCache::resource_path_cache[p_path].erase(get_path());
  326. ResourceCache::path_cache_lock.write_unlock();
  327. } else {
  328. ResourceCache::path_cache_lock.write_lock();
  329. ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
  330. ResourceCache::path_cache_lock.write_unlock();
  331. }
  332. }
  333. String Resource::get_id_for_path(const String &p_path) const {
  334. ResourceCache::path_cache_lock.read_lock();
  335. if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
  336. String result = ResourceCache::resource_path_cache[p_path][get_path()];
  337. ResourceCache::path_cache_lock.read_unlock();
  338. return result;
  339. } else {
  340. ResourceCache::path_cache_lock.read_unlock();
  341. return "";
  342. }
  343. }
  344. #endif
  345. void Resource::_bind_methods() {
  346. ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
  347. ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
  348. ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
  349. ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
  350. ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
  351. ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
  352. ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
  353. ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
  354. ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
  355. ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
  356. ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
  357. ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
  358. ADD_SIGNAL(MethodInfo("changed"));
  359. ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));
  360. ADD_GROUP("Resource", "resource_");
  361. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
  362. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
  363. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name");
  364. MethodInfo get_rid_bind("_get_rid");
  365. get_rid_bind.return_val.type = Variant::RID;
  366. ::ClassDB::add_virtual_method(get_class_static(), get_rid_bind, true, Vector<String>(), true);
  367. }
  368. Resource::Resource() :
  369. remapped_list(this) {}
  370. Resource::~Resource() {
  371. if (!path_cache.is_empty()) {
  372. ResourceCache::lock.lock();
  373. ResourceCache::resources.erase(path_cache);
  374. ResourceCache::lock.unlock();
  375. }
  376. if (owners.size()) {
  377. WARN_PRINT("Resource is still owned.");
  378. }
  379. }
  380. HashMap<String, Resource *> ResourceCache::resources;
  381. #ifdef TOOLS_ENABLED
  382. HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache;
  383. #endif
  384. Mutex ResourceCache::lock;
  385. #ifdef TOOLS_ENABLED
  386. RWLock ResourceCache::path_cache_lock;
  387. #endif
  388. void ResourceCache::clear() {
  389. if (resources.size()) {
  390. ERR_PRINT("Resources still in use at exit (run with --verbose for details).");
  391. if (OS::get_singleton()->is_stdout_verbose()) {
  392. for (const KeyValue<String, Resource *> &E : resources) {
  393. print_line(vformat("Resource still in use: %s (%s)", E.key, E.value->get_class()));
  394. }
  395. }
  396. }
  397. resources.clear();
  398. }
  399. void ResourceCache::reload_externals() {
  400. }
  401. bool ResourceCache::has(const String &p_path) {
  402. lock.lock();
  403. Resource **res = resources.getptr(p_path);
  404. if (res && (*res)->reference_get_count() == 0) {
  405. // This resource is in the process of being deleted, ignore its existence.
  406. (*res)->path_cache = String();
  407. resources.erase(p_path);
  408. res = nullptr;
  409. }
  410. lock.unlock();
  411. if (!res) {
  412. return false;
  413. }
  414. return true;
  415. }
  416. Ref<Resource> ResourceCache::get_ref(const String &p_path) {
  417. Ref<Resource> ref;
  418. lock.lock();
  419. Resource **res = resources.getptr(p_path);
  420. if (res) {
  421. ref = Ref<Resource>(*res);
  422. }
  423. if (res && !ref.is_valid()) {
  424. // This resource is in the process of being deleted, ignore its existence
  425. (*res)->path_cache = String();
  426. resources.erase(p_path);
  427. res = nullptr;
  428. }
  429. lock.unlock();
  430. return ref;
  431. }
  432. void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
  433. lock.lock();
  434. for (KeyValue<String, Resource *> &E : resources) {
  435. p_resources->push_back(Ref<Resource>(E.value));
  436. }
  437. lock.unlock();
  438. }
  439. int ResourceCache::get_cached_resource_count() {
  440. lock.lock();
  441. int rc = resources.size();
  442. lock.unlock();
  443. return rc;
  444. }