pluginscript_instance.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*************************************************************************/
  2. /* pluginscript_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. // Godot imports
  31. #include "core/os/os.h"
  32. #include "core/variant.h"
  33. // PluginScript imports
  34. #include "pluginscript_instance.h"
  35. #include "pluginscript_language.h"
  36. #include "pluginscript_script.h"
  37. bool PluginScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  38. String name = String(p_name);
  39. return _desc->set_prop(_data, (const godot_string *)&name, (const godot_variant *)&p_value);
  40. }
  41. bool PluginScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  42. String name = String(p_name);
  43. return _desc->get_prop(_data, (const godot_string *)&name, (godot_variant *)&r_ret);
  44. }
  45. Ref<Script> PluginScriptInstance::get_script() const {
  46. return _script;
  47. }
  48. ScriptLanguage *PluginScriptInstance::get_language() {
  49. return _script->get_language();
  50. }
  51. Variant::Type PluginScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  52. if (!_script->has_property(p_name)) {
  53. if (r_is_valid) {
  54. *r_is_valid = false;
  55. }
  56. return Variant::NIL;
  57. }
  58. if (r_is_valid) {
  59. *r_is_valid = true;
  60. }
  61. return _script->get_property_info(p_name).type;
  62. }
  63. void PluginScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  64. _script->get_script_property_list(p_properties);
  65. }
  66. void PluginScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  67. _script->get_script_method_list(p_list);
  68. }
  69. bool PluginScriptInstance::has_method(const StringName &p_method) const {
  70. return _script->has_method(p_method);
  71. }
  72. Variant PluginScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  73. // TODO: optimize when calling a Godot method from Godot to avoid param conversion ?
  74. godot_variant ret = _desc->call_method(
  75. _data, (godot_string_name *)&p_method, (const godot_variant **)p_args,
  76. p_argcount, (godot_variant_call_error *)&r_error);
  77. Variant var_ret = *(Variant *)&ret;
  78. godot_variant_destroy(&ret);
  79. return var_ret;
  80. }
  81. #if 0 // TODO: Don't rely on default implementations provided by ScriptInstance ?
  82. void PluginScriptInstance::call_multilevel(const StringName& p_method,const Variant** p_args,int p_argcount) {
  83. #if 0
  84. PluginScript *sptr=script.ptr();
  85. Variant::CallError ce;
  86. while(sptr) {
  87. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  88. if (E) {
  89. E->get()->call(this,p_args,p_argcount,ce);
  90. }
  91. sptr = sptr->_base;
  92. }
  93. #endif
  94. }
  95. #if 0
  96. void PluginScriptInstance::_ml_call_reversed(PluginScript *sptr,const StringName& p_method,const Variant** p_args,int p_argcount) {
  97. if (sptr->_base)
  98. _ml_call_reversed(sptr->_base,p_method,p_args,p_argcount);
  99. Variant::CallError ce;
  100. Map<StringName,GDFunction*>::Element *E = sptr->member_functions.find(p_method);
  101. if (E) {
  102. E->get()->call(this,p_args,p_argcount,ce);
  103. }
  104. }
  105. #endif
  106. void PluginScriptInstance::call_multilevel_reversed(const StringName& p_method,const Variant** p_args,int p_argcount) {
  107. #if 0
  108. if (script.ptr()) {
  109. _ml_call_reversed(script.ptr(),p_method,p_args,p_argcount);
  110. }
  111. #endif
  112. }
  113. #endif // Multilevel stuff
  114. void PluginScriptInstance::notification(int p_notification) {
  115. _desc->notification(_data, p_notification);
  116. }
  117. MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode(const StringName &p_method) const {
  118. return _script->get_rpc_mode(p_method);
  119. }
  120. MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode(const StringName &p_variable) const {
  121. return _script->get_rset_mode(p_variable);
  122. }
  123. void PluginScriptInstance::refcount_incremented() {
  124. if (_desc->refcount_decremented) {
  125. _desc->refcount_incremented(_data);
  126. }
  127. }
  128. bool PluginScriptInstance::refcount_decremented() {
  129. // Return true if it can die
  130. if (_desc->refcount_decremented) {
  131. return _desc->refcount_decremented(_data);
  132. }
  133. return true;
  134. }
  135. PluginScriptInstance::PluginScriptInstance() {
  136. }
  137. bool PluginScriptInstance::init(PluginScript *p_script, Object *p_owner) {
  138. _owner = p_owner;
  139. _owner_variant = Variant(p_owner);
  140. _script = Ref<PluginScript>(p_script);
  141. _desc = &p_script->_desc->instance_desc;
  142. _data = _desc->init(p_script->_data, (godot_object *)p_owner);
  143. ERR_FAIL_COND_V(_data == NULL, false);
  144. p_owner->set_script_instance(this);
  145. return true;
  146. }
  147. PluginScriptInstance::~PluginScriptInstance() {
  148. _desc->finish(_data);
  149. _script->_language->lock();
  150. _script->_instances.erase(_owner);
  151. _script->_language->unlock();
  152. }