variant_utility.cpp 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /**************************************************************************/
  2. /* variant_utility.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 "variant_utility.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/object/ref_counted.h"
  34. #include "core/os/os.h"
  35. #include "core/templates/oa_hash_map.h"
  36. #include "core/templates/rid.h"
  37. #include "core/templates/rid_owner.h"
  38. #include "core/variant/binder_common.h"
  39. #include "core/variant/variant_parser.h"
  40. // Math
  41. double VariantUtilityFunctions::sin(double arg) {
  42. return Math::sin(arg);
  43. }
  44. double VariantUtilityFunctions::cos(double arg) {
  45. return Math::cos(arg);
  46. }
  47. double VariantUtilityFunctions::tan(double arg) {
  48. return Math::tan(arg);
  49. }
  50. double VariantUtilityFunctions::sinh(double arg) {
  51. return Math::sinh(arg);
  52. }
  53. double VariantUtilityFunctions::cosh(double arg) {
  54. return Math::cosh(arg);
  55. }
  56. double VariantUtilityFunctions::tanh(double arg) {
  57. return Math::tanh(arg);
  58. }
  59. double VariantUtilityFunctions::asin(double arg) {
  60. return Math::asin(arg);
  61. }
  62. double VariantUtilityFunctions::acos(double arg) {
  63. return Math::acos(arg);
  64. }
  65. double VariantUtilityFunctions::atan(double arg) {
  66. return Math::atan(arg);
  67. }
  68. double VariantUtilityFunctions::atan2(double y, double x) {
  69. return Math::atan2(y, x);
  70. }
  71. double VariantUtilityFunctions::asinh(double arg) {
  72. return Math::asinh(arg);
  73. }
  74. double VariantUtilityFunctions::acosh(double arg) {
  75. return Math::acosh(arg);
  76. }
  77. double VariantUtilityFunctions::atanh(double arg) {
  78. return Math::atanh(arg);
  79. }
  80. double VariantUtilityFunctions::sqrt(double x) {
  81. return Math::sqrt(x);
  82. }
  83. double VariantUtilityFunctions::fmod(double b, double r) {
  84. return Math::fmod(b, r);
  85. }
  86. double VariantUtilityFunctions::fposmod(double b, double r) {
  87. return Math::fposmod(b, r);
  88. }
  89. int64_t VariantUtilityFunctions::posmod(int64_t b, int64_t r) {
  90. return Math::posmod(b, r);
  91. }
  92. Variant VariantUtilityFunctions::floor(Variant x, Callable::CallError &r_error) {
  93. r_error.error = Callable::CallError::CALL_OK;
  94. switch (x.get_type()) {
  95. case Variant::INT: {
  96. return VariantInternalAccessor<int64_t>::get(&x);
  97. } break;
  98. case Variant::FLOAT: {
  99. return Math::floor(VariantInternalAccessor<double>::get(&x));
  100. } break;
  101. case Variant::VECTOR2: {
  102. return VariantInternalAccessor<Vector2>::get(&x).floor();
  103. } break;
  104. case Variant::VECTOR2I: {
  105. return VariantInternalAccessor<Vector2i>::get(&x);
  106. } break;
  107. case Variant::VECTOR3: {
  108. return VariantInternalAccessor<Vector3>::get(&x).floor();
  109. } break;
  110. case Variant::VECTOR3I: {
  111. return VariantInternalAccessor<Vector3i>::get(&x);
  112. } break;
  113. case Variant::VECTOR4: {
  114. return VariantInternalAccessor<Vector4>::get(&x).floor();
  115. } break;
  116. case Variant::VECTOR4I: {
  117. return VariantInternalAccessor<Vector4i>::get(&x);
  118. } break;
  119. default: {
  120. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  121. r_error.argument = 0;
  122. r_error.expected = Variant::NIL;
  123. return R"(Argument "x" must be "int", "float", "Vector2", "Vector2i", "Vector3", "Vector3i", "Vector4", or "Vector4i".)";
  124. } break;
  125. }
  126. }
  127. double VariantUtilityFunctions::floorf(double x) {
  128. return Math::floor(x);
  129. }
  130. int64_t VariantUtilityFunctions::floori(double x) {
  131. return int64_t(Math::floor(x));
  132. }
  133. Variant VariantUtilityFunctions::ceil(Variant x, Callable::CallError &r_error) {
  134. r_error.error = Callable::CallError::CALL_OK;
  135. switch (x.get_type()) {
  136. case Variant::INT: {
  137. return VariantInternalAccessor<int64_t>::get(&x);
  138. } break;
  139. case Variant::FLOAT: {
  140. return Math::ceil(VariantInternalAccessor<double>::get(&x));
  141. } break;
  142. case Variant::VECTOR2: {
  143. return VariantInternalAccessor<Vector2>::get(&x).ceil();
  144. } break;
  145. case Variant::VECTOR2I: {
  146. return VariantInternalAccessor<Vector2i>::get(&x);
  147. } break;
  148. case Variant::VECTOR3: {
  149. return VariantInternalAccessor<Vector3>::get(&x).ceil();
  150. } break;
  151. case Variant::VECTOR3I: {
  152. return VariantInternalAccessor<Vector3i>::get(&x);
  153. } break;
  154. case Variant::VECTOR4: {
  155. return VariantInternalAccessor<Vector4>::get(&x).ceil();
  156. } break;
  157. case Variant::VECTOR4I: {
  158. return VariantInternalAccessor<Vector4i>::get(&x);
  159. } break;
  160. default: {
  161. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  162. r_error.argument = 0;
  163. r_error.expected = Variant::NIL;
  164. return R"(Argument "x" must be "int", "float", "Vector2", "Vector2i", "Vector3", "Vector3i", "Vector4", or "Vector4i".)";
  165. } break;
  166. }
  167. }
  168. double VariantUtilityFunctions::ceilf(double x) {
  169. return Math::ceil(x);
  170. }
  171. int64_t VariantUtilityFunctions::ceili(double x) {
  172. return int64_t(Math::ceil(x));
  173. }
  174. Variant VariantUtilityFunctions::round(Variant x, Callable::CallError &r_error) {
  175. r_error.error = Callable::CallError::CALL_OK;
  176. switch (x.get_type()) {
  177. case Variant::INT: {
  178. return VariantInternalAccessor<int64_t>::get(&x);
  179. } break;
  180. case Variant::FLOAT: {
  181. return Math::round(VariantInternalAccessor<double>::get(&x));
  182. } break;
  183. case Variant::VECTOR2: {
  184. return VariantInternalAccessor<Vector2>::get(&x).round();
  185. } break;
  186. case Variant::VECTOR2I: {
  187. return VariantInternalAccessor<Vector2i>::get(&x);
  188. } break;
  189. case Variant::VECTOR3: {
  190. return VariantInternalAccessor<Vector3>::get(&x).round();
  191. } break;
  192. case Variant::VECTOR3I: {
  193. return VariantInternalAccessor<Vector3i>::get(&x);
  194. } break;
  195. case Variant::VECTOR4: {
  196. return VariantInternalAccessor<Vector4>::get(&x).round();
  197. } break;
  198. case Variant::VECTOR4I: {
  199. return VariantInternalAccessor<Vector4i>::get(&x);
  200. } break;
  201. default: {
  202. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  203. r_error.argument = 0;
  204. r_error.expected = Variant::NIL;
  205. return R"(Argument "x" must be "int", "float", "Vector2", "Vector2i", "Vector3", "Vector3i", "Vector4", or "Vector4i".)";
  206. } break;
  207. }
  208. }
  209. double VariantUtilityFunctions::roundf(double x) {
  210. return Math::round(x);
  211. }
  212. int64_t VariantUtilityFunctions::roundi(double x) {
  213. return int64_t(Math::round(x));
  214. }
  215. Variant VariantUtilityFunctions::abs(const Variant &x, Callable::CallError &r_error) {
  216. r_error.error = Callable::CallError::CALL_OK;
  217. switch (x.get_type()) {
  218. case Variant::INT: {
  219. return ABS(VariantInternalAccessor<int64_t>::get(&x));
  220. } break;
  221. case Variant::FLOAT: {
  222. return Math::absd(VariantInternalAccessor<double>::get(&x));
  223. } break;
  224. case Variant::VECTOR2: {
  225. return VariantInternalAccessor<Vector2>::get(&x).abs();
  226. } break;
  227. case Variant::VECTOR2I: {
  228. return VariantInternalAccessor<Vector2i>::get(&x).abs();
  229. } break;
  230. case Variant::VECTOR3: {
  231. return VariantInternalAccessor<Vector3>::get(&x).abs();
  232. } break;
  233. case Variant::VECTOR3I: {
  234. return VariantInternalAccessor<Vector3i>::get(&x).abs();
  235. } break;
  236. case Variant::VECTOR4: {
  237. return VariantInternalAccessor<Vector4>::get(&x).abs();
  238. } break;
  239. case Variant::VECTOR4I: {
  240. return VariantInternalAccessor<Vector4i>::get(&x).abs();
  241. } break;
  242. default: {
  243. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  244. r_error.argument = 0;
  245. r_error.expected = Variant::NIL;
  246. return R"(Argument "x" must be "int", "float", "Vector2", "Vector2i", "Vector3", "Vector3i", "Vector4", or "Vector4i".)";
  247. } break;
  248. }
  249. }
  250. double VariantUtilityFunctions::absf(double x) {
  251. return Math::absd(x);
  252. }
  253. int64_t VariantUtilityFunctions::absi(int64_t x) {
  254. return ABS(x);
  255. }
  256. Variant VariantUtilityFunctions::sign(const Variant &x, Callable::CallError &r_error) {
  257. r_error.error = Callable::CallError::CALL_OK;
  258. switch (x.get_type()) {
  259. case Variant::INT: {
  260. return SIGN(VariantInternalAccessor<int64_t>::get(&x));
  261. } break;
  262. case Variant::FLOAT: {
  263. return SIGN(VariantInternalAccessor<double>::get(&x));
  264. } break;
  265. case Variant::VECTOR2: {
  266. return VariantInternalAccessor<Vector2>::get(&x).sign();
  267. } break;
  268. case Variant::VECTOR2I: {
  269. return VariantInternalAccessor<Vector2i>::get(&x).sign();
  270. } break;
  271. case Variant::VECTOR3: {
  272. return VariantInternalAccessor<Vector3>::get(&x).sign();
  273. } break;
  274. case Variant::VECTOR3I: {
  275. return VariantInternalAccessor<Vector3i>::get(&x).sign();
  276. } break;
  277. case Variant::VECTOR4: {
  278. return VariantInternalAccessor<Vector4>::get(&x).sign();
  279. } break;
  280. case Variant::VECTOR4I: {
  281. return VariantInternalAccessor<Vector4i>::get(&x).sign();
  282. } break;
  283. default: {
  284. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  285. r_error.argument = 0;
  286. r_error.expected = Variant::NIL;
  287. return R"(Argument "x" must be "int", "float", "Vector2", "Vector2i", "Vector3", "Vector3i", "Vector4", or "Vector4i".)";
  288. } break;
  289. }
  290. }
  291. double VariantUtilityFunctions::signf(double x) {
  292. return SIGN(x);
  293. }
  294. int64_t VariantUtilityFunctions::signi(int64_t x) {
  295. return SIGN(x);
  296. }
  297. double VariantUtilityFunctions::pow(double x, double y) {
  298. return Math::pow(x, y);
  299. }
  300. double VariantUtilityFunctions::log(double x) {
  301. return Math::log(x);
  302. }
  303. double VariantUtilityFunctions::exp(double x) {
  304. return Math::exp(x);
  305. }
  306. bool VariantUtilityFunctions::is_nan(double x) {
  307. return Math::is_nan(x);
  308. }
  309. bool VariantUtilityFunctions::is_inf(double x) {
  310. return Math::is_inf(x);
  311. }
  312. bool VariantUtilityFunctions::is_equal_approx(double x, double y) {
  313. return Math::is_equal_approx(x, y);
  314. }
  315. bool VariantUtilityFunctions::is_zero_approx(double x) {
  316. return Math::is_zero_approx(x);
  317. }
  318. bool VariantUtilityFunctions::is_finite(double x) {
  319. return Math::is_finite(x);
  320. }
  321. double VariantUtilityFunctions::ease(float x, float curve) {
  322. return Math::ease(x, curve);
  323. }
  324. int VariantUtilityFunctions::step_decimals(float step) {
  325. return Math::step_decimals(step);
  326. }
  327. Variant VariantUtilityFunctions::snapped(const Variant &x, const Variant &step, Callable::CallError &r_error) {
  328. switch (x.get_type()) {
  329. case Variant::INT:
  330. case Variant::FLOAT:
  331. case Variant::VECTOR2:
  332. case Variant::VECTOR2I:
  333. case Variant::VECTOR3:
  334. case Variant::VECTOR3I:
  335. case Variant::VECTOR4:
  336. case Variant::VECTOR4I:
  337. break;
  338. default:
  339. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  340. r_error.argument = 0;
  341. r_error.expected = Variant::NIL;
  342. return R"(Argument "x" must be "int", "float", "Vector2", "Vector2i", "Vector3", "Vector3i", "Vector4", or "Vector4i".)";
  343. }
  344. if (x.get_type() != step.get_type()) {
  345. if (x.get_type() == Variant::INT || x.get_type() == Variant::FLOAT) {
  346. if (step.get_type() != Variant::INT && step.get_type() != Variant::FLOAT) {
  347. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  348. r_error.argument = 1;
  349. r_error.expected = Variant::NIL;
  350. return R"(Argument "step" must be "int" or "float".)";
  351. }
  352. } else {
  353. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  354. r_error.argument = 1;
  355. r_error.expected = x.get_type();
  356. return Variant();
  357. }
  358. }
  359. r_error.error = Callable::CallError::CALL_OK;
  360. switch (step.get_type()) {
  361. case Variant::INT: {
  362. return snappedi(x, VariantInternalAccessor<int64_t>::get(&step));
  363. } break;
  364. case Variant::FLOAT: {
  365. return snappedf(x, VariantInternalAccessor<double>::get(&step));
  366. } break;
  367. case Variant::VECTOR2: {
  368. return VariantInternalAccessor<Vector2>::get(&x).snapped(VariantInternalAccessor<Vector2>::get(&step));
  369. } break;
  370. case Variant::VECTOR2I: {
  371. return VariantInternalAccessor<Vector2i>::get(&x).snapped(VariantInternalAccessor<Vector2i>::get(&step));
  372. } break;
  373. case Variant::VECTOR3: {
  374. return VariantInternalAccessor<Vector3>::get(&x).snapped(VariantInternalAccessor<Vector3>::get(&step));
  375. } break;
  376. case Variant::VECTOR3I: {
  377. return VariantInternalAccessor<Vector3i>::get(&x).snapped(VariantInternalAccessor<Vector3i>::get(&step));
  378. } break;
  379. case Variant::VECTOR4: {
  380. return VariantInternalAccessor<Vector4>::get(&x).snapped(VariantInternalAccessor<Vector4>::get(&step));
  381. } break;
  382. case Variant::VECTOR4I: {
  383. return VariantInternalAccessor<Vector4i>::get(&x).snapped(VariantInternalAccessor<Vector4i>::get(&step));
  384. } break;
  385. default: {
  386. return Variant(); // Already handled.
  387. } break;
  388. }
  389. }
  390. double VariantUtilityFunctions::snappedf(double x, double step) {
  391. return Math::snapped(x, step);
  392. }
  393. int64_t VariantUtilityFunctions::snappedi(double x, int64_t step) {
  394. return Math::snapped(x, step);
  395. }
  396. Variant VariantUtilityFunctions::lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error) {
  397. switch (from.get_type()) {
  398. case Variant::INT:
  399. case Variant::FLOAT:
  400. case Variant::VECTOR2:
  401. case Variant::VECTOR3:
  402. case Variant::VECTOR4:
  403. case Variant::QUATERNION:
  404. case Variant::BASIS:
  405. case Variant::COLOR:
  406. break;
  407. default:
  408. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  409. r_error.argument = 0;
  410. r_error.expected = Variant::NIL;
  411. return R"(Argument "from" must be "int", "float", "Vector2", "Vector3", "Vector4", "Quaternion", "Basis, or "Color".)";
  412. }
  413. if (from.get_type() != to.get_type()) {
  414. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  415. r_error.argument = 1;
  416. r_error.expected = from.get_type();
  417. return Variant();
  418. }
  419. r_error.error = Callable::CallError::CALL_OK;
  420. switch (from.get_type()) {
  421. case Variant::INT: {
  422. return lerpf(VariantInternalAccessor<int64_t>::get(&from), to, weight);
  423. } break;
  424. case Variant::FLOAT: {
  425. return lerpf(VariantInternalAccessor<double>::get(&from), to, weight);
  426. } break;
  427. case Variant::VECTOR2: {
  428. return VariantInternalAccessor<Vector2>::get(&from).lerp(VariantInternalAccessor<Vector2>::get(&to), weight);
  429. } break;
  430. case Variant::VECTOR3: {
  431. return VariantInternalAccessor<Vector3>::get(&from).lerp(VariantInternalAccessor<Vector3>::get(&to), weight);
  432. } break;
  433. case Variant::VECTOR4: {
  434. return VariantInternalAccessor<Vector4>::get(&from).lerp(VariantInternalAccessor<Vector4>::get(&to), weight);
  435. } break;
  436. case Variant::QUATERNION: {
  437. return VariantInternalAccessor<Quaternion>::get(&from).slerp(VariantInternalAccessor<Quaternion>::get(&to), weight);
  438. } break;
  439. case Variant::BASIS: {
  440. return VariantInternalAccessor<Basis>::get(&from).slerp(VariantInternalAccessor<Basis>::get(&to), weight);
  441. } break;
  442. case Variant::COLOR: {
  443. return VariantInternalAccessor<Color>::get(&from).lerp(VariantInternalAccessor<Color>::get(&to), weight);
  444. } break;
  445. default: {
  446. return Variant(); // Already handled.
  447. } break;
  448. }
  449. }
  450. double VariantUtilityFunctions::lerpf(double from, double to, double weight) {
  451. return Math::lerp(from, to, weight);
  452. }
  453. double VariantUtilityFunctions::cubic_interpolate(double from, double to, double pre, double post, double weight) {
  454. return Math::cubic_interpolate(from, to, pre, post, weight);
  455. }
  456. double VariantUtilityFunctions::cubic_interpolate_angle(double from, double to, double pre, double post, double weight) {
  457. return Math::cubic_interpolate_angle(from, to, pre, post, weight);
  458. }
  459. double VariantUtilityFunctions::cubic_interpolate_in_time(double from, double to, double pre, double post, double weight,
  460. double to_t, double pre_t, double post_t) {
  461. return Math::cubic_interpolate_in_time(from, to, pre, post, weight, to_t, pre_t, post_t);
  462. }
  463. double VariantUtilityFunctions::cubic_interpolate_angle_in_time(double from, double to, double pre, double post, double weight,
  464. double to_t, double pre_t, double post_t) {
  465. return Math::cubic_interpolate_angle_in_time(from, to, pre, post, weight, to_t, pre_t, post_t);
  466. }
  467. double VariantUtilityFunctions::bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  468. return Math::bezier_interpolate(p_start, p_control_1, p_control_2, p_end, p_t);
  469. }
  470. double VariantUtilityFunctions::bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
  471. return Math::bezier_derivative(p_start, p_control_1, p_control_2, p_end, p_t);
  472. }
  473. double VariantUtilityFunctions::angle_difference(double from, double to) {
  474. return Math::angle_difference(from, to);
  475. }
  476. double VariantUtilityFunctions::lerp_angle(double from, double to, double weight) {
  477. return Math::lerp_angle(from, to, weight);
  478. }
  479. double VariantUtilityFunctions::inverse_lerp(double from, double to, double weight) {
  480. return Math::inverse_lerp(from, to, weight);
  481. }
  482. double VariantUtilityFunctions::remap(double value, double istart, double istop, double ostart, double ostop) {
  483. return Math::remap(value, istart, istop, ostart, ostop);
  484. }
  485. double VariantUtilityFunctions::smoothstep(double from, double to, double val) {
  486. return Math::smoothstep(from, to, val);
  487. }
  488. double VariantUtilityFunctions::move_toward(double from, double to, double delta) {
  489. return Math::move_toward(from, to, delta);
  490. }
  491. double VariantUtilityFunctions::rotate_toward(double from, double to, double delta) {
  492. return Math::rotate_toward(from, to, delta);
  493. }
  494. double VariantUtilityFunctions::deg_to_rad(double angle_deg) {
  495. return Math::deg_to_rad(angle_deg);
  496. }
  497. double VariantUtilityFunctions::rad_to_deg(double angle_rad) {
  498. return Math::rad_to_deg(angle_rad);
  499. }
  500. double VariantUtilityFunctions::linear_to_db(double linear) {
  501. return Math::linear_to_db(linear);
  502. }
  503. double VariantUtilityFunctions::db_to_linear(double db) {
  504. return Math::db_to_linear(db);
  505. }
  506. Variant VariantUtilityFunctions::wrap(const Variant &p_x, const Variant &p_min, const Variant &p_max, Callable::CallError &r_error) {
  507. Variant::Type x_type = p_x.get_type();
  508. if (x_type != Variant::INT && x_type != Variant::FLOAT) {
  509. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  510. r_error.argument = 0;
  511. r_error.expected = Variant::FLOAT;
  512. return Variant();
  513. }
  514. Variant::Type min_type = p_min.get_type();
  515. if (min_type != Variant::INT && min_type != Variant::FLOAT) {
  516. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  517. r_error.argument = 1;
  518. r_error.expected = x_type;
  519. return Variant();
  520. }
  521. Variant::Type max_type = p_max.get_type();
  522. if (max_type != Variant::INT && max_type != Variant::FLOAT) {
  523. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  524. r_error.argument = 2;
  525. r_error.expected = x_type;
  526. return Variant();
  527. }
  528. Variant value;
  529. switch (x_type) {
  530. case Variant::INT: {
  531. if (x_type != min_type || x_type != max_type) {
  532. value = wrapf((double)p_x, (double)p_min, (double)p_max);
  533. } else {
  534. value = wrapi((int)p_x, (int)p_min, (int)p_max);
  535. }
  536. } break;
  537. case Variant::FLOAT: {
  538. value = wrapf((double)p_x, (double)p_min, (double)p_max);
  539. } break;
  540. default:
  541. break;
  542. }
  543. r_error.error = Callable::CallError::CALL_OK;
  544. return value;
  545. }
  546. int64_t VariantUtilityFunctions::wrapi(int64_t value, int64_t min, int64_t max) {
  547. return Math::wrapi(value, min, max);
  548. }
  549. double VariantUtilityFunctions::wrapf(double value, double min, double max) {
  550. return Math::wrapf(value, min, max);
  551. }
  552. double VariantUtilityFunctions::pingpong(double value, double length) {
  553. return Math::pingpong(value, length);
  554. }
  555. Variant VariantUtilityFunctions::max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  556. if (p_argcount < 2) {
  557. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  558. r_error.expected = 2;
  559. return Variant();
  560. }
  561. Variant base = *p_args[0];
  562. Variant ret;
  563. for (int i = 0; i < p_argcount; i++) {
  564. Variant::Type arg_type = p_args[i]->get_type();
  565. if (arg_type != Variant::INT && arg_type != Variant::FLOAT) {
  566. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  567. r_error.argument = i;
  568. r_error.expected = Variant::FLOAT;
  569. return Variant();
  570. }
  571. if (i == 0) {
  572. continue;
  573. }
  574. bool valid;
  575. Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid);
  576. if (!valid) {
  577. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  578. r_error.argument = i;
  579. r_error.expected = base.get_type();
  580. return Variant();
  581. }
  582. if (ret.booleanize()) {
  583. base = *p_args[i];
  584. }
  585. }
  586. r_error.error = Callable::CallError::CALL_OK;
  587. return base;
  588. }
  589. double VariantUtilityFunctions::maxf(double x, double y) {
  590. return MAX(x, y);
  591. }
  592. int64_t VariantUtilityFunctions::maxi(int64_t x, int64_t y) {
  593. return MAX(x, y);
  594. }
  595. Variant VariantUtilityFunctions::min(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  596. if (p_argcount < 2) {
  597. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  598. r_error.expected = 2;
  599. return Variant();
  600. }
  601. Variant base = *p_args[0];
  602. Variant ret;
  603. for (int i = 0; i < p_argcount; i++) {
  604. Variant::Type arg_type = p_args[i]->get_type();
  605. if (arg_type != Variant::INT && arg_type != Variant::FLOAT) {
  606. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  607. r_error.argument = i;
  608. r_error.expected = Variant::FLOAT;
  609. return Variant();
  610. }
  611. if (i == 0) {
  612. continue;
  613. }
  614. bool valid;
  615. Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid);
  616. if (!valid) {
  617. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  618. r_error.argument = i;
  619. r_error.expected = base.get_type();
  620. return Variant();
  621. }
  622. if (ret.booleanize()) {
  623. base = *p_args[i];
  624. }
  625. }
  626. r_error.error = Callable::CallError::CALL_OK;
  627. return base;
  628. }
  629. double VariantUtilityFunctions::minf(double x, double y) {
  630. return MIN(x, y);
  631. }
  632. int64_t VariantUtilityFunctions::mini(int64_t x, int64_t y) {
  633. return MIN(x, y);
  634. }
  635. Variant VariantUtilityFunctions::clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error) {
  636. Variant value = x;
  637. Variant ret;
  638. bool valid;
  639. Variant::evaluate(Variant::OP_LESS, value, min, ret, valid);
  640. if (!valid) {
  641. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  642. r_error.argument = 1;
  643. r_error.expected = value.get_type();
  644. return Variant();
  645. }
  646. if (ret.booleanize()) {
  647. value = min;
  648. }
  649. Variant::evaluate(Variant::OP_GREATER, value, max, ret, valid);
  650. if (!valid) {
  651. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  652. r_error.argument = 2;
  653. r_error.expected = value.get_type();
  654. return Variant();
  655. }
  656. if (ret.booleanize()) {
  657. value = max;
  658. }
  659. r_error.error = Callable::CallError::CALL_OK;
  660. return value;
  661. }
  662. double VariantUtilityFunctions::clampf(double x, double min, double max) {
  663. return CLAMP(x, min, max);
  664. }
  665. int64_t VariantUtilityFunctions::clampi(int64_t x, int64_t min, int64_t max) {
  666. return CLAMP(x, min, max);
  667. }
  668. int64_t VariantUtilityFunctions::nearest_po2(int64_t x) {
  669. return nearest_power_of_2_templated(uint64_t(x));
  670. }
  671. // Random
  672. void VariantUtilityFunctions::randomize() {
  673. Math::randomize();
  674. }
  675. int64_t VariantUtilityFunctions::randi() {
  676. return Math::rand();
  677. }
  678. double VariantUtilityFunctions::randf() {
  679. return Math::randf();
  680. }
  681. double VariantUtilityFunctions::randfn(double mean, double deviation) {
  682. return Math::randfn(mean, deviation);
  683. }
  684. int64_t VariantUtilityFunctions::randi_range(int64_t from, int64_t to) {
  685. return Math::random((int32_t)from, (int32_t)to);
  686. }
  687. double VariantUtilityFunctions::randf_range(double from, double to) {
  688. return Math::random(from, to);
  689. }
  690. void VariantUtilityFunctions::seed(int64_t s) {
  691. return Math::seed(s);
  692. }
  693. PackedInt64Array VariantUtilityFunctions::rand_from_seed(int64_t seed) {
  694. uint64_t s = seed;
  695. PackedInt64Array arr;
  696. arr.resize(2);
  697. arr.write[0] = Math::rand_from_seed(&s);
  698. arr.write[1] = s;
  699. return arr;
  700. }
  701. // Utility
  702. Variant VariantUtilityFunctions::weakref(const Variant &obj, Callable::CallError &r_error) {
  703. if (obj.get_type() == Variant::OBJECT) {
  704. r_error.error = Callable::CallError::CALL_OK;
  705. if (obj.is_ref_counted()) {
  706. Ref<WeakRef> wref = memnew(WeakRef);
  707. Ref<RefCounted> r = obj;
  708. if (r.is_valid()) {
  709. wref->set_ref(r);
  710. }
  711. return wref;
  712. } else {
  713. Ref<WeakRef> wref = memnew(WeakRef);
  714. Object *o = obj.get_validated_object();
  715. if (o) {
  716. wref->set_obj(o);
  717. }
  718. return wref;
  719. }
  720. } else if (obj.get_type() == Variant::NIL) {
  721. r_error.error = Callable::CallError::CALL_OK;
  722. Ref<WeakRef> wref = memnew(WeakRef);
  723. return wref;
  724. } else {
  725. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  726. r_error.argument = 0;
  727. r_error.expected = Variant::OBJECT;
  728. return Variant();
  729. }
  730. }
  731. int64_t VariantUtilityFunctions::_typeof(const Variant &obj) {
  732. return obj.get_type();
  733. }
  734. Variant VariantUtilityFunctions::type_convert(const Variant &p_variant, const Variant::Type p_type) {
  735. switch (p_type) {
  736. case Variant::Type::NIL:
  737. return Variant();
  738. case Variant::Type::BOOL:
  739. return p_variant.operator bool();
  740. case Variant::Type::INT:
  741. return p_variant.operator int64_t();
  742. case Variant::Type::FLOAT:
  743. return p_variant.operator double();
  744. case Variant::Type::STRING:
  745. return p_variant.operator String();
  746. case Variant::Type::VECTOR2:
  747. return p_variant.operator Vector2();
  748. case Variant::Type::VECTOR2I:
  749. return p_variant.operator Vector2i();
  750. case Variant::Type::RECT2:
  751. return p_variant.operator Rect2();
  752. case Variant::Type::RECT2I:
  753. return p_variant.operator Rect2i();
  754. case Variant::Type::VECTOR3:
  755. return p_variant.operator Vector3();
  756. case Variant::Type::VECTOR3I:
  757. return p_variant.operator Vector3i();
  758. case Variant::Type::TRANSFORM2D:
  759. return p_variant.operator Transform2D();
  760. case Variant::Type::VECTOR4:
  761. return p_variant.operator Vector4();
  762. case Variant::Type::VECTOR4I:
  763. return p_variant.operator Vector4i();
  764. case Variant::Type::PLANE:
  765. return p_variant.operator Plane();
  766. case Variant::Type::QUATERNION:
  767. return p_variant.operator Quaternion();
  768. case Variant::Type::AABB:
  769. return p_variant.operator ::AABB();
  770. case Variant::Type::BASIS:
  771. return p_variant.operator Basis();
  772. case Variant::Type::TRANSFORM3D:
  773. return p_variant.operator Transform3D();
  774. case Variant::Type::PROJECTION:
  775. return p_variant.operator Projection();
  776. case Variant::Type::COLOR:
  777. return p_variant.operator Color();
  778. case Variant::Type::STRING_NAME:
  779. return p_variant.operator StringName();
  780. case Variant::Type::NODE_PATH:
  781. return p_variant.operator NodePath();
  782. case Variant::Type::RID:
  783. return p_variant.operator ::RID();
  784. case Variant::Type::OBJECT:
  785. return p_variant.operator Object *();
  786. case Variant::Type::CALLABLE:
  787. return p_variant.operator Callable();
  788. case Variant::Type::SIGNAL:
  789. return p_variant.operator Signal();
  790. case Variant::Type::DICTIONARY:
  791. return p_variant.operator Dictionary();
  792. case Variant::Type::ARRAY:
  793. return p_variant.operator Array();
  794. case Variant::Type::PACKED_BYTE_ARRAY:
  795. return p_variant.operator PackedByteArray();
  796. case Variant::Type::PACKED_INT32_ARRAY:
  797. return p_variant.operator PackedInt32Array();
  798. case Variant::Type::PACKED_INT64_ARRAY:
  799. return p_variant.operator PackedInt64Array();
  800. case Variant::Type::PACKED_FLOAT32_ARRAY:
  801. return p_variant.operator PackedFloat32Array();
  802. case Variant::Type::PACKED_FLOAT64_ARRAY:
  803. return p_variant.operator PackedFloat64Array();
  804. case Variant::Type::PACKED_STRING_ARRAY:
  805. return p_variant.operator PackedStringArray();
  806. case Variant::Type::PACKED_VECTOR2_ARRAY:
  807. return p_variant.operator PackedVector2Array();
  808. case Variant::Type::PACKED_VECTOR3_ARRAY:
  809. return p_variant.operator PackedVector3Array();
  810. case Variant::Type::PACKED_COLOR_ARRAY:
  811. return p_variant.operator PackedColorArray();
  812. case Variant::Type::VARIANT_MAX:
  813. ERR_PRINT("Invalid type argument to type_convert(), use the TYPE_* constants. Returning the unconverted Variant.");
  814. }
  815. return p_variant;
  816. }
  817. String VariantUtilityFunctions::str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  818. if (p_arg_count < 1) {
  819. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  820. r_error.expected = 1;
  821. return String();
  822. }
  823. String s;
  824. for (int i = 0; i < p_arg_count; i++) {
  825. String os = p_args[i]->operator String();
  826. if (i == 0) {
  827. s = os;
  828. } else {
  829. s += os;
  830. }
  831. }
  832. r_error.error = Callable::CallError::CALL_OK;
  833. return s;
  834. }
  835. String VariantUtilityFunctions::error_string(Error error) {
  836. if (error < 0 || error >= ERR_MAX) {
  837. return String("(invalid error code)");
  838. }
  839. return String(error_names[error]);
  840. }
  841. String VariantUtilityFunctions::type_string(Variant::Type p_type) {
  842. ERR_FAIL_INDEX_V_MSG((int)p_type, (int)Variant::VARIANT_MAX, "<invalid type>", "Invalid type argument to type_string(), use the TYPE_* constants.");
  843. return Variant::get_type_name(p_type);
  844. }
  845. void VariantUtilityFunctions::print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  846. String s;
  847. for (int i = 0; i < p_arg_count; i++) {
  848. String os = p_args[i]->operator String();
  849. if (i == 0) {
  850. s = os;
  851. } else {
  852. s += os;
  853. }
  854. }
  855. print_line(s);
  856. r_error.error = Callable::CallError::CALL_OK;
  857. }
  858. void VariantUtilityFunctions::print_rich(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  859. String s;
  860. for (int i = 0; i < p_arg_count; i++) {
  861. String os = p_args[i]->operator String();
  862. if (i == 0) {
  863. s = os;
  864. } else {
  865. s += os;
  866. }
  867. }
  868. print_line_rich(s);
  869. r_error.error = Callable::CallError::CALL_OK;
  870. }
  871. #undef print_verbose
  872. void VariantUtilityFunctions::print_verbose(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  873. if (OS::get_singleton()->is_stdout_verbose()) {
  874. String s;
  875. for (int i = 0; i < p_arg_count; i++) {
  876. String os = p_args[i]->operator String();
  877. if (i == 0) {
  878. s = os;
  879. } else {
  880. s += os;
  881. }
  882. }
  883. // No need to use `print_verbose()` as this call already only happens
  884. // when verbose mode is enabled. This avoids performing string argument concatenation
  885. // when not needed.
  886. print_line(s);
  887. }
  888. r_error.error = Callable::CallError::CALL_OK;
  889. }
  890. void VariantUtilityFunctions::printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  891. String s;
  892. for (int i = 0; i < p_arg_count; i++) {
  893. String os = p_args[i]->operator String();
  894. if (i == 0) {
  895. s = os;
  896. } else {
  897. s += os;
  898. }
  899. }
  900. print_error(s);
  901. r_error.error = Callable::CallError::CALL_OK;
  902. }
  903. void VariantUtilityFunctions::printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  904. String s;
  905. for (int i = 0; i < p_arg_count; i++) {
  906. if (i) {
  907. s += "\t";
  908. }
  909. s += p_args[i]->operator String();
  910. }
  911. print_line(s);
  912. r_error.error = Callable::CallError::CALL_OK;
  913. }
  914. void VariantUtilityFunctions::prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  915. String s;
  916. for (int i = 0; i < p_arg_count; i++) {
  917. if (i) {
  918. s += " ";
  919. }
  920. s += p_args[i]->operator String();
  921. }
  922. print_line(s);
  923. r_error.error = Callable::CallError::CALL_OK;
  924. }
  925. void VariantUtilityFunctions::printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  926. String s;
  927. for (int i = 0; i < p_arg_count; i++) {
  928. String os = p_args[i]->operator String();
  929. if (i == 0) {
  930. s = os;
  931. } else {
  932. s += os;
  933. }
  934. }
  935. OS::get_singleton()->print("%s", s.utf8().get_data());
  936. r_error.error = Callable::CallError::CALL_OK;
  937. }
  938. void VariantUtilityFunctions::push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  939. if (p_arg_count < 1) {
  940. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  941. r_error.expected = 1;
  942. }
  943. String s;
  944. for (int i = 0; i < p_arg_count; i++) {
  945. String os = p_args[i]->operator String();
  946. if (i == 0) {
  947. s = os;
  948. } else {
  949. s += os;
  950. }
  951. }
  952. ERR_PRINT(s);
  953. r_error.error = Callable::CallError::CALL_OK;
  954. }
  955. void VariantUtilityFunctions::push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  956. if (p_arg_count < 1) {
  957. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  958. r_error.expected = 1;
  959. }
  960. String s;
  961. for (int i = 0; i < p_arg_count; i++) {
  962. String os = p_args[i]->operator String();
  963. if (i == 0) {
  964. s = os;
  965. } else {
  966. s += os;
  967. }
  968. }
  969. WARN_PRINT(s);
  970. r_error.error = Callable::CallError::CALL_OK;
  971. }
  972. String VariantUtilityFunctions::var_to_str(const Variant &p_var) {
  973. String vars;
  974. VariantWriter::write_to_string(p_var, vars);
  975. return vars;
  976. }
  977. Variant VariantUtilityFunctions::str_to_var(const String &p_var) {
  978. VariantParser::StreamString ss;
  979. ss.s = p_var;
  980. String errs;
  981. int line;
  982. Variant ret;
  983. (void)VariantParser::parse(&ss, ret, errs, line);
  984. return ret;
  985. }
  986. PackedByteArray VariantUtilityFunctions::var_to_bytes(const Variant &p_var) {
  987. int len;
  988. Error err = encode_variant(p_var, nullptr, len, false);
  989. if (err != OK) {
  990. return PackedByteArray();
  991. }
  992. PackedByteArray barr;
  993. barr.resize(len);
  994. {
  995. uint8_t *w = barr.ptrw();
  996. err = encode_variant(p_var, w, len, false);
  997. if (err != OK) {
  998. return PackedByteArray();
  999. }
  1000. }
  1001. return barr;
  1002. }
  1003. PackedByteArray VariantUtilityFunctions::var_to_bytes_with_objects(const Variant &p_var) {
  1004. int len;
  1005. Error err = encode_variant(p_var, nullptr, len, true);
  1006. if (err != OK) {
  1007. return PackedByteArray();
  1008. }
  1009. PackedByteArray barr;
  1010. barr.resize(len);
  1011. {
  1012. uint8_t *w = barr.ptrw();
  1013. err = encode_variant(p_var, w, len, true);
  1014. if (err != OK) {
  1015. return PackedByteArray();
  1016. }
  1017. }
  1018. return barr;
  1019. }
  1020. Variant VariantUtilityFunctions::bytes_to_var(const PackedByteArray &p_arr) {
  1021. Variant ret;
  1022. {
  1023. const uint8_t *r = p_arr.ptr();
  1024. Error err = decode_variant(ret, r, p_arr.size(), nullptr, false);
  1025. if (err != OK) {
  1026. return Variant();
  1027. }
  1028. }
  1029. return ret;
  1030. }
  1031. Variant VariantUtilityFunctions::bytes_to_var_with_objects(const PackedByteArray &p_arr) {
  1032. Variant ret;
  1033. {
  1034. const uint8_t *r = p_arr.ptr();
  1035. Error err = decode_variant(ret, r, p_arr.size(), nullptr, true);
  1036. if (err != OK) {
  1037. return Variant();
  1038. }
  1039. }
  1040. return ret;
  1041. }
  1042. int64_t VariantUtilityFunctions::hash(const Variant &p_arr) {
  1043. return p_arr.hash();
  1044. }
  1045. Object *VariantUtilityFunctions::instance_from_id(int64_t p_id) {
  1046. ObjectID id = ObjectID((uint64_t)p_id);
  1047. Object *ret = ObjectDB::get_instance(id);
  1048. return ret;
  1049. }
  1050. bool VariantUtilityFunctions::is_instance_id_valid(int64_t p_id) {
  1051. return ObjectDB::get_instance(ObjectID((uint64_t)p_id)) != nullptr;
  1052. }
  1053. bool VariantUtilityFunctions::is_instance_valid(const Variant &p_instance) {
  1054. if (p_instance.get_type() != Variant::OBJECT) {
  1055. return false;
  1056. }
  1057. return p_instance.get_validated_object() != nullptr;
  1058. }
  1059. uint64_t VariantUtilityFunctions::rid_allocate_id() {
  1060. return RID_AllocBase::_gen_id();
  1061. }
  1062. RID VariantUtilityFunctions::rid_from_int64(uint64_t p_base) {
  1063. return RID::from_uint64(p_base);
  1064. }
  1065. bool VariantUtilityFunctions::is_same(const Variant &p_a, const Variant &p_b) {
  1066. return p_a.identity_compare(p_b);
  1067. }
  1068. #ifdef DEBUG_METHODS_ENABLED
  1069. #define VCALLR *ret = p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...)
  1070. #define VCALL p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...)
  1071. #else
  1072. #define VCALLR *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...)
  1073. #define VCALL p_func(VariantCaster<P>::cast(*p_args[Is])...)
  1074. #endif
  1075. template <class R, class... P, size_t... Is>
  1076. static _FORCE_INLINE_ void call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  1077. r_error.error = Callable::CallError::CALL_OK;
  1078. VCALLR;
  1079. (void)p_args; // avoid gcc warning
  1080. (void)r_error;
  1081. }
  1082. template <class R, class... P, size_t... Is>
  1083. static _FORCE_INLINE_ void validated_call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, IndexSequence<Is...>) {
  1084. *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...);
  1085. (void)p_args;
  1086. }
  1087. template <class R, class... P, size_t... Is>
  1088. static _FORCE_INLINE_ void ptr_call_helperpr(R (*p_func)(P...), void *ret, const void **p_args, IndexSequence<Is...>) {
  1089. PtrToArg<R>::encode(p_func(PtrToArg<P>::convert(p_args[Is])...), ret);
  1090. (void)p_args;
  1091. }
  1092. template <class R, class... P>
  1093. static _FORCE_INLINE_ void call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error) {
  1094. call_helperpr(p_func, ret, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  1095. }
  1096. template <class R, class... P>
  1097. static _FORCE_INLINE_ void validated_call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args) {
  1098. validated_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{});
  1099. }
  1100. template <class R, class... P>
  1101. static _FORCE_INLINE_ void ptr_call_helperr(R (*p_func)(P...), void *ret, const void **p_args) {
  1102. ptr_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{});
  1103. }
  1104. template <class R, class... P>
  1105. static _FORCE_INLINE_ int get_arg_count_helperr(R (*p_func)(P...)) {
  1106. return sizeof...(P);
  1107. }
  1108. template <class R, class... P>
  1109. static _FORCE_INLINE_ Variant::Type get_arg_type_helperr(R (*p_func)(P...), int p_arg) {
  1110. return call_get_argument_type<P...>(p_arg);
  1111. }
  1112. template <class R, class... P>
  1113. static _FORCE_INLINE_ Variant::Type get_ret_type_helperr(R (*p_func)(P...)) {
  1114. return GetTypeInfo<R>::VARIANT_TYPE;
  1115. }
  1116. // WITHOUT RET
  1117. template <class... P, size_t... Is>
  1118. static _FORCE_INLINE_ void call_helperp(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  1119. r_error.error = Callable::CallError::CALL_OK;
  1120. VCALL;
  1121. (void)p_args;
  1122. (void)r_error;
  1123. }
  1124. template <class... P, size_t... Is>
  1125. static _FORCE_INLINE_ void validated_call_helperp(void (*p_func)(P...), const Variant **p_args, IndexSequence<Is...>) {
  1126. p_func(VariantCaster<P>::cast(*p_args[Is])...);
  1127. (void)p_args;
  1128. }
  1129. template <class... P, size_t... Is>
  1130. static _FORCE_INLINE_ void ptr_call_helperp(void (*p_func)(P...), const void **p_args, IndexSequence<Is...>) {
  1131. p_func(PtrToArg<P>::convert(p_args[Is])...);
  1132. (void)p_args;
  1133. }
  1134. template <class... P>
  1135. static _FORCE_INLINE_ void call_helper(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error) {
  1136. call_helperp(p_func, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  1137. }
  1138. template <class... P>
  1139. static _FORCE_INLINE_ void validated_call_helper(void (*p_func)(P...), const Variant **p_args) {
  1140. validated_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{});
  1141. }
  1142. template <class... P>
  1143. static _FORCE_INLINE_ void ptr_call_helper(void (*p_func)(P...), const void **p_args) {
  1144. ptr_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{});
  1145. }
  1146. template <class... P>
  1147. static _FORCE_INLINE_ int get_arg_count_helper(void (*p_func)(P...)) {
  1148. return sizeof...(P);
  1149. }
  1150. template <class... P>
  1151. static _FORCE_INLINE_ Variant::Type get_arg_type_helper(void (*p_func)(P...), int p_arg) {
  1152. return call_get_argument_type<P...>(p_arg);
  1153. }
  1154. template <class... P>
  1155. static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) {
  1156. return Variant::NIL;
  1157. }
  1158. #define FUNCBINDR(m_func, m_args, m_category) \
  1159. class Func_##m_func { \
  1160. public: \
  1161. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1162. call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args, r_error); \
  1163. } \
  1164. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1165. validated_call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args); \
  1166. } \
  1167. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1168. ptr_call_helperr(VariantUtilityFunctions::m_func, ret, p_args); \
  1169. } \
  1170. static int get_argument_count() { \
  1171. return get_arg_count_helperr(VariantUtilityFunctions::m_func); \
  1172. } \
  1173. static Variant::Type get_argument_type(int p_arg) { \
  1174. return get_arg_type_helperr(VariantUtilityFunctions::m_func, p_arg); \
  1175. } \
  1176. static Variant::Type get_return_type() { \
  1177. return get_ret_type_helperr(VariantUtilityFunctions::m_func); \
  1178. } \
  1179. static bool has_return_type() { \
  1180. return true; \
  1181. } \
  1182. static bool is_vararg() { return false; } \
  1183. static Variant::UtilityFunctionType get_type() { return m_category; } \
  1184. }; \
  1185. register_utility_function<Func_##m_func>(#m_func, m_args)
  1186. #define FUNCBINDVR(m_func, m_args, m_category) \
  1187. class Func_##m_func { \
  1188. public: \
  1189. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1190. r_error.error = Callable::CallError::CALL_OK; \
  1191. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], r_error); \
  1192. } \
  1193. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1194. Callable::CallError ce; \
  1195. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], ce); \
  1196. } \
  1197. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1198. Callable::CallError ce; \
  1199. PtrToArg<Variant>::encode(VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), ce), ret); \
  1200. } \
  1201. static int get_argument_count() { \
  1202. return 1; \
  1203. } \
  1204. static Variant::Type get_argument_type(int p_arg) { \
  1205. return Variant::NIL; \
  1206. } \
  1207. static Variant::Type get_return_type() { \
  1208. return Variant::NIL; \
  1209. } \
  1210. static bool has_return_type() { \
  1211. return true; \
  1212. } \
  1213. static bool is_vararg() { return false; } \
  1214. static Variant::UtilityFunctionType get_type() { return m_category; } \
  1215. }; \
  1216. register_utility_function<Func_##m_func>(#m_func, m_args)
  1217. #define FUNCBINDVR2(m_func, m_args, m_category) \
  1218. class Func_##m_func { \
  1219. public: \
  1220. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1221. r_error.error = Callable::CallError::CALL_OK; \
  1222. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], r_error); \
  1223. } \
  1224. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1225. Callable::CallError ce; \
  1226. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], ce); \
  1227. } \
  1228. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1229. Callable::CallError ce; \
  1230. Variant r; \
  1231. r = VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), PtrToArg<Variant>::convert(p_args[1]), ce); \
  1232. PtrToArg<Variant>::encode(r, ret); \
  1233. } \
  1234. static int get_argument_count() { \
  1235. return 2; \
  1236. } \
  1237. static Variant::Type get_argument_type(int p_arg) { \
  1238. return Variant::NIL; \
  1239. } \
  1240. static Variant::Type get_return_type() { \
  1241. return Variant::NIL; \
  1242. } \
  1243. static bool has_return_type() { \
  1244. return true; \
  1245. } \
  1246. static bool is_vararg() { return false; } \
  1247. static Variant::UtilityFunctionType get_type() { return m_category; } \
  1248. }; \
  1249. register_utility_function<Func_##m_func>(#m_func, m_args)
  1250. #define FUNCBINDVR3(m_func, m_args, m_category) \
  1251. class Func_##m_func { \
  1252. public: \
  1253. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1254. r_error.error = Callable::CallError::CALL_OK; \
  1255. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], r_error); \
  1256. } \
  1257. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1258. Callable::CallError ce; \
  1259. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], ce); \
  1260. } \
  1261. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1262. Callable::CallError ce; \
  1263. Variant r; \
  1264. r = VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), PtrToArg<Variant>::convert(p_args[1]), PtrToArg<Variant>::convert(p_args[2]), ce); \
  1265. PtrToArg<Variant>::encode(r, ret); \
  1266. } \
  1267. static int get_argument_count() { \
  1268. return 3; \
  1269. } \
  1270. static Variant::Type get_argument_type(int p_arg) { \
  1271. return Variant::NIL; \
  1272. } \
  1273. static Variant::Type get_return_type() { \
  1274. return Variant::NIL; \
  1275. } \
  1276. static bool has_return_type() { \
  1277. return true; \
  1278. } \
  1279. static bool is_vararg() { return false; } \
  1280. static Variant::UtilityFunctionType get_type() { return m_category; } \
  1281. }; \
  1282. register_utility_function<Func_##m_func>(#m_func, m_args)
  1283. #define FUNCBINDVARARG(m_func, m_args, m_category) \
  1284. class Func_##m_func { \
  1285. public: \
  1286. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1287. r_error.error = Callable::CallError::CALL_OK; \
  1288. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
  1289. } \
  1290. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1291. Callable::CallError c; \
  1292. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
  1293. } \
  1294. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1295. Vector<Variant> args; \
  1296. for (int i = 0; i < p_argcount; i++) { \
  1297. args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
  1298. } \
  1299. Vector<const Variant *> argsp; \
  1300. for (int i = 0; i < p_argcount; i++) { \
  1301. argsp.push_back(&args[i]); \
  1302. } \
  1303. Variant r; \
  1304. validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
  1305. PtrToArg<Variant>::encode(r, ret); \
  1306. } \
  1307. static int get_argument_count() { \
  1308. return 2; \
  1309. } \
  1310. static Variant::Type get_argument_type(int p_arg) { \
  1311. return Variant::NIL; \
  1312. } \
  1313. static Variant::Type get_return_type() { \
  1314. return Variant::NIL; \
  1315. } \
  1316. static bool has_return_type() { \
  1317. return true; \
  1318. } \
  1319. static bool is_vararg() { \
  1320. return true; \
  1321. } \
  1322. static Variant::UtilityFunctionType get_type() { \
  1323. return m_category; \
  1324. } \
  1325. }; \
  1326. register_utility_function<Func_##m_func>(#m_func, m_args)
  1327. #define FUNCBINDVARARGS(m_func, m_args, m_category) \
  1328. class Func_##m_func { \
  1329. public: \
  1330. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1331. r_error.error = Callable::CallError::CALL_OK; \
  1332. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
  1333. } \
  1334. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1335. Callable::CallError c; \
  1336. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
  1337. } \
  1338. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1339. Vector<Variant> args; \
  1340. for (int i = 0; i < p_argcount; i++) { \
  1341. args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
  1342. } \
  1343. Vector<const Variant *> argsp; \
  1344. for (int i = 0; i < p_argcount; i++) { \
  1345. argsp.push_back(&args[i]); \
  1346. } \
  1347. Variant r; \
  1348. validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
  1349. PtrToArg<String>::encode(r.operator String(), ret); \
  1350. } \
  1351. static int get_argument_count() { \
  1352. return 1; \
  1353. } \
  1354. static Variant::Type get_argument_type(int p_arg) { \
  1355. return Variant::NIL; \
  1356. } \
  1357. static Variant::Type get_return_type() { \
  1358. return Variant::STRING; \
  1359. } \
  1360. static bool has_return_type() { \
  1361. return true; \
  1362. } \
  1363. static bool is_vararg() { \
  1364. return true; \
  1365. } \
  1366. static Variant::UtilityFunctionType get_type() { \
  1367. return m_category; \
  1368. } \
  1369. }; \
  1370. register_utility_function<Func_##m_func>(#m_func, m_args)
  1371. #define FUNCBINDVARARGV(m_func, m_args, m_category) \
  1372. class Func_##m_func { \
  1373. public: \
  1374. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1375. r_error.error = Callable::CallError::CALL_OK; \
  1376. VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
  1377. } \
  1378. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1379. Callable::CallError c; \
  1380. VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
  1381. } \
  1382. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1383. Vector<Variant> args; \
  1384. for (int i = 0; i < p_argcount; i++) { \
  1385. args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
  1386. } \
  1387. Vector<const Variant *> argsp; \
  1388. for (int i = 0; i < p_argcount; i++) { \
  1389. argsp.push_back(&args[i]); \
  1390. } \
  1391. Variant r; \
  1392. validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
  1393. } \
  1394. static int get_argument_count() { \
  1395. return 1; \
  1396. } \
  1397. static Variant::Type get_argument_type(int p_arg) { \
  1398. return Variant::NIL; \
  1399. } \
  1400. static Variant::Type get_return_type() { \
  1401. return Variant::NIL; \
  1402. } \
  1403. static bool has_return_type() { \
  1404. return false; \
  1405. } \
  1406. static bool is_vararg() { \
  1407. return true; \
  1408. } \
  1409. static Variant::UtilityFunctionType get_type() { \
  1410. return m_category; \
  1411. } \
  1412. }; \
  1413. register_utility_function<Func_##m_func>(#m_func, m_args)
  1414. #define FUNCBIND(m_func, m_args, m_category) \
  1415. class Func_##m_func { \
  1416. public: \
  1417. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  1418. call_helper(VariantUtilityFunctions::m_func, p_args, r_error); \
  1419. } \
  1420. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  1421. validated_call_helper(VariantUtilityFunctions::m_func, p_args); \
  1422. } \
  1423. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  1424. ptr_call_helper(VariantUtilityFunctions::m_func, p_args); \
  1425. } \
  1426. static int get_argument_count() { \
  1427. return get_arg_count_helper(VariantUtilityFunctions::m_func); \
  1428. } \
  1429. static Variant::Type get_argument_type(int p_arg) { \
  1430. return get_arg_type_helper(VariantUtilityFunctions::m_func, p_arg); \
  1431. } \
  1432. static Variant::Type get_return_type() { \
  1433. return get_ret_type_helper(VariantUtilityFunctions::m_func); \
  1434. } \
  1435. static bool has_return_type() { \
  1436. return false; \
  1437. } \
  1438. static bool is_vararg() { return false; } \
  1439. static Variant::UtilityFunctionType get_type() { return m_category; } \
  1440. }; \
  1441. register_utility_function<Func_##m_func>(#m_func, m_args)
  1442. struct VariantUtilityFunctionInfo {
  1443. void (*call_utility)(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = nullptr;
  1444. Variant::ValidatedUtilityFunction validated_call_utility = nullptr;
  1445. Variant::PTRUtilityFunction ptr_call_utility = nullptr;
  1446. Vector<String> argnames;
  1447. bool is_vararg = false;
  1448. bool returns_value = false;
  1449. int argcount = 0;
  1450. Variant::Type (*get_arg_type)(int) = nullptr;
  1451. Variant::Type return_type;
  1452. Variant::UtilityFunctionType type;
  1453. };
  1454. static OAHashMap<StringName, VariantUtilityFunctionInfo> utility_function_table;
  1455. static List<StringName> utility_function_name_table;
  1456. template <class T>
  1457. static void register_utility_function(const String &p_name, const Vector<String> &argnames) {
  1458. String name = p_name;
  1459. if (name.begins_with("_")) {
  1460. name = name.substr(1, name.length() - 1);
  1461. }
  1462. StringName sname = name;
  1463. ERR_FAIL_COND(utility_function_table.has(sname));
  1464. VariantUtilityFunctionInfo bfi;
  1465. bfi.call_utility = T::call;
  1466. bfi.validated_call_utility = T::validated_call;
  1467. bfi.ptr_call_utility = T::ptrcall;
  1468. bfi.is_vararg = T::is_vararg();
  1469. bfi.argnames = argnames;
  1470. bfi.argcount = T::get_argument_count();
  1471. if (!bfi.is_vararg) {
  1472. ERR_FAIL_COND_MSG(argnames.size() != bfi.argcount, "wrong number of arguments binding utility function: " + name);
  1473. }
  1474. bfi.get_arg_type = T::get_argument_type;
  1475. bfi.return_type = T::get_return_type();
  1476. bfi.type = T::get_type();
  1477. bfi.returns_value = T::has_return_type();
  1478. utility_function_table.insert(sname, bfi);
  1479. utility_function_name_table.push_back(sname);
  1480. }
  1481. void Variant::_register_variant_utility_functions() {
  1482. // Math
  1483. FUNCBINDR(sin, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  1484. FUNCBINDR(cos, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  1485. FUNCBINDR(tan, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  1486. FUNCBINDR(sinh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1487. FUNCBINDR(cosh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1488. FUNCBINDR(tanh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1489. FUNCBINDR(asin, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1490. FUNCBINDR(acos, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1491. FUNCBINDR(atan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1492. FUNCBINDR(atan2, sarray("y", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1493. FUNCBINDR(asinh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1494. FUNCBINDR(acosh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1495. FUNCBINDR(atanh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1496. FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1497. FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
  1498. FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
  1499. FUNCBINDR(posmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
  1500. FUNCBINDVR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1501. FUNCBINDR(floorf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1502. FUNCBINDR(floori, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1503. FUNCBINDVR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1504. FUNCBINDR(ceilf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1505. FUNCBINDR(ceili, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1506. FUNCBINDVR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1507. FUNCBINDR(roundf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1508. FUNCBINDR(roundi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1509. FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1510. FUNCBINDR(absf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1511. FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1512. FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1513. FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1514. FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1515. FUNCBINDVR2(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH);
  1516. FUNCBINDR(snappedf, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH);
  1517. FUNCBINDR(snappedi, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH);
  1518. FUNCBINDR(pow, sarray("base", "exp"), Variant::UTILITY_FUNC_TYPE_MATH);
  1519. FUNCBINDR(log, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1520. FUNCBINDR(exp, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1521. FUNCBINDR(is_nan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1522. FUNCBINDR(is_inf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1523. FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1524. FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1525. FUNCBINDR(is_finite, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1526. FUNCBINDR(ease, sarray("x", "curve"), Variant::UTILITY_FUNC_TYPE_MATH);
  1527. FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1528. FUNCBINDVR3(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1529. FUNCBINDR(lerpf, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1530. FUNCBINDR(cubic_interpolate, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1531. FUNCBINDR(cubic_interpolate_angle, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1532. FUNCBINDR(cubic_interpolate_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH);
  1533. FUNCBINDR(cubic_interpolate_angle_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH);
  1534. FUNCBINDR(bezier_interpolate, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH);
  1535. FUNCBINDR(bezier_derivative, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH);
  1536. FUNCBINDR(angle_difference, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_MATH);
  1537. FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1538. FUNCBINDR(inverse_lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1539. FUNCBINDR(remap, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH);
  1540. FUNCBINDR(smoothstep, sarray("from", "to", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1541. FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
  1542. FUNCBINDR(rotate_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
  1543. FUNCBINDR(deg_to_rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH);
  1544. FUNCBINDR(rad_to_deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  1545. FUNCBINDR(linear_to_db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH);
  1546. FUNCBINDR(db_to_linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH);
  1547. FUNCBINDVR3(wrap, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1548. FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1549. FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1550. FUNCBINDVARARG(max, sarray(), Variant::UTILITY_FUNC_TYPE_MATH);
  1551. FUNCBINDR(maxi, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1552. FUNCBINDR(maxf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1553. FUNCBINDVARARG(min, sarray(), Variant::UTILITY_FUNC_TYPE_MATH);
  1554. FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1555. FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1556. FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1557. FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1558. FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1559. FUNCBINDR(nearest_po2, sarray("value"), Variant::UTILITY_FUNC_TYPE_MATH);
  1560. FUNCBINDR(pingpong, sarray("value", "length"), Variant::UTILITY_FUNC_TYPE_MATH);
  1561. // Random
  1562. FUNCBIND(randomize, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1563. FUNCBINDR(randi, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1564. FUNCBINDR(randf, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1565. FUNCBINDR(randi_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1566. FUNCBINDR(randf_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1567. FUNCBINDR(randfn, sarray("mean", "deviation"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1568. FUNCBIND(seed, sarray("base"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1569. FUNCBINDR(rand_from_seed, sarray("seed"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1570. // Utility
  1571. FUNCBINDVR(weakref, sarray("obj"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1572. FUNCBINDR(_typeof, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1573. FUNCBINDR(type_convert, sarray("variant", "type"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1574. FUNCBINDVARARGS(str, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1575. FUNCBINDR(error_string, sarray("error"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1576. FUNCBINDR(type_string, sarray("type"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1577. FUNCBINDVARARGV(print, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1578. FUNCBINDVARARGV(print_rich, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1579. FUNCBINDVARARGV(printerr, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1580. FUNCBINDVARARGV(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1581. FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1582. FUNCBINDVARARGV(printraw, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1583. FUNCBINDVARARGV(print_verbose, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1584. FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1585. FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1586. FUNCBINDR(var_to_str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1587. FUNCBINDR(str_to_var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1588. FUNCBINDR(var_to_bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1589. FUNCBINDR(bytes_to_var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1590. FUNCBINDR(var_to_bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1591. FUNCBINDR(bytes_to_var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1592. FUNCBINDR(hash, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1593. FUNCBINDR(instance_from_id, sarray("instance_id"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1594. FUNCBINDR(is_instance_id_valid, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1595. FUNCBINDR(is_instance_valid, sarray("instance"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1596. FUNCBINDR(rid_allocate_id, Vector<String>(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1597. FUNCBINDR(rid_from_int64, sarray("base"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1598. FUNCBINDR(is_same, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1599. }
  1600. void Variant::_unregister_variant_utility_functions() {
  1601. utility_function_table.clear();
  1602. utility_function_name_table.clear();
  1603. }
  1604. void Variant::call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1605. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1606. if (!bfi) {
  1607. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1608. r_error.argument = 0;
  1609. r_error.expected = 0;
  1610. return;
  1611. }
  1612. if (unlikely(!bfi->is_vararg && p_argcount < bfi->argcount)) {
  1613. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1614. r_error.expected = bfi->argcount;
  1615. return;
  1616. }
  1617. if (unlikely(!bfi->is_vararg && p_argcount > bfi->argcount)) {
  1618. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1619. r_error.expected = bfi->argcount;
  1620. return;
  1621. }
  1622. bfi->call_utility(r_ret, p_args, p_argcount, r_error);
  1623. }
  1624. bool Variant::has_utility_function(const StringName &p_name) {
  1625. return utility_function_table.has(p_name);
  1626. }
  1627. Variant::ValidatedUtilityFunction Variant::get_validated_utility_function(const StringName &p_name) {
  1628. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1629. if (!bfi) {
  1630. return nullptr;
  1631. }
  1632. return bfi->validated_call_utility;
  1633. }
  1634. Variant::PTRUtilityFunction Variant::get_ptr_utility_function(const StringName &p_name) {
  1635. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1636. if (!bfi) {
  1637. return nullptr;
  1638. }
  1639. return bfi->ptr_call_utility;
  1640. }
  1641. Variant::UtilityFunctionType Variant::get_utility_function_type(const StringName &p_name) {
  1642. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1643. if (!bfi) {
  1644. return Variant::UTILITY_FUNC_TYPE_MATH;
  1645. }
  1646. return bfi->type;
  1647. }
  1648. MethodInfo Variant::get_utility_function_info(const StringName &p_name) {
  1649. MethodInfo info;
  1650. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1651. if (bfi) {
  1652. info.name = p_name;
  1653. if (bfi->returns_value && bfi->return_type == Variant::NIL) {
  1654. info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1655. }
  1656. info.return_val.type = bfi->return_type;
  1657. if (bfi->is_vararg) {
  1658. info.flags |= METHOD_FLAG_VARARG;
  1659. }
  1660. for (int i = 0; i < bfi->argnames.size(); ++i) {
  1661. PropertyInfo arg;
  1662. arg.type = bfi->get_arg_type(i);
  1663. arg.name = bfi->argnames[i];
  1664. info.arguments.push_back(arg);
  1665. }
  1666. }
  1667. return info;
  1668. }
  1669. int Variant::get_utility_function_argument_count(const StringName &p_name) {
  1670. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1671. if (!bfi) {
  1672. return 0;
  1673. }
  1674. return bfi->argcount;
  1675. }
  1676. Variant::Type Variant::get_utility_function_argument_type(const StringName &p_name, int p_arg) {
  1677. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1678. if (!bfi) {
  1679. return Variant::NIL;
  1680. }
  1681. return bfi->get_arg_type(p_arg);
  1682. }
  1683. String Variant::get_utility_function_argument_name(const StringName &p_name, int p_arg) {
  1684. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1685. if (!bfi) {
  1686. return String();
  1687. }
  1688. ERR_FAIL_INDEX_V(p_arg, bfi->argnames.size(), String());
  1689. ERR_FAIL_COND_V(bfi->is_vararg, String());
  1690. return bfi->argnames[p_arg];
  1691. }
  1692. bool Variant::has_utility_function_return_value(const StringName &p_name) {
  1693. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1694. if (!bfi) {
  1695. return false;
  1696. }
  1697. return bfi->returns_value;
  1698. }
  1699. Variant::Type Variant::get_utility_function_return_type(const StringName &p_name) {
  1700. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1701. if (!bfi) {
  1702. return Variant::NIL;
  1703. }
  1704. return bfi->return_type;
  1705. }
  1706. bool Variant::is_utility_function_vararg(const StringName &p_name) {
  1707. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1708. if (!bfi) {
  1709. return false;
  1710. }
  1711. return bfi->is_vararg;
  1712. }
  1713. uint32_t Variant::get_utility_function_hash(const StringName &p_name) {
  1714. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1715. ERR_FAIL_NULL_V(bfi, 0);
  1716. uint32_t hash = hash_murmur3_one_32(bfi->is_vararg);
  1717. hash = hash_murmur3_one_32(bfi->returns_value, hash);
  1718. if (bfi->returns_value) {
  1719. hash = hash_murmur3_one_32(bfi->return_type, hash);
  1720. }
  1721. hash = hash_murmur3_one_32(bfi->argcount, hash);
  1722. for (int i = 0; i < bfi->argcount; i++) {
  1723. hash = hash_murmur3_one_32(bfi->get_arg_type(i), hash);
  1724. }
  1725. return hash_fmix32(hash);
  1726. }
  1727. void Variant::get_utility_function_list(List<StringName> *r_functions) {
  1728. for (const StringName &E : utility_function_name_table) {
  1729. r_functions->push_back(E);
  1730. }
  1731. }
  1732. int Variant::get_utility_function_count() {
  1733. return utility_function_name_table.size();
  1734. }