Merge pull request #90969 from Naros/expose-resource-and-saver-methods

Expose several resource/resource-saver functions
This commit is contained in:
Rémi Verschelde 2024-09-23 12:27:40 +02:00
commit 510bc3dc6a
No known key found for this signature in database
GPG Key ID: C3336907360768E1
6 changed files with 84 additions and 4 deletions

View File

@ -184,6 +184,10 @@ void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_form
::ResourceSaver::remove_resource_format_saver(p_format_saver); ::ResourceSaver::remove_resource_format_saver(p_format_saver);
} }
ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
return ::ResourceSaver::get_resource_id_for_path(p_path, p_generate);
}
ResourceSaver *ResourceSaver::singleton = nullptr; ResourceSaver *ResourceSaver::singleton = nullptr;
void ResourceSaver::_bind_methods() { void ResourceSaver::_bind_methods() {
@ -191,6 +195,7 @@ void ResourceSaver::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &ResourceSaver::get_recognized_extensions); ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &ResourceSaver::get_recognized_extensions);
ClassDB::bind_method(D_METHOD("add_resource_format_saver", "format_saver", "at_front"), &ResourceSaver::add_resource_format_saver, DEFVAL(false)); ClassDB::bind_method(D_METHOD("add_resource_format_saver", "format_saver", "at_front"), &ResourceSaver::add_resource_format_saver, DEFVAL(false));
ClassDB::bind_method(D_METHOD("remove_resource_format_saver", "format_saver"), &ResourceSaver::remove_resource_format_saver); ClassDB::bind_method(D_METHOD("remove_resource_format_saver", "format_saver"), &ResourceSaver::remove_resource_format_saver);
ClassDB::bind_method(D_METHOD("get_resource_id_for_path", "path", "generate"), &ResourceSaver::get_resource_id_for_path, DEFVAL(false));
BIND_BITFIELD_FLAG(FLAG_NONE); BIND_BITFIELD_FLAG(FLAG_NONE);
BIND_BITFIELD_FLAG(FLAG_RELATIVE_PATHS); BIND_BITFIELD_FLAG(FLAG_RELATIVE_PATHS);

View File

@ -116,6 +116,8 @@ public:
void add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front); void add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front);
void remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver); void remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver);
ResourceUID::ID get_resource_id_for_path(const String &p_path, bool p_generate = false);
ResourceSaver() { singleton = this; } ResourceSaver() { singleton = this; }
}; };

View File

