pluginscript_instance.cpp 5.3 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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/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, Callable::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. String PluginScriptInstance::to_string(bool *r_valid) {
  83. godot_string ret = _desc->to_string(_data, r_valid);
  84. String str_ret = *(String *)&ret;
  85. godot_string_destroy(&ret);
  86. return str_ret;
  87. }
  88. const Vector<MultiplayerAPI::RPCConfig> PluginScriptInstance::get_rpc_methods() const {
  89. return _script->get_rpc_methods();
  90. }
  91. void PluginScriptInstance::refcount_incremented() {
  92. if (_desc->refcount_decremented) {
  93. _desc->refcount_incremented(_data);
  94. }
  95. }
  96. bool PluginScriptInstance::refcount_decremented() {
  97. // Return true if it can die
  98. if (_desc->refcount_decremented) {
  99. return _desc->refcount_decremented(_data);
  100. }
  101. return true;
  102. }
  103. PluginScriptInstance::PluginScriptInstance() {
  104. }
  105. bool PluginScriptInstance::init(PluginScript *p_script, Object *p_owner) {
  106. _owner = p_owner;
  107. _owner_variant = Variant(p_owner);
  108. _script = Ref<PluginScript>(p_script);
  109. _desc = &p_script->_desc->instance_desc;
  110. _data = _desc->init(p_script->_data, (godot_object *)p_owner);
  111. ERR_FAIL_COND_V(_data == nullptr, false);
  112. p_owner->set_script_instance(this);
  113. return true;
  114. }
  115. PluginScriptInstance::~PluginScriptInstance() {
  116. _desc->finish(_data);
  117. _script->_language->lock();
  118. _script->_instances.erase(_owner);
  119. _script->_language->unlock();
  120. }