summaryrefslogtreecommitdiff
path: root/modules/gdnative/pluginscript/pluginscript_script.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gdnative/pluginscript/pluginscript_script.cpp')
-rw-r--r--modules/gdnative/pluginscript/pluginscript_script.cpp224
1 files changed, 153 insertions, 71 deletions
diff --git a/modules/gdnative/pluginscript/pluginscript_script.cpp b/modules/gdnative/pluginscript/pluginscript_script.cpp
index f7c961d38b..87c6288806 100644
--- a/modules/gdnative/pluginscript/pluginscript_script.cpp
+++ b/modules/gdnative/pluginscript/pluginscript_script.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -34,6 +34,8 @@
#include "pluginscript_instance.h"
#include "pluginscript_script.h"
+#include <stdint.h>
+
#ifdef DEBUG_ENABLED
#define __ASSERT_SCRIPT_REASON "Cannot retrieve PluginScript class for this script, is your code correct?"
#define ASSERT_SCRIPT_VALID() \
@@ -53,9 +55,8 @@ void PluginScript::_bind_methods() {
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &PluginScript::_new, MethodInfo("new"));
}
-PluginScriptInstance *PluginScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Variant::CallError &r_error) {
-
- r_error.error = Variant::CallError::CALL_OK;
+PluginScriptInstance *PluginScript::_create_instance(const Variant **p_args, int p_argcount, Object *p_owner, Callable::CallError &r_error) {
+ r_error.error = Callable::CallError::CALL_OK;
// Create instance
PluginScriptInstance *instance = memnew(PluginScriptInstance());
@@ -65,9 +66,9 @@ PluginScriptInstance *PluginScript::_create_instance(const Variant **p_args, int
_instances.insert(instance->get_owner());
_language->unlock();
} else {
- r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
+ r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
memdelete(instance);
- ERR_FAIL_V(NULL);
+ ERR_FAIL_V(nullptr);
}
// Construct
@@ -81,17 +82,16 @@ PluginScriptInstance *PluginScript::_create_instance(const Variant **p_args, int
return instance;
}
-Variant PluginScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
-
- r_error.error = Variant::CallError::CALL_OK;
+Variant PluginScript::_new(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
+ r_error.error = Callable::CallError::CALL_OK;
if (!_valid) {
- r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
+ r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
return Variant();
}
REF ref;
- Object *owner = NULL;
+ Object *owner = nullptr;
if (get_instance_base_type() == "") {
owner = memnew(Reference);
@@ -100,7 +100,7 @@ Variant PluginScript::_new(const Variant **p_args, int p_argcount, Variant::Call
}
if (!owner) {
- r_error.error = Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL;
+ r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
return Variant();
}
@@ -138,6 +138,13 @@ bool PluginScript::can_instance() const {
return can;
}
+bool PluginScript::inherits_script(const Ref<Script> &p_script) const {
+#ifndef _MSC_VER
+#warning inheritance needs to be implemented in PluginScript
+#endif
+ return false;
+}
+
Ref<Script> PluginScript::get_base_script() const {
if (_ref_base_parent.is_valid()) {
return Ref<PluginScript>(_ref_base_parent);
@@ -147,10 +154,12 @@ Ref<Script> PluginScript::get_base_script() const {
}
StringName PluginScript::get_instance_base_type() const {
- if (_native_parent)
+ if (_native_parent) {
return _native_parent;
- if (_ref_base_parent.is_valid())
+ }
+ if (_ref_base_parent.is_valid()) {
return _ref_base_parent->get_instance_base_type();
+ }
return StringName();
}
@@ -158,7 +167,6 @@ void PluginScript::update_exports() {
#ifdef TOOLS_ENABLED
ASSERT_SCRIPT_VALID();
if (placeholders.size()) {
-
//update placeholders if any
Map<StringName, Variant> propdefvalues;
List<PropertyInfo> propinfos;
@@ -173,7 +181,7 @@ void PluginScript::update_exports() {
// TODO: rename p_this "p_owner" ?
ScriptInstance *PluginScript::instance_create(Object *p_this) {
- ASSERT_SCRIPT_VALID_V(NULL);
+ ASSERT_SCRIPT_VALID_V(nullptr);
// TODO check script validity ?
if (!_tool && !ScriptServer::is_scripting_enabled()) {
#ifdef TOOLS_ENABLED
@@ -183,7 +191,7 @@ ScriptInstance *PluginScript::instance_create(Object *p_this) {
update_exports();
return si;
#else
- return NULL;
+ return nullptr;
#endif
}
@@ -192,15 +200,15 @@ ScriptInstance *PluginScript::instance_create(Object *p_this) {
if (!ClassDB::is_parent_class(p_this->get_class_name(), base_type)) {
String msg = "Script inherits from native type '" + String(base_type) + "', so it can't be instanced in object of type: '" + p_this->get_class() + "'";
// TODO: implement PluginscriptLanguage::debug_break_parse
- // if (ScriptDebugger::get_singleton()) {
+ // if (EngineDebugger::is_active()) {
// _language->debug_break_parse(get_path(), 0, msg);
// }
- ERR_FAIL_V_MSG(NULL, msg);
+ ERR_FAIL_V_MSG(nullptr, msg);
}
}
- Variant::CallError unchecked_error;
- return _create_instance(NULL, 0, p_this, unchecked_error);
+ Callable::CallError unchecked_error;
+ return _create_instance(nullptr, 0, p_this, unchecked_error);
}
bool PluginScript::instance_has(const Object *p_this) const {
@@ -220,8 +228,9 @@ String PluginScript::get_source_code() const {
}
void PluginScript::set_source_code(const String &p_code) {
- if (_source == p_code)
+ if (_source == p_code) {
return;
+ }
_source = p_code;
}
@@ -235,11 +244,13 @@ Error PluginScript::reload(bool p_keep_state) {
_valid = false;
String basedir = _path;
- if (basedir == "")
+ if (basedir == "") {
basedir = get_path();
+ }
- if (basedir != "")
+ if (basedir != "") {
basedir = basedir.get_base_dir();
+ }
if (_data) {
_desc->finish(_data);
@@ -251,7 +262,19 @@ Error PluginScript::reload(bool p_keep_state) {
(godot_string *)&_path,
(godot_string *)&_source,
(godot_error *)&err);
+// Manifest's attributes must be explicitly freed
+#define FREE_SCRIPT_MANIFEST(manifest) \
+ { \
+ godot_string_name_destroy(&manifest.name); \
+ godot_string_name_destroy(&manifest.base); \
+ godot_dictionary_destroy(&manifest.member_lines); \
+ godot_array_destroy(&manifest.methods); \
+ godot_array_destroy(&manifest.signals); \
+ godot_array_destroy(&manifest.properties); \
+ }
+
if (err) {
+ FREE_SCRIPT_MANIFEST(manifest);
// TODO: GDscript uses `ScriptDebugger` here to jump into the parsing error
return err;
}
@@ -260,7 +283,6 @@ Error PluginScript::reload(bool p_keep_state) {
// ClassDB name (i.e. `Node2D`) or a resource path (i.e. `res://foo/bar.gd`)
StringName *base_name = (StringName *)&manifest.base;
if (*base_name) {
-
if (ClassDB::class_exists(*base_name)) {
_native_parent = *base_name;
} else {
@@ -269,6 +291,7 @@ Error PluginScript::reload(bool p_keep_state) {
_ref_base_parent = res;
} else {
String name = *(StringName *)&manifest.name;
+ FREE_SCRIPT_MANIFEST(manifest);
ERR_FAIL_V_MSG(ERR_PARSE_ERROR, _path + ": Script '" + name + "' has an invalid parent '" + *base_name + "'.");
}
}
@@ -281,22 +304,35 @@ Error PluginScript::reload(bool p_keep_state) {
_tool = manifest.is_tool;
Dictionary *members = (Dictionary *)&manifest.member_lines;
- for (const Variant *key = members->next(); key != NULL; key = members->next(key)) {
+ for (const Variant *key = members->next(); key != nullptr; key = members->next(key)) {
_member_lines[*key] = (*members)[*key];
}
Array *methods = (Array *)&manifest.methods;
+ _rpc_methods.clear();
+ _rpc_variables.clear();
+ if (_ref_base_parent.is_valid()) {
+ _rpc_methods = _ref_base_parent->get_rpc_methods();
+ _rpc_variables = _ref_base_parent->get_rset_properties();
+ }
for (int i = 0; i < methods->size(); ++i) {
Dictionary v = (*methods)[i];
MethodInfo mi = MethodInfo::from_dict(v);
_methods_info[mi.name] = mi;
// rpc_mode is passed as an optional field and is not part of MethodInfo
Variant var = v["rpc_mode"];
- if (var == Variant()) {
- _methods_rpc_mode[mi.name] = MultiplayerAPI::RPC_MODE_DISABLED;
- } else {
- _methods_rpc_mode[mi.name] = MultiplayerAPI::RPCMode(int(var));
+ if (var != Variant()) {
+ ScriptNetData nd;
+ nd.name = mi.name;
+ nd.mode = MultiplayerAPI::RPCMode(int(var));
+ if (_rpc_methods.find(nd) == -1) {
+ _rpc_methods.push_back(nd);
+ }
}
}
+
+ // Sort so we are 100% that they are always the same.
+ _rpc_methods.sort_custom<SortNetData>();
+
Array *signals = (Array *)&manifest.signals;
for (int i = 0; i < signals->size(); ++i) {
Variant v = (*signals)[i];
@@ -311,19 +347,18 @@ Error PluginScript::reload(bool p_keep_state) {
_properties_default_values[pi.name] = v["default_value"];
// rset_mode is passed as an optional field and is not part of PropertyInfo
Variant var = v["rset_mode"];
- if (var == Variant()) {
- _methods_rpc_mode[pi.name] = MultiplayerAPI::RPC_MODE_DISABLED;
- } else {
- _methods_rpc_mode[pi.name] = MultiplayerAPI::RPCMode(int(var));
+ if (var != Variant()) {
+ ScriptNetData nd;
+ nd.name = pi.name;
+ nd.mode = MultiplayerAPI::RPCMode(int(var));
+ if (_rpc_variables.find(nd) == -1) {
+ _rpc_variables.push_back(nd);
+ }
}
}
- // Manifest's attributes must be explicitly freed
- godot_string_name_destroy(&manifest.name);
- godot_string_name_destroy(&manifest.base);
- godot_dictionary_destroy(&manifest.member_lines);
- godot_array_destroy(&manifest.methods);
- godot_array_destroy(&manifest.signals);
- godot_array_destroy(&manifest.properties);
+
+ // Sort so we are 100% that they are always the same.
+ _rpc_variables.sort_custom<SortNetData>();
#ifdef TOOLS_ENABLED
/*for (Set<PlaceHolderScriptInstance*>::Element *E=placeholders.front();E;E=E->next()) {
@@ -331,19 +366,22 @@ Error PluginScript::reload(bool p_keep_state) {
_update_placeholder(E->get());
}*/
#endif
+
+ FREE_SCRIPT_MANIFEST(manifest);
return OK;
+#undef FREE_SCRIPT_MANIFEST
}
void PluginScript::get_script_method_list(List<MethodInfo> *r_methods) const {
ASSERT_SCRIPT_VALID();
- for (Map<StringName, MethodInfo>::Element *e = _methods_info.front(); e != NULL; e = e->next()) {
+ for (Map<StringName, MethodInfo>::Element *e = _methods_info.front(); e != nullptr; e = e->next()) {
r_methods->push_back(e->get());
}
}
void PluginScript::get_script_property_list(List<PropertyInfo> *r_properties) const {
ASSERT_SCRIPT_VALID();
- for (Map<StringName, PropertyInfo>::Element *e = _properties_info.front(); e != NULL; e = e->next()) {
+ for (Map<StringName, PropertyInfo>::Element *e = _properties_info.front(); e != nullptr; e = e->next()) {
r_properties->push_back(e->get());
}
}
@@ -356,7 +394,7 @@ bool PluginScript::has_method(const StringName &p_method) const {
MethodInfo PluginScript::get_method_info(const StringName &p_method) const {
ASSERT_SCRIPT_VALID_V(MethodInfo());
const Map<StringName, MethodInfo>::Element *e = _methods_info.find(p_method);
- if (e != NULL) {
+ if (e != nullptr) {
return e->get();
} else {
return MethodInfo();
@@ -371,7 +409,7 @@ bool PluginScript::has_property(const StringName &p_method) const {
PropertyInfo PluginScript::get_property_info(const StringName &p_property) const {
ASSERT_SCRIPT_VALID_V(PropertyInfo());
const Map<StringName, PropertyInfo>::Element *e = _properties_info.find(p_property);
- if (e != NULL) {
+ if (e != nullptr) {
return e->get();
} else {
return PropertyInfo();
@@ -382,7 +420,7 @@ bool PluginScript::get_property_default_value(const StringName &p_property, Vari
ASSERT_SCRIPT_VALID_V(false);
#ifdef TOOLS_ENABLED
const Map<StringName, Variant>::Element *e = _properties_default_values.find(p_property);
- if (e != NULL) {
+ if (e != nullptr) {
r_value = e->get();
return true;
} else {
@@ -397,23 +435,22 @@ ScriptLanguage *PluginScript::get_language() const {
}
Error PluginScript::load_source_code(const String &p_path) {
-
- PoolVector<uint8_t> sourcef;
+ Vector<uint8_t> sourcef;
Error err;
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
ERR_FAIL_COND_V_MSG(err, err, "Cannot open file '" + p_path + "'.");
int len = f->get_len();
sourcef.resize(len + 1);
- PoolVector<uint8_t>::Write w = sourcef.write();
- int r = f->get_buffer(w.ptr(), len);
+ uint8_t *w = sourcef.ptrw();
+ int r = f->get_buffer(w, len);
f->close();
memdelete(f);
ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
w[len] = 0;
String s;
- if (s.parse_utf8((const char *)w.ptr())) {
+ if (s.parse_utf8((const char *)w)) {
ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Script '" + p_path + "' contains invalid unicode (UTF-8), so it was not loaded. Please ensure that scripts are saved in valid UTF-8 unicode.");
}
@@ -432,46 +469,91 @@ bool PluginScript::has_script_signal(const StringName &p_signal) const {
void PluginScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
ASSERT_SCRIPT_VALID();
- for (Map<StringName, MethodInfo>::Element *e = _signals_info.front(); e != NULL; e = e->next()) {
+ for (Map<StringName, MethodInfo>::Element *e = _signals_info.front(); e != nullptr; e = e->next()) {
r_signals->push_back(e->get());
}
}
int PluginScript::get_member_line(const StringName &p_member) const {
#ifdef TOOLS_ENABLED
- if (_member_lines.has(p_member))
+ if (_member_lines.has(p_member)) {
return _member_lines[p_member];
- else
+ }
#endif
- return -1;
+ return -1;
}
-MultiplayerAPI::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const {
+Vector<ScriptNetData> PluginScript::get_rpc_methods() const {
+ return _rpc_methods;
+}
+
+uint16_t PluginScript::get_rpc_method_id(const StringName &p_method) const {
+ ASSERT_SCRIPT_VALID_V(UINT16_MAX);
+ for (int i = 0; i < _rpc_methods.size(); i++) {
+ if (_rpc_methods[i].name == p_method) {
+ return i;
+ }
+ }
+ return UINT16_MAX;
+}
+
+StringName PluginScript::get_rpc_method(const uint16_t p_rpc_method_id) const {
+ ASSERT_SCRIPT_VALID_V(StringName());
+ if (p_rpc_method_id >= _rpc_methods.size()) {
+ return StringName();
+ }
+ return _rpc_methods[p_rpc_method_id].name;
+}
+
+MultiplayerAPI::RPCMode PluginScript::get_rpc_mode_by_id(const uint16_t p_rpc_method_id) const {
ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
- const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _methods_rpc_mode.find(p_method);
- if (e != NULL) {
- return e->get();
- } else {
+ if (p_rpc_method_id >= _rpc_methods.size()) {
return MultiplayerAPI::RPC_MODE_DISABLED;
}
+ return _rpc_methods[p_rpc_method_id].mode;
}
-MultiplayerAPI::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const {
+MultiplayerAPI::RPCMode PluginScript::get_rpc_mode(const StringName &p_method) const {
ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
- const Map<StringName, MultiplayerAPI::RPCMode>::Element *e = _variables_rset_mode.find(p_variable);
- if (e != NULL) {
- return e->get();
- } else {
+ return get_rpc_mode_by_id(get_rpc_method_id(p_method));
+}
+
+Vector<ScriptNetData> PluginScript::get_rset_properties() const {
+ return _rpc_variables;
+}
+
+uint16_t PluginScript::get_rset_property_id(const StringName &p_property) const {
+ ASSERT_SCRIPT_VALID_V(UINT16_MAX);
+ for (int i = 0; i < _rpc_variables.size(); i++) {
+ if (_rpc_variables[i].name == p_property) {
+ return i;
+ }
+ }
+ return UINT16_MAX;
+}
+
+StringName PluginScript::get_rset_property(const uint16_t p_rset_property_id) const {
+ ASSERT_SCRIPT_VALID_V(StringName());
+ if (p_rset_property_id >= _rpc_variables.size()) {
+ return StringName();
+ }
+ return _rpc_variables[p_rset_property_id].name;
+}
+
+MultiplayerAPI::RPCMode PluginScript::get_rset_mode_by_id(const uint16_t p_rset_property_id) const {
+ ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
+ if (p_rset_property_id >= _rpc_variables.size()) {
return MultiplayerAPI::RPC_MODE_DISABLED;
}
+ return _rpc_variables[p_rset_property_id].mode;
+}
+
+MultiplayerAPI::RPCMode PluginScript::get_rset_mode(const StringName &p_variable) const {
+ ASSERT_SCRIPT_VALID_V(MultiplayerAPI::RPC_MODE_DISABLED);
+ return get_rset_mode_by_id(get_rset_property_id(p_variable));
}
PluginScript::PluginScript() :
- _data(NULL),
- _desc(NULL),
- _language(NULL),
- _tool(false),
- _valid(false),
_script_list(this) {
}