/src/lipsofsuna/script/script-args.c

https://gitlab.com/xenodora/lipsofsuna · C · 1164 lines · 1029 code · 98 blank · 37 comment · 242 complexity · 80fd4e661e49a33bbbc77e22f87b5da5 MD5 · raw file

  1. /* Lips of Suna
  2. * Copyright© 2007-2010 Lips of Suna development team.
  3. *
  4. * Lips of Suna is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation, either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Lips of Suna is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with Lips of Suna. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \addtogroup LIScr Script
  19. * @{
  20. * \addtogroup LIScrArgs Args
  21. * @{
  22. */
  23. #include "script-args.h"
  24. #include "script-library.h"
  25. #include "script-private.h"
  26. #include "script-util.h"
  27. void liscr_args_init_func (
  28. LIScrArgs* self,
  29. lua_State* lua,
  30. LIScrScript* script,
  31. LIScrData* data)
  32. {
  33. memset (self, 0, sizeof (LIScrArgs));
  34. self->lua = lua;
  35. self->script = script;
  36. self->data = data;
  37. if (data != NULL)
  38. {
  39. self->self = data->data;
  40. self->args_start = 2;
  41. }
  42. else
  43. self->args_start = 1;
  44. self->args_count = lua_gettop (lua) + 1 - self->args_start;
  45. if (lua_type (self->lua, self->args_start) == LUA_TTABLE)
  46. {
  47. self->input_mode = LISCR_ARGS_INPUT_TABLE;
  48. self->input_table = self->args_start;
  49. }
  50. }
  51. int liscr_args_geti_bool (
  52. LIScrArgs* self,
  53. int index,
  54. int* result)
  55. {
  56. int ret = 0;
  57. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  58. {
  59. lua_pushnumber (self->lua, index + 1);
  60. lua_gettable (self->lua, self->input_table);
  61. if (lua_type (self->lua, -1) == LUA_TBOOLEAN)
  62. {
  63. *result = (int) lua_toboolean (self->lua, -1);
  64. ret = 1;
  65. }
  66. lua_pop (self->lua, 1);
  67. }
  68. else
  69. {
  70. if (index < 0 || index >= self->args_count)
  71. return 0;
  72. index += self->args_start;
  73. if (lua_type (self->lua, index) == LUA_TBOOLEAN)
  74. {
  75. *result = (int) lua_toboolean (self->lua, index);
  76. ret = 1;
  77. }
  78. }
  79. return ret;
  80. }
  81. int liscr_args_geti_bool_convert (
  82. LIScrArgs* self,
  83. int index,
  84. int* result)
  85. {
  86. int ret = 0;
  87. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  88. {
  89. lua_pushnumber (self->lua, index + 1);
  90. lua_gettable (self->lua, self->input_table);
  91. *result = (int) lua_toboolean (self->lua, -1);
  92. ret = 1;
  93. lua_pop (self->lua, 1);
  94. }
  95. else
  96. {
  97. if (index < 0 || index >= self->args_count)
  98. return 0;
  99. index += self->args_start;
  100. *result = (int) lua_toboolean (self->lua, index);
  101. ret = 1;
  102. }
  103. return ret;
  104. }
  105. int
  106. liscr_args_geti_data (LIScrArgs* self,
  107. int index,
  108. const char* type,
  109. LIScrData** result)
  110. {
  111. LIScrData* tmp = NULL;
  112. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  113. {
  114. lua_pushnumber (self->lua, index + 1);
  115. lua_gettable (self->lua, self->input_table);
  116. if (type != NULL)
  117. tmp = liscr_isdata (self->lua, -1, type);
  118. else
  119. tmp = liscr_isanydata (self->lua, -1);
  120. lua_pop (self->lua, 1);
  121. if (tmp != NULL)
  122. *result = tmp;
  123. }
  124. else
  125. {
  126. if (index < 0 || index >= self->args_count)
  127. return 0;
  128. index += self->args_start;
  129. if (type != NULL)
  130. tmp = liscr_isdata (self->lua, index, type);
  131. else
  132. tmp = liscr_isanydata (self->lua, index);
  133. if (tmp != NULL)
  134. *result = tmp;
  135. }
  136. return tmp != NULL;
  137. }
  138. int liscr_args_geti_float (
  139. LIScrArgs* self,
  140. int index,
  141. float* result)
  142. {
  143. int ret = 0;
  144. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  145. {
  146. lua_pushnumber (self->lua, index + 1);
  147. lua_gettable (self->lua, self->input_table);
  148. if (lua_isnumber (self->lua, -1))
  149. {
  150. *result = lua_tonumber (self->lua, -1);
  151. if (isnan (*result) || isinf (*result))
  152. *result = 0.0f;
  153. ret = 1;
  154. }
  155. lua_pop (self->lua, 1);
  156. }
  157. else
  158. {
  159. if (index < 0 || index >= self->args_count)
  160. return 0;
  161. index += self->args_start;
  162. if (lua_isnumber (self->lua, index))
  163. {
  164. *result = lua_tonumber (self->lua, index);
  165. if (isnan (*result) || isinf (*result))
  166. *result = 0.0f;
  167. ret = 1;
  168. }
  169. }
  170. return ret;
  171. }
  172. int
  173. liscr_args_geti_int (LIScrArgs* self,
  174. int index,
  175. int* result)
  176. {
  177. int ret = 0;
  178. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  179. {
  180. lua_pushnumber (self->lua, index + 1);
  181. lua_gettable (self->lua, self->input_table);
  182. if (lua_isnumber (self->lua, -1))
  183. {
  184. *result = (int) lua_tonumber (self->lua, -1);
  185. ret = 1;
  186. }
  187. lua_pop (self->lua, 1);
  188. }
  189. else
  190. {
  191. if (index < 0 || index >= self->args_count)
  192. return 0;
  193. index += self->args_start;
  194. if (lua_isnumber (self->lua, index))
  195. {
  196. *result = (int) lua_tonumber (self->lua, index);
  197. ret = 1;
  198. }
  199. }
  200. return ret;
  201. }
  202. int liscr_args_geti_intv (
  203. LIScrArgs* self,
  204. int index,
  205. int count,
  206. int* result)
  207. {
  208. int i;
  209. int ret = 0;
  210. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  211. {
  212. lua_pushnumber (self->lua, index);
  213. lua_gettable (self->lua, self->input_table);
  214. if (lua_type (self->lua, -1) == LUA_TTABLE)
  215. {
  216. for (i = 0 ; i < count ; i++)
  217. {
  218. lua_pushnumber (self->lua, i + 1);
  219. lua_gettable (self->lua, -2);
  220. if (!lua_isnumber (self->lua, -1))
  221. {
  222. lua_pop (self->lua, 1);
  223. break;
  224. }
  225. result[i] = lua_tonumber (self->lua, -1);
  226. lua_pop (self->lua, 1);
  227. }
  228. ret = i;
  229. }
  230. lua_pop (self->lua, 1);
  231. }
  232. else
  233. {
  234. if (index < 0 || index >= self->args_count)
  235. return 0;
  236. index += self->args_start;
  237. if (lua_type (self->lua, index) == LUA_TTABLE)
  238. {
  239. for (i = 0 ; i < count ; i++)
  240. {
  241. lua_pushnumber (self->lua, i + 1);
  242. lua_gettable (self->lua, index);
  243. if (!lua_isnumber (self->lua, -1))
  244. {
  245. lua_pop (self->lua, 1);
  246. break;
  247. }
  248. result[i] = lua_tonumber (self->lua, -1);
  249. lua_pop (self->lua, 1);
  250. }
  251. ret = i;
  252. }
  253. }
  254. return ret;
  255. }
  256. int liscr_args_geti_quaternion (
  257. LIScrArgs* self,
  258. int index,
  259. LIMatQuaternion* result)
  260. {
  261. LIScrData* tmp = NULL;
  262. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  263. {
  264. lua_pushnumber (self->lua, index + 1);
  265. lua_gettable (self->lua, self->input_table);
  266. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_QUATERNION);
  267. lua_pop (self->lua, 1);
  268. if (tmp != NULL)
  269. {
  270. *result = *((LIMatQuaternion*) tmp->data);
  271. *result = limat_quaternion_validate (*result);
  272. }
  273. }
  274. else
  275. {
  276. if (index < 0 || index >= self->args_count)
  277. return 0;
  278. index += self->args_start;
  279. tmp = liscr_isdata (self->lua, index, LISCR_SCRIPT_QUATERNION);
  280. if (tmp != NULL)
  281. {
  282. *result = *((LIMatQuaternion*) tmp->data);
  283. *result = limat_quaternion_validate (*result);
  284. }
  285. }
  286. return tmp != NULL;
  287. }
  288. int
  289. liscr_args_geti_string (LIScrArgs* self,
  290. int index,
  291. const char** result)
  292. {
  293. const char* tmp = NULL;
  294. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  295. {
  296. lua_pushnumber (self->lua, index + 1);
  297. lua_gettable (self->lua, self->input_table);
  298. if (lua_type (self->lua, -1) == LUA_TSTRING)
  299. tmp = lua_tostring (self->lua, -1);
  300. lua_pop (self->lua, 1);
  301. if (tmp != NULL)
  302. *result = tmp;
  303. }
  304. else
  305. {
  306. if (index < 0 || index >= self->args_count)
  307. return 0;
  308. index += self->args_start;
  309. if (lua_type (self->lua, index) == LUA_TSTRING)
  310. tmp = lua_tostring (self->lua, index);
  311. if (tmp != NULL)
  312. *result = tmp;
  313. }
  314. return tmp != NULL;
  315. }
  316. /**
  317. * \brief Gets a table by index and pushes it to the stack.
  318. * \param self Arguments.
  319. * \param index Argument index.
  320. * \return Nonzero on success.
  321. */
  322. int liscr_args_geti_table (
  323. LIScrArgs* self,
  324. int index)
  325. {
  326. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  327. {
  328. lua_pushnumber (self->lua, index + 1);
  329. lua_gettable (self->lua, self->input_table);
  330. if (lua_type (self->lua, -1) == LUA_TTABLE)
  331. return 1;
  332. lua_pop (self->lua, 1);
  333. }
  334. else
  335. {
  336. if (index < 0 || index >= self->args_count)
  337. return 0;
  338. index += self->args_start;
  339. if (lua_type (self->lua, index) == LUA_TTABLE)
  340. {
  341. lua_pushvalue (self->lua, index);
  342. return 1;
  343. }
  344. }
  345. return 0;
  346. }
  347. int liscr_args_geti_vector (
  348. LIScrArgs* self,
  349. int index,
  350. LIMatVector* result)
  351. {
  352. LIScrData* tmp = NULL;
  353. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  354. {
  355. lua_pushnumber (self->lua, index + 1);
  356. lua_gettable (self->lua, self->input_table);
  357. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_VECTOR);
  358. lua_pop (self->lua, 1);
  359. if (tmp != NULL)
  360. {
  361. *result = *((LIMatVector*) tmp->data);
  362. *result = limat_vector_validate (*result);
  363. }
  364. }
  365. else
  366. {
  367. if (index < 0 || index >= self->args_count)
  368. return 0;
  369. index += self->args_start;
  370. tmp = liscr_isdata (self->lua, index, LISCR_SCRIPT_VECTOR);
  371. if (tmp != NULL)
  372. {
  373. *result = *((LIMatVector*) tmp->data);
  374. *result = limat_vector_validate (*result);
  375. }
  376. }
  377. return tmp != NULL;
  378. }
  379. int
  380. liscr_args_gets_bool (LIScrArgs* self,
  381. const char* name,
  382. int* result)
  383. {
  384. int ret = 0;
  385. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  386. {
  387. lua_getfield (self->lua, self->input_table, name);
  388. if (lua_type (self->lua, -1) == LUA_TBOOLEAN)
  389. {
  390. *result = (int) lua_toboolean (self->lua, -1);
  391. ret = 1;
  392. }
  393. lua_pop (self->lua, 1);
  394. }
  395. return ret;
  396. }
  397. int
  398. liscr_args_gets_data (LIScrArgs* self,
  399. const char* name,
  400. const char* type,
  401. LIScrData** result)
  402. {
  403. LIScrData* tmp = NULL;
  404. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  405. {
  406. lua_getfield (self->lua, self->input_table, name);
  407. if (type != NULL)
  408. tmp = liscr_isdata (self->lua, -1, type);
  409. else
  410. tmp = liscr_isanydata (self->lua, -1);
  411. lua_pop (self->lua, 1);
  412. if (tmp != NULL)
  413. *result = tmp;
  414. }
  415. return tmp != NULL;
  416. }
  417. int liscr_args_gets_float (
  418. LIScrArgs* self,
  419. const char* name,
  420. float* result)
  421. {
  422. int ret = 0;
  423. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  424. {
  425. lua_getfield (self->lua, self->input_table, name);
  426. if (lua_isnumber (self->lua, -1))
  427. {
  428. *result = lua_tonumber (self->lua, -1);
  429. if (isnan (*result) || isinf (*result))
  430. *result = 0.0f;
  431. ret = 1;
  432. }
  433. lua_pop (self->lua, 1);
  434. }
  435. return ret;
  436. }
  437. int liscr_args_gets_floatv (
  438. LIScrArgs* self,
  439. const char* name,
  440. int count,
  441. float* result)
  442. {
  443. int i;
  444. int ret = 0;
  445. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  446. {
  447. lua_getfield (self->lua, self->input_table, name);
  448. if (lua_type (self->lua, -1) == LUA_TTABLE)
  449. {
  450. for (i = 0 ; i < count ; i++)
  451. {
  452. lua_pushnumber (self->lua, i + 1);
  453. lua_gettable (self->lua, -2);
  454. if (!lua_isnumber (self->lua, -1))
  455. {
  456. lua_pop (self->lua, 1);
  457. break;
  458. }
  459. result[i] = lua_tonumber (self->lua, -1);
  460. if (isnan (result[i]) || isinf (result[i]))
  461. result[i] = 0.0f;
  462. lua_pop (self->lua, 1);
  463. }
  464. ret = i;
  465. }
  466. lua_pop (self->lua, 1);
  467. }
  468. return ret;
  469. }
  470. int liscr_args_gets_int (
  471. LIScrArgs* self,
  472. const char* name,
  473. int* result)
  474. {
  475. int ret = 0;
  476. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  477. {
  478. lua_getfield (self->lua, self->input_table, name);
  479. if (lua_isnumber (self->lua, -1))
  480. {
  481. *result = (int) lua_tonumber (self->lua, -1);
  482. ret = 1;
  483. }
  484. lua_pop (self->lua, 1);
  485. }
  486. return ret;
  487. }
  488. int liscr_args_gets_intv (
  489. LIScrArgs* self,
  490. const char* name,
  491. int count,
  492. int* result)
  493. {
  494. int i;
  495. int ret = 0;
  496. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  497. {
  498. lua_getfield (self->lua, self->input_table, name);
  499. if (lua_type (self->lua, -1) == LUA_TTABLE)
  500. {
  501. for (i = 0 ; i < count ; i++)
  502. {
  503. lua_pushnumber (self->lua, i + 1);
  504. lua_gettable (self->lua, -2);
  505. if (!lua_isnumber (self->lua, -1))
  506. {
  507. lua_pop (self->lua, 1);
  508. break;
  509. }
  510. result[i] = lua_tonumber (self->lua, -1);
  511. lua_pop (self->lua, 1);
  512. }
  513. ret = i;
  514. }
  515. lua_pop (self->lua, 1);
  516. }
  517. return ret;
  518. }
  519. int liscr_args_gets_quaternion (
  520. LIScrArgs* self,
  521. const char* name,
  522. LIMatQuaternion* result)
  523. {
  524. LIScrData* tmp = NULL;
  525. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  526. {
  527. lua_getfield (self->lua, self->input_table, name);
  528. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_QUATERNION);
  529. lua_pop (self->lua, 1);
  530. if (tmp != NULL)
  531. {
  532. *result = *((LIMatQuaternion*) tmp->data);
  533. *result = limat_quaternion_validate (*result);
  534. }
  535. }
  536. return tmp != NULL;
  537. }
  538. int
  539. liscr_args_gets_string (LIScrArgs* self,
  540. const char* name,
  541. const char** result)
  542. {
  543. const char* tmp = NULL;
  544. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  545. {
  546. lua_getfield (self->lua, self->input_table, name);
  547. if (lua_type (self->lua, -1) == LUA_TSTRING)
  548. tmp = lua_tostring (self->lua, -1);
  549. lua_pop (self->lua, 1);
  550. if (tmp != NULL)
  551. *result = tmp;
  552. }
  553. return tmp != NULL;
  554. }
  555. /**
  556. * \brief Gets a table by name and pushes it to the stack.
  557. * \param self Arguments.
  558. * \param name Argument name.
  559. * \return Nonzero on success.
  560. */
  561. int liscr_args_gets_table (
  562. LIScrArgs* self,
  563. const char* name)
  564. {
  565. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  566. {
  567. lua_getfield (self->lua, self->input_table, name);
  568. if (lua_type (self->lua, -1) == LUA_TTABLE)
  569. return 1;
  570. lua_pop (self->lua, 1);
  571. }
  572. return 0;
  573. }
  574. int liscr_args_gets_vector (
  575. LIScrArgs* self,
  576. const char* name,
  577. LIMatVector* result)
  578. {
  579. LIScrData* tmp = NULL;
  580. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  581. {
  582. lua_getfield (self->lua, self->input_table, name);
  583. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_VECTOR);
  584. lua_pop (self->lua, 1);
  585. if (tmp != NULL)
  586. {
  587. *result = *((LIMatVector*) tmp->data);
  588. *result = limat_vector_validate (*result);
  589. }
  590. }
  591. return tmp != NULL;
  592. }
  593. void
  594. liscr_args_set_output (LIScrArgs* self,
  595. int value)
  596. {
  597. lisys_assert (self->ret == 0);
  598. self->output_mode = value;
  599. if (value == LISCR_ARGS_OUTPUT_TABLE_FORCE)
  600. {
  601. lua_newtable (self->lua);
  602. self->output_table = lua_gettop (self->lua);
  603. self->output_mode = LISCR_ARGS_OUTPUT_TABLE;
  604. }
  605. else
  606. self->output_mode = value;
  607. }
  608. void
  609. liscr_args_setf_data (LIScrArgs* self,
  610. double name,
  611. LIScrData* value)
  612. {
  613. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  614. {
  615. if (!self->output_table)
  616. {
  617. lua_newtable (self->lua);
  618. self->output_table = lua_gettop (self->lua);
  619. }
  620. if (value != NULL)
  621. {
  622. lua_pushnumber (self->lua, name);
  623. liscr_pushdata (self->lua, value);
  624. lua_settable (self->lua, self->output_table);
  625. }
  626. }
  627. }
  628. void
  629. liscr_args_setf_float (LIScrArgs* self,
  630. double name,
  631. double value)
  632. {
  633. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  634. {
  635. if (!self->output_table)
  636. {
  637. lua_newtable (self->lua);
  638. self->output_table = lua_gettop (self->lua);
  639. }
  640. lua_pushnumber (self->lua, name);
  641. lua_pushnumber (self->lua, value);
  642. lua_settable (self->lua, self->output_table);
  643. }
  644. }
  645. void
  646. liscr_args_setf_string (LIScrArgs* self,
  647. double name,
  648. const char* value)
  649. {
  650. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  651. {
  652. if (!self->output_table)
  653. {
  654. lua_newtable (self->lua);
  655. self->output_table = lua_gettop (self->lua);
  656. }
  657. lua_pushnumber (self->lua, name);
  658. lua_pushstring (self->lua, value);
  659. lua_settable (self->lua, self->output_table);
  660. }
  661. }
  662. void
  663. liscr_args_seti_bool (LIScrArgs* self,
  664. int value)
  665. {
  666. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  667. {
  668. lua_pushboolean (self->lua, value);
  669. self->ret++;
  670. }
  671. else
  672. {
  673. if (!self->output_table)
  674. {
  675. lua_newtable (self->lua);
  676. self->output_table = lua_gettop (self->lua);
  677. }
  678. lua_pushnumber (self->lua, ++self->ret);
  679. lua_pushboolean (self->lua, value);
  680. lua_settable (self->lua, self->output_table);
  681. }
  682. }
  683. int liscr_args_seti_data (
  684. LIScrArgs* self,
  685. LIScrData* value)
  686. {
  687. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  688. {
  689. if (value != NULL)
  690. {
  691. if (!liscr_pushdata (self->lua, value))
  692. return 0;
  693. }
  694. else
  695. lua_pushnil (self->lua);
  696. self->ret++;
  697. }
  698. else
  699. {
  700. if (!self->output_table)
  701. {
  702. lua_newtable (self->lua);
  703. self->output_table = lua_gettop (self->lua);
  704. }
  705. if (value != NULL)
  706. {
  707. lua_pushnumber (self->lua, ++self->ret);
  708. if (!liscr_pushdata (self->lua, value))
  709. {
  710. lua_pop (self->lua, 1);
  711. return 0;
  712. }
  713. lua_settable (self->lua, self->output_table);
  714. }
  715. }
  716. return 1;
  717. }
  718. void
  719. liscr_args_seti_float (LIScrArgs* self,
  720. float value)
  721. {
  722. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  723. {
  724. lua_pushnumber (self->lua, value);
  725. self->ret++;
  726. }
  727. else
  728. {
  729. if (!self->output_table)
  730. {
  731. lua_newtable (self->lua);
  732. self->output_table = lua_gettop (self->lua);
  733. }
  734. lua_pushnumber (self->lua, ++self->ret);
  735. lua_pushnumber (self->lua, value);
  736. lua_settable (self->lua, self->output_table);
  737. }
  738. }
  739. void
  740. liscr_args_seti_int (LIScrArgs* self,
  741. int value)
  742. {
  743. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  744. {
  745. lua_pushnumber (self->lua, value);
  746. self->ret++;
  747. }
  748. else
  749. {
  750. if (!self->output_table)
  751. {
  752. lua_newtable (self->lua);
  753. self->output_table = lua_gettop (self->lua);
  754. }
  755. lua_pushnumber (self->lua, ++self->ret);
  756. lua_pushnumber (self->lua, value);
  757. lua_settable (self->lua, self->output_table);
  758. }
  759. }
  760. void liscr_args_seti_nil (
  761. LIScrArgs* self)
  762. {
  763. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  764. {
  765. lua_pushnil (self->lua);
  766. self->ret++;
  767. }
  768. else
  769. self->ret++;
  770. }
  771. void
  772. liscr_args_seti_quaternion (LIScrArgs* self,
  773. const LIMatQuaternion* value)
  774. {
  775. LIScrData* quat;
  776. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  777. {
  778. quat = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatQuaternion), LISCR_SCRIPT_QUATERNION);
  779. if (quat != NULL)
  780. {
  781. *((LIMatQuaternion*) quat->data) = *value;
  782. self->ret++;
  783. }
  784. }
  785. else
  786. {
  787. if (!self->output_table)
  788. {
  789. lua_newtable (self->lua);
  790. self->output_table = lua_gettop (self->lua);
  791. }
  792. lua_pushnumber (self->lua, ++self->ret);
  793. quat = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatQuaternion), LISCR_SCRIPT_QUATERNION);
  794. if (quat != NULL)
  795. {
  796. *((LIMatQuaternion*) quat->data) = *value;
  797. lua_settable (self->lua, self->output_table);
  798. }
  799. else
  800. lua_pop (self->lua, 1);
  801. }
  802. }
  803. void
  804. liscr_args_seti_stack (LIScrArgs* self)
  805. {
  806. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  807. {
  808. self->ret++;
  809. }
  810. else
  811. {
  812. if (!self->output_table)
  813. {
  814. lua_newtable (self->lua);
  815. lua_pushnumber (self->lua, ++self->ret);
  816. lua_pushvalue (self->lua, -3);
  817. lua_remove (self->lua, -4);
  818. self->output_table = lua_gettop (self->lua) - 2;
  819. lua_settable (self->lua, self->output_table);
  820. }
  821. else
  822. {
  823. lua_pushnumber (self->lua, ++self->ret);
  824. lua_pushvalue (self->lua, -2);
  825. lua_settable (self->lua, self->output_table);
  826. lua_pop (self->lua, 1);
  827. }
  828. }
  829. }
  830. void
  831. liscr_args_seti_string (LIScrArgs* self,
  832. const char* value)
  833. {
  834. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  835. {
  836. if (value != NULL)
  837. lua_pushstring (self->lua, value);
  838. else
  839. lua_pushnil (self->lua);
  840. self->ret++;
  841. }
  842. else
  843. {
  844. if (!self->output_table)
  845. {
  846. lua_newtable (self->lua);
  847. self->output_table = lua_gettop (self->lua);
  848. }
  849. if (value != NULL)
  850. {
  851. lua_pushnumber (self->lua, ++self->ret);
  852. lua_pushstring (self->lua, value);
  853. lua_settable (self->lua, self->output_table);
  854. }
  855. }
  856. }
  857. void liscr_args_seti_vector (
  858. LIScrArgs* self,
  859. const LIMatVector* value)
  860. {
  861. LIScrData* vector;
  862. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  863. {
  864. vector = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatVector), LISCR_SCRIPT_VECTOR);
  865. if (vector != NULL)
  866. {
  867. *((LIMatVector*) vector->data) = *value;
  868. self->ret++;
  869. }
  870. }
  871. else
  872. {
  873. if (!self->output_table)
  874. {
  875. lua_newtable (self->lua);
  876. self->output_table = lua_gettop (self->lua);
  877. }
  878. lua_pushnumber (self->lua, ++self->ret);
  879. vector = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatVector), LISCR_SCRIPT_VECTOR);
  880. if (vector != NULL)
  881. {
  882. *((LIMatVector*) vector->data) = *value;
  883. lua_settable (self->lua, self->output_table);
  884. }
  885. else
  886. lua_pop (self->lua, 1);
  887. }
  888. }
  889. void
  890. liscr_args_sets_bool (LIScrArgs* self,
  891. const char* name,
  892. int value)
  893. {
  894. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  895. {
  896. if (!self->output_table)
  897. {
  898. lua_newtable (self->lua);
  899. self->output_table = lua_gettop (self->lua);
  900. }
  901. lua_pushboolean (self->lua, value);
  902. lua_setfield (self->lua, self->output_table, name);
  903. }
  904. }
  905. void
  906. liscr_args_sets_data (LIScrArgs* self,
  907. const char* name,
  908. LIScrData* value)
  909. {
  910. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  911. {
  912. if (!self->output_table)
  913. {
  914. lua_newtable (self->lua);
  915. self->output_table = lua_gettop (self->lua);
  916. }
  917. if (value != NULL)
  918. {
  919. liscr_pushdata (self->lua, value);
  920. lua_setfield (self->lua, self->output_table, name);
  921. }
  922. }
  923. }
  924. void
  925. liscr_args_sets_float (LIScrArgs* self,
  926. const char* name,
  927. float value)
  928. {
  929. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  930. {
  931. if (!self->output_table)
  932. {
  933. lua_newtable (self->lua);
  934. self->output_table = lua_gettop (self->lua);
  935. }
  936. lua_pushnumber (self->lua, value);
  937. lua_setfield (self->lua, self->output_table, name);
  938. }
  939. }
  940. void
  941. liscr_args_sets_int (LIScrArgs* self,
  942. const char* name,
  943. int value)
  944. {
  945. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  946. {
  947. if (!self->output_table)
  948. {
  949. lua_newtable (self->lua);
  950. self->output_table = lua_gettop (self->lua);
  951. }
  952. lua_pushnumber (self->lua, value);
  953. lua_setfield (self->lua, self->output_table, name);
  954. }
  955. }
  956. void liscr_args_sets_quaternion (
  957. LIScrArgs* self,
  958. const char* name,
  959. const LIMatQuaternion* value)
  960. {
  961. LIScrData* quat;
  962. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  963. {
  964. if (!self->output_table)
  965. {
  966. lua_newtable (self->lua);
  967. self->output_table = lua_gettop (self->lua);
  968. }
  969. quat = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatQuaternion), LISCR_SCRIPT_QUATERNION);
  970. if (quat != NULL)
  971. {
  972. *((LIMatQuaternion*) quat->data) = *value;
  973. lua_setfield (self->lua, self->output_table, name);
  974. }
  975. }
  976. }
  977. void
  978. liscr_args_sets_stack (LIScrArgs* self,
  979. const char* name)
  980. {
  981. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  982. {
  983. if (!self->output_table)
  984. {
  985. lua_newtable (self->lua);
  986. lua_pushvalue (self->lua, -2);
  987. lua_remove (self->lua, -3);
  988. self->output_table = lua_gettop (self->lua) - 1;
  989. }
  990. lua_setfield (self->lua, self->output_table, name);
  991. }
  992. }
  993. void
  994. liscr_args_sets_string (LIScrArgs* self,
  995. const char* name,
  996. const char* value)
  997. {
  998. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  999. {
  1000. if (!self->output_table)
  1001. {
  1002. lua_newtable (self->lua);
  1003. self->output_table = lua_gettop (self->lua);
  1004. }
  1005. lua_pushstring (self->lua, value);
  1006. lua_setfield (self->lua, self->output_table, name);
  1007. }
  1008. }
  1009. void liscr_args_sets_vector (
  1010. LIScrArgs* self,
  1011. const char* name,
  1012. const LIMatVector* value)
  1013. {
  1014. LIScrData* vector;
  1015. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  1016. {
  1017. if (!self->output_table)
  1018. {
  1019. lua_newtable (self->lua);
  1020. self->output_table = lua_gettop (self->lua);
  1021. }
  1022. vector = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatVector), LISCR_SCRIPT_VECTOR);
  1023. if (vector != NULL)
  1024. {
  1025. *((LIMatVector*) vector->data) = *value;
  1026. lua_setfield (self->lua, self->output_table, name);
  1027. }
  1028. }
  1029. }
  1030. /*****************************************************************************/
  1031. int liscr_marshal_CLASS (lua_State* lua)
  1032. {
  1033. LIScrArgs args;
  1034. LIScrScript* script;
  1035. void (*func)(LIScrArgs*);
  1036. script = lua_touserdata (lua, lua_upvalueindex (1));
  1037. func = lua_touserdata (lua, lua_upvalueindex (2));
  1038. liscr_args_init_func (&args, lua, script, NULL);
  1039. func (&args);
  1040. if (args.output_table)
  1041. return 1;
  1042. else
  1043. return args.ret;
  1044. }
  1045. int liscr_marshal_DATA (lua_State* lua)
  1046. {
  1047. LIScrArgs args;
  1048. LIScrData* data;
  1049. LIScrScript* script;
  1050. const char* type;
  1051. void (*func)(LIScrArgs*);
  1052. script = lua_touserdata (lua, lua_upvalueindex (1));
  1053. type = lua_tostring (lua, lua_upvalueindex (2));
  1054. func = lua_touserdata (lua, lua_upvalueindex (3));
  1055. data = liscr_isdata (lua, 1, type);
  1056. if (data == NULL)
  1057. return 0;
  1058. liscr_args_init_func (&args, lua, script, data);
  1059. func (&args);
  1060. if (args.output_table)
  1061. return 1;
  1062. else
  1063. return args.ret;
  1064. }
  1065. /** @} */
  1066. /** @} */