@ -96,6 +96,7 @@ String Resource::get_path() const {
void Resource::set_path_cache(const String &p_path) { void Resource::set_path_cache(const String &p_path) {
path_cache = p_path; path_cache = p_path;
GDVIRTUAL_CALL(_set_path_cache, p_path);
} }
String Resource::generate_scene_unique_id() { String Resource::generate_scene_unique_id() {
@ -188,6 +189,7 @@ void Resource::disconnect_changed(const Callable &p_callable) {
} }
void Resource::reset_state() { void Resource::reset_state() {
GDVIRTUAL_CALL(_reset_state);
} }
Error Resource::copy_from(const Ref<Resource> &p_resource) { Error Resource::copy_from(const Ref<Resource> &p_resource) {
@ -495,9 +497,9 @@ void Resource::set_as_translation_remapped(bool p_remapped) {
} }
} }
#ifdef TOOLS_ENABLED
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
void Resource::set_id_for_path(const String &p_path, const String &p_id) { void Resource::set_id_for_path(const String &p_path, const String &p_id) {
#ifdef TOOLS_ENABLED
if (p_id.is_empty()) { if (p_id.is_empty()) {
ResourceCache::path_cache_lock.write_lock(); ResourceCache::path_cache_lock.write_lock();
ResourceCache::resource_path_cache[p_path].erase(get_path()); ResourceCache::resource_path_cache[p_path].erase(get_path());
@ -507,9 +509,11 @@ void Resource::set_id_for_path(const String &p_path, const String &p_id) {
ResourceCache::resource_path_cache[p_path][get_path()] = p_id; ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
ResourceCache::path_cache_lock.write_unlock(); ResourceCache::path_cache_lock.write_unlock();
} }
#endif
} }
String Resource::get_id_for_path(const String &p_path) const { String Resource::get_id_for_path(const String &p_path) const {
#ifdef TOOLS_ENABLED
ResourceCache::path_cache_lock.read_lock(); ResourceCache::path_cache_lock.read_lock();
if (ResourceCache::resource_path_cache[p_path].has(get_path())) { if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
String result = ResourceCache::resource_path_cache[p_path][get_path()]; String result = ResourceCache::resource_path_cache[p_path][get_path()];
@ -519,13 +523,16 @@ String Resource::get_id_for_path(const String &p_path) const {
ResourceCache::path_cache_lock.read_unlock(); ResourceCache::path_cache_lock.read_unlock();
return ""; return "";
} }
} #else
return "";
#endif #endif
}
void Resource::_bind_methods() { void Resource::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path); ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path); ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path); ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
ClassDB::bind_method(D_METHOD("set_path_cache", "path"), &Resource::set_path_cache);
ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name); ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name); ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid); ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
@ -533,6 +540,12 @@ void Resource::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene); ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene); ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene); ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
ClassDB::bind_method(D_METHOD("reset_state"), &Resource::reset_state);
ClassDB::bind_method(D_METHOD("set_id_for_path", "path", "id"), &Resource::set_id_for_path);
ClassDB::bind_method(D_METHOD("get_id_for_path", "path"), &Resource::get_id_for_path);
ClassDB::bind_method(D_METHOD("is_built_in"), &Resource::is_built_in);
ClassDB::bind_static_method("Resource", D_METHOD("generate_scene_unique_id"), &Resource::generate_scene_unique_id); ClassDB::bind_static_method("Resource", D_METHOD("generate_scene_unique_id"), &Resource::generate_scene_unique_id);
ClassDB::bind_method(D_METHOD("set_scene_unique_id", "id"), &Resource::set_scene_unique_id); ClassDB::bind_method(D_METHOD("set_scene_unique_id", "id"), &Resource::set_scene_unique_id);
@ -552,6 +565,8 @@ void Resource::_bind_methods() {
GDVIRTUAL_BIND(_setup_local_to_scene); GDVIRTUAL_BIND(_setup_local_to_scene);
GDVIRTUAL_BIND(_get_rid); GDVIRTUAL_BIND(_get_rid);
GDVIRTUAL_BIND(_reset_state);
GDVIRTUAL_BIND(_set_path_cache, "path");
} }
Resource::Resource() : Resource::Resource() :

View File

@ -89,6 +89,9 @@ protected:
GDVIRTUAL0RC(RID, _get_rid); GDVIRTUAL0RC(RID, _get_rid);
GDVIRTUAL1C(_set_path_cache, String);
GDVIRTUAL0(_reset_state);
public: public:
static Node *(*_get_local_scene_func)(); //used by editor static Node *(*_get_local_scene_func)(); //used by editor
static void (*_update_configuration_warning)(); //used by editor static void (*_update_configuration_warning)(); //used by editor
@ -144,11 +147,9 @@ public:
virtual RID get_rid() const; // some resources may offer conversion to RID virtual RID get_rid() const; // some resources may offer conversion to RID
#ifdef TOOLS_ENABLED
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored //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, const String &p_id); void set_id_for_path(const String &p_path, const String &p_id);
String get_id_for_path(const String &p_path) const; String get_id_for_path(const String &p_path) const;
#endif
Resource(); Resource();
~Resource(); ~Resource();

View File

