summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Leblond <emmanuel.leblond@gmail.com>2022-10-09 22:03:59 +0200
committerRĂ©mi Verschelde <rverschelde@gmail.com>2023-02-10 19:50:24 +0100
commit58f4139164552793bfb16065e31388cdd2481492 (patch)
treed65f62581e0aa8fa7bf5187adae1f35aec90bc54
parented85a2c8ceae048c2b29bc6ccf6f53a9c70ec811 (diff)
Add Engine::unregister_script_language and make Engine::register_script_language returns a Error
-rw-r--r--core/core_bind.cpp9
-rw-r--r--core/core_bind.h3
-rw-r--r--core/object/script_language.cpp18
-rw-r--r--core/object/script_language.h4
-rw-r--r--doc/classes/Engine.xml16
5 files changed, 39 insertions, 11 deletions
diff --git a/core/core_bind.cpp b/core/core_bind.cpp
index c752bdd057..f2eb7823e2 100644
--- a/core/core_bind.cpp
+++ b/core/core_bind.cpp
@@ -1590,8 +1590,12 @@ Vector<String> Engine::get_singleton_list() const {
return ret;
}
-void Engine::register_script_language(ScriptLanguage *p_language) {
- ScriptServer::register_language(p_language);
+Error Engine::register_script_language(ScriptLanguage *p_language) {
+ return ScriptServer::register_language(p_language);
+}
+
+Error Engine::unregister_script_language(const ScriptLanguage *p_language) {
+ return ScriptServer::unregister_language(p_language);
}
int Engine::get_script_language_count() {
@@ -1662,6 +1666,7 @@ void Engine::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list);
ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language);
+ ClassDB::bind_method(D_METHOD("unregister_script_language", "language"), &Engine::unregister_script_language);
ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count);
ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language);
diff --git a/core/core_bind.h b/core/core_bind.h
index 8852463234..675da48591 100644
--- a/core/core_bind.h
+++ b/core/core_bind.h
@@ -499,7 +499,8 @@ public:
void unregister_singleton(const StringName &p_name);
Vector<String> get_singleton_list() const;
- void register_script_language(ScriptLanguage *p_language);
+ Error register_script_language(ScriptLanguage *p_language);
+ Error unregister_script_language(const ScriptLanguage *p_language);
int get_script_language_count();
ScriptLanguage *get_script_language(int p_index) const;
diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp
index 1d53cf66d4..71f40660f4 100644
--- a/core/object/script_language.cpp
+++ b/core/object/script_language.cpp
@@ -165,22 +165,30 @@ ScriptLanguage *ScriptServer::get_language(int p_idx) {
return _languages[p_idx];
}
-void ScriptServer::register_language(ScriptLanguage *p_language) {
- ERR_FAIL_NULL(p_language);
- ERR_FAIL_COND(_language_count >= MAX_LANGUAGES);
+Error ScriptServer::register_language(ScriptLanguage *p_language) {
+ ERR_FAIL_NULL_V(p_language, ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V_MSG(_language_count >= MAX_LANGUAGES, ERR_UNAVAILABLE, "Script languages limit has been reach, cannot register more.");
+ for (int i = 0; i < _language_count; i++) {
+ const ScriptLanguage *other_language = _languages[i];
+ ERR_FAIL_COND_V_MSG(other_language->get_extension() == p_language->get_extension(), ERR_ALREADY_EXISTS, "A script language with extension '" + p_language->get_extension() + "' is already registered.");
+ ERR_FAIL_COND_V_MSG(other_language->get_name() == p_language->get_name(), ERR_ALREADY_EXISTS, "A script language with name '" + p_language->get_name() + "' is already registered.");
+ ERR_FAIL_COND_V_MSG(other_language->get_type() == p_language->get_type(), ERR_ALREADY_EXISTS, "A script language with type '" + p_language->get_type() + "' is already registered.");
+ }
_languages[_language_count++] = p_language;
+ return OK;
}
-void ScriptServer::unregister_language(const ScriptLanguage *p_language) {
+Error ScriptServer::unregister_language(const ScriptLanguage *p_language) {
for (int i = 0; i < _language_count; i++) {
if (_languages[i] == p_language) {
_language_count--;
if (i < _language_count) {
SWAP(_languages[i], _languages[_language_count]);
}
- return;
+ return OK;
}
}
+ return ERR_DOES_NOT_EXIST;
}
void ScriptServer::init_languages() {
diff --git a/core/object/script_language.h b/core/object/script_language.h
index 14cc30e029..3ef121a8e7 100644
--- a/core/object/script_language.h
+++ b/core/object/script_language.h
@@ -70,8 +70,8 @@ public:
static bool is_scripting_enabled();
_FORCE_INLINE_ static int get_language_count() { return _language_count; }
static ScriptLanguage *get_language(int p_idx);
- static void register_language(ScriptLanguage *p_language);
- static void unregister_language(const ScriptLanguage *p_language);
+ static Error register_language(ScriptLanguage *p_language);
+ static Error unregister_language(const ScriptLanguage *p_language);
static void set_reload_scripts_on_save(bool p_enable);
static bool is_reload_scripts_on_save_enabled();
diff --git a/doc/classes/Engine.xml b/doc/classes/Engine.xml
index d583e07f59..461ffcb2e0 100644
--- a/doc/classes/Engine.xml
+++ b/doc/classes/Engine.xml
@@ -244,10 +244,14 @@
</description>
</method>
<method name="register_script_language">
- <return type="void" />
+ <return type="int" enum="Error" />
<param index="0" name="language" type="ScriptLanguage" />
<description>
Registers a [ScriptLanguage] instance to be available with [code]ScriptServer[/code].
+ Returns:
+ - [constant OK] on success
+ - [constant ERR_UNAVAILABLE] if [code]ScriptServer[/code] has reached it limit and cannot register any new language
+ - [constant ERR_ALREADY_EXISTS] if [code]ScriptServer[/code] already contains a language with similar extension/name/type
</description>
</method>
<method name="register_singleton">
@@ -258,6 +262,16 @@
Registers the given object as a singleton, globally available under [param name].
</description>
</method>
+ <method name="unregister_script_language">
+ <return type="int" enum="Error" />
+ <param index="0" name="language" type="ScriptLanguage" />
+ <description>
+ Unregisters the [ScriptLanguage] instance from [code]ScriptServer[/code].
+ Returns:
+ - [constant OK] on success
+ - [constant ERR_DOES_NOT_EXIST] if the language is already not registered in [code]ScriptServer[/code]
+ </description>
+ </method>
<method name="unregister_singleton">
<return type="void" />
<param index="0" name="name" type="StringName" />