| 
									
										
										
										
											2023-01-10 15:26:54 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  resource.h                                                            */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifndef RESOURCE_H
 | 
					
						
							|  |  |  | #define RESOURCE_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/class_db.h"
 | 
					
						
							|  |  |  | #include "core/object.h"
 | 
					
						
							|  |  |  | #include "core/ref_ptr.h"
 | 
					
						
							|  |  |  | #include "core/reference.h"
 | 
					
						
							|  |  |  | #include "core/safe_refcount.h"
 | 
					
						
							|  |  |  | #include "core/self_list.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #define RES_BASE_EXTENSION(m_ext)                                                                                   \
 | 
					
						
							|  |  |  | public:                                                                                                             \ | 
					
						
							|  |  |  | 	static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension(m_ext, get_class_static()); } \ | 
					
						
							|  |  |  | 	virtual String get_base_extension() const { return m_ext; }                                                     \ | 
					
						
							|  |  |  |                                                                                                                     \ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Resource : public Reference { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	GDCLASS(Resource, Reference); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	OBJ_CATEGORY("Resources"); | 
					
						
							|  |  |  | 	RES_BASE_EXTENSION("res"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Set<ObjectID> owners; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	friend class ResBase; | 
					
						
							|  |  |  | 	friend class ResourceCache; | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	String name; | 
					
						
							|  |  |  | 	String path_cache; | 
					
						
							| 
									
										
										
										
											2015-06-22 00:03:19 -03:00
										 |  |  | 	int subindex; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							|  |  |  | 	uint64_t last_modified_time; | 
					
						
							| 
									
										
										
										
											2017-02-01 09:45:45 -03:00
										 |  |  | 	uint64_t import_last_modified_time; | 
					
						
							|  |  |  | 	String import_path; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 01:04:31 -03:00
										 |  |  | 	bool local_to_scene; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	friend class SceneState; | 
					
						
							|  |  |  | 	Node *local_scene; | 
					
						
							| 
									
										
										
										
											2017-01-10 01:04:31 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:18 -03:00
										 |  |  | 	SelfList<Resource> remapped_list; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | protected: | 
					
						
							|  |  |  | 	void emit_changed(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void notify_change_to_owners(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	virtual void _resource_path_changed(); | 
					
						
							|  |  |  | 	static void _bind_methods(); | 
					
						
							| 
									
										
										
										
											2014-06-30 01:28:05 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void _set_path(const String &p_path); | 
					
						
							|  |  |  | 	void _take_over_path(const String &p_path); | 
					
						
							| 
									
										
										
										
											2019-07-16 09:46:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | public: | 
					
						
							|  |  |  | 	static Node *(*_get_local_scene_func)(); //used by editor
 | 
					
						
							| 
									
										
										
										
											2017-01-10 01:04:31 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 13:17:20 -03:00
										 |  |  | 	virtual bool editor_can_reload_from_file(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	virtual void reload_from_file(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void register_owner(Object *p_owner); | 
					
						
							|  |  |  | 	void unregister_owner(Object *p_owner); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void set_name(const String &p_name); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	String get_name() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	virtual void set_path(const String &p_path, bool p_take_over = false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	String get_path() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 00:03:19 -03:00
										 |  |  | 	void set_subindex(int p_sub_index); | 
					
						
							|  |  |  | 	int get_subindex() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 07:36:47 -03:00
										 |  |  | 	virtual Ref<Resource> duplicate(bool p_subresources = false) const; | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 	Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache); | 
					
						
							|  |  |  | 	void configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache); | 
					
						
							| 
									
										
										
										
											2017-01-10 01:04:31 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set_local_to_scene(bool p_enable); | 
					
						
							|  |  |  | 	bool is_local_to_scene() const; | 
					
						
							|  |  |  | 	virtual void setup_local_to_scene(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Node *get_local_scene() const; | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-27 14:18:40 -03:00
										 |  |  | 	uint32_t hash_edited_version() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	uint64_t get_last_modified_time() const { return last_modified_time; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; } | 
					
						
							| 
									
										
										
										
											2017-02-01 09:45:45 -03:00
										 |  |  | 	uint64_t get_import_last_modified_time() const { return import_last_modified_time; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	void set_import_path(const String &p_path) { import_path = p_path; } | 
					
						
							| 
									
										
										
										
											2017-02-01 09:45:45 -03:00
										 |  |  | 	String get_import_path() const { return import_path; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:18 -03:00
										 |  |  | 	void set_as_translation_remapped(bool p_remapped); | 
					
						
							|  |  |  | 	bool is_translation_remapped() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	virtual RID get_rid() const; // some resources may offer conversion to RID
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:35:23 -03:00
										 |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							|  |  |  | 	//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
 | 
					
						
							|  |  |  | 	void set_id_for_path(const String &p_path, int p_id); | 
					
						
							|  |  |  | 	int get_id_for_path(const String &p_path) const; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 	Resource(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	~Resource(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef Ref<Resource> RES; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceCache { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	friend class Resource; | 
					
						
							| 
									
										
										
										
											2017-06-28 17:00:18 -03:00
										 |  |  | 	friend class ResourceLoader; //need the lock
 | 
					
						
							| 
									
										
										
										
											2021-01-25 19:48:38 +01:00
										 |  |  | 	static RWLock lock; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	static HashMap<String, Resource *> resources; | 
					
						
							| 
									
										
										
										
											2019-07-16 09:46:40 -07:00
										 |  |  | #ifdef TOOLS_ENABLED
 | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 	static HashMap<String, HashMap<String, int>> resource_path_cache; // each tscn has a set of resource paths and IDs
 | 
					
						
							| 
									
										
										
										
											2021-01-25 19:48:38 +01:00
										 |  |  | 	static RWLock path_cache_lock; | 
					
						
							| 
									
										
										
										
											2019-07-16 09:46:40 -07:00
										 |  |  | #endif // TOOLS_ENABLED
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	friend void unregister_core_types(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	static void clear(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	friend void register_core_types(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	static void reload_externals(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	static bool has(const String &p_path); | 
					
						
							|  |  |  | 	static Resource *get(const String &p_path); | 
					
						
							| 
									
										
										
										
											2021-05-04 16:00:45 +02:00
										 |  |  | 	static void dump(const char *p_file = nullptr, bool p_short = false); | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 	static void get_cached_resources(List<Ref<Resource>> *p_resources); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	static int get_cached_resource_count(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-25 12:33:41 +02:00
										 |  |  | #endif // RESOURCE_H
 |