1 /*************************************************************************/
2 /* pluginscript_instance.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "pluginscript_instance.h"
32
33 // Godot imports
34 #include "core/os/os.h"
35 #include "core/variant.h"
36
37 // PluginScript imports
38 #include "pluginscript_language.h"
39 #include "pluginscript_script.h"
40
set(const StringName & p_name,const Variant & p_value)41 bool PluginScriptInstance::set(const StringName &p_name, const Variant &p_value) {
42 String name = String(p_name);
43 return _desc->set_prop(_data, (const godot_string *)&name, (const godot_variant *)&p_value);
44 }
45
get(const StringName & p_name,Variant & r_ret) const46 bool PluginScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
47 String name = String(p_name);
48 return _desc->get_prop(_data, (const godot_string *)&name, (godot_variant *)&r_ret);
49 }
50
get_script() const51 Ref<Script> PluginScriptInstance::get_script() const {
52 return _script;
53 }
54
get_language()55 ScriptLanguage *PluginScriptInstance::get_language() {
56 return _script->get_language();
57 }
58
get_property_type(const StringName & p_name,bool * r_is_valid) const59 Variant::Type PluginScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
60 if (!_script->has_property(p_name)) {
61 if (r_is_valid) {
62 *r_is_valid = false;
63 }
64 return Variant::NIL;
65 }
66 if (r_is_valid) {
67 *r_is_valid = true;
68 }
69 return _script->get_property_info(p_name).type;
70 }
71
get_property_list(List<PropertyInfo> * p_properties) const72 void PluginScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
73 _script->get_script_property_list(p_properties);
74 }
75
get_method_list(List<MethodInfo> * p_list) const76 void PluginScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
77 _script->get_script_method_list(p_list);
78 }
79
has_method(const StringName & p_method) const80 bool PluginScriptInstance::has_method(const StringName &p_method) const {
81 return _script->has_method(p_method);
82 }
83
call(const StringName & p_method,const Variant ** p_args,int p_argcount,Variant::CallError & r_error)84 Variant PluginScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
85 // TODO: optimize when calling a Godot method from Godot to avoid param conversion ?
86 godot_variant ret = _desc->call_method(
87 _data, (godot_string_name *)&p_method, (const godot_variant **)p_args,
88 p_argcount, (godot_variant_call_error *)&r_error);
89 Variant var_ret = *(Variant *)&ret;
90 godot_variant_destroy(&ret);
91 return var_ret;
92 }
93
notification(int p_notification)94 void PluginScriptInstance::notification(int p_notification) {
95 _desc->notification(_data, p_notification);
96 }
97
get_rpc_mode(const StringName & p_method) const98 MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode(const StringName &p_method) const {
99 return _script->get_rpc_mode(p_method);
100 }
101
get_rset_mode(const StringName & p_variable) const102 MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode(const StringName &p_variable) const {
103 return _script->get_rset_mode(p_variable);
104 }
105
refcount_incremented()106 void PluginScriptInstance::refcount_incremented() {
107 if (_desc->refcount_decremented) {
108 _desc->refcount_incremented(_data);
109 }
110 }
111
refcount_decremented()112 bool PluginScriptInstance::refcount_decremented() {
113 // Return true if it can die
114 if (_desc->refcount_decremented) {
115 return _desc->refcount_decremented(_data);
116 }
117 return true;
118 }
119
PluginScriptInstance()120 PluginScriptInstance::PluginScriptInstance() {
121 }
122
init(PluginScript * p_script,Object * p_owner)123 bool PluginScriptInstance::init(PluginScript *p_script, Object *p_owner) {
124 _owner = p_owner;
125 _owner_variant = Variant(p_owner);
126 _script = Ref<PluginScript>(p_script);
127 _desc = &p_script->_desc->instance_desc;
128 _data = _desc->init(p_script->_data, (godot_object *)p_owner);
129 ERR_FAIL_COND_V(_data == NULL, false);
130 p_owner->set_script_instance(this);
131 return true;
132 }
133
~PluginScriptInstance()134 PluginScriptInstance::~PluginScriptInstance() {
135 _desc->finish(_data);
136 _script->_language->lock();
137 _script->_instances.erase(_owner);
138 _script->_language->unlock();
139 }
140