pluginscript_instance.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**************************************************************************/
  2. /* pluginscript_instance.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. 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. void PluginScriptInstance::notification(int p_notification) {
  82. _desc->notification(_data, p_notification);
  83. }
  84. MultiplayerAPI::RPCMode PluginScriptInstance::get_rpc_mode(const StringName &p_method) const {
  85. return _script->get_rpc_mode(p_method);
  86. }
  87. MultiplayerAPI::RPCMode PluginScriptInstance::get_rset_mode(const StringName &p_variable) const {
  88. return _script->get_rset_mode(p_variable);
  89. }
  90. void PluginScriptInstance::refcount_incremented() {
  91. if (_desc->refcount_decremented) {
  92. _desc->refcount_incremented(_data);
  93. }
  94. }
  95. bool PluginScriptInstance::refcount_decremented() {
  96. // Return true if it can die
  97. if (_desc->refcount_decremented) {
  98. return _desc->refcount_decremented(_data);
  99. }
  100. return true;
  101. }
  102. PluginScriptInstance::PluginScriptInstance() {
  103. }
  104. bool PluginScriptInstance::init(PluginScript *p_script, Object *p_owner) {
  105. _owner = p_owner;
  106. _owner_variant = Variant(p_owner);
  107. _script = Ref<PluginScript>(p_script);
  108. _desc = &p_script->_desc->instance_desc;
  109. _data = _desc->init(p_script->_data, (godot_object *)p_owner);
  110. ERR_FAIL_COND_V(_data == nullptr, false);
  111. p_owner->set_script_instance(this);
  112. return true;
  113. }
  114. PluginScriptInstance::~PluginScriptInstance() {
  115. _desc->finish(_data);
  116. _script->_language->lock();
  117. _script->_instances.erase(_owner);
  118. _script->_language->unlock();
  119. }