Merge pull request #71665 from bitsawer/expose_global_classes

Expose and document ProjectSettings.get_global_class_list()
This commit is contained in:
Yuri Sizov 2023-02-05 15:55:15 +03:00 committed by GitHub
commit e13e4b7b6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 6 deletions

View File

@ -40,6 +40,7 @@
#include "core/io/file_access_pack.h" #include "core/io/file_access_pack.h"
#include "core/io/marshalls.h" #include "core/io/marshalls.h"
#include "core/os/keyboard.h" #include "core/os/keyboard.h"
#include "core/variant/typed_array.h"
#include "core/variant/variant_parser.h" #include "core/variant/variant_parser.h"
#include "core/version.h" #include "core/version.h"
@ -1136,20 +1137,27 @@ Variant ProjectSettings::get_setting(const String &p_setting, const Variant &p_d
} }
} }
Array ProjectSettings::get_global_class_list() { TypedArray<Dictionary> ProjectSettings::get_global_class_list() {
Array script_classes; if (is_global_class_list_loaded) {
return global_class_list;
}
Ref<ConfigFile> cf; Ref<ConfigFile> cf;
cf.instantiate(); cf.instantiate();
if (cf->load(get_global_class_list_path()) == OK) { if (cf->load(get_global_class_list_path()) == OK) {
script_classes = cf->get_value("", "list", Array()); global_class_list = cf->get_value("", "list", Array());
} else { } else {
#ifndef TOOLS_ENABLED #ifndef TOOLS_ENABLED
// Script classes can't be recreated in exported project, so print an error. // Script classes can't be recreated in exported project, so print an error.
ERR_PRINT("Could not load global script cache."); ERR_PRINT("Could not load global script cache.");
#endif #endif
} }
return script_classes;
// File read succeeded or failed. If it failed, assume everything is still okay.
// We will later receive updated class data in store_global_class_list().
is_global_class_list_loaded = true;
return global_class_list;
} }
String ProjectSettings::get_global_class_list_path() const { String ProjectSettings::get_global_class_list_path() const {
@ -1161,6 +1169,8 @@ void ProjectSettings::store_global_class_list(const Array &p_classes) {
cf.instantiate(); cf.instantiate();
cf->set_value("", "list", p_classes); cf->set_value("", "list", p_classes);
cf->save(get_global_class_list_path()); cf->save(get_global_class_list_path());
global_class_list = p_classes;
} }
bool ProjectSettings::has_custom_feature(const String &p_feature) const { bool ProjectSettings::has_custom_feature(const String &p_feature) const {
@ -1195,6 +1205,7 @@ void ProjectSettings::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting); ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
ClassDB::bind_method(D_METHOD("get_setting", "name", "default_value"), &ProjectSettings::get_setting, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("get_setting", "name", "default_value"), &ProjectSettings::get_setting, DEFVAL(Variant()));
ClassDB::bind_method(D_METHOD("get_setting_with_override", "name"), &ProjectSettings::get_setting_with_override); ClassDB::bind_method(D_METHOD("get_setting_with_override", "name"), &ProjectSettings::get_setting_with_override);
ClassDB::bind_method(D_METHOD("get_global_class_list"), &ProjectSettings::get_global_class_list);
ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order); ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order); ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value); ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);

View File

@ -37,6 +37,9 @@
#include "core/templates/local_vector.h" #include "core/templates/local_vector.h"
#include "core/templates/rb_set.h" #include "core/templates/rb_set.h"
template <typename T>
class TypedArray;
class ProjectSettings : public Object { class ProjectSettings : public Object {
GDCLASS(ProjectSettings, Object); GDCLASS(ProjectSettings, Object);
_THREAD_SAFE_CLASS_ _THREAD_SAFE_CLASS_
@ -99,6 +102,9 @@ protected:
HashMap<StringName, AutoloadInfo> autoloads; HashMap<StringName, AutoloadInfo> autoloads;
Array global_class_list;
bool is_global_class_list_loaded = false;
String project_data_dir_name; String project_data_dir_name;
bool _set(const StringName &p_name, const Variant &p_value); bool _set(const StringName &p_name, const Variant &p_value);
@ -141,7 +147,7 @@ public:
void set_setting(const String &p_setting, const Variant &p_value); void set_setting(const String &p_setting, const Variant &p_value);
Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const; Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const;
Array get_global_class_list(); TypedArray<Dictionary> get_global_class_list();
void store_global_class_list(const Array &p_classes); void store_global_class_list(const Array &p_classes);
String get_global_class_list_path() const; String get_global_class_list_path() const;

View File

@ -60,6 +60,18 @@
Clears the whole configuration (not recommended, may break things). Clears the whole configuration (not recommended, may break things).
</description> </description>
</method> </method>
<method name="get_global_class_list">
<return type="Dictionary[]" />
<description>
Returns an [Array] of registered global classes. Each global class is represented as a [Dictionary] that contains the following entries:
- [code]base[/code] is a name of the base class;
- [code]class[/code] is a name of the registered global class;
- [code]icon[/code] is a path to a custom icon of the global class, if it has any;
- [code]language[/code] is a name of a programming language in which the global class is written;
- [code]path[/code] is a path to a file containing the global class.
[b]Note:[/b] Both the script and the icon paths are local to the project filesystem, i.e. they start with [code]res://[/code].
</description>
</method>
<method name="get_order" qualifiers="const"> <method name="get_order" qualifiers="const">
<return type="int" /> <return type="int" />
<param index="0" name="name" type="String" /> <param index="0" name="name" type="String" />

View File

@ -43,7 +43,7 @@
# To get the name of the underlying Object type, you need the `get_class()` method. # To get the name of the underlying Object type, you need the `get_class()` method.
print("foo is a(n) %s" % foo.get_class()) # inject the class name into a formatted string. print("foo is a(n) %s" % foo.get_class()) # inject the class name into a formatted string.
# Note also that there is not yet any way to get a script's `class_name` string easily. # Note also that there is not yet any way to get a script's `class_name` string easily.
# To fetch that value, you can parse the [code]res://.godot/global_script_class_cache.cfg[/code] file with the [ConfigFile] API. # To fetch that value, you can use [member ProjectSettings.get_global_class_list].
# Open your project.godot file to see it up close. # Open your project.godot file to see it up close.
[/gdscript] [/gdscript]
[csharp] [csharp]