| 1 | /**************************************************************************/ |
| 2 | /* resource_uid.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #ifndef RESOURCE_UID_H |
| 32 | #define RESOURCE_UID_H |
| 33 | |
| 34 | #include "core/object/ref_counted.h" |
| 35 | #include "core/string/string_name.h" |
| 36 | #include "core/templates/hash_map.h" |
| 37 | |
| 38 | class ResourceUID : public Object { |
| 39 | GDCLASS(ResourceUID, Object) |
| 40 | public: |
| 41 | typedef int64_t ID; |
| 42 | enum { |
| 43 | INVALID_ID = -1 |
| 44 | }; |
| 45 | |
| 46 | static String get_cache_file(); |
| 47 | |
| 48 | private: |
| 49 | void *crypto = nullptr; // CryptoCore::RandomGenerator (avoid including crypto_core.h) |
| 50 | Mutex mutex; |
| 51 | struct Cache { |
| 52 | CharString cs; |
| 53 | bool saved_to_cache = false; |
| 54 | }; |
| 55 | |
| 56 | HashMap<ID, Cache> unique_ids; //unique IDs and utf8 paths (less memory used) |
| 57 | static ResourceUID *singleton; |
| 58 | |
| 59 | uint32_t cache_entries = 0; |
| 60 | bool changed = false; |
| 61 | |
| 62 | protected: |
| 63 | static void _bind_methods(); |
| 64 | |
| 65 | public: |
| 66 | String id_to_text(ID p_id) const; |
| 67 | ID text_to_id(const String &p_text) const; |
| 68 | |
| 69 | ID create_id(); |
| 70 | bool has_id(ID p_id) const; |
| 71 | void add_id(ID p_id, const String &p_path); |
| 72 | void set_id(ID p_id, const String &p_path); |
| 73 | String get_id_path(ID p_id) const; |
| 74 | void remove_id(ID p_id); |
| 75 | |
| 76 | Error load_from_cache(); |
| 77 | Error save_to_cache(); |
| 78 | Error update_cache(); |
| 79 | |
| 80 | void clear(); |
| 81 | |
| 82 | static ResourceUID *get_singleton() { return singleton; } |
| 83 | |
| 84 | ResourceUID(); |
| 85 | ~ResourceUID(); |
| 86 | }; |
| 87 | |
| 88 | #endif // RESOURCE_UID_H |
| 89 | |