pluginscript_instance.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "pluginscript_instance.h"
  31. // Godot imports
  32. #include "core/os/os.h"
  33. #include "core/variant.h"
  34. // PluginScript imports
  35. #include "pluginscript_language.h"
  36. #include "pluginscript_script.h"
  37. bool PluginScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  38. return _desc->set_prop(_data, (const godot_string_name *)&p_name, (const godot_variant *)&p_value);
  39. }
  40. bool PluginScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  41. return _desc->get_prop(_data, (const godot_string_name *)&p_name, (godot_variant *)&r_ret);
  42. }
  43. Ref<Script> PluginScriptInstance::get_script() const {
  44. return _script;
  45. }
  46. ScriptLanguage *PluginScriptInstance::get_language() {
  47. return _script->get_language();
  48. }
  49. Variant::Type PluginScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  50. if (!_script->has_property(p_name)) {
  51. if (r_is_valid) {
  52. *r_is_valid = false;
  53. }
  54. return Variant::NIL;
  55. }
  56. if (r_is_valid) {
  57. *r_is_valid = true;
  58. }
  59. return _script->get_property_info(p_name).type;
  60. }
  61. void PluginScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  62. _script->get_script_property_list(p_properties);
  63. }
  64. void PluginScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  65. _script->get_script_method_list(p_list);
  66. }
  67. bool PluginScriptInstance::has_method(const StringName &p_method) const {
  68. return _script->has_method(p_method);
  69. }
  70. Variant PluginScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  71. // TODO: optimize when calling a Godot method from Godot to avoid param conversion ?
  72. godot_variant ret = _desc->call_method(
  73. _data, (godot_string_name *)&p_method, (const godot_variant **)p_args,
  74. p_argcount, (godot_variant_call_error *)&r_error);
  75. Variant var_ret = *(Variant *)&ret;
  76. godot_variant_destroy(&ret);
  77. return var_ret;
  78. }
  79. void PluginScriptInstance::notification(int p_notification) {
  80. _desc->notification(_data, p_notification);
  81. }
  82. Vector<ScriptNetData> PluginScriptInstance::get_rpc_methods() const {
  83. return _script->get_rpc_methods();
  84. }
  85. uint16_t PluginScriptInstance::get_rpc_method_id(const StringName &p_variable) const {
  86. return _script->get_rpc_method_id(p_variable);
  87. }
  88. StringName PluginScriptInstance::get_rpc_method(uint16_t p_id) const {
  89. return _script->get_rpc_method(p_id);
  90. }
  91. MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode_by_id(uint16_t p_id) const {
  92. return _script->get_rpc_mode_by_id(p_id);
  93. }
  94. MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode(const StringName &p_method) const {
  95. return _script->get_rpc_mode(p_method);
  96. }
  97. Vector<ScriptNetData> PluginScriptInstance::get_rset_properties() const {
  98. return _script->get_rset_properties();
  99. }
  100. uint16_t PluginScriptInstance::get_rset_property_id(const StringName &p_variable) const {
  101. return _script->get_rset_property_id(p_variable);
  102. }
  103. StringName PluginScriptInstance::get_rset_property(uint16_t p_id) const {
  104. return _script->get_rset_property(p_id);
  105. }
  106. MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode_by_id(uint16_t p_id) const {
  107. return _script->get_rset_mode_by_id(p_id);
  108. }
  109. MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode(const StringName &p_variable) const {
  110. return _script->get_rset_mode(p_variable);
  111. }
  112. void PluginScriptInstance::refcount_incremented() {
  113. if (_desc->refcount_decremented) {
  114. _desc->refcount_incremented(_data);
  115. }
  116. }
  117. bool PluginScriptInstance::refcount_decremented() {
  118. // Return true if it can die
  119. if (_desc->refcount_decremented) {
  120. return _desc->refcount_decremented(_data);
  121. }
  122. return true;
  123. }
  124. PluginScriptInstance::PluginScriptInstance() {
  125. }
  126. bool PluginScriptInstance::init(PluginScript *p_script, Object *p_owner) {
  127. _owner = p_owner;
  128. _owner_variant = Variant(p_owner);
  129. _script = Ref<PluginScript>(p_script);
  130. _desc = &p_script->_desc->instance_desc;
  131. _data = _desc->init(p_script->_data, (godot_object *)p_owner);
  132. ERR_FAIL_COND_V(_data == NULL, false);
  133. p_owner->set_script_instance(this);
  134. return true;
  135. }
  136. PluginScriptInstance::~PluginScriptInstance() {
  137. _desc->finish(_data);
  138. _script->_language->lock();
  139. _script->_instances.erase(_owner);
  140. _script->_language->unlock();
  141. }