2014-02-10 09:10:30 +08: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 07:50:27 +08:00
2014-02-10 09:10:30 +08:00
# ifndef RESOURCE_H
# define RESOURCE_H
2021-07-24 03:01:18 +08:00
# include "core/io/resource_uid.h"
2020-11-08 06:33:38 +08:00
# include "core/object/class_db.h"
2022-10-08 20:47:58 +08:00
# include "core/object/gdvirtual.gen.inc"
2021-06-05 00:03:15 +08:00
# include "core/object/ref_counted.h"
2020-11-08 06:33:38 +08:00
# include "core/templates/safe_refcount.h"
# include "core/templates/self_list.h"
2014-02-10 09:10:30 +08:00
2022-01-25 23:37:41 +08:00
class Node ;
2014-02-10 09:10:30 +08:00
# define RES_BASE_EXTENSION(m_ext) \
public : \
2017-01-03 10:03:46 +08:00
static void register_custom_data_to_otdb ( ) { ClassDB : : add_resource_base_extension ( m_ext , get_class_static ( ) ) ; } \
2020-07-10 18:34:39 +08:00
virtual String get_base_extension ( ) const override { return m_ext ; } \
2017-03-05 23:44:50 +08:00
\
2014-02-10 09:10:30 +08:00
private :
2021-06-05 00:03:15 +08:00
class Resource : public RefCounted {
GDCLASS ( Resource , RefCounted ) ;
2014-02-10 09:10:30 +08:00
2020-07-10 18:34:39 +08:00
public :
static void register_custom_data_to_otdb ( ) { ClassDB : : add_resource_base_extension ( " res " , get_class_static ( ) ) ; }
virtual String get_base_extension ( ) const { return " res " ; }
private :
2014-02-10 09:10:30 +08:00
friend class ResBase ;
2016-03-09 07:00:52 +08:00
friend class ResourceCache ;
2014-02-10 09:10:30 +08:00
String name ;
String path_cache ;
2021-07-21 08:36:56 +08:00
String scene_unique_id ;
2014-02-10 09:10:30 +08:00
# ifdef TOOLS_ENABLED
2020-05-12 23:01:17 +08:00
uint64_t last_modified_time = 0 ;
uint64_t import_last_modified_time = 0 ;
2017-02-01 20:45:45 +08:00
String import_path ;
2014-02-10 09:10:30 +08:00
# endif
2020-05-12 23:01:17 +08:00
bool local_to_scene = false ;
2017-01-10 12:04:31 +08:00
friend class SceneState ;
2020-05-12 23:01:17 +08:00
Node * local_scene = nullptr ;
2017-01-10 12:04:31 +08:00
2017-06-29 04:00:18 +08:00
SelfList < Resource > remapped_list ;
2014-02-10 09:10:30 +08:00
protected :
virtual void _resource_path_changed ( ) ;
static void _bind_methods ( ) ;
2014-06-30 12:28:05 +08:00
void _set_path ( const String & p_path ) ;
void _take_over_path ( const String & p_path ) ;
2019-07-17 00:46:40 +08:00
2023-07-08 19:38:27 +08:00
virtual void reset_local_to_scene ( ) ;
2022-10-08 20:47:58 +08:00
GDVIRTUAL0 ( _setup_local_to_scene ) ;
2023-07-08 19:38:27 +08:00
2017-03-05 23:44:50 +08:00
public :
2017-01-10 12:04:31 +08:00
static Node * ( * _get_local_scene_func ) ( ) ; //used by editor
2021-04-27 23:43:49 +08:00
static void ( * _update_configuration_warning ) ( ) ; //used by editor
2017-01-10 12:04:31 +08:00
2021-04-27 23:43:49 +08:00
void update_configuration_warning ( ) ;
2016-06-28 00:17:20 +08:00
virtual bool editor_can_reload_from_file ( ) ;
2021-02-12 01:18:45 +08:00
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
virtual Error copy_from ( const Ref < Resource > & p_resource ) ;
2014-02-10 09:10:30 +08:00
virtual void reload_from_file ( ) ;
2023-07-04 03:29:37 +08:00
void emit_changed ( ) ;
void connect_changed ( const Callable & p_callable , uint32_t p_flags = 0 ) ;
void disconnect_changed ( const Callable & p_callable ) ;
2014-02-10 09:10:30 +08:00
void set_name ( const String & p_name ) ;
String get_name ( ) const ;
2015-10-10 20:09:09 +08:00
virtual void set_path ( const String & p_path , bool p_take_over = false ) ;
2014-02-10 09:10:30 +08:00
String get_path ( ) const ;
2023-08-05 08:07:16 +08:00
void set_path_cache ( const String & p_path ) ; // Set raw path without involving resource cache.
2022-02-04 00:03:38 +08:00
_FORCE_INLINE_ bool is_built_in ( ) const { return path_cache . is_empty ( ) | | path_cache . contains ( " :: " ) | | path_cache . begins_with ( " local:// " ) ; }
2014-02-10 09:10:30 +08:00
2021-07-21 08:36:56 +08:00
static String generate_scene_unique_id ( ) ;
void set_scene_unique_id ( const String & p_id ) ;
String get_scene_unique_id ( ) const ;
2015-06-22 11:03:19 +08:00
2017-05-17 18:36:47 +08:00
virtual Ref < Resource > duplicate ( bool p_subresources = false ) const ;
2022-05-13 21:04:37 +08:00
Ref < Resource > duplicate_for_local_scene ( Node * p_for_scene , HashMap < Ref < Resource > , Ref < Resource > > & remap_cache ) ;
void configure_for_local_scene ( Node * p_for_scene , HashMap < Ref < Resource > , Ref < Resource > > & remap_cache ) ;
2017-01-10 12:04:31 +08:00
void set_local_to_scene ( bool p_enable ) ;
bool is_local_to_scene ( ) const ;
virtual void setup_local_to_scene ( ) ;
2014-02-10 09:10:30 +08:00
2017-01-10 12:04:31 +08:00
Node * get_local_scene ( ) const ;
2016-05-28 01:18:40 +08:00
2014-02-10 09:10:30 +08:00
# ifdef TOOLS_ENABLED
2016-05-28 01:18:40 +08:00
uint32_t hash_edited_version ( ) const ;
2015-12-14 07:39:01 +08:00
virtual void set_last_modified_time ( uint64_t p_time ) { last_modified_time = p_time ; }
2014-02-10 09:10:30 +08:00
uint64_t get_last_modified_time ( ) const { return last_modified_time ; }
2017-02-01 20:45:45 +08:00
virtual void set_import_last_modified_time ( uint64_t p_time ) { import_last_modified_time = p_time ; }
uint64_t get_import_last_modified_time ( ) const { return import_last_modified_time ; }
void set_import_path ( const String & p_path ) { import_path = p_path ; }
String get_import_path ( ) const { return import_path ; }
2014-02-10 09:10:30 +08:00
# endif
2017-06-29 04:00:18 +08:00
void set_as_translation_remapped ( bool p_remapped ) ;
2014-02-10 09:10:30 +08:00
virtual RID get_rid ( ) const ; // some resources may offer conversion to RID
2019-04-12 01:35:23 +08:00
# ifdef TOOLS_ENABLED
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
2021-07-21 08:36:56 +08:00
void set_id_for_path ( const String & p_path , const String & p_id ) ;
String get_id_for_path ( const String & p_path ) const ;
2019-04-12 01:35:23 +08:00
# endif
2016-03-09 07:00:52 +08:00
Resource ( ) ;
2014-02-10 09:10:30 +08:00
~ Resource ( ) ;
} ;
class ResourceCache {
2016-03-09 07:00:52 +08:00
friend class Resource ;
2017-06-29 04:00:18 +08:00
friend class ResourceLoader ; //need the lock
2022-06-22 19:46:46 +08:00
static Mutex lock ;
2016-03-09 07:00:52 +08:00
static HashMap < String , Resource * > resources ;
2019-07-17 00:46:40 +08:00
# ifdef TOOLS_ENABLED
2021-07-21 08:36:56 +08:00
static HashMap < String , HashMap < String , String > > resource_path_cache ; // Each tscn has a set of resource paths and IDs.
2021-01-18 21:01:38 +08:00
static RWLock path_cache_lock ;
2019-07-17 00:46:40 +08:00
# endif // TOOLS_ENABLED
2014-02-10 09:10:30 +08:00
friend void unregister_core_types ( ) ;
static void clear ( ) ;
2017-01-08 05:25:37 +08:00
friend void register_core_types ( ) ;
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
public :
2014-02-10 09:10:30 +08:00
static bool has ( const String & p_path ) ;
2022-06-22 19:46:46 +08:00
static Ref < Resource > get_ref ( const String & p_path ) ;
2020-03-17 14:33:00 +08:00
static void get_cached_resources ( List < Ref < Resource > > * p_resources ) ;
2014-02-10 09:10:30 +08:00
static int get_cached_resource_count ( ) ;
} ;
2020-03-25 18:10:34 +08:00
# endif // RESOURCE_H