gdextension_interface.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. /**************************************************************************/
  2. /* gdextension_interface.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 "gdextension_interface.h"
  31. #include "core/config/engine.h"
  32. #include "core/extension/gdextension.h"
  33. #include "core/extension/gdextension_compat_hashes.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/xml_parser.h"
  36. #include "core/object/class_db.h"
  37. #include "core/object/script_language_extension.h"
  38. #include "core/object/worker_thread_pool.h"
  39. #include "core/os/memory.h"
  40. #include "core/variant/variant.h"
  41. #include "core/version.h"
  42. #include <string.h>
  43. class CallableCustomExtension : public CallableCustom {
  44. void *userdata;
  45. void *token;
  46. ObjectID object;
  47. GDExtensionCallableCustomCall call_func;
  48. GDExtensionCallableCustomIsValid is_valid_func;
  49. GDExtensionCallableCustomFree free_func;
  50. GDExtensionCallableCustomEqual equal_func;
  51. GDExtensionCallableCustomLessThan less_than_func;
  52. GDExtensionCallableCustomToString to_string_func;
  53. GDExtensionCallableCustomGetArgumentCount get_argument_count_func;
  54. uint32_t _hash;
  55. static bool default_compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  56. const CallableCustomExtension *a = static_cast<const CallableCustomExtension *>(p_a);
  57. const CallableCustomExtension *b = static_cast<const CallableCustomExtension *>(p_b);
  58. if (a->call_func != b->call_func || a->userdata != b->userdata) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. static bool default_compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  64. const CallableCustomExtension *a = static_cast<const CallableCustomExtension *>(p_a);
  65. const CallableCustomExtension *b = static_cast<const CallableCustomExtension *>(p_b);
  66. if (a->call_func != b->call_func) {
  67. return a->call_func < b->call_func;
  68. }
  69. return a->userdata < b->userdata;
  70. }
  71. static bool custom_compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  72. const CallableCustomExtension *a = static_cast<const CallableCustomExtension *>(p_a);
  73. const CallableCustomExtension *b = static_cast<const CallableCustomExtension *>(p_b);
  74. if (a->equal_func != b->equal_func) {
  75. return false;
  76. }
  77. return a->equal_func(a->userdata, b->userdata);
  78. }
  79. static bool custom_compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  80. const CallableCustomExtension *a = static_cast<const CallableCustomExtension *>(p_a);
  81. const CallableCustomExtension *b = static_cast<const CallableCustomExtension *>(p_b);
  82. if (a->less_than_func != b->less_than_func) {
  83. return default_compare_less(p_a, p_b);
  84. }
  85. return a->less_than_func(a->userdata, b->userdata);
  86. }
  87. public:
  88. uint32_t hash() const override {
  89. return _hash;
  90. }
  91. String get_as_text() const override {
  92. if (to_string_func != nullptr) {
  93. String out;
  94. GDExtensionBool is_valid = false;
  95. to_string_func(userdata, &is_valid, (GDExtensionStringPtr)&out);
  96. if (is_valid) {
  97. return out;
  98. }
  99. }
  100. return "<CallableCustom>";
  101. }
  102. CompareEqualFunc get_compare_equal_func() const override {
  103. return (equal_func != nullptr) ? custom_compare_equal : default_compare_equal;
  104. }
  105. CompareLessFunc get_compare_less_func() const override {
  106. return (less_than_func != nullptr) ? custom_compare_less : default_compare_less;
  107. }
  108. bool is_valid() const override {
  109. if (is_valid_func != nullptr && !is_valid_func(userdata)) {
  110. return false;
  111. }
  112. return call_func != nullptr;
  113. }
  114. StringName get_method() const override {
  115. return StringName();
  116. }
  117. ObjectID get_object() const override {
  118. return object;
  119. }
  120. int get_argument_count(bool &r_is_valid) const override {
  121. if (get_argument_count_func != nullptr) {
  122. GDExtensionBool is_valid = false;
  123. GDExtensionInt ret = get_argument_count_func(userdata, &is_valid);
  124. if (is_valid) {
  125. r_is_valid = true;
  126. return ret;
  127. }
  128. }
  129. r_is_valid = false;
  130. return 0;
  131. }
  132. void *get_userdata(void *p_token) const {
  133. return (p_token == token) ? userdata : nullptr;
  134. }
  135. void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override {
  136. GDExtensionCallError error;
  137. call_func(userdata, (GDExtensionConstVariantPtr *)p_arguments, p_argcount, (GDExtensionVariantPtr)&r_return_value, &error);
  138. r_call_error.error = (Callable::CallError::Error)error.error;
  139. r_call_error.argument = error.argument;
  140. r_call_error.expected = error.expected;
  141. }
  142. #ifndef DISABLE_DEPRECATED
  143. CallableCustomExtension(GDExtensionCallableCustomInfo *p_info) {
  144. userdata = p_info->callable_userdata;
  145. token = p_info->token;
  146. object = p_info->object_id;
  147. call_func = p_info->call_func;
  148. is_valid_func = p_info->is_valid_func;
  149. free_func = p_info->free_func;
  150. equal_func = p_info->equal_func;
  151. less_than_func = p_info->less_than_func;
  152. to_string_func = p_info->to_string_func;
  153. get_argument_count_func = nullptr;
  154. // Pre-calculate the hash.
  155. if (p_info->hash_func != nullptr) {
  156. _hash = p_info->hash_func(userdata);
  157. } else {
  158. _hash = hash_murmur3_one_64((uint64_t)call_func);
  159. _hash = hash_murmur3_one_64((uint64_t)userdata, _hash);
  160. }
  161. }
  162. #endif
  163. CallableCustomExtension(GDExtensionCallableCustomInfo2 *p_info) {
  164. userdata = p_info->callable_userdata;
  165. token = p_info->token;
  166. object = p_info->object_id;
  167. call_func = p_info->call_func;
  168. is_valid_func = p_info->is_valid_func;
  169. free_func = p_info->free_func;
  170. equal_func = p_info->equal_func;
  171. less_than_func = p_info->less_than_func;
  172. to_string_func = p_info->to_string_func;
  173. get_argument_count_func = p_info->get_argument_count_func;
  174. // Pre-calculate the hash.
  175. if (p_info->hash_func != nullptr) {
  176. _hash = p_info->hash_func(userdata);
  177. } else {
  178. _hash = hash_murmur3_one_64((uint64_t)call_func);
  179. _hash = hash_murmur3_one_64((uint64_t)userdata, _hash);
  180. }
  181. }
  182. ~CallableCustomExtension() {
  183. if (free_func != nullptr) {
  184. free_func(userdata);
  185. }
  186. }
  187. };
  188. // Core interface functions.
  189. GDExtensionInterfaceFunctionPtr gdextension_get_proc_address(const char *p_name) {
  190. return GDExtension::get_interface_function(p_name);
  191. }
  192. static void gdextension_get_godot_version(GDExtensionGodotVersion *r_godot_version) {
  193. r_godot_version->major = VERSION_MAJOR;
  194. r_godot_version->minor = VERSION_MINOR;
  195. r_godot_version->patch = VERSION_PATCH;
  196. r_godot_version->string = VERSION_FULL_NAME;
  197. }
  198. // Memory Functions
  199. static void *gdextension_mem_alloc(size_t p_size) {
  200. return memalloc(p_size);
  201. }
  202. static void *gdextension_mem_realloc(void *p_mem, size_t p_size) {
  203. return memrealloc(p_mem, p_size);
  204. }
  205. static void gdextension_mem_free(void *p_mem) {
  206. memfree(p_mem);
  207. }
  208. // Helper print functions.
  209. static void gdextension_print_error(const char *p_description, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
  210. _err_print_error(p_function, p_file, p_line, p_description, p_editor_notify, ERR_HANDLER_ERROR);
  211. }
  212. static void gdextension_print_error_with_message(const char *p_description, const char *p_message, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
  213. _err_print_error(p_function, p_file, p_line, p_description, p_message, p_editor_notify, ERR_HANDLER_ERROR);
  214. }
  215. static void gdextension_print_warning(const char *p_description, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
  216. _err_print_error(p_function, p_file, p_line, p_description, p_editor_notify, ERR_HANDLER_WARNING);
  217. }
  218. static void gdextension_print_warning_with_message(const char *p_description, const char *p_message, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
  219. _err_print_error(p_function, p_file, p_line, p_description, p_message, p_editor_notify, ERR_HANDLER_WARNING);
  220. }
  221. static void gdextension_print_script_error(const char *p_description, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
  222. _err_print_error(p_function, p_file, p_line, p_description, p_editor_notify, ERR_HANDLER_SCRIPT);
  223. }
  224. static void gdextension_print_script_error_with_message(const char *p_description, const char *p_message, const char *p_function, const char *p_file, int32_t p_line, GDExtensionBool p_editor_notify) {
  225. _err_print_error(p_function, p_file, p_line, p_description, p_message, p_editor_notify, ERR_HANDLER_SCRIPT);
  226. }
  227. uint64_t gdextension_get_native_struct_size(GDExtensionConstStringNamePtr p_name) {
  228. const StringName name = *reinterpret_cast<const StringName *>(p_name);
  229. return ClassDB::get_native_struct_size(name);
  230. }
  231. // Variant functions
  232. static void gdextension_variant_new_copy(GDExtensionUninitializedVariantPtr r_dest, GDExtensionConstVariantPtr p_src) {
  233. memnew_placement(reinterpret_cast<Variant *>(r_dest), Variant(*reinterpret_cast<const Variant *>(p_src)));
  234. }
  235. static void gdextension_variant_new_nil(GDExtensionUninitializedVariantPtr r_dest) {
  236. memnew_placement(reinterpret_cast<Variant *>(r_dest), Variant);
  237. }
  238. static void gdextension_variant_destroy(GDExtensionVariantPtr p_self) {
  239. reinterpret_cast<Variant *>(p_self)->~Variant();
  240. }
  241. // variant type
  242. static void gdextension_variant_call(GDExtensionVariantPtr p_self, GDExtensionConstStringNamePtr p_method, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argcount, GDExtensionUninitializedVariantPtr r_return, GDExtensionCallError *r_error) {
  243. Variant *self = (Variant *)p_self;
  244. const StringName method = *reinterpret_cast<const StringName *>(p_method);
  245. const Variant **args = (const Variant **)p_args;
  246. Callable::CallError error;
  247. memnew_placement(r_return, Variant);
  248. Variant *ret = reinterpret_cast<Variant *>(r_return);
  249. self->callp(method, args, p_argcount, *ret, error);
  250. if (r_error) {
  251. r_error->error = (GDExtensionCallErrorType)(error.error);
  252. r_error->argument = error.argument;
  253. r_error->expected = error.expected;
  254. }
  255. }
  256. static void gdextension_variant_call_static(GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_method, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argcount, GDExtensionUninitializedVariantPtr r_return, GDExtensionCallError *r_error) {
  257. Variant::Type type = (Variant::Type)p_type;
  258. const StringName method = *reinterpret_cast<const StringName *>(p_method);
  259. const Variant **args = (const Variant **)p_args;
  260. Callable::CallError error;
  261. memnew_placement(r_return, Variant);
  262. Variant *ret = reinterpret_cast<Variant *>(r_return);
  263. Variant::call_static(type, method, args, p_argcount, *ret, error);
  264. if (r_error) {
  265. r_error->error = (GDExtensionCallErrorType)error.error;
  266. r_error->argument = error.argument;
  267. r_error->expected = error.expected;
  268. }
  269. }
  270. static void gdextension_variant_evaluate(GDExtensionVariantOperator p_op, GDExtensionConstVariantPtr p_a, GDExtensionConstVariantPtr p_b, GDExtensionUninitializedVariantPtr r_return, GDExtensionBool *r_valid) {
  271. Variant::Operator op = (Variant::Operator)p_op;
  272. const Variant *a = (const Variant *)p_a;
  273. const Variant *b = (const Variant *)p_b;
  274. bool valid;
  275. memnew_placement(r_return, Variant);
  276. Variant *ret = reinterpret_cast<Variant *>(r_return);
  277. Variant::evaluate(op, *a, *b, *ret, valid);
  278. *r_valid = valid;
  279. }
  280. static void gdextension_variant_set(GDExtensionVariantPtr p_self, GDExtensionConstVariantPtr p_key, GDExtensionConstVariantPtr p_value, GDExtensionBool *r_valid) {
  281. Variant *self = (Variant *)p_self;
  282. const Variant *key = (const Variant *)p_key;
  283. const Variant *value = (const Variant *)p_value;
  284. bool valid;
  285. self->set(*key, *value, &valid);
  286. *r_valid = valid;
  287. }
  288. static void gdextension_variant_set_named(GDExtensionVariantPtr p_self, GDExtensionConstStringNamePtr p_key, GDExtensionConstVariantPtr p_value, GDExtensionBool *r_valid) {
  289. Variant *self = (Variant *)p_self;
  290. const StringName *key = (const StringName *)p_key;
  291. const Variant *value = (const Variant *)p_value;
  292. bool valid;
  293. self->set_named(*key, *value, valid);
  294. *r_valid = valid;
  295. }
  296. static void gdextension_variant_set_keyed(GDExtensionVariantPtr p_self, GDExtensionConstVariantPtr p_key, GDExtensionConstVariantPtr p_value, GDExtensionBool *r_valid) {
  297. Variant *self = (Variant *)p_self;
  298. const Variant *key = (const Variant *)p_key;
  299. const Variant *value = (const Variant *)p_value;
  300. bool valid;
  301. self->set_keyed(*key, *value, valid);
  302. *r_valid = valid;
  303. }
  304. static void gdextension_variant_set_indexed(GDExtensionVariantPtr p_self, GDExtensionInt p_index, GDExtensionConstVariantPtr p_value, GDExtensionBool *r_valid, GDExtensionBool *r_oob) {
  305. Variant *self = (Variant *)p_self;
  306. const Variant *value = (const Variant *)p_value;
  307. bool valid;
  308. bool oob;
  309. self->set_indexed(p_index, *value, valid, oob);
  310. *r_valid = valid;
  311. *r_oob = oob;
  312. }
  313. static void gdextension_variant_get(GDExtensionConstVariantPtr p_self, GDExtensionConstVariantPtr p_key, GDExtensionUninitializedVariantPtr r_ret, GDExtensionBool *r_valid) {
  314. const Variant *self = (const Variant *)p_self;
  315. const Variant *key = (const Variant *)p_key;
  316. bool valid;
  317. memnew_placement(r_ret, Variant(self->get(*key, &valid)));
  318. *r_valid = valid;
  319. }
  320. static void gdextension_variant_get_named(GDExtensionConstVariantPtr p_self, GDExtensionConstStringNamePtr p_key, GDExtensionUninitializedVariantPtr r_ret, GDExtensionBool *r_valid) {
  321. const Variant *self = (const Variant *)p_self;
  322. const StringName *key = (const StringName *)p_key;
  323. bool valid;
  324. memnew_placement(r_ret, Variant(self->get_named(*key, valid)));
  325. *r_valid = valid;
  326. }
  327. static void gdextension_variant_get_keyed(GDExtensionConstVariantPtr p_self, GDExtensionConstVariantPtr p_key, GDExtensionUninitializedVariantPtr r_ret, GDExtensionBool *r_valid) {
  328. const Variant *self = (const Variant *)p_self;
  329. const Variant *key = (const Variant *)p_key;
  330. bool valid;
  331. memnew_placement(r_ret, Variant(self->get_keyed(*key, valid)));
  332. *r_valid = valid;
  333. }
  334. static void gdextension_variant_get_indexed(GDExtensionConstVariantPtr p_self, GDExtensionInt p_index, GDExtensionUninitializedVariantPtr r_ret, GDExtensionBool *r_valid, GDExtensionBool *r_oob) {
  335. const Variant *self = (const Variant *)p_self;
  336. bool valid;
  337. bool oob;
  338. memnew_placement(r_ret, Variant(self->get_indexed(p_index, valid, oob)));
  339. *r_valid = valid;
  340. *r_oob = oob;
  341. }
  342. /// Iteration.
  343. static GDExtensionBool gdextension_variant_iter_init(GDExtensionConstVariantPtr p_self, GDExtensionUninitializedVariantPtr r_iter, GDExtensionBool *r_valid) {
  344. const Variant *self = (const Variant *)p_self;
  345. memnew_placement(r_iter, Variant);
  346. Variant *iter = reinterpret_cast<Variant *>(r_iter);
  347. bool valid;
  348. bool ret = self->iter_init(*iter, valid);
  349. *r_valid = valid;
  350. return ret;
  351. }
  352. static GDExtensionBool gdextension_variant_iter_next(GDExtensionConstVariantPtr p_self, GDExtensionVariantPtr r_iter, GDExtensionBool *r_valid) {
  353. const Variant *self = (const Variant *)p_self;
  354. Variant *iter = (Variant *)r_iter;
  355. bool valid;
  356. bool ret = self->iter_next(*iter, valid);
  357. *r_valid = valid;
  358. return ret;
  359. }
  360. static void gdextension_variant_iter_get(GDExtensionConstVariantPtr p_self, GDExtensionVariantPtr r_iter, GDExtensionUninitializedVariantPtr r_ret, GDExtensionBool *r_valid) {
  361. const Variant *self = (const Variant *)p_self;
  362. Variant *iter = (Variant *)r_iter;
  363. bool valid;
  364. memnew_placement(r_ret, Variant(self->iter_get(*iter, valid)));
  365. *r_valid = valid;
  366. }
  367. /// Variant functions.
  368. static GDExtensionInt gdextension_variant_hash(GDExtensionConstVariantPtr p_self) {
  369. const Variant *self = (const Variant *)p_self;
  370. return self->hash();
  371. }
  372. static GDExtensionInt gdextension_variant_recursive_hash(GDExtensionConstVariantPtr p_self, GDExtensionInt p_recursion_count) {
  373. const Variant *self = (const Variant *)p_self;
  374. return self->recursive_hash(p_recursion_count);
  375. }
  376. static GDExtensionBool gdextension_variant_hash_compare(GDExtensionConstVariantPtr p_self, GDExtensionConstVariantPtr p_other) {
  377. const Variant *self = (const Variant *)p_self;
  378. const Variant *other = (const Variant *)p_other;
  379. return self->hash_compare(*other);
  380. }
  381. static GDExtensionBool gdextension_variant_booleanize(GDExtensionConstVariantPtr p_self) {
  382. const Variant *self = (const Variant *)p_self;
  383. return self->booleanize();
  384. }
  385. static void gdextension_variant_duplicate(GDExtensionConstVariantPtr p_self, GDExtensionUninitializedVariantPtr r_ret, GDExtensionBool p_deep) {
  386. const Variant *self = (const Variant *)p_self;
  387. memnew_placement(r_ret, Variant(self->duplicate(p_deep)));
  388. }
  389. static void gdextension_variant_stringify(GDExtensionConstVariantPtr p_self, GDExtensionUninitializedVariantPtr r_ret) {
  390. const Variant *self = (const Variant *)p_self;
  391. memnew_placement(r_ret, String(*self));
  392. }
  393. static GDExtensionVariantType gdextension_variant_get_type(GDExtensionConstVariantPtr p_self) {
  394. const Variant *self = (const Variant *)p_self;
  395. return (GDExtensionVariantType)self->get_type();
  396. }
  397. static GDExtensionBool gdextension_variant_has_method(GDExtensionConstVariantPtr p_self, GDExtensionConstStringNamePtr p_method) {
  398. const Variant *self = (const Variant *)p_self;
  399. const StringName *method = (const StringName *)p_method;
  400. return self->has_method(*method);
  401. }
  402. static GDExtensionBool gdextension_variant_has_member(GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_member) {
  403. return Variant::has_member((Variant::Type)p_type, *((const StringName *)p_member));
  404. }
  405. static GDExtensionBool gdextension_variant_has_key(GDExtensionConstVariantPtr p_self, GDExtensionConstVariantPtr p_key, GDExtensionBool *r_valid) {
  406. const Variant *self = (const Variant *)p_self;
  407. const Variant *key = (const Variant *)p_key;
  408. bool valid;
  409. bool ret = self->has_key(*key, valid);
  410. *r_valid = valid;
  411. return ret;
  412. }
  413. static void gdextension_variant_get_type_name(GDExtensionVariantType p_type, GDExtensionUninitializedVariantPtr r_ret) {
  414. String name = Variant::get_type_name((Variant::Type)p_type);
  415. memnew_placement(r_ret, String(name));
  416. }
  417. static GDExtensionBool gdextension_variant_can_convert(GDExtensionVariantType p_from, GDExtensionVariantType p_to) {
  418. return Variant::can_convert((Variant::Type)p_from, (Variant::Type)p_to);
  419. }
  420. static GDExtensionBool gdextension_variant_can_convert_strict(GDExtensionVariantType p_from, GDExtensionVariantType p_to) {
  421. return Variant::can_convert_strict((Variant::Type)p_from, (Variant::Type)p_to);
  422. }
  423. // Variant interaction.
  424. static GDExtensionVariantFromTypeConstructorFunc gdextension_get_variant_from_type_constructor(GDExtensionVariantType p_type) {
  425. switch (p_type) {
  426. case GDEXTENSION_VARIANT_TYPE_BOOL:
  427. return VariantTypeConstructor<bool>::variant_from_type;
  428. case GDEXTENSION_VARIANT_TYPE_INT:
  429. return VariantTypeConstructor<int64_t>::variant_from_type;
  430. case GDEXTENSION_VARIANT_TYPE_FLOAT:
  431. return VariantTypeConstructor<double>::variant_from_type;
  432. case GDEXTENSION_VARIANT_TYPE_STRING:
  433. return VariantTypeConstructor<String>::variant_from_type;
  434. case GDEXTENSION_VARIANT_TYPE_VECTOR2:
  435. return VariantTypeConstructor<Vector2>::variant_from_type;
  436. case GDEXTENSION_VARIANT_TYPE_VECTOR2I:
  437. return VariantTypeConstructor<Vector2i>::variant_from_type;
  438. case GDEXTENSION_VARIANT_TYPE_RECT2:
  439. return VariantTypeConstructor<Rect2>::variant_from_type;
  440. case GDEXTENSION_VARIANT_TYPE_RECT2I:
  441. return VariantTypeConstructor<Rect2i>::variant_from_type;
  442. case GDEXTENSION_VARIANT_TYPE_VECTOR3:
  443. return VariantTypeConstructor<Vector3>::variant_from_type;
  444. case GDEXTENSION_VARIANT_TYPE_VECTOR3I:
  445. return VariantTypeConstructor<Vector3i>::variant_from_type;
  446. case GDEXTENSION_VARIANT_TYPE_TRANSFORM2D:
  447. return VariantTypeConstructor<Transform2D>::variant_from_type;
  448. case GDEXTENSION_VARIANT_TYPE_VECTOR4:
  449. return VariantTypeConstructor<Vector4>::variant_from_type;
  450. case GDEXTENSION_VARIANT_TYPE_VECTOR4I:
  451. return VariantTypeConstructor<Vector4i>::variant_from_type;
  452. case GDEXTENSION_VARIANT_TYPE_PLANE:
  453. return VariantTypeConstructor<Plane>::variant_from_type;
  454. case GDEXTENSION_VARIANT_TYPE_QUATERNION:
  455. return VariantTypeConstructor<Quaternion>::variant_from_type;
  456. case GDEXTENSION_VARIANT_TYPE_AABB:
  457. return VariantTypeConstructor<AABB>::variant_from_type;
  458. case GDEXTENSION_VARIANT_TYPE_BASIS:
  459. return VariantTypeConstructor<Basis>::variant_from_type;
  460. case GDEXTENSION_VARIANT_TYPE_TRANSFORM3D:
  461. return VariantTypeConstructor<Transform3D>::variant_from_type;
  462. case GDEXTENSION_VARIANT_TYPE_PROJECTION:
  463. return VariantTypeConstructor<Projection>::variant_from_type;
  464. case GDEXTENSION_VARIANT_TYPE_COLOR:
  465. return VariantTypeConstructor<Color>::variant_from_type;
  466. case GDEXTENSION_VARIANT_TYPE_STRING_NAME:
  467. return VariantTypeConstructor<StringName>::variant_from_type;
  468. case GDEXTENSION_VARIANT_TYPE_NODE_PATH:
  469. return VariantTypeConstructor<NodePath>::variant_from_type;
  470. case GDEXTENSION_VARIANT_TYPE_RID:
  471. return VariantTypeConstructor<RID>::variant_from_type;
  472. case GDEXTENSION_VARIANT_TYPE_OBJECT:
  473. return VariantTypeConstructor<Object *>::variant_from_type;
  474. case GDEXTENSION_VARIANT_TYPE_CALLABLE:
  475. return VariantTypeConstructor<Callable>::variant_from_type;
  476. case GDEXTENSION_VARIANT_TYPE_SIGNAL:
  477. return VariantTypeConstructor<Signal>::variant_from_type;
  478. case GDEXTENSION_VARIANT_TYPE_DICTIONARY:
  479. return VariantTypeConstructor<Dictionary>::variant_from_type;
  480. case GDEXTENSION_VARIANT_TYPE_ARRAY:
  481. return VariantTypeConstructor<Array>::variant_from_type;
  482. case GDEXTENSION_VARIANT_TYPE_PACKED_BYTE_ARRAY:
  483. return VariantTypeConstructor<PackedByteArray>::variant_from_type;
  484. case GDEXTENSION_VARIANT_TYPE_PACKED_INT32_ARRAY:
  485. return VariantTypeConstructor<PackedInt32Array>::variant_from_type;
  486. case GDEXTENSION_VARIANT_TYPE_PACKED_INT64_ARRAY:
  487. return VariantTypeConstructor<PackedInt64Array>::variant_from_type;
  488. case GDEXTENSION_VARIANT_TYPE_PACKED_FLOAT32_ARRAY:
  489. return VariantTypeConstructor<PackedFloat32Array>::variant_from_type;
  490. case GDEXTENSION_VARIANT_TYPE_PACKED_FLOAT64_ARRAY:
  491. return VariantTypeConstructor<PackedFloat64Array>::variant_from_type;
  492. case GDEXTENSION_VARIANT_TYPE_PACKED_STRING_ARRAY:
  493. return VariantTypeConstructor<PackedStringArray>::variant_from_type;
  494. case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR2_ARRAY:
  495. return VariantTypeConstructor<PackedVector2Array>::variant_from_type;
  496. case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR3_ARRAY:
  497. return VariantTypeConstructor<PackedVector3Array>::variant_from_type;
  498. case GDEXTENSION_VARIANT_TYPE_PACKED_COLOR_ARRAY:
  499. return VariantTypeConstructor<PackedColorArray>::variant_from_type;
  500. case GDEXTENSION_VARIANT_TYPE_NIL:
  501. case GDEXTENSION_VARIANT_TYPE_VARIANT_MAX:
  502. ERR_FAIL_V_MSG(nullptr, "Getting Variant conversion function with invalid type");
  503. }
  504. ERR_FAIL_V_MSG(nullptr, "Getting Variant conversion function with invalid type");
  505. }
  506. static GDExtensionTypeFromVariantConstructorFunc gdextension_get_variant_to_type_constructor(GDExtensionVariantType p_type) {
  507. switch (p_type) {
  508. case GDEXTENSION_VARIANT_TYPE_BOOL:
  509. return VariantTypeConstructor<bool>::type_from_variant;
  510. case GDEXTENSION_VARIANT_TYPE_INT:
  511. return VariantTypeConstructor<int64_t>::type_from_variant;
  512. case GDEXTENSION_VARIANT_TYPE_FLOAT:
  513. return VariantTypeConstructor<double>::type_from_variant;
  514. case GDEXTENSION_VARIANT_TYPE_STRING:
  515. return VariantTypeConstructor<String>::type_from_variant;
  516. case GDEXTENSION_VARIANT_TYPE_VECTOR2:
  517. return VariantTypeConstructor<Vector2>::type_from_variant;
  518. case GDEXTENSION_VARIANT_TYPE_VECTOR2I:
  519. return VariantTypeConstructor<Vector2i>::type_from_variant;
  520. case GDEXTENSION_VARIANT_TYPE_RECT2:
  521. return VariantTypeConstructor<Rect2>::type_from_variant;
  522. case GDEXTENSION_VARIANT_TYPE_RECT2I:
  523. return VariantTypeConstructor<Rect2i>::type_from_variant;
  524. case GDEXTENSION_VARIANT_TYPE_VECTOR3:
  525. return VariantTypeConstructor<Vector3>::type_from_variant;
  526. case GDEXTENSION_VARIANT_TYPE_VECTOR3I:
  527. return VariantTypeConstructor<Vector3i>::type_from_variant;
  528. case GDEXTENSION_VARIANT_TYPE_TRANSFORM2D:
  529. return VariantTypeConstructor<Transform2D>::type_from_variant;
  530. case GDEXTENSION_VARIANT_TYPE_VECTOR4:
  531. return VariantTypeConstructor<Vector4>::type_from_variant;
  532. case GDEXTENSION_VARIANT_TYPE_VECTOR4I:
  533. return VariantTypeConstructor<Vector4i>::type_from_variant;
  534. case GDEXTENSION_VARIANT_TYPE_PLANE:
  535. return VariantTypeConstructor<Plane>::type_from_variant;
  536. case GDEXTENSION_VARIANT_TYPE_QUATERNION:
  537. return VariantTypeConstructor<Quaternion>::type_from_variant;
  538. case GDEXTENSION_VARIANT_TYPE_AABB:
  539. return VariantTypeConstructor<AABB>::type_from_variant;
  540. case GDEXTENSION_VARIANT_TYPE_BASIS:
  541. return VariantTypeConstructor<Basis>::type_from_variant;
  542. case GDEXTENSION_VARIANT_TYPE_TRANSFORM3D:
  543. return VariantTypeConstructor<Transform3D>::type_from_variant;
  544. case GDEXTENSION_VARIANT_TYPE_PROJECTION:
  545. return VariantTypeConstructor<Projection>::type_from_variant;
  546. case GDEXTENSION_VARIANT_TYPE_COLOR:
  547. return VariantTypeConstructor<Color>::type_from_variant;
  548. case GDEXTENSION_VARIANT_TYPE_STRING_NAME:
  549. return VariantTypeConstructor<StringName>::type_from_variant;
  550. case GDEXTENSION_VARIANT_TYPE_NODE_PATH:
  551. return VariantTypeConstructor<NodePath>::type_from_variant;
  552. case GDEXTENSION_VARIANT_TYPE_RID:
  553. return VariantTypeConstructor<RID>::type_from_variant;
  554. case GDEXTENSION_VARIANT_TYPE_OBJECT:
  555. return VariantTypeConstructor<Object *>::type_from_variant;
  556. case GDEXTENSION_VARIANT_TYPE_CALLABLE:
  557. return VariantTypeConstructor<Callable>::type_from_variant;
  558. case GDEXTENSION_VARIANT_TYPE_SIGNAL:
  559. return VariantTypeConstructor<Signal>::type_from_variant;
  560. case GDEXTENSION_VARIANT_TYPE_DICTIONARY:
  561. return VariantTypeConstructor<Dictionary>::type_from_variant;
  562. case GDEXTENSION_VARIANT_TYPE_ARRAY:
  563. return VariantTypeConstructor<Array>::type_from_variant;
  564. case GDEXTENSION_VARIANT_TYPE_PACKED_BYTE_ARRAY:
  565. return VariantTypeConstructor<PackedByteArray>::type_from_variant;
  566. case GDEXTENSION_VARIANT_TYPE_PACKED_INT32_ARRAY:
  567. return VariantTypeConstructor<PackedInt32Array>::type_from_variant;
  568. case GDEXTENSION_VARIANT_TYPE_PACKED_INT64_ARRAY:
  569. return VariantTypeConstructor<PackedInt64Array>::type_from_variant;
  570. case GDEXTENSION_VARIANT_TYPE_PACKED_FLOAT32_ARRAY:
  571. return VariantTypeConstructor<PackedFloat32Array>::type_from_variant;
  572. case GDEXTENSION_VARIANT_TYPE_PACKED_FLOAT64_ARRAY:
  573. return VariantTypeConstructor<PackedFloat64Array>::type_from_variant;
  574. case GDEXTENSION_VARIANT_TYPE_PACKED_STRING_ARRAY:
  575. return VariantTypeConstructor<PackedStringArray>::type_from_variant;
  576. case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR2_ARRAY:
  577. return VariantTypeConstructor<PackedVector2Array>::type_from_variant;
  578. case GDEXTENSION_VARIANT_TYPE_PACKED_VECTOR3_ARRAY:
  579. return VariantTypeConstructor<PackedVector3Array>::type_from_variant;
  580. case GDEXTENSION_VARIANT_TYPE_PACKED_COLOR_ARRAY:
  581. return VariantTypeConstructor<PackedColorArray>::type_from_variant;
  582. case GDEXTENSION_VARIANT_TYPE_NIL:
  583. case GDEXTENSION_VARIANT_TYPE_VARIANT_MAX:
  584. ERR_FAIL_V_MSG(nullptr, "Getting Variant conversion function with invalid type");
  585. }
  586. ERR_FAIL_V_MSG(nullptr, "Getting Variant conversion function with invalid type");
  587. }
  588. // ptrcalls
  589. static GDExtensionPtrOperatorEvaluator gdextension_variant_get_ptr_operator_evaluator(GDExtensionVariantOperator p_operator, GDExtensionVariantType p_type_a, GDExtensionVariantType p_type_b) {
  590. return (GDExtensionPtrOperatorEvaluator)Variant::get_ptr_operator_evaluator(Variant::Operator(p_operator), Variant::Type(p_type_a), Variant::Type(p_type_b));
  591. }
  592. static GDExtensionPtrBuiltInMethod gdextension_variant_get_ptr_builtin_method(GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_method, GDExtensionInt p_hash) {
  593. const StringName method = *reinterpret_cast<const StringName *>(p_method);
  594. uint32_t hash = Variant::get_builtin_method_hash(Variant::Type(p_type), method);
  595. if (hash != p_hash) {
  596. ERR_PRINT_ONCE("Error getting method " + method + ", hash mismatch.");
  597. return nullptr;
  598. }
  599. return (GDExtensionPtrBuiltInMethod)Variant::get_ptr_builtin_method(Variant::Type(p_type), method);
  600. }
  601. static GDExtensionPtrConstructor gdextension_variant_get_ptr_constructor(GDExtensionVariantType p_type, int32_t p_constructor) {
  602. return (GDExtensionPtrConstructor)Variant::get_ptr_constructor(Variant::Type(p_type), p_constructor);
  603. }
  604. static GDExtensionPtrDestructor gdextension_variant_get_ptr_destructor(GDExtensionVariantType p_type) {
  605. return (GDExtensionPtrDestructor)Variant::get_ptr_destructor(Variant::Type(p_type));
  606. }
  607. static void gdextension_variant_construct(GDExtensionVariantType p_type, GDExtensionUninitializedVariantPtr r_base, const GDExtensionConstVariantPtr *p_args, int32_t p_argument_count, GDExtensionCallError *r_error) {
  608. memnew_placement(r_base, Variant);
  609. Variant *base = reinterpret_cast<Variant *>(r_base);
  610. Callable::CallError error;
  611. Variant::construct(Variant::Type(p_type), *base, (const Variant **)p_args, p_argument_count, error);
  612. if (r_error) {
  613. r_error->error = (GDExtensionCallErrorType)(error.error);
  614. r_error->argument = error.argument;
  615. r_error->expected = error.expected;
  616. }
  617. }
  618. static GDExtensionPtrSetter gdextension_variant_get_ptr_setter(GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_member) {
  619. const StringName member = *reinterpret_cast<const StringName *>(p_member);
  620. return (GDExtensionPtrSetter)Variant::get_member_ptr_setter(Variant::Type(p_type), member);
  621. }
  622. static GDExtensionPtrGetter gdextension_variant_get_ptr_getter(GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_member) {
  623. const StringName member = *reinterpret_cast<const StringName *>(p_member);
  624. return (GDExtensionPtrGetter)Variant::get_member_ptr_getter(Variant::Type(p_type), member);
  625. }
  626. static GDExtensionPtrIndexedSetter gdextension_variant_get_ptr_indexed_setter(GDExtensionVariantType p_type) {
  627. return (GDExtensionPtrIndexedSetter)Variant::get_member_ptr_indexed_setter(Variant::Type(p_type));
  628. }
  629. static GDExtensionPtrIndexedGetter gdextension_variant_get_ptr_indexed_getter(GDExtensionVariantType p_type) {
  630. return (GDExtensionPtrIndexedGetter)Variant::get_member_ptr_indexed_getter(Variant::Type(p_type));
  631. }
  632. static GDExtensionPtrKeyedSetter gdextension_variant_get_ptr_keyed_setter(GDExtensionVariantType p_type) {
  633. return (GDExtensionPtrKeyedSetter)Variant::get_member_ptr_keyed_setter(Variant::Type(p_type));
  634. }
  635. static GDExtensionPtrKeyedGetter gdextension_variant_get_ptr_keyed_getter(GDExtensionVariantType p_type) {
  636. return (GDExtensionPtrKeyedGetter)Variant::get_member_ptr_keyed_getter(Variant::Type(p_type));
  637. }
  638. static GDExtensionPtrKeyedChecker gdextension_variant_get_ptr_keyed_checker(GDExtensionVariantType p_type) {
  639. return (GDExtensionPtrKeyedChecker)Variant::get_member_ptr_keyed_checker(Variant::Type(p_type));
  640. }
  641. static void gdextension_variant_get_constant_value(GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_constant, GDExtensionUninitializedVariantPtr r_ret) {
  642. StringName constant = *reinterpret_cast<const StringName *>(p_constant);
  643. memnew_placement(r_ret, Variant(Variant::get_constant_value(Variant::Type(p_type), constant)));
  644. }
  645. static GDExtensionPtrUtilityFunction gdextension_variant_get_ptr_utility_function(GDExtensionConstStringNamePtr p_function, GDExtensionInt p_hash) {
  646. StringName function = *reinterpret_cast<const StringName *>(p_function);
  647. uint32_t hash = Variant::get_utility_function_hash(function);
  648. if (hash != p_hash) {
  649. ERR_PRINT_ONCE("Error getting utility function " + function + ", hash mismatch.");
  650. return nullptr;
  651. }
  652. return (GDExtensionPtrUtilityFunction)Variant::get_ptr_utility_function(function);
  653. }
  654. //string helpers
  655. static void gdextension_string_new_with_latin1_chars(GDExtensionUninitializedStringPtr r_dest, const char *p_contents) {
  656. memnew_placement(r_dest, String(p_contents));
  657. }
  658. static void gdextension_string_new_with_utf8_chars(GDExtensionUninitializedStringPtr r_dest, const char *p_contents) {
  659. memnew_placement(r_dest, String);
  660. String *dest = reinterpret_cast<String *>(r_dest);
  661. dest->parse_utf8(p_contents);
  662. }
  663. static void gdextension_string_new_with_utf16_chars(GDExtensionUninitializedStringPtr r_dest, const char16_t *p_contents) {
  664. memnew_placement(r_dest, String);
  665. String *dest = reinterpret_cast<String *>(r_dest);
  666. dest->parse_utf16(p_contents);
  667. }
  668. static void gdextension_string_new_with_utf32_chars(GDExtensionUninitializedStringPtr r_dest, const char32_t *p_contents) {
  669. memnew_placement(r_dest, String((const char32_t *)p_contents));
  670. }
  671. static void gdextension_string_new_with_wide_chars(GDExtensionUninitializedStringPtr r_dest, const wchar_t *p_contents) {
  672. if constexpr (sizeof(wchar_t) == 2) {
  673. // wchar_t is 16 bit, parse.
  674. memnew_placement(r_dest, String);
  675. String *dest = reinterpret_cast<String *>(r_dest);
  676. dest->parse_utf16((const char16_t *)p_contents);
  677. } else {
  678. // wchar_t is 32 bit, copy.
  679. memnew_placement(r_dest, String((const char32_t *)p_contents));
  680. }
  681. }
  682. static void gdextension_string_new_with_latin1_chars_and_len(GDExtensionUninitializedStringPtr r_dest, const char *p_contents, GDExtensionInt p_size) {
  683. memnew_placement(r_dest, String(p_contents, p_size));
  684. }
  685. static void gdextension_string_new_with_utf8_chars_and_len(GDExtensionUninitializedStringPtr r_dest, const char *p_contents, GDExtensionInt p_size) {
  686. memnew_placement(r_dest, String);
  687. String *dest = reinterpret_cast<String *>(r_dest);
  688. dest->parse_utf8(p_contents, p_size);
  689. }
  690. static void gdextension_string_new_with_utf16_chars_and_len(GDExtensionUninitializedStringPtr r_dest, const char16_t *p_contents, GDExtensionInt p_char_count) {
  691. memnew_placement(r_dest, String);
  692. String *dest = reinterpret_cast<String *>(r_dest);
  693. dest->parse_utf16(p_contents, p_char_count);
  694. }
  695. static void gdextension_string_new_with_utf32_chars_and_len(GDExtensionUninitializedStringPtr r_dest, const char32_t *p_contents, GDExtensionInt p_char_count) {
  696. memnew_placement(r_dest, String((const char32_t *)p_contents, p_char_count));
  697. }
  698. static void gdextension_string_new_with_wide_chars_and_len(GDExtensionUninitializedStringPtr r_dest, const wchar_t *p_contents, GDExtensionInt p_char_count) {
  699. if constexpr (sizeof(wchar_t) == 2) {
  700. // wchar_t is 16 bit, parse.
  701. memnew_placement(r_dest, String);
  702. String *dest = reinterpret_cast<String *>(r_dest);
  703. dest->parse_utf16((const char16_t *)p_contents, p_char_count);
  704. } else {
  705. // wchar_t is 32 bit, copy.
  706. memnew_placement(r_dest, String((const char32_t *)p_contents, p_char_count));
  707. }
  708. }
  709. static GDExtensionInt gdextension_string_to_latin1_chars(GDExtensionConstStringPtr p_self, char *r_text, GDExtensionInt p_max_write_length) {
  710. String *self = (String *)p_self;
  711. CharString cs = self->ascii(true);
  712. GDExtensionInt len = cs.length();
  713. if (r_text) {
  714. const char *s_text = cs.ptr();
  715. for (GDExtensionInt i = 0; i < MIN(len, p_max_write_length); i++) {
  716. r_text[i] = s_text[i];
  717. }
  718. }
  719. return len;
  720. }
  721. static GDExtensionInt gdextension_string_to_utf8_chars(GDExtensionConstStringPtr p_self, char *r_text, GDExtensionInt p_max_write_length) {
  722. String *self = (String *)p_self;
  723. CharString cs = self->utf8();
  724. GDExtensionInt len = cs.length();
  725. if (r_text) {
  726. const char *s_text = cs.ptr();
  727. for (GDExtensionInt i = 0; i < MIN(len, p_max_write_length); i++) {
  728. r_text[i] = s_text[i];
  729. }
  730. }
  731. return len;
  732. }
  733. static GDExtensionInt gdextension_string_to_utf16_chars(GDExtensionConstStringPtr p_self, char16_t *r_text, GDExtensionInt p_max_write_length) {
  734. String *self = (String *)p_self;
  735. Char16String cs = self->utf16();
  736. GDExtensionInt len = cs.length();
  737. if (r_text) {
  738. const char16_t *s_text = cs.ptr();
  739. for (GDExtensionInt i = 0; i < MIN(len, p_max_write_length); i++) {
  740. r_text[i] = s_text[i];
  741. }
  742. }
  743. return len;
  744. }
  745. static GDExtensionInt gdextension_string_to_utf32_chars(GDExtensionConstStringPtr p_self, char32_t *r_text, GDExtensionInt p_max_write_length) {
  746. String *self = (String *)p_self;
  747. GDExtensionInt len = self->length();
  748. if (r_text) {
  749. const char32_t *s_text = self->ptr();
  750. for (GDExtensionInt i = 0; i < MIN(len, p_max_write_length); i++) {
  751. r_text[i] = s_text[i];
  752. }
  753. }
  754. return len;
  755. }
  756. static GDExtensionInt gdextension_string_to_wide_chars(GDExtensionConstStringPtr p_self, wchar_t *r_text, GDExtensionInt p_max_write_length) {
  757. if constexpr (sizeof(wchar_t) == 4) {
  758. return gdextension_string_to_utf32_chars(p_self, (char32_t *)r_text, p_max_write_length);
  759. } else {
  760. return gdextension_string_to_utf16_chars(p_self, (char16_t *)r_text, p_max_write_length);
  761. }
  762. }
  763. static char32_t *gdextension_string_operator_index(GDExtensionStringPtr p_self, GDExtensionInt p_index) {
  764. String *self = (String *)p_self;
  765. if (unlikely(p_index < 0 || p_index >= self->length() + 1)) {
  766. return nullptr;
  767. }
  768. return &self->ptrw()[p_index];
  769. }
  770. static const char32_t *gdextension_string_operator_index_const(GDExtensionConstStringPtr p_self, GDExtensionInt p_index) {
  771. const String *self = (const String *)p_self;
  772. if (unlikely(p_index < 0 || p_index >= self->length() + 1)) {
  773. return nullptr;
  774. }
  775. return &self->ptr()[p_index];
  776. }
  777. static void gdextension_string_operator_plus_eq_string(GDExtensionStringPtr p_self, GDExtensionConstStringPtr p_b) {
  778. String *self = (String *)p_self;
  779. const String *b = (const String *)p_b;
  780. *self += *b;
  781. }
  782. static void gdextension_string_operator_plus_eq_char(GDExtensionStringPtr p_self, char32_t p_b) {
  783. String *self = (String *)p_self;
  784. *self += p_b;
  785. }
  786. static void gdextension_string_operator_plus_eq_cstr(GDExtensionStringPtr p_self, const char *p_b) {
  787. String *self = (String *)p_self;
  788. *self += p_b;
  789. }
  790. static void gdextension_string_operator_plus_eq_wcstr(GDExtensionStringPtr p_self, const wchar_t *p_b) {
  791. String *self = (String *)p_self;
  792. *self += p_b;
  793. }
  794. static void gdextension_string_operator_plus_eq_c32str(GDExtensionStringPtr p_self, const char32_t *p_b) {
  795. String *self = (String *)p_self;
  796. *self += p_b;
  797. }
  798. static GDExtensionInt gdextension_string_resize(GDExtensionStringPtr p_self, GDExtensionInt p_length) {
  799. String *self = (String *)p_self;
  800. return (*self).resize(p_length);
  801. }
  802. static void gdextension_string_name_new_with_latin1_chars(GDExtensionUninitializedStringNamePtr r_dest, const char *p_contents, GDExtensionBool p_is_static) {
  803. memnew_placement(r_dest, StringName(p_contents, static_cast<bool>(p_is_static)));
  804. }
  805. static void gdextension_string_name_new_with_utf8_chars(GDExtensionUninitializedStringNamePtr r_dest, const char *p_contents) {
  806. String tmp;
  807. tmp.parse_utf8(p_contents);
  808. memnew_placement(r_dest, StringName(tmp));
  809. }
  810. static void gdextension_string_name_new_with_utf8_chars_and_len(GDExtensionUninitializedStringNamePtr r_dest, const char *p_contents, GDExtensionInt p_size) {
  811. String tmp;
  812. tmp.parse_utf8(p_contents, p_size);
  813. memnew_placement(r_dest, StringName(tmp));
  814. }
  815. static GDExtensionInt gdextension_xml_parser_open_buffer(GDExtensionObjectPtr p_instance, const uint8_t *p_buffer, size_t p_size) {
  816. XMLParser *xml = (XMLParser *)p_instance;
  817. return (GDExtensionInt)xml->_open_buffer(p_buffer, p_size);
  818. }
  819. static void gdextension_file_access_store_buffer(GDExtensionObjectPtr p_instance, const uint8_t *p_src, uint64_t p_length) {
  820. FileAccess *fa = (FileAccess *)p_instance;
  821. fa->store_buffer(p_src, p_length);
  822. }
  823. static uint64_t gdextension_file_access_get_buffer(GDExtensionConstObjectPtr p_instance, uint8_t *p_dst, uint64_t p_length) {
  824. const FileAccess *fa = (FileAccess *)p_instance;
  825. return fa->get_buffer(p_dst, p_length);
  826. }
  827. static int64_t gdextension_worker_thread_pool_add_native_group_task(GDExtensionObjectPtr p_instance, void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks, GDExtensionBool p_high_priority, GDExtensionConstStringPtr p_description) {
  828. WorkerThreadPool *p = (WorkerThreadPool *)p_instance;
  829. const String *description = (const String *)p_description;
  830. return (int64_t)p->add_native_group_task(p_func, p_userdata, p_elements, p_tasks, static_cast<bool>(p_high_priority), *description);
  831. }
  832. static int64_t gdextension_worker_thread_pool_add_native_task(GDExtensionObjectPtr p_instance, void (*p_func)(void *), void *p_userdata, GDExtensionBool p_high_priority, GDExtensionConstStringPtr p_description) {
  833. WorkerThreadPool *p = (WorkerThreadPool *)p_instance;
  834. const String *description = (const String *)p_description;
  835. return (int64_t)p->add_native_task(p_func, p_userdata, static_cast<bool>(p_high_priority), *description);
  836. }
  837. /* Packed array functions */
  838. static uint8_t *gdextension_packed_byte_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  839. PackedByteArray *self = (PackedByteArray *)p_self;
  840. if (unlikely(p_index < 0 || p_index >= self->size())) {
  841. return nullptr;
  842. }
  843. return &self->ptrw()[p_index];
  844. }
  845. static const uint8_t *gdextension_packed_byte_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  846. const PackedByteArray *self = (const PackedByteArray *)p_self;
  847. if (unlikely(p_index < 0 || p_index >= self->size())) {
  848. return nullptr;
  849. }
  850. return &self->ptr()[p_index];
  851. }
  852. static GDExtensionTypePtr gdextension_packed_color_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  853. PackedColorArray *self = (PackedColorArray *)p_self;
  854. if (unlikely(p_index < 0 || p_index >= self->size())) {
  855. return nullptr;
  856. }
  857. return (GDExtensionTypePtr)&self->ptrw()[p_index];
  858. }
  859. static GDExtensionTypePtr gdextension_packed_color_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  860. const PackedColorArray *self = (const PackedColorArray *)p_self;
  861. if (unlikely(p_index < 0 || p_index >= self->size())) {
  862. return nullptr;
  863. }
  864. return (GDExtensionTypePtr)&self->ptr()[p_index];
  865. }
  866. static float *gdextension_packed_float32_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  867. PackedFloat32Array *self = (PackedFloat32Array *)p_self;
  868. if (unlikely(p_index < 0 || p_index >= self->size())) {
  869. return nullptr;
  870. }
  871. return &self->ptrw()[p_index];
  872. }
  873. static const float *gdextension_packed_float32_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  874. const PackedFloat32Array *self = (const PackedFloat32Array *)p_self;
  875. if (unlikely(p_index < 0 || p_index >= self->size())) {
  876. return nullptr;
  877. }
  878. return &self->ptr()[p_index];
  879. }
  880. static double *gdextension_packed_float64_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  881. PackedFloat64Array *self = (PackedFloat64Array *)p_self;
  882. if (unlikely(p_index < 0 || p_index >= self->size())) {
  883. return nullptr;
  884. }
  885. return &self->ptrw()[p_index];
  886. }
  887. static const double *gdextension_packed_float64_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  888. const PackedFloat64Array *self = (const PackedFloat64Array *)p_self;
  889. if (unlikely(p_index < 0 || p_index >= self->size())) {
  890. return nullptr;
  891. }
  892. return &self->ptr()[p_index];
  893. }
  894. static int32_t *gdextension_packed_int32_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  895. PackedInt32Array *self = (PackedInt32Array *)p_self;
  896. if (unlikely(p_index < 0 || p_index >= self->size())) {
  897. return nullptr;
  898. }
  899. return &self->ptrw()[p_index];
  900. }
  901. static const int32_t *gdextension_packed_int32_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  902. const PackedInt32Array *self = (const PackedInt32Array *)p_self;
  903. if (unlikely(p_index < 0 || p_index >= self->size())) {
  904. return nullptr;
  905. }
  906. return &self->ptr()[p_index];
  907. }
  908. static int64_t *gdextension_packed_int64_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  909. PackedInt64Array *self = (PackedInt64Array *)p_self;
  910. if (unlikely(p_index < 0 || p_index >= self->size())) {
  911. return nullptr;
  912. }
  913. return &self->ptrw()[p_index];
  914. }
  915. static const int64_t *gdextension_packed_int64_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  916. const PackedInt64Array *self = (const PackedInt64Array *)p_self;
  917. if (unlikely(p_index < 0 || p_index >= self->size())) {
  918. return nullptr;
  919. }
  920. return &self->ptr()[p_index];
  921. }
  922. static GDExtensionStringPtr gdextension_packed_string_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  923. PackedStringArray *self = (PackedStringArray *)p_self;
  924. if (unlikely(p_index < 0 || p_index >= self->size())) {
  925. return nullptr;
  926. }
  927. return (GDExtensionStringPtr)&self->ptrw()[p_index];
  928. }
  929. static GDExtensionStringPtr gdextension_packed_string_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  930. const PackedStringArray *self = (const PackedStringArray *)p_self;
  931. if (unlikely(p_index < 0 || p_index >= self->size())) {
  932. return nullptr;
  933. }
  934. return (GDExtensionStringPtr)&self->ptr()[p_index];
  935. }
  936. static GDExtensionTypePtr gdextension_packed_vector2_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  937. PackedVector2Array *self = (PackedVector2Array *)p_self;
  938. if (unlikely(p_index < 0 || p_index >= self->size())) {
  939. return nullptr;
  940. }
  941. return (GDExtensionTypePtr)&self->ptrw()[p_index];
  942. }
  943. static GDExtensionTypePtr gdextension_packed_vector2_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  944. const PackedVector2Array *self = (const PackedVector2Array *)p_self;
  945. if (unlikely(p_index < 0 || p_index >= self->size())) {
  946. return nullptr;
  947. }
  948. return (GDExtensionTypePtr)&self->ptr()[p_index];
  949. }
  950. static GDExtensionTypePtr gdextension_packed_vector3_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  951. PackedVector3Array *self = (PackedVector3Array *)p_self;
  952. if (unlikely(p_index < 0 || p_index >= self->size())) {
  953. return nullptr;
  954. }
  955. return (GDExtensionTypePtr)&self->ptrw()[p_index];
  956. }
  957. static GDExtensionTypePtr gdextension_packed_vector3_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  958. const PackedVector3Array *self = (const PackedVector3Array *)p_self;
  959. if (unlikely(p_index < 0 || p_index >= self->size())) {
  960. return nullptr;
  961. }
  962. return (GDExtensionTypePtr)&self->ptr()[p_index];
  963. }
  964. static GDExtensionVariantPtr gdextension_array_operator_index(GDExtensionTypePtr p_self, GDExtensionInt p_index) {
  965. Array *self = (Array *)p_self;
  966. if (unlikely(p_index < 0 || p_index >= self->size())) {
  967. return nullptr;
  968. }
  969. return (GDExtensionVariantPtr)&self->operator[](p_index);
  970. }
  971. static GDExtensionVariantPtr gdextension_array_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionInt p_index) {
  972. const Array *self = (const Array *)p_self;
  973. if (unlikely(p_index < 0 || p_index >= self->size())) {
  974. return nullptr;
  975. }
  976. return (GDExtensionVariantPtr)&self->operator[](p_index);
  977. }
  978. void gdextension_array_ref(GDExtensionTypePtr p_self, GDExtensionConstTypePtr p_from) {
  979. Array *self = (Array *)p_self;
  980. const Array *from = (const Array *)p_from;
  981. self->_ref(*from);
  982. }
  983. void gdextension_array_set_typed(GDExtensionTypePtr p_self, GDExtensionVariantType p_type, GDExtensionConstStringNamePtr p_class_name, GDExtensionConstVariantPtr p_script) {
  984. Array *self = reinterpret_cast<Array *>(p_self);
  985. const StringName *class_name = reinterpret_cast<const StringName *>(p_class_name);
  986. const Variant *script = reinterpret_cast<const Variant *>(p_script);
  987. self->set_typed((uint32_t)p_type, *class_name, *script);
  988. }
  989. /* Dictionary functions */
  990. static GDExtensionVariantPtr gdextension_dictionary_operator_index(GDExtensionTypePtr p_self, GDExtensionConstVariantPtr p_key) {
  991. Dictionary *self = (Dictionary *)p_self;
  992. return (GDExtensionVariantPtr)&self->operator[](*(const Variant *)p_key);
  993. }
  994. static GDExtensionVariantPtr gdextension_dictionary_operator_index_const(GDExtensionConstTypePtr p_self, GDExtensionConstVariantPtr p_key) {
  995. const Dictionary *self = (const Dictionary *)p_self;
  996. return (GDExtensionVariantPtr)&self->operator[](*(const Variant *)p_key);
  997. }
  998. /* OBJECT API */
  999. static void gdextension_object_method_bind_call(GDExtensionMethodBindPtr p_method_bind, GDExtensionObjectPtr p_instance, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_arg_count, GDExtensionUninitializedVariantPtr r_return, GDExtensionCallError *r_error) {
  1000. const MethodBind *mb = reinterpret_cast<const MethodBind *>(p_method_bind);
  1001. Object *o = (Object *)p_instance;
  1002. const Variant **args = (const Variant **)p_args;
  1003. Callable::CallError error;
  1004. memnew_placement(r_return, Variant(mb->call(o, args, p_arg_count, error)));
  1005. if (r_error) {
  1006. r_error->error = (GDExtensionCallErrorType)(error.error);
  1007. r_error->argument = error.argument;
  1008. r_error->expected = error.expected;
  1009. }
  1010. }
  1011. static void gdextension_object_method_bind_ptrcall(GDExtensionMethodBindPtr p_method_bind, GDExtensionObjectPtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr p_ret) {
  1012. const MethodBind *mb = reinterpret_cast<const MethodBind *>(p_method_bind);
  1013. Object *o = (Object *)p_instance;
  1014. mb->ptrcall(o, (const void **)p_args, p_ret);
  1015. }
  1016. static void gdextension_object_destroy(GDExtensionObjectPtr p_o) {
  1017. memdelete((Object *)p_o);
  1018. }
  1019. static GDExtensionObjectPtr gdextension_global_get_singleton(GDExtensionConstStringNamePtr p_name) {
  1020. const StringName name = *reinterpret_cast<const StringName *>(p_name);
  1021. return (GDExtensionObjectPtr)Engine::get_singleton()->get_singleton_object(name);
  1022. }
  1023. static void *gdextension_object_get_instance_binding(GDExtensionObjectPtr p_object, void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1024. Object *o = (Object *)p_object;
  1025. return o->get_instance_binding(p_token, p_callbacks);
  1026. }
  1027. static void gdextension_object_set_instance_binding(GDExtensionObjectPtr p_object, void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1028. Object *o = (Object *)p_object;
  1029. o->set_instance_binding(p_token, p_binding, p_callbacks);
  1030. }
  1031. static void gdextension_object_free_instance_binding(GDExtensionObjectPtr p_object, void *p_token) {
  1032. Object *o = (Object *)p_object;
  1033. o->free_instance_binding(p_token);
  1034. }
  1035. static void gdextension_object_set_instance(GDExtensionObjectPtr p_object, GDExtensionConstStringNamePtr p_classname, GDExtensionClassInstancePtr p_instance) {
  1036. const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
  1037. Object *o = (Object *)p_object;
  1038. ClassDB::set_object_extension_instance(o, classname, p_instance);
  1039. }
  1040. static GDExtensionObjectPtr gdextension_object_get_instance_from_id(GDObjectInstanceID p_instance_id) {
  1041. return (GDExtensionObjectPtr)ObjectDB::get_instance(ObjectID(p_instance_id));
  1042. }
  1043. static GDExtensionBool gdextension_object_get_class_name(GDExtensionConstObjectPtr p_object, GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringNamePtr r_class_name) {
  1044. if (!p_object) {
  1045. return false;
  1046. }
  1047. const Object *o = (const Object *)p_object;
  1048. memnew_placement(r_class_name, StringName);
  1049. StringName *class_name = reinterpret_cast<StringName *>(r_class_name);
  1050. *class_name = o->get_class_name_for_extension((GDExtension *)p_library);
  1051. return true;
  1052. }
  1053. static GDExtensionObjectPtr gdextension_object_cast_to(GDExtensionConstObjectPtr p_object, void *p_class_tag) {
  1054. if (!p_object) {
  1055. return nullptr;
  1056. }
  1057. Object *o = (Object *)p_object;
  1058. return o->is_class_ptr(p_class_tag) ? (GDExtensionObjectPtr)o : (GDExtensionObjectPtr) nullptr;
  1059. }
  1060. static GDObjectInstanceID gdextension_object_get_instance_id(GDExtensionConstObjectPtr p_object) {
  1061. const Object *o = (const Object *)p_object;
  1062. return (GDObjectInstanceID)o->get_instance_id();
  1063. }
  1064. static GDExtensionBool gdextension_object_has_script_method(GDExtensionConstObjectPtr p_object, GDExtensionConstStringNamePtr p_method) {
  1065. Object *o = (Object *)p_object;
  1066. const StringName method = *reinterpret_cast<const StringName *>(p_method);
  1067. ScriptInstance *script_instance = o->get_script_instance();
  1068. if (script_instance) {
  1069. return script_instance->has_method(method);
  1070. }
  1071. return false;
  1072. }
  1073. static void gdextension_object_call_script_method(GDExtensionObjectPtr p_object, GDExtensionConstStringNamePtr p_method, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionUninitializedVariantPtr r_return, GDExtensionCallError *r_error) {
  1074. Object *o = (Object *)p_object;
  1075. const StringName method = *reinterpret_cast<const StringName *>(p_method);
  1076. const Variant **args = (const Variant **)p_args;
  1077. Callable::CallError error;
  1078. memnew_placement(r_return, Variant);
  1079. *(Variant *)r_return = o->callp(method, args, p_argument_count, error);
  1080. if (r_error) {
  1081. r_error->error = (GDExtensionCallErrorType)(error.error);
  1082. r_error->argument = error.argument;
  1083. r_error->expected = error.expected;
  1084. }
  1085. }
  1086. static GDExtensionObjectPtr gdextension_ref_get_object(GDExtensionConstRefPtr p_ref) {
  1087. const Ref<RefCounted> *ref = (const Ref<RefCounted> *)p_ref;
  1088. if (ref == nullptr || ref->is_null()) {
  1089. return (GDExtensionObjectPtr) nullptr;
  1090. } else {
  1091. return (GDExtensionObjectPtr)ref->ptr();
  1092. }
  1093. }
  1094. static void gdextension_ref_set_object(GDExtensionRefPtr p_ref, GDExtensionObjectPtr p_object) {
  1095. Ref<RefCounted> *ref = (Ref<RefCounted> *)p_ref;
  1096. ERR_FAIL_NULL(ref);
  1097. Object *o = (RefCounted *)p_object;
  1098. ref->reference_ptr(o);
  1099. }
  1100. #ifndef DISABLE_DEPRECATED
  1101. static GDExtensionScriptInstancePtr gdextension_script_instance_create(const GDExtensionScriptInstanceInfo *p_info, GDExtensionScriptInstanceDataPtr p_instance_data) {
  1102. GDExtensionScriptInstanceInfo3 *info_3 = memnew(GDExtensionScriptInstanceInfo3);
  1103. info_3->set_func = p_info->set_func;
  1104. info_3->get_func = p_info->get_func;
  1105. info_3->get_property_list_func = p_info->get_property_list_func;
  1106. info_3->free_property_list_func = nullptr;
  1107. info_3->get_class_category_func = nullptr;
  1108. info_3->property_can_revert_func = p_info->property_can_revert_func;
  1109. info_3->property_get_revert_func = p_info->property_get_revert_func;
  1110. info_3->get_owner_func = p_info->get_owner_func;
  1111. info_3->get_property_state_func = p_info->get_property_state_func;
  1112. info_3->get_method_list_func = p_info->get_method_list_func;
  1113. info_3->free_method_list_func = nullptr;
  1114. info_3->get_property_type_func = p_info->get_property_type_func;
  1115. info_3->validate_property_func = nullptr;
  1116. info_3->has_method_func = p_info->has_method_func;
  1117. info_3->get_method_argument_count_func = nullptr;
  1118. info_3->call_func = p_info->call_func;
  1119. info_3->notification_func = nullptr;
  1120. info_3->to_string_func = p_info->to_string_func;
  1121. info_3->refcount_incremented_func = p_info->refcount_incremented_func;
  1122. info_3->refcount_decremented_func = p_info->refcount_decremented_func;
  1123. info_3->get_script_func = p_info->get_script_func;
  1124. info_3->is_placeholder_func = p_info->is_placeholder_func;
  1125. info_3->set_fallback_func = p_info->set_fallback_func;
  1126. info_3->get_fallback_func = p_info->get_fallback_func;
  1127. info_3->get_language_func = p_info->get_language_func;
  1128. info_3->free_func = p_info->free_func;
  1129. ScriptInstanceExtension *script_instance_extension = memnew(ScriptInstanceExtension);
  1130. script_instance_extension->instance = p_instance_data;
  1131. script_instance_extension->native_info = info_3;
  1132. script_instance_extension->free_native_info = true;
  1133. script_instance_extension->deprecated_native_info = memnew(ScriptInstanceExtension::DeprecatedNativeInfo);
  1134. script_instance_extension->deprecated_native_info->notification_func = p_info->notification_func;
  1135. script_instance_extension->deprecated_native_info->free_property_list_func = p_info->free_property_list_func;
  1136. script_instance_extension->deprecated_native_info->free_method_list_func = p_info->free_method_list_func;
  1137. return reinterpret_cast<GDExtensionScriptInstancePtr>(script_instance_extension);
  1138. }
  1139. static GDExtensionScriptInstancePtr gdextension_script_instance_create2(const GDExtensionScriptInstanceInfo2 *p_info, GDExtensionScriptInstanceDataPtr p_instance_data) {
  1140. GDExtensionScriptInstanceInfo3 *info_3 = memnew(GDExtensionScriptInstanceInfo3);
  1141. info_3->set_func = p_info->set_func;
  1142. info_3->get_func = p_info->get_func;
  1143. info_3->get_property_list_func = p_info->get_property_list_func;
  1144. info_3->free_property_list_func = nullptr;
  1145. info_3->get_class_category_func = nullptr;
  1146. info_3->property_can_revert_func = p_info->property_can_revert_func;
  1147. info_3->property_get_revert_func = p_info->property_get_revert_func;
  1148. info_3->get_owner_func = p_info->get_owner_func;
  1149. info_3->get_property_state_func = p_info->get_property_state_func;
  1150. info_3->get_method_list_func = p_info->get_method_list_func;
  1151. info_3->free_method_list_func = nullptr;
  1152. info_3->get_property_type_func = p_info->get_property_type_func;
  1153. info_3->validate_property_func = nullptr;
  1154. info_3->has_method_func = p_info->has_method_func;
  1155. info_3->get_method_argument_count_func = nullptr;
  1156. info_3->call_func = p_info->call_func;
  1157. info_3->notification_func = p_info->notification_func;
  1158. info_3->to_string_func = p_info->to_string_func;
  1159. info_3->refcount_incremented_func = p_info->refcount_incremented_func;
  1160. info_3->refcount_decremented_func = p_info->refcount_decremented_func;
  1161. info_3->get_script_func = p_info->get_script_func;
  1162. info_3->is_placeholder_func = p_info->is_placeholder_func;
  1163. info_3->set_fallback_func = p_info->set_fallback_func;
  1164. info_3->get_fallback_func = p_info->get_fallback_func;
  1165. info_3->get_language_func = p_info->get_language_func;
  1166. info_3->free_func = p_info->free_func;
  1167. ScriptInstanceExtension *script_instance_extension = memnew(ScriptInstanceExtension);
  1168. script_instance_extension->instance = p_instance_data;
  1169. script_instance_extension->native_info = info_3;
  1170. script_instance_extension->free_native_info = true;
  1171. script_instance_extension->deprecated_native_info = memnew(ScriptInstanceExtension::DeprecatedNativeInfo);
  1172. script_instance_extension->deprecated_native_info->free_property_list_func = p_info->free_property_list_func;
  1173. script_instance_extension->deprecated_native_info->free_method_list_func = p_info->free_method_list_func;
  1174. return reinterpret_cast<GDExtensionScriptInstancePtr>(script_instance_extension);
  1175. }
  1176. #endif // DISABLE_DEPRECATED
  1177. static GDExtensionScriptInstancePtr gdextension_script_instance_create3(const GDExtensionScriptInstanceInfo3 *p_info, GDExtensionScriptInstanceDataPtr p_instance_data) {
  1178. ScriptInstanceExtension *script_instance_extension = memnew(ScriptInstanceExtension);
  1179. script_instance_extension->instance = p_instance_data;
  1180. script_instance_extension->native_info = p_info;
  1181. return reinterpret_cast<GDExtensionScriptInstancePtr>(script_instance_extension);
  1182. }
  1183. static GDExtensionScriptInstancePtr gdextension_placeholder_script_instance_create(GDExtensionObjectPtr p_language, GDExtensionObjectPtr p_script, GDExtensionObjectPtr p_owner) {
  1184. ScriptLanguage *language = (ScriptLanguage *)p_language;
  1185. Ref<Script> script;
  1186. script.reference_ptr((Script *)p_script);
  1187. Object *owner = (Object *)p_owner;
  1188. PlaceHolderScriptInstance *placeholder = memnew(PlaceHolderScriptInstance(language, script, owner));
  1189. return reinterpret_cast<GDExtensionScriptInstancePtr>(placeholder);
  1190. }
  1191. static void gdextension_placeholder_script_instance_update(GDExtensionScriptInstancePtr p_placeholder, GDExtensionConstTypePtr p_properties, GDExtensionConstTypePtr p_values) {
  1192. PlaceHolderScriptInstance *placeholder = dynamic_cast<PlaceHolderScriptInstance *>(reinterpret_cast<ScriptInstance *>(p_placeholder));
  1193. ERR_FAIL_NULL_MSG(placeholder, "Unable to update placeholder, expected a PlaceHolderScriptInstance but received an invalid type.");
  1194. const Array &properties = *reinterpret_cast<const Array *>(p_properties);
  1195. const Dictionary &values = *reinterpret_cast<const Dictionary *>(p_values);
  1196. List<PropertyInfo> properties_list;
  1197. HashMap<StringName, Variant> values_map;
  1198. for (int i = 0; i < properties.size(); i++) {
  1199. Dictionary d = properties[i];
  1200. properties_list.push_back(PropertyInfo::from_dict(d));
  1201. }
  1202. List<Variant> keys;
  1203. values.get_key_list(&keys);
  1204. for (const Variant &E : keys) {
  1205. values_map.insert(E, values[E]);
  1206. }
  1207. placeholder->update(properties_list, values_map);
  1208. }
  1209. static GDExtensionScriptInstancePtr gdextension_object_get_script_instance(GDExtensionConstObjectPtr p_object, GDExtensionConstObjectPtr p_language) {
  1210. if (!p_object || !p_language) {
  1211. return nullptr;
  1212. }
  1213. const Object *o = (const Object *)p_object;
  1214. ScriptInstanceExtension *script_instance_extension = reinterpret_cast<ScriptInstanceExtension *>(o->get_script_instance());
  1215. if (!script_instance_extension) {
  1216. return nullptr;
  1217. }
  1218. const ScriptLanguage *language = script_instance_extension->get_language();
  1219. if (language != p_language) {
  1220. return nullptr;
  1221. }
  1222. return script_instance_extension->instance;
  1223. }
  1224. #ifndef DISABLE_DEPRECATED
  1225. static void gdextension_callable_custom_create(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_custom_callable_info) {
  1226. memnew_placement(r_callable, Callable(memnew(CallableCustomExtension(p_custom_callable_info))));
  1227. }
  1228. #endif
  1229. static void gdextension_callable_custom_create2(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo2 *p_custom_callable_info) {
  1230. memnew_placement(r_callable, Callable(memnew(CallableCustomExtension(p_custom_callable_info))));
  1231. }
  1232. static void *gdextension_callable_custom_get_userdata(GDExtensionTypePtr p_callable, void *p_token) {
  1233. const Callable &callable = *reinterpret_cast<const Callable *>(p_callable);
  1234. if (!callable.is_custom()) {
  1235. return nullptr;
  1236. }
  1237. const CallableCustomExtension *custom_callable = dynamic_cast<const CallableCustomExtension *>(callable.get_custom());
  1238. if (!custom_callable) {
  1239. return nullptr;
  1240. }
  1241. return custom_callable->get_userdata(p_token);
  1242. }
  1243. static GDExtensionMethodBindPtr gdextension_classdb_get_method_bind(GDExtensionConstStringNamePtr p_classname, GDExtensionConstStringNamePtr p_methodname, GDExtensionInt p_hash) {
  1244. const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
  1245. const StringName methodname = *reinterpret_cast<const StringName *>(p_methodname);
  1246. bool exists = false;
  1247. MethodBind *mb = ClassDB::get_method_with_compatibility(classname, methodname, p_hash, &exists);
  1248. #ifndef DISABLE_DEPRECATED
  1249. // If lookup failed, see if this is one of the broken hashes from issue #81386.
  1250. if (!mb && exists) {
  1251. uint32_t mapped_hash;
  1252. if (GDExtensionCompatHashes::lookup_current_hash(classname, methodname, p_hash, &mapped_hash)) {
  1253. mb = ClassDB::get_method_with_compatibility(classname, methodname, mapped_hash, &exists);
  1254. }
  1255. }
  1256. #endif
  1257. if (!mb && exists) {
  1258. ERR_PRINT("Method '" + classname + "." + methodname + "' has changed and no compatibility fallback has been provided. Please open an issue.");
  1259. return nullptr;
  1260. }
  1261. ERR_FAIL_NULL_V(mb, nullptr);
  1262. return (GDExtensionMethodBindPtr)mb;
  1263. }
  1264. static GDExtensionObjectPtr gdextension_classdb_construct_object(GDExtensionConstStringNamePtr p_classname) {
  1265. const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
  1266. return (GDExtensionObjectPtr)ClassDB::instantiate_no_placeholders(classname);
  1267. }
  1268. static void *gdextension_classdb_get_class_tag(GDExtensionConstStringNamePtr p_classname) {
  1269. const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
  1270. ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(classname);
  1271. return class_info ? class_info->class_ptr : nullptr;
  1272. }
  1273. static void gdextension_editor_add_plugin(GDExtensionConstStringNamePtr p_classname) {
  1274. #ifdef TOOLS_ENABLED
  1275. const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
  1276. GDExtensionEditorPlugins::add_extension_class(classname);
  1277. #endif
  1278. }
  1279. static void gdextension_editor_remove_plugin(GDExtensionConstStringNamePtr p_classname) {
  1280. #ifdef TOOLS_ENABLED
  1281. const StringName classname = *reinterpret_cast<const StringName *>(p_classname);
  1282. GDExtensionEditorPlugins::remove_extension_class(classname);
  1283. #endif
  1284. }
  1285. static void gdextension_editor_help_load_xml_from_utf8_chars_and_len(const char *p_data, GDExtensionInt p_size) {
  1286. #ifdef TOOLS_ENABLED
  1287. GDExtensionEditorHelp::load_xml_buffer((const uint8_t *)p_data, p_size);
  1288. #endif
  1289. }
  1290. static void gdextension_editor_help_load_xml_from_utf8_chars(const char *p_data) {
  1291. #ifdef TOOLS_ENABLED
  1292. size_t len = strlen(p_data);
  1293. gdextension_editor_help_load_xml_from_utf8_chars_and_len(p_data, len);
  1294. #endif
  1295. }
  1296. #define REGISTER_INTERFACE_FUNC(m_name) GDExtension::register_interface_function(#m_name, (GDExtensionInterfaceFunctionPtr) & gdextension_##m_name)
  1297. void gdextension_setup_interface() {
  1298. REGISTER_INTERFACE_FUNC(get_godot_version);
  1299. REGISTER_INTERFACE_FUNC(mem_alloc);
  1300. REGISTER_INTERFACE_FUNC(mem_realloc);
  1301. REGISTER_INTERFACE_FUNC(mem_free);
  1302. REGISTER_INTERFACE_FUNC(print_error);
  1303. REGISTER_INTERFACE_FUNC(print_error_with_message);
  1304. REGISTER_INTERFACE_FUNC(print_warning);
  1305. REGISTER_INTERFACE_FUNC(print_warning_with_message);
  1306. REGISTER_INTERFACE_FUNC(print_script_error);
  1307. REGISTER_INTERFACE_FUNC(print_script_error_with_message);
  1308. REGISTER_INTERFACE_FUNC(get_native_struct_size);
  1309. REGISTER_INTERFACE_FUNC(variant_new_copy);
  1310. REGISTER_INTERFACE_FUNC(variant_new_nil);
  1311. REGISTER_INTERFACE_FUNC(variant_destroy);
  1312. REGISTER_INTERFACE_FUNC(variant_call);
  1313. REGISTER_INTERFACE_FUNC(variant_call_static);
  1314. REGISTER_INTERFACE_FUNC(variant_evaluate);
  1315. REGISTER_INTERFACE_FUNC(variant_set);
  1316. REGISTER_INTERFACE_FUNC(variant_set_named);
  1317. REGISTER_INTERFACE_FUNC(variant_set_keyed);
  1318. REGISTER_INTERFACE_FUNC(variant_set_indexed);
  1319. REGISTER_INTERFACE_FUNC(variant_get);
  1320. REGISTER_INTERFACE_FUNC(variant_get_named);
  1321. REGISTER_INTERFACE_FUNC(variant_get_keyed);
  1322. REGISTER_INTERFACE_FUNC(variant_get_indexed);
  1323. REGISTER_INTERFACE_FUNC(variant_iter_init);
  1324. REGISTER_INTERFACE_FUNC(variant_iter_next);
  1325. REGISTER_INTERFACE_FUNC(variant_iter_get);
  1326. REGISTER_INTERFACE_FUNC(variant_hash);
  1327. REGISTER_INTERFACE_FUNC(variant_recursive_hash);
  1328. REGISTER_INTERFACE_FUNC(variant_hash_compare);
  1329. REGISTER_INTERFACE_FUNC(variant_booleanize);
  1330. REGISTER_INTERFACE_FUNC(variant_duplicate);
  1331. REGISTER_INTERFACE_FUNC(variant_stringify);
  1332. REGISTER_INTERFACE_FUNC(variant_get_type);
  1333. REGISTER_INTERFACE_FUNC(variant_has_method);
  1334. REGISTER_INTERFACE_FUNC(variant_has_member);
  1335. REGISTER_INTERFACE_FUNC(variant_has_key);
  1336. REGISTER_INTERFACE_FUNC(variant_get_type_name);
  1337. REGISTER_INTERFACE_FUNC(variant_can_convert);
  1338. REGISTER_INTERFACE_FUNC(variant_can_convert_strict);
  1339. REGISTER_INTERFACE_FUNC(get_variant_from_type_constructor);
  1340. REGISTER_INTERFACE_FUNC(get_variant_to_type_constructor);
  1341. REGISTER_INTERFACE_FUNC(variant_get_ptr_operator_evaluator);
  1342. REGISTER_INTERFACE_FUNC(variant_get_ptr_builtin_method);
  1343. REGISTER_INTERFACE_FUNC(variant_get_ptr_constructor);
  1344. REGISTER_INTERFACE_FUNC(variant_get_ptr_destructor);
  1345. REGISTER_INTERFACE_FUNC(variant_construct);
  1346. REGISTER_INTERFACE_FUNC(variant_get_ptr_setter);
  1347. REGISTER_INTERFACE_FUNC(variant_get_ptr_getter);
  1348. REGISTER_INTERFACE_FUNC(variant_get_ptr_indexed_setter);
  1349. REGISTER_INTERFACE_FUNC(variant_get_ptr_indexed_getter);
  1350. REGISTER_INTERFACE_FUNC(variant_get_ptr_keyed_setter);
  1351. REGISTER_INTERFACE_FUNC(variant_get_ptr_keyed_getter);
  1352. REGISTER_INTERFACE_FUNC(variant_get_ptr_keyed_checker);
  1353. REGISTER_INTERFACE_FUNC(variant_get_constant_value);
  1354. REGISTER_INTERFACE_FUNC(variant_get_ptr_utility_function);
  1355. REGISTER_INTERFACE_FUNC(string_new_with_latin1_chars);
  1356. REGISTER_INTERFACE_FUNC(string_new_with_utf8_chars);
  1357. REGISTER_INTERFACE_FUNC(string_new_with_utf16_chars);
  1358. REGISTER_INTERFACE_FUNC(string_new_with_utf32_chars);
  1359. REGISTER_INTERFACE_FUNC(string_new_with_wide_chars);
  1360. REGISTER_INTERFACE_FUNC(string_new_with_latin1_chars_and_len);
  1361. REGISTER_INTERFACE_FUNC(string_new_with_utf8_chars_and_len);
  1362. REGISTER_INTERFACE_FUNC(string_new_with_utf16_chars_and_len);
  1363. REGISTER_INTERFACE_FUNC(string_new_with_utf32_chars_and_len);
  1364. REGISTER_INTERFACE_FUNC(string_new_with_wide_chars_and_len);
  1365. REGISTER_INTERFACE_FUNC(string_to_latin1_chars);
  1366. REGISTER_INTERFACE_FUNC(string_to_utf8_chars);
  1367. REGISTER_INTERFACE_FUNC(string_to_utf16_chars);
  1368. REGISTER_INTERFACE_FUNC(string_to_utf32_chars);
  1369. REGISTER_INTERFACE_FUNC(string_to_wide_chars);
  1370. REGISTER_INTERFACE_FUNC(string_operator_index);
  1371. REGISTER_INTERFACE_FUNC(string_operator_index_const);
  1372. REGISTER_INTERFACE_FUNC(string_operator_plus_eq_string);
  1373. REGISTER_INTERFACE_FUNC(string_operator_plus_eq_char);
  1374. REGISTER_INTERFACE_FUNC(string_operator_plus_eq_cstr);
  1375. REGISTER_INTERFACE_FUNC(string_operator_plus_eq_wcstr);
  1376. REGISTER_INTERFACE_FUNC(string_operator_plus_eq_c32str);
  1377. REGISTER_INTERFACE_FUNC(string_resize);
  1378. REGISTER_INTERFACE_FUNC(string_name_new_with_latin1_chars);
  1379. REGISTER_INTERFACE_FUNC(string_name_new_with_utf8_chars);
  1380. REGISTER_INTERFACE_FUNC(string_name_new_with_utf8_chars_and_len);
  1381. REGISTER_INTERFACE_FUNC(xml_parser_open_buffer);
  1382. REGISTER_INTERFACE_FUNC(file_access_store_buffer);
  1383. REGISTER_INTERFACE_FUNC(file_access_get_buffer);
  1384. REGISTER_INTERFACE_FUNC(worker_thread_pool_add_native_group_task);
  1385. REGISTER_INTERFACE_FUNC(worker_thread_pool_add_native_task);
  1386. REGISTER_INTERFACE_FUNC(packed_byte_array_operator_index);
  1387. REGISTER_INTERFACE_FUNC(packed_byte_array_operator_index_const);
  1388. REGISTER_INTERFACE_FUNC(packed_color_array_operator_index);
  1389. REGISTER_INTERFACE_FUNC(packed_color_array_operator_index_const);
  1390. REGISTER_INTERFACE_FUNC(packed_float32_array_operator_index);
  1391. REGISTER_INTERFACE_FUNC(packed_float32_array_operator_index_const);
  1392. REGISTER_INTERFACE_FUNC(packed_float64_array_operator_index);
  1393. REGISTER_INTERFACE_FUNC(packed_float64_array_operator_index_const);
  1394. REGISTER_INTERFACE_FUNC(packed_int32_array_operator_index);
  1395. REGISTER_INTERFACE_FUNC(packed_int32_array_operator_index_const);
  1396. REGISTER_INTERFACE_FUNC(packed_int64_array_operator_index);
  1397. REGISTER_INTERFACE_FUNC(packed_int64_array_operator_index_const);
  1398. REGISTER_INTERFACE_FUNC(packed_string_array_operator_index);
  1399. REGISTER_INTERFACE_FUNC(packed_string_array_operator_index_const);
  1400. REGISTER_INTERFACE_FUNC(packed_vector2_array_operator_index);
  1401. REGISTER_INTERFACE_FUNC(packed_vector2_array_operator_index_const);
  1402. REGISTER_INTERFACE_FUNC(packed_vector3_array_operator_index);
  1403. REGISTER_INTERFACE_FUNC(packed_vector3_array_operator_index_const);
  1404. REGISTER_INTERFACE_FUNC(array_operator_index);
  1405. REGISTER_INTERFACE_FUNC(array_operator_index_const);
  1406. REGISTER_INTERFACE_FUNC(array_ref);
  1407. REGISTER_INTERFACE_FUNC(array_set_typed);
  1408. REGISTER_INTERFACE_FUNC(dictionary_operator_index);
  1409. REGISTER_INTERFACE_FUNC(dictionary_operator_index_const);
  1410. REGISTER_INTERFACE_FUNC(object_method_bind_call);
  1411. REGISTER_INTERFACE_FUNC(object_method_bind_ptrcall);
  1412. REGISTER_INTERFACE_FUNC(object_destroy);
  1413. REGISTER_INTERFACE_FUNC(global_get_singleton);
  1414. REGISTER_INTERFACE_FUNC(object_get_instance_binding);
  1415. REGISTER_INTERFACE_FUNC(object_set_instance_binding);
  1416. REGISTER_INTERFACE_FUNC(object_free_instance_binding);
  1417. REGISTER_INTERFACE_FUNC(object_set_instance);
  1418. REGISTER_INTERFACE_FUNC(object_get_class_name);
  1419. REGISTER_INTERFACE_FUNC(object_cast_to);
  1420. REGISTER_INTERFACE_FUNC(object_get_instance_from_id);
  1421. REGISTER_INTERFACE_FUNC(object_get_instance_id);
  1422. REGISTER_INTERFACE_FUNC(object_has_script_method);
  1423. REGISTER_INTERFACE_FUNC(object_call_script_method);
  1424. REGISTER_INTERFACE_FUNC(ref_get_object);
  1425. REGISTER_INTERFACE_FUNC(ref_set_object);
  1426. #ifndef DISABLE_DEPRECATED
  1427. REGISTER_INTERFACE_FUNC(script_instance_create);
  1428. REGISTER_INTERFACE_FUNC(script_instance_create2);
  1429. #endif // DISABLE_DEPRECATED
  1430. REGISTER_INTERFACE_FUNC(script_instance_create3);
  1431. REGISTER_INTERFACE_FUNC(placeholder_script_instance_create);
  1432. REGISTER_INTERFACE_FUNC(placeholder_script_instance_update);
  1433. REGISTER_INTERFACE_FUNC(object_get_script_instance);
  1434. #ifndef DISABLE_DEPRECATED
  1435. REGISTER_INTERFACE_FUNC(callable_custom_create);
  1436. #endif // DISABLE_DEPRECATED
  1437. REGISTER_INTERFACE_FUNC(callable_custom_create2);
  1438. REGISTER_INTERFACE_FUNC(callable_custom_get_userdata);
  1439. REGISTER_INTERFACE_FUNC(classdb_construct_object);
  1440. REGISTER_INTERFACE_FUNC(classdb_get_method_bind);
  1441. REGISTER_INTERFACE_FUNC(classdb_get_class_tag);
  1442. REGISTER_INTERFACE_FUNC(editor_add_plugin);
  1443. REGISTER_INTERFACE_FUNC(editor_remove_plugin);
  1444. REGISTER_INTERFACE_FUNC(editor_help_load_xml_from_utf8_chars);
  1445. REGISTER_INTERFACE_FUNC(editor_help_load_xml_from_utf8_chars_and_len);
  1446. }
  1447. #undef REGISTER_INTERFACE_FUNCTION