gdscript_utility_functions.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**************************************************************************/
  2. /* gdscript_utility_functions.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 "gdscript_utility_functions.h"
  31. #include "gdscript.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/object.h"
  35. #include "core/templates/a_hash_map.h"
  36. #include "core/templates/vector.h"
  37. #include "core/variant/typed_array.h"
  38. #ifdef DEBUG_ENABLED
  39. #define DEBUG_VALIDATE_ARG_COUNT(m_min_count, m_max_count) \
  40. if (unlikely(p_arg_count < m_min_count)) { \
  41. *r_ret = Variant(); \
  42. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
  43. r_error.expected = m_min_count; \
  44. return; \
  45. } \
  46. if (unlikely(p_arg_count > m_max_count)) { \
  47. *r_ret = Variant(); \
  48. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \
  49. r_error.expected = m_max_count; \
  50. return; \
  51. }
  52. #define DEBUG_VALIDATE_ARG_TYPE(m_arg, m_type) \
  53. if (unlikely(!Variant::can_convert_strict(p_args[m_arg]->get_type(), m_type))) { \
  54. *r_ret = Variant(); \
  55. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  56. r_error.argument = m_arg; \
  57. r_error.expected = m_type; \
  58. return; \
  59. }
  60. #define DEBUG_VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) \
  61. if (unlikely(m_cond)) { \
  62. *r_ret = m_msg; \
  63. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  64. r_error.argument = m_arg; \
  65. r_error.expected = m_type; \
  66. return; \
  67. }
  68. #else // !DEBUG_ENABLED
  69. #define DEBUG_VALIDATE_ARG_COUNT(m_min_count, m_max_count)
  70. #define DEBUG_VALIDATE_ARG_TYPE(m_arg, m_type)
  71. #define DEBUG_VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg)
  72. #endif // DEBUG_ENABLED
  73. #define VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) \
  74. if (unlikely(m_cond)) { \
  75. *r_ret = m_msg; \
  76. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  77. r_error.argument = m_arg; \
  78. r_error.expected = m_type; \
  79. return; \
  80. }
  81. #define GDFUNC_FAIL_COND_MSG(m_cond, m_msg) \
  82. if (unlikely(m_cond)) { \
  83. *r_ret = m_msg; \
  84. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; \
  85. return; \
  86. }
  87. struct GDScriptUtilityFunctionsDefinitions {
  88. #ifndef DISABLE_DEPRECATED
  89. static inline void convert(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  90. DEBUG_VALIDATE_ARG_COUNT(2, 2);
  91. DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
  92. int type = *p_args[1];
  93. DEBUG_VALIDATE_ARG_CUSTOM(1, Variant::INT, type < 0 || type >= Variant::VARIANT_MAX,
  94. RTR("Invalid type argument to convert(), use TYPE_* constants."));
  95. Variant::construct(Variant::Type(type), *r_ret, p_args, 1, r_error);
  96. }
  97. #endif // DISABLE_DEPRECATED
  98. static inline void type_exists(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  99. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  100. DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING_NAME);
  101. *r_ret = ClassDB::class_exists(*p_args[0]);
  102. }
  103. static inline void _char(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  104. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  105. DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
  106. const int64_t code = *p_args[0];
  107. VALIDATE_ARG_CUSTOM(0, Variant::INT, code < 0 || code > UINT32_MAX, RTR("Expected an integer between 0 and 2^32 - 1."));
  108. *r_ret = String::chr(code);
  109. }
  110. static inline void ord(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  111. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  112. DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING);
  113. const String string = *p_args[0];
  114. VALIDATE_ARG_CUSTOM(0, Variant::STRING, string.length() != 1, RTR("Expected a string of length 1 (a character)."));
  115. *r_ret = string.get(0);
  116. }
  117. static inline void range(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  118. DEBUG_VALIDATE_ARG_COUNT(1, 3);
  119. switch (p_arg_count) {
  120. case 1: {
  121. DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
  122. int count = *p_args[0];
  123. Array arr;
  124. if (count <= 0) {
  125. *r_ret = arr;
  126. return;
  127. }
  128. Error err = arr.resize(count);
  129. GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
  130. for (int i = 0; i < count; i++) {
  131. arr[i] = i;
  132. }
  133. *r_ret = arr;
  134. } break;
  135. case 2: {
  136. DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
  137. DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
  138. int from = *p_args[0];
  139. int to = *p_args[1];
  140. Array arr;
  141. if (from >= to) {
  142. *r_ret = arr;
  143. return;
  144. }
  145. Error err = arr.resize(to - from);
  146. GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
  147. for (int i = from; i < to; i++) {
  148. arr[i - from] = i;
  149. }
  150. *r_ret = arr;
  151. } break;
  152. case 3: {
  153. DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
  154. DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
  155. DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT);
  156. int from = *p_args[0];
  157. int to = *p_args[1];
  158. int incr = *p_args[2];
  159. VALIDATE_ARG_CUSTOM(2, Variant::INT, incr == 0, RTR("Step argument is zero!"));
  160. Array arr;
  161. if (from >= to && incr > 0) {
  162. *r_ret = arr;
  163. return;
  164. }
  165. if (from <= to && incr < 0) {
  166. *r_ret = arr;
  167. return;
  168. }
  169. // Calculate how many.
  170. int count = 0;
  171. if (incr > 0) {
  172. count = Math::division_round_up(to - from, incr);
  173. } else {
  174. count = Math::division_round_up(from - to, -incr);
  175. }
  176. Error err = arr.resize(count);
  177. GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
  178. if (incr > 0) {
  179. int idx = 0;
  180. for (int i = from; i < to; i += incr) {
  181. arr[idx++] = i;
  182. }
  183. } else {
  184. int idx = 0;
  185. for (int i = from; i > to; i += incr) {
  186. arr[idx++] = i;
  187. }
  188. }
  189. *r_ret = arr;
  190. } break;
  191. }
  192. }
  193. static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  194. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  195. DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING);
  196. *r_ret = ResourceLoader::load(*p_args[0]);
  197. }
  198. #ifndef DISABLE_DEPRECATED
  199. static inline void inst_to_dict(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  200. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  201. DEBUG_VALIDATE_ARG_TYPE(0, Variant::OBJECT);
  202. if (p_args[0]->get_type() == Variant::NIL) {
  203. *r_ret = Variant();
  204. return;
  205. }
  206. Object *obj = *p_args[0];
  207. if (!obj) {
  208. *r_ret = Variant();
  209. return;
  210. }
  211. VALIDATE_ARG_CUSTOM(0, Variant::OBJECT,
  212. !obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton(),
  213. RTR("Not a script with an instance."));
  214. GDScriptInstance *inst = static_cast<GDScriptInstance *>(obj->get_script_instance());
  215. Ref<GDScript> base = inst->get_script();
  216. VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, base.is_null(), RTR("Not based on a script."));
  217. GDScript *p = base.ptr();
  218. String path = p->get_script_path();
  219. Vector<StringName> sname;
  220. while (p->_owner) {
  221. sname.push_back(p->local_name);
  222. p = p->_owner;
  223. }
  224. sname.reverse();
  225. VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, !path.is_resource_file(), RTR("Not based on a resource file."));
  226. NodePath cp(sname, Vector<StringName>(), false);
  227. Dictionary d;
  228. d["@subpath"] = cp;
  229. d["@path"] = path;
  230. for (const KeyValue<StringName, GDScript::MemberInfo> &E : base->member_indices) {
  231. if (!d.has(E.key)) {
  232. d[E.key] = inst->members[E.value.index];
  233. }
  234. }
  235. *r_ret = d;
  236. }
  237. static inline void dict_to_inst(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  238. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  239. DEBUG_VALIDATE_ARG_TYPE(0, Variant::DICTIONARY);
  240. Dictionary d = *p_args[0];
  241. VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, !d.has("@path"), RTR("Invalid instance dictionary format (missing @path)."));
  242. Ref<Script> scr = ResourceLoader::load(d["@path"]);
  243. VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, scr.is_null(), RTR("Invalid instance dictionary format (can't load script at @path)."));
  244. Ref<GDScript> gdscr = scr;
  245. VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, gdscr.is_null(), RTR("Invalid instance dictionary format (invalid script at @path)."));
  246. NodePath sub;
  247. if (d.has("@subpath")) {
  248. sub = d["@subpath"];
  249. }
  250. for (int i = 0; i < sub.get_name_count(); i++) {
  251. gdscr = gdscr->subclasses[sub.get_name(i)];
  252. VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, gdscr.is_null(), RTR("Invalid instance dictionary (invalid subclasses)."));
  253. }
  254. *r_ret = gdscr->_new(nullptr, -1 /* skip initializer */, r_error);
  255. if (r_error.error != Callable::CallError::CALL_OK) {
  256. *r_ret = RTR("Cannot instantiate GDScript class.");
  257. return;
  258. }
  259. GDScriptInstance *inst = static_cast<GDScriptInstance *>(static_cast<Object *>(*r_ret)->get_script_instance());
  260. Ref<GDScript> gd_ref = inst->get_script();
  261. for (KeyValue<StringName, GDScript::MemberInfo> &E : gd_ref->member_indices) {
  262. if (d.has(E.key)) {
  263. inst->members.write[E.value.index] = d[E.key];
  264. }
  265. }
  266. }
  267. static inline void Color8(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  268. DEBUG_VALIDATE_ARG_COUNT(3, 4);
  269. DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
  270. DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
  271. DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT);
  272. int64_t alpha = 255;
  273. if (p_arg_count == 4) {
  274. DEBUG_VALIDATE_ARG_TYPE(3, Variant::INT);
  275. alpha = *p_args[3];
  276. }
  277. *r_ret = Color::from_rgba8(*p_args[0], *p_args[1], *p_args[2], alpha);
  278. }
  279. #endif // DISABLE_DEPRECATED
  280. static inline void print_debug(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  281. String s;
  282. for (int i = 0; i < p_arg_count; i++) {
  283. s += p_args[i]->operator String();
  284. }
  285. if (Thread::get_caller_id() == Thread::get_main_id()) {
  286. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  287. if (script->debug_get_stack_level_count() > 0) {
  288. s += "\n At: " + script->debug_get_stack_level_source(0) + ":" + itos(script->debug_get_stack_level_line(0)) + ":" + script->debug_get_stack_level_function(0) + "()";
  289. }
  290. } else {
  291. s += "\n At: Cannot retrieve debug info outside the main thread. Thread ID: " + itos(Thread::get_caller_id());
  292. }
  293. print_line(s);
  294. *r_ret = Variant();
  295. }
  296. static inline void print_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  297. DEBUG_VALIDATE_ARG_COUNT(0, 0);
  298. if (Thread::get_caller_id() != Thread::get_main_id()) {
  299. print_line("Cannot retrieve debug info outside the main thread. Thread ID: " + itos(Thread::get_caller_id()));
  300. return;
  301. }
  302. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  303. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  304. print_line("Frame " + itos(i) + " - " + script->debug_get_stack_level_source(i) + ":" + itos(script->debug_get_stack_level_line(i)) + " in function '" + script->debug_get_stack_level_function(i) + "'");
  305. };
  306. *r_ret = Variant();
  307. }
  308. static inline void get_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  309. DEBUG_VALIDATE_ARG_COUNT(0, 0);
  310. if (Thread::get_caller_id() != Thread::get_main_id()) {
  311. *r_ret = TypedArray<Dictionary>();
  312. return;
  313. }
  314. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  315. TypedArray<Dictionary> ret;
  316. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  317. Dictionary frame;
  318. frame["source"] = script->debug_get_stack_level_source(i);
  319. frame["function"] = script->debug_get_stack_level_function(i);
  320. frame["line"] = script->debug_get_stack_level_line(i);
  321. ret.push_back(frame);
  322. };
  323. *r_ret = ret;
  324. }
  325. static inline void len(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  326. DEBUG_VALIDATE_ARG_COUNT(1, 1);
  327. switch (p_args[0]->get_type()) {
  328. case Variant::STRING:
  329. case Variant::STRING_NAME: {
  330. String d = *p_args[0];
  331. *r_ret = d.length();
  332. } break;
  333. case Variant::DICTIONARY: {
  334. Dictionary d = *p_args[0];
  335. *r_ret = d.size();
  336. } break;
  337. case Variant::ARRAY: {
  338. Array d = *p_args[0];
  339. *r_ret = d.size();
  340. } break;
  341. case Variant::PACKED_BYTE_ARRAY: {
  342. Vector<uint8_t> d = *p_args[0];
  343. *r_ret = d.size();
  344. } break;
  345. case Variant::PACKED_INT32_ARRAY: {
  346. Vector<int32_t> d = *p_args[0];
  347. *r_ret = d.size();
  348. } break;
  349. case Variant::PACKED_INT64_ARRAY: {
  350. Vector<int64_t> d = *p_args[0];
  351. *r_ret = d.size();
  352. } break;
  353. case Variant::PACKED_FLOAT32_ARRAY: {
  354. Vector<float> d = *p_args[0];
  355. *r_ret = d.size();
  356. } break;
  357. case Variant::PACKED_FLOAT64_ARRAY: {
  358. Vector<double> d = *p_args[0];
  359. *r_ret = d.size();
  360. } break;
  361. case Variant::PACKED_STRING_ARRAY: {
  362. Vector<String> d = *p_args[0];
  363. *r_ret = d.size();
  364. } break;
  365. case Variant::PACKED_VECTOR2_ARRAY: {
  366. Vector<Vector2> d = *p_args[0];
  367. *r_ret = d.size();
  368. } break;
  369. case Variant::PACKED_VECTOR3_ARRAY: {
  370. Vector<Vector3> d = *p_args[0];
  371. *r_ret = d.size();
  372. } break;
  373. case Variant::PACKED_COLOR_ARRAY: {
  374. Vector<Color> d = *p_args[0];
  375. *r_ret = d.size();
  376. } break;
  377. case Variant::PACKED_VECTOR4_ARRAY: {
  378. Vector<Vector4> d = *p_args[0];
  379. *r_ret = d.size();
  380. } break;
  381. default: {
  382. *r_ret = vformat(RTR("Value of type '%s' can't provide a length."), Variant::get_type_name(p_args[0]->get_type()));
  383. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  384. r_error.argument = 0;
  385. r_error.expected = Variant::NIL;
  386. } break;
  387. }
  388. }
  389. static inline void is_instance_of(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  390. DEBUG_VALIDATE_ARG_COUNT(2, 2);
  391. if (p_args[1]->get_type() == Variant::INT) {
  392. int builtin_type = *p_args[1];
  393. DEBUG_VALIDATE_ARG_CUSTOM(1, Variant::NIL, builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX,
  394. RTR("Invalid type argument for is_instance_of(), use TYPE_* constants for built-in types."));
  395. *r_ret = p_args[0]->get_type() == builtin_type;
  396. return;
  397. }
  398. bool was_type_freed = false;
  399. Object *type_object = p_args[1]->get_validated_object_with_check(was_type_freed);
  400. VALIDATE_ARG_CUSTOM(1, Variant::NIL, was_type_freed, RTR("Type argument is a previously freed instance."));
  401. VALIDATE_ARG_CUSTOM(1, Variant::NIL, !type_object,
  402. RTR("Invalid type argument for is_instance_of(), should be a TYPE_* constant, a class or a script."));
  403. bool was_value_freed = false;
  404. Object *value_object = p_args[0]->get_validated_object_with_check(was_value_freed);
  405. VALIDATE_ARG_CUSTOM(0, Variant::NIL, was_value_freed, RTR("Value argument is a previously freed instance."));
  406. if (!value_object) {
  407. *r_ret = false;
  408. return;
  409. }
  410. GDScriptNativeClass *native_type = Object::cast_to<GDScriptNativeClass>(type_object);
  411. if (native_type) {
  412. *r_ret = ClassDB::is_parent_class(value_object->get_class_name(), native_type->get_name());
  413. return;
  414. }
  415. Script *script_type = Object::cast_to<Script>(type_object);
  416. if (script_type) {
  417. bool result = false;
  418. if (value_object->get_script_instance()) {
  419. Script *script_ptr = value_object->get_script_instance()->get_script().ptr();
  420. while (script_ptr) {
  421. if (script_ptr == script_type) {
  422. result = true;
  423. break;
  424. }
  425. script_ptr = script_ptr->get_base_script().ptr();
  426. }
  427. }
  428. *r_ret = result;
  429. return;
  430. }
  431. *r_ret = RTR("Invalid type argument for is_instance_of(), should be a TYPE_* constant, a class or a script.");
  432. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  433. r_error.argument = 1;
  434. r_error.expected = Variant::NIL;
  435. }
  436. };
  437. struct GDScriptUtilityFunctionInfo {
  438. GDScriptUtilityFunctions::FunctionPtr function = nullptr;
  439. MethodInfo info;
  440. bool is_constant = false;
  441. };
  442. static AHashMap<StringName, GDScriptUtilityFunctionInfo> utility_function_table;
  443. static List<StringName> utility_function_name_table;
  444. static void _register_function(const StringName &p_name, const MethodInfo &p_method_info, GDScriptUtilityFunctions::FunctionPtr p_function, bool p_is_const) {
  445. ERR_FAIL_COND(utility_function_table.has(p_name));
  446. GDScriptUtilityFunctionInfo function;
  447. function.function = p_function;
  448. function.info = p_method_info;
  449. function.is_constant = p_is_const;
  450. utility_function_table.insert(p_name, function);
  451. utility_function_name_table.push_back(p_name);
  452. }
  453. #define REGISTER_FUNC(m_func, m_is_const, m_return, m_args, m_is_vararg, m_default_args) \
  454. { \
  455. String name(#m_func); \
  456. if (name.begins_with("_")) { \
  457. name = name.substr(1); \
  458. } \
  459. MethodInfo info = m_args; \
  460. info.name = name; \
  461. info.return_val = m_return; \
  462. info.default_arguments = m_default_args; \
  463. if (m_is_vararg) { \
  464. info.flags |= METHOD_FLAG_VARARG; \
  465. } \
  466. _register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \
  467. }
  468. #define RET(m_type) \
  469. PropertyInfo(Variant::m_type, "")
  470. #define RETVAR \
  471. PropertyInfo(Variant::NIL, "", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)
  472. #define RETCLS(m_class) \
  473. PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, m_class)
  474. #define NOARGS \
  475. MethodInfo()
  476. #define ARGS(...) \
  477. MethodInfo("", __VA_ARGS__)
  478. #define ARG(m_name, m_type) \
  479. PropertyInfo(Variant::m_type, m_name)
  480. #define ARGVAR(m_name) \
  481. PropertyInfo(Variant::NIL, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)
  482. #define ARGTYPE(m_name) \
  483. PropertyInfo(Variant::INT, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "Variant.Type")
  484. void GDScriptUtilityFunctions::register_functions() {
  485. /* clang-format off */
  486. #ifndef DISABLE_DEPRECATED
  487. REGISTER_FUNC( convert, true, RETVAR, ARGS( ARGVAR("what"), ARGTYPE("type") ), false, varray( ));
  488. #endif // DISABLE_DEPRECATED
  489. REGISTER_FUNC( type_exists, true, RET(BOOL), ARGS( ARG("type", STRING_NAME) ), false, varray( ));
  490. REGISTER_FUNC( _char, true, RET(STRING), ARGS( ARG("code", INT) ), false, varray( ));
  491. REGISTER_FUNC( ord, true, RET(INT), ARGS( ARG("char", STRING) ), false, varray( ));
  492. REGISTER_FUNC( range, false, RET(ARRAY), NOARGS, true, varray( ));
  493. REGISTER_FUNC( load, false, RETCLS("Resource"), ARGS( ARG("path", STRING) ), false, varray( ));
  494. #ifndef DISABLE_DEPRECATED
  495. REGISTER_FUNC( inst_to_dict, false, RET(DICTIONARY), ARGS( ARG("instance", OBJECT) ), false, varray( ));
  496. REGISTER_FUNC( dict_to_inst, false, RET(OBJECT), ARGS( ARG("dictionary", DICTIONARY) ), false, varray( ));
  497. REGISTER_FUNC( Color8, true, RET(COLOR), ARGS( ARG("r8", INT), ARG("g8", INT),
  498. ARG("b8", INT), ARG("a8", INT) ), false, varray( 255 ));
  499. #endif // DISABLE_DEPRECATED
  500. REGISTER_FUNC( print_debug, false, RET(NIL), NOARGS, true, varray( ));
  501. REGISTER_FUNC( print_stack, false, RET(NIL), NOARGS, false, varray( ));
  502. REGISTER_FUNC( get_stack, false, RET(ARRAY), NOARGS, false, varray( ));
  503. REGISTER_FUNC( len, true, RET(INT), ARGS( ARGVAR("var") ), false, varray( ));
  504. REGISTER_FUNC( is_instance_of, true, RET(BOOL), ARGS( ARGVAR("value"), ARGVAR("type") ), false, varray( ));
  505. /* clang-format on */
  506. }
  507. void GDScriptUtilityFunctions::unregister_functions() {
  508. utility_function_name_table.clear();
  509. utility_function_table.clear();
  510. }
  511. GDScriptUtilityFunctions::FunctionPtr GDScriptUtilityFunctions::get_function(const StringName &p_function) {
  512. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  513. ERR_FAIL_NULL_V(info, nullptr);
  514. return info->function;
  515. }
  516. bool GDScriptUtilityFunctions::has_function_return_value(const StringName &p_function) {
  517. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  518. ERR_FAIL_NULL_V(info, false);
  519. return info->info.return_val.type != Variant::NIL || bool(info->info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  520. }
  521. Variant::Type GDScriptUtilityFunctions::get_function_return_type(const StringName &p_function) {
  522. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  523. ERR_FAIL_NULL_V(info, Variant::NIL);
  524. return info->info.return_val.type;
  525. }
  526. StringName GDScriptUtilityFunctions::get_function_return_class(const StringName &p_function) {
  527. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  528. ERR_FAIL_NULL_V(info, StringName());
  529. return info->info.return_val.class_name;
  530. }
  531. Variant::Type GDScriptUtilityFunctions::get_function_argument_type(const StringName &p_function, int p_arg) {
  532. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  533. ERR_FAIL_NULL_V(info, Variant::NIL);
  534. ERR_FAIL_INDEX_V(p_arg, info->info.arguments.size(), Variant::NIL);
  535. return info->info.arguments[p_arg].type;
  536. }
  537. int GDScriptUtilityFunctions::get_function_argument_count(const StringName &p_function) {
  538. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  539. ERR_FAIL_NULL_V(info, 0);
  540. return info->info.arguments.size();
  541. }
  542. bool GDScriptUtilityFunctions::is_function_vararg(const StringName &p_function) {
  543. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  544. ERR_FAIL_NULL_V(info, false);
  545. return (bool)(info->info.flags & METHOD_FLAG_VARARG);
  546. }
  547. bool GDScriptUtilityFunctions::is_function_constant(const StringName &p_function) {
  548. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  549. ERR_FAIL_NULL_V(info, false);
  550. return info->is_constant;
  551. }
  552. bool GDScriptUtilityFunctions::function_exists(const StringName &p_function) {
  553. return utility_function_table.has(p_function);
  554. }
  555. void GDScriptUtilityFunctions::get_function_list(List<StringName> *r_functions) {
  556. for (const StringName &E : utility_function_name_table) {
  557. r_functions->push_back(E);
  558. }
  559. }
  560. MethodInfo GDScriptUtilityFunctions::get_function_info(const StringName &p_function) {
  561. GDScriptUtilityFunctionInfo *info = utility_function_table.getptr(p_function);
  562. ERR_FAIL_NULL_V(info, MethodInfo());
  563. return info->info;
  564. }