variant_utility.cpp 87 KB

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