gdscript_utility_functions.cpp 25 KB

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