Debian Codegraph @

core/io/resource_uid.h

raw symbols SCIP
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 #pragma once
32
33 #include "core/object/ref_counted.h"
34 #include "core/string/string_name.h"
35 #include "core/templates/hash_map.h"
36
37 class FileAccess;
38
39 typedef void (*ResourceUIDScanForUIDOnStartup)();
40
41 class ResourceUID : public Object {
42 GDCLASS(ResourceUID, Object)
43 public:
44 typedef int64_t ID;
45 constexpr const static ID INVALID_ID = -1;
46
47 static String get_cache_file();
48
49 private:
50 void *crypto = nullptr; // CryptoCore::RandomGenerator (avoid including crypto_core.h)
51 Mutex mutex;
52 struct Cache {
53 CharString cs;
54 bool saved_to_cache = false;
55 };
56
57 HashMap<ID, Cache> unique_ids; // Unique IDs and utf8 paths (less memory used).
58 bool use_reverse_cache = false;
59 HashMap<CharString, ID> reverse_cache; // Used at runtime.
60 static ResourceUID *singleton;
61
62 uint32_t cache_entries = 0;
63 bool changed = false;
64 bool cache_initialized = false;
65
66 protected:
67 static void _bind_methods();
68
69 public:
70 inline static ResourceUIDScanForUIDOnStartup scan_for_uid_on_startup = nullptr;
71
72 String id_to_text(ID p_id) const;
73 ID text_to_id(const String &p_text) const;
74
75 ID create_id();
76 ID create_id_for_path(const String &p_path);
77 bool has_id(ID p_id) const;
78 void add_id(ID p_id, const String &p_path);
79 void set_id(ID p_id, const String &p_path);
80 String get_id_path(ID p_id) const;
81 ID get_path_id(const String &p_path) const;
82 void remove_id(ID p_id);
83
84 static String uid_to_path(const String &p_uid);
85 static String path_to_uid(const String &p_path);
86 static String ensure_path(const String &p_uid_or_path);
87
88 Error load_from_cache(bool p_reset);
89 Error save_to_cache();
90 Error update_cache();
91 static String get_path_from_cache(Ref<FileAccess> &p_cache_file, const String &p_uid_string);
92
93 void enable_reverse_cache() { use_reverse_cache = true; }
94 void clear();
95
96 static ResourceUID *get_singleton() { return singleton; }
97
98 ResourceUID();
99 ~ResourceUID();
100 };