1/**************************************************************************/
2/* resource.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_H
32#define RESOURCE_H
33
34#include "core/io/resource_uid.h"
35#include "core/object/class_db.h"
36#include "core/object/ref_counted.h"
37#include "core/templates/safe_refcount.h"
38#include "core/templates/self_list.h"
39
40class Node;
41
42#define RES_BASE_EXTENSION(m_ext) \
43public: \
44 static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension(m_ext, get_class_static()); } \
45 virtual String get_base_extension() const override { return m_ext; } \
46 \
47private:
48
49class Resource : public RefCounted {
50 GDCLASS(Resource, RefCounted);
51
52public:
53 static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); }
54 virtual String get_base_extension() const { return "res"; }
55
56private:
57 friend class ResBase;
58 friend class ResourceCache;
59
60 String name;
61 String path_cache;
62 String scene_unique_id;
63
64#ifdef TOOLS_ENABLED
65 uint64_t last_modified_time = 0;
66 uint64_t import_last_modified_time = 0;
67 String import_path;
68#endif
69
70 bool local_to_scene = false;
71 friend class SceneState;
72 Node *local_scene = nullptr;
73
74 SelfList<Resource> remapped_list;
75
76protected:
77 virtual void _resource_path_changed();
78 static void _bind_methods();
79
80 void _set_path(const String &p_path);
81 void _take_over_path(const String &p_path);
82
83 virtual void reset_local_to_scene();
84
85public:
86 static Node *(*_get_local_scene_func)(); //used by editor
87 static void (*_update_configuration_warning)(); //used by editor
88
89 void update_configuration_warning();
90 virtual bool editor_can_reload_from_file();
91 virtual void reset_state(); //for resources that use variable amount of properties, either via _validate_property or _get_property_list, this function needs to be implemented to correctly clear state
92 virtual Error copy_from(const Ref<Resource> &p_resource);
93 virtual void reload_from_file();
94
95 void emit_changed();
96 void connect_changed(const Callable &p_callable, uint32_t p_flags = 0);
97 void disconnect_changed(const Callable &p_callable);
98
99 void set_name(const String &p_name);
100 String get_name() const;
101
102 virtual void set_path(const String &p_path, bool p_take_over = false);
103 String get_path() const;
104 _FORCE_INLINE_ bool is_built_in() const { return path_cache.is_empty() || path_cache.contains("::") || path_cache.begins_with("local://"); }
105
106 static String generate_scene_unique_id();
107 void set_scene_unique_id(const String &p_id);
108 String get_scene_unique_id() const;
109
110 virtual Ref<Resource> duplicate(bool p_subresources = false) const;
111 Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache);
112 void configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &remap_cache);
113
114 void set_local_to_scene(bool p_enable);
115 bool is_local_to_scene() const;
116 virtual void setup_local_to_scene();
117
118 Node *get_local_scene() const;
119
120#ifdef TOOLS_ENABLED
121
122 uint32_t hash_edited_version() const;
123
124 virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; }
125 uint64_t get_last_modified_time() const { return last_modified_time; }
126
127 virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; }
128 uint64_t get_import_last_modified_time() const { return import_last_modified_time; }
129
130 void set_import_path(const String &p_path) { import_path = p_path; }
131 String get_import_path() const { return import_path; }
132
133#endif
134
135 void set_as_translation_remapped(bool p_remapped);
136
137 virtual RID get_rid() const; // some resources may offer conversion to RID
138
139#ifdef TOOLS_ENABLED
140 //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
141 void set_id_for_path(const String &p_path, const String &p_id);
142 String get_id_for_path(const String &p_path) const;
143#endif
144
145 Resource();
146 ~Resource();
147};
148
149class ResourceCache {
150 friend class Resource;
151 friend class ResourceLoader; //need the lock
152 static Mutex lock;
153 static HashMap<String, Resource *> resources;
154#ifdef TOOLS_ENABLED
155 static HashMap<String, HashMap<String, String>> resource_path_cache; // Each tscn has a set of resource paths and IDs.
156 static RWLock path_cache_lock;
157#endif // TOOLS_ENABLED
158 friend void unregister_core_types();
159 static void clear();
160 friend void register_core_types();
161
162public:
163 static bool has(const String &p_path);
164 static Ref<Resource> get_ref(const String &p_path);
165 static void get_cached_resources(List<Ref<Resource>> *p_resources);
166 static int get_cached_resource_count();
167};
168
169#endif // RESOURCE_H
170