@ -20,6 +20,19 @@
Override this method to return a custom [RID] when [method get_rid] is called. Override this method to return a custom [RID] when [method get_rid] is called.
</description> </description>
</method> </method>
<method name="_reset_state" qualifiers="virtual">
<return type="void" />
<description>
For resources that use a variable number of properties, either via [method Object._validate_property] or [method Object._get_property_list], this method should be implemented to correctly clear the resource's state.
</description>
</method>
<method name="_set_path_cache" qualifiers="virtual const">
<return type="void" />
<param index="0" name="path" type="String" />
<description>
Sets the resource's path to [param path] without involving the resource cache.
</description>
</method>
<method name="_setup_local_to_scene" qualifiers="virtual"> <method name="_setup_local_to_scene" qualifiers="virtual">
<return type="void" /> <return type="void" />
<description> <description>
@ -68,6 +81,14 @@
Generates a unique identifier for a resource to be contained inside a [PackedScene], based on the current date, time, and a random value. The returned string is only composed of letters ([code]a[/code] to [code]y[/code]) and numbers ([code]0[/code] to [code]8[/code]). See also [member resource_scene_unique_id]. Generates a unique identifier for a resource to be contained inside a [PackedScene], based on the current date, time, and a random value. The returned string is only composed of letters ([code]a[/code] to [code]y[/code]) and numbers ([code]0[/code] to [code]8[/code]). See also [member resource_scene_unique_id].
</description> </description>
</method> </method>
<method name="get_id_for_path" qualifiers="const">
<return type="String" />
<param index="0" name="path" type="String" />
<description>
Returns the unique identifier for the resource with the given [param path] from the resource cache. If the resource is not loaded and cached, an empty string is returned.
[b]Note:[/b] This method is only implemented when running in an editor context. At runtime, it returns an empty string.
</description>
</method>
<method name="get_local_scene" qualifiers="const"> <method name="get_local_scene" qualifiers="const">
<return type="Node" /> <return type="Node" />
<description> <description>
@ -80,6 +101,34 @@
Returns the [RID] of this resource (or an empty RID). Many resources (such as [Texture2D], [Mesh], and so on) are high-level abstractions of resources stored in a specialized server ([DisplayServer], [RenderingServer], etc.), so this function will return the original [RID]. Returns the [RID] of this resource (or an empty RID). Many resources (such as [Texture2D], [Mesh], and so on) are high-level abstractions of resources stored in a specialized server ([DisplayServer], [RenderingServer], etc.), so this function will return the original [RID].
</description> </description>
</method> </method>
<method name="is_built_in" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the resource is built-in (from the engine) or [code]false[/code] if it is user-defined.
</description>
</method>
<method name="reset_state">
<return type="void" />
<description>
For resources that use a variable number of properties, either via [method Object._validate_property] or [method Object._get_property_list], override [method _reset_state] to correctly clear the resource's state.
</description>
</method>
<method name="set_id_for_path">
<return type="void" />
<param index="0" name="path" type="String" />
<param index="1" name="id" type="String" />
<description>
Sets the unique identifier to [param id] for the resource with the given [param path] in the resource cache. If the unique identifier is empty, the cache entry using [param path] is removed if it exists.
[b]Note:[/b] This method is only implemented when running in an editor context.
</description>
</method>
<method name="set_path_cache">
<return type="void" />
<param index="0" name="path" type="String" />
<description>
Sets the resource's path to [param path] without involving the resource cache.
</description>
</method>
<method name="setup_local_to_scene" deprecated="This method should only be called internally."> <method name="setup_local_to_scene" deprecated="This method should only be called internally.">
<return type="void" /> <return type="void" />
<description> <description>

View File

@ -26,6 +26,14 @@
Returns the list of extensions available for saving a resource of a given type. Returns the list of extensions available for saving a resource of a given type.
</description> </description>
</method> </method>
<method name="get_resource_id_for_path">
<return type="int" />
<param index="0" name="path" type="String" />
<param index="1" name="generate" type="bool" default="false" />
<description>
Returns the resource ID for the given path. If [param generate] is [code]true[/code], a new resource ID will be generated if one for the path is not found. If [param generate] is [code]false[/code] and the path is not found, [constant ResourceUID.INVALID_ID] is returned.
</description>
</method>
<method name="remove_resource_format_saver"> <method name="remove_resource_format_saver">
<return type="void" /> <return type="void" />
<param index="0" name="format_saver" type="ResourceFormatSaver" /> <param index="0" name="format_saver" type="ResourceFormatSaver" />