PageRenderTime 30ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/deldiablo/GodheadLips
C | 1144 lines | 1009 code | 98 blank | 37 comment | 240 complexity | 9983145c15f53f13f84b588975750c13 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
  257. liscr_args_geti_quaternion (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. *result = *((LIMatQuaternion*) tmp->data);
  270. }
  271. else
  272. {
  273. if (index < 0 || index >= self->args_count)
  274. return 0;
  275. index += self->args_start;
  276. tmp = liscr_isdata (self->lua, index, LISCR_SCRIPT_QUATERNION);
  277. if (tmp != NULL)
  278. *result = *((LIMatQuaternion*) tmp->data);
  279. }
  280. return tmp != NULL;
  281. }
  282. int
  283. liscr_args_geti_string (LIScrArgs* self,
  284. int index,
  285. const char** result)
  286. {
  287. const char* tmp = NULL;
  288. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  289. {
  290. lua_pushnumber (self->lua, index + 1);
  291. lua_gettable (self->lua, self->input_table);
  292. if (lua_type (self->lua, -1) == LUA_TSTRING)
  293. tmp = lua_tostring (self->lua, -1);
  294. lua_pop (self->lua, 1);
  295. if (tmp != NULL)
  296. *result = tmp;
  297. }
  298. else
  299. {
  300. if (index < 0 || index >= self->args_count)
  301. return 0;
  302. index += self->args_start;
  303. if (lua_type (self->lua, index) == LUA_TSTRING)
  304. tmp = lua_tostring (self->lua, index);
  305. if (tmp != NULL)
  306. *result = tmp;
  307. }
  308. return tmp != NULL;
  309. }
  310. /**
  311. * \brief Gets a table by index and pushes it to the stack.
  312. * \param self Arguments.
  313. * \param index Argument index.
  314. * \return Nonzero on success.
  315. */
  316. int liscr_args_geti_table (
  317. LIScrArgs* self,
  318. int index)
  319. {
  320. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  321. {
  322. lua_pushnumber (self->lua, index + 1);
  323. lua_gettable (self->lua, self->input_table);
  324. if (lua_type (self->lua, -1) == LUA_TTABLE)
  325. return 1;
  326. lua_pop (self->lua, 1);
  327. }
  328. else
  329. {
  330. if (index < 0 || index >= self->args_count)
  331. return 0;
  332. index += self->args_start;
  333. if (lua_type (self->lua, index) == LUA_TTABLE)
  334. {
  335. lua_pushvalue (self->lua, index);
  336. return 1;
  337. }
  338. }
  339. return 0;
  340. }
  341. int
  342. liscr_args_geti_vector (LIScrArgs* self,
  343. int index,
  344. LIMatVector* result)
  345. {
  346. LIScrData* tmp = NULL;
  347. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  348. {
  349. lua_pushnumber (self->lua, index + 1);
  350. lua_gettable (self->lua, self->input_table);
  351. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_VECTOR);
  352. lua_pop (self->lua, 1);
  353. if (tmp != NULL)
  354. *result = *((LIMatVector*) tmp->data);
  355. }
  356. else
  357. {
  358. if (index < 0 || index >= self->args_count)
  359. return 0;
  360. index += self->args_start;
  361. tmp = liscr_isdata (self->lua, index, LISCR_SCRIPT_VECTOR);
  362. if (tmp != NULL)
  363. *result = *((LIMatVector*) tmp->data);
  364. }
  365. return tmp != NULL;
  366. }
  367. int
  368. liscr_args_gets_bool (LIScrArgs* self,
  369. const char* name,
  370. int* result)
  371. {
  372. int ret = 0;
  373. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  374. {
  375. lua_getfield (self->lua, self->input_table, name);
  376. if (lua_type (self->lua, -1) == LUA_TBOOLEAN)
  377. {
  378. *result = (int) lua_toboolean (self->lua, -1);
  379. ret = 1;
  380. }
  381. lua_pop (self->lua, 1);
  382. }
  383. return ret;
  384. }
  385. int
  386. liscr_args_gets_data (LIScrArgs* self,
  387. const char* name,
  388. const char* type,
  389. LIScrData** result)
  390. {
  391. LIScrData* tmp = NULL;
  392. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  393. {
  394. lua_getfield (self->lua, self->input_table, name);
  395. if (type != NULL)
  396. tmp = liscr_isdata (self->lua, -1, type);
  397. else
  398. tmp = liscr_isanydata (self->lua, -1);
  399. lua_pop (self->lua, 1);
  400. if (tmp != NULL)
  401. *result = tmp;
  402. }
  403. return tmp != NULL;
  404. }
  405. int liscr_args_gets_float (
  406. LIScrArgs* self,
  407. const char* name,
  408. float* result)
  409. {
  410. int ret = 0;
  411. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  412. {
  413. lua_getfield (self->lua, self->input_table, name);
  414. if (lua_isnumber (self->lua, -1))
  415. {
  416. *result = lua_tonumber (self->lua, -1);
  417. ret = 1;
  418. }
  419. lua_pop (self->lua, 1);
  420. }
  421. return ret;
  422. }
  423. int liscr_args_gets_floatv (
  424. LIScrArgs* self,
  425. const char* name,
  426. int count,
  427. float* result)
  428. {
  429. int i;
  430. int ret = 0;
  431. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  432. {
  433. lua_getfield (self->lua, self->input_table, name);
  434. if (lua_type (self->lua, -1) == LUA_TTABLE)
  435. {
  436. for (i = 0 ; i < count ; i++)
  437. {
  438. lua_pushnumber (self->lua, i + 1);
  439. lua_gettable (self->lua, -2);
  440. if (!lua_isnumber (self->lua, -1))
  441. {
  442. lua_pop (self->lua, 1);
  443. break;
  444. }
  445. result[i] = lua_tonumber (self->lua, -1);
  446. if (isnan (result[i]) || isinf (result[i]))
  447. result[i] = 0.0f;
  448. lua_pop (self->lua, 1);
  449. }
  450. ret = i;
  451. }
  452. lua_pop (self->lua, 1);
  453. }
  454. return ret;
  455. }
  456. int liscr_args_gets_int (
  457. LIScrArgs* self,
  458. const char* name,
  459. int* result)
  460. {
  461. int ret = 0;
  462. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  463. {
  464. lua_getfield (self->lua, self->input_table, name);
  465. if (lua_isnumber (self->lua, -1))
  466. {
  467. *result = (int) lua_tonumber (self->lua, -1);
  468. ret = 1;
  469. }
  470. lua_pop (self->lua, 1);
  471. }
  472. return ret;
  473. }
  474. int liscr_args_gets_intv (
  475. LIScrArgs* self,
  476. const char* name,
  477. int count,
  478. int* result)
  479. {
  480. int i;
  481. int ret = 0;
  482. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  483. {
  484. lua_getfield (self->lua, self->input_table, name);
  485. if (lua_type (self->lua, -1) == LUA_TTABLE)
  486. {
  487. for (i = 0 ; i < count ; i++)
  488. {
  489. lua_pushnumber (self->lua, i + 1);
  490. lua_gettable (self->lua, -2);
  491. if (!lua_isnumber (self->lua, -1))
  492. {
  493. lua_pop (self->lua, 1);
  494. break;
  495. }
  496. result[i] = lua_tonumber (self->lua, -1);
  497. lua_pop (self->lua, 1);
  498. }
  499. ret = i;
  500. }
  501. lua_pop (self->lua, 1);
  502. }
  503. return ret;
  504. }
  505. int
  506. liscr_args_gets_quaternion (LIScrArgs* self,
  507. const char* name,
  508. LIMatQuaternion* result)
  509. {
  510. LIScrData* tmp = NULL;
  511. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  512. {
  513. lua_getfield (self->lua, self->input_table, name);
  514. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_QUATERNION);
  515. lua_pop (self->lua, 1);
  516. if (tmp != NULL)
  517. *result = *((LIMatQuaternion*) tmp->data);
  518. }
  519. return tmp != NULL;
  520. }
  521. int
  522. liscr_args_gets_string (LIScrArgs* self,
  523. const char* name,
  524. const char** result)
  525. {
  526. const char* tmp = NULL;
  527. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  528. {
  529. lua_getfield (self->lua, self->input_table, name);
  530. if (lua_type (self->lua, -1) == LUA_TSTRING)
  531. tmp = lua_tostring (self->lua, -1);
  532. lua_pop (self->lua, 1);
  533. if (tmp != NULL)
  534. *result = tmp;
  535. }
  536. return tmp != NULL;
  537. }
  538. /**
  539. * \brief Gets a table by name and pushes it to the stack.
  540. * \param self Arguments.
  541. * \param name Argument name.
  542. * \return Nonzero on success.
  543. */
  544. int liscr_args_gets_table (
  545. LIScrArgs* self,
  546. const char* name)
  547. {
  548. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  549. {
  550. lua_getfield (self->lua, self->input_table, name);
  551. if (lua_type (self->lua, -1) == LUA_TTABLE)
  552. return 1;
  553. lua_pop (self->lua, 1);
  554. }
  555. return 0;
  556. }
  557. int
  558. liscr_args_gets_vector (LIScrArgs* self,
  559. const char* name,
  560. LIMatVector* result)
  561. {
  562. LIScrData* tmp = NULL;
  563. if (self->input_mode == LISCR_ARGS_INPUT_TABLE)
  564. {
  565. lua_getfield (self->lua, self->input_table, name);
  566. tmp = liscr_isdata (self->lua, -1, LISCR_SCRIPT_VECTOR);
  567. lua_pop (self->lua, 1);
  568. if (tmp != NULL)
  569. *result = *((LIMatVector*) tmp->data);
  570. }
  571. return tmp != NULL;
  572. }
  573. void
  574. liscr_args_set_output (LIScrArgs* self,
  575. int value)
  576. {
  577. lisys_assert (self->ret == 0);
  578. self->output_mode = value;
  579. if (value == LISCR_ARGS_OUTPUT_TABLE_FORCE)
  580. {
  581. lua_newtable (self->lua);
  582. self->output_table = lua_gettop (self->lua);
  583. self->output_mode = LISCR_ARGS_OUTPUT_TABLE;
  584. }
  585. else
  586. self->output_mode = value;
  587. }
  588. void
  589. liscr_args_setf_data (LIScrArgs* self,
  590. double name,
  591. LIScrData* value)
  592. {
  593. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  594. {
  595. if (!self->output_table)
  596. {
  597. lua_newtable (self->lua);
  598. self->output_table = lua_gettop (self->lua);
  599. }
  600. if (value != NULL)
  601. {
  602. lua_pushnumber (self->lua, name);
  603. liscr_pushdata (self->lua, value);
  604. lua_settable (self->lua, self->output_table);
  605. }
  606. }
  607. }
  608. void
  609. liscr_args_setf_float (LIScrArgs* self,
  610. double name,
  611. double 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. lua_pushnumber (self->lua, name);
  621. lua_pushnumber (self->lua, value);
  622. lua_settable (self->lua, self->output_table);
  623. }
  624. }
  625. void
  626. liscr_args_setf_string (LIScrArgs* self,
  627. double name,
  628. const char* value)
  629. {
  630. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  631. {
  632. if (!self->output_table)
  633. {
  634. lua_newtable (self->lua);
  635. self->output_table = lua_gettop (self->lua);
  636. }
  637. lua_pushnumber (self->lua, name);
  638. lua_pushstring (self->lua, value);
  639. lua_settable (self->lua, self->output_table);
  640. }
  641. }
  642. void
  643. liscr_args_seti_bool (LIScrArgs* self,
  644. int value)
  645. {
  646. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  647. {
  648. lua_pushboolean (self->lua, value);
  649. self->ret++;
  650. }
  651. else
  652. {
  653. if (!self->output_table)
  654. {
  655. lua_newtable (self->lua);
  656. self->output_table = lua_gettop (self->lua);
  657. }
  658. lua_pushnumber (self->lua, ++self->ret);
  659. lua_pushboolean (self->lua, value);
  660. lua_settable (self->lua, self->output_table);
  661. }
  662. }
  663. int liscr_args_seti_data (
  664. LIScrArgs* self,
  665. LIScrData* value)
  666. {
  667. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  668. {
  669. if (value != NULL)
  670. {
  671. if (!liscr_pushdata (self->lua, value))
  672. return 0;
  673. }
  674. else
  675. lua_pushnil (self->lua);
  676. self->ret++;
  677. }
  678. else
  679. {
  680. if (!self->output_table)
  681. {
  682. lua_newtable (self->lua);
  683. self->output_table = lua_gettop (self->lua);
  684. }
  685. if (value != NULL)
  686. {
  687. lua_pushnumber (self->lua, ++self->ret);
  688. if (!liscr_pushdata (self->lua, value))
  689. {
  690. lua_pop (self->lua, 1);
  691. return 0;
  692. }
  693. lua_settable (self->lua, self->output_table);
  694. }
  695. }
  696. return 1;
  697. }
  698. void
  699. liscr_args_seti_float (LIScrArgs* self,
  700. float value)
  701. {
  702. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  703. {
  704. lua_pushnumber (self->lua, value);
  705. self->ret++;
  706. }
  707. else
  708. {
  709. if (!self->output_table)
  710. {
  711. lua_newtable (self->lua);
  712. self->output_table = lua_gettop (self->lua);
  713. }
  714. lua_pushnumber (self->lua, ++self->ret);
  715. lua_pushnumber (self->lua, value);
  716. lua_settable (self->lua, self->output_table);
  717. }
  718. }
  719. void
  720. liscr_args_seti_int (LIScrArgs* self,
  721. int value)
  722. {
  723. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  724. {
  725. lua_pushnumber (self->lua, value);
  726. self->ret++;
  727. }
  728. else
  729. {
  730. if (!self->output_table)
  731. {
  732. lua_newtable (self->lua);
  733. self->output_table = lua_gettop (self->lua);
  734. }
  735. lua_pushnumber (self->lua, ++self->ret);
  736. lua_pushnumber (self->lua, value);
  737. lua_settable (self->lua, self->output_table);
  738. }
  739. }
  740. void liscr_args_seti_nil (
  741. LIScrArgs* self)
  742. {
  743. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  744. {
  745. lua_pushnil (self->lua);
  746. self->ret++;
  747. }
  748. else
  749. self->ret++;
  750. }
  751. void
  752. liscr_args_seti_quaternion (LIScrArgs* self,
  753. const LIMatQuaternion* value)
  754. {
  755. LIScrData* quat;
  756. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  757. {
  758. quat = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatQuaternion), LISCR_SCRIPT_QUATERNION);
  759. if (quat != NULL)
  760. {
  761. *((LIMatQuaternion*) quat->data) = *value;
  762. self->ret++;
  763. }
  764. }
  765. else
  766. {
  767. if (!self->output_table)
  768. {
  769. lua_newtable (self->lua);
  770. self->output_table = lua_gettop (self->lua);
  771. }
  772. lua_pushnumber (self->lua, ++self->ret);
  773. quat = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatQuaternion), LISCR_SCRIPT_QUATERNION);
  774. if (quat != NULL)
  775. {
  776. *((LIMatQuaternion*) quat->data) = *value;
  777. lua_settable (self->lua, self->output_table);
  778. }
  779. else
  780. lua_pop (self->lua, 1);
  781. }
  782. }
  783. void
  784. liscr_args_seti_stack (LIScrArgs* self)
  785. {
  786. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  787. {
  788. self->ret++;
  789. }
  790. else
  791. {
  792. if (!self->output_table)
  793. {
  794. lua_newtable (self->lua);
  795. lua_pushnumber (self->lua, ++self->ret);
  796. lua_pushvalue (self->lua, -3);
  797. lua_remove (self->lua, -4);
  798. self->output_table = lua_gettop (self->lua) - 2;
  799. lua_settable (self->lua, self->output_table);
  800. }
  801. else
  802. {
  803. lua_pushnumber (self->lua, ++self->ret);
  804. lua_pushvalue (self->lua, -2);
  805. lua_settable (self->lua, self->output_table);
  806. lua_pop (self->lua, 1);
  807. }
  808. }
  809. }
  810. void
  811. liscr_args_seti_string (LIScrArgs* self,
  812. const char* value)
  813. {
  814. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  815. {
  816. if (value != NULL)
  817. lua_pushstring (self->lua, value);
  818. else
  819. lua_pushnil (self->lua);
  820. self->ret++;
  821. }
  822. else
  823. {
  824. if (!self->output_table)
  825. {
  826. lua_newtable (self->lua);
  827. self->output_table = lua_gettop (self->lua);
  828. }
  829. if (value != NULL)
  830. {
  831. lua_pushnumber (self->lua, ++self->ret);
  832. lua_pushstring (self->lua, value);
  833. lua_settable (self->lua, self->output_table);
  834. }
  835. }
  836. }
  837. void liscr_args_seti_vector (
  838. LIScrArgs* self,
  839. const LIMatVector* value)
  840. {
  841. LIScrData* vector;
  842. if (self->output_mode != LISCR_ARGS_OUTPUT_TABLE)
  843. {
  844. vector = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatVector), LISCR_SCRIPT_VECTOR);
  845. if (vector != NULL)
  846. {
  847. *((LIMatVector*) vector->data) = *value;
  848. self->ret++;
  849. }
  850. }
  851. else
  852. {
  853. if (!self->output_table)
  854. {
  855. lua_newtable (self->lua);
  856. self->output_table = lua_gettop (self->lua);
  857. }
  858. lua_pushnumber (self->lua, ++self->ret);
  859. vector = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatVector), LISCR_SCRIPT_VECTOR);
  860. if (vector != NULL)
  861. {
  862. *((LIMatVector*) vector->data) = *value;
  863. lua_settable (self->lua, self->output_table);
  864. }
  865. else
  866. lua_pop (self->lua, 1);
  867. }
  868. }
  869. void
  870. liscr_args_sets_bool (LIScrArgs* self,
  871. const char* name,
  872. int value)
  873. {
  874. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  875. {
  876. if (!self->output_table)
  877. {
  878. lua_newtable (self->lua);
  879. self->output_table = lua_gettop (self->lua);
  880. }
  881. lua_pushboolean (self->lua, value);
  882. lua_setfield (self->lua, self->output_table, name);
  883. }
  884. }
  885. void
  886. liscr_args_sets_data (LIScrArgs* self,
  887. const char* name,
  888. LIScrData* value)
  889. {
  890. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  891. {
  892. if (!self->output_table)
  893. {
  894. lua_newtable (self->lua);
  895. self->output_table = lua_gettop (self->lua);
  896. }
  897. if (value != NULL)
  898. {
  899. liscr_pushdata (self->lua, value);
  900. lua_setfield (self->lua, self->output_table, name);
  901. }
  902. }
  903. }
  904. void
  905. liscr_args_sets_float (LIScrArgs* self,
  906. const char* name,
  907. float value)
  908. {
  909. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  910. {
  911. if (!self->output_table)
  912. {
  913. lua_newtable (self->lua);
  914. self->output_table = lua_gettop (self->lua);
  915. }
  916. lua_pushnumber (self->lua, value);
  917. lua_setfield (self->lua, self->output_table, name);
  918. }
  919. }
  920. void
  921. liscr_args_sets_int (LIScrArgs* self,
  922. const char* name,
  923. int value)
  924. {
  925. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  926. {
  927. if (!self->output_table)
  928. {
  929. lua_newtable (self->lua);
  930. self->output_table = lua_gettop (self->lua);
  931. }
  932. lua_pushnumber (self->lua, value);
  933. lua_setfield (self->lua, self->output_table, name);
  934. }
  935. }
  936. void liscr_args_sets_quaternion (
  937. LIScrArgs* self,
  938. const char* name,
  939. const LIMatQuaternion* value)
  940. {
  941. LIScrData* quat;
  942. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  943. {
  944. if (!self->output_table)
  945. {
  946. lua_newtable (self->lua);
  947. self->output_table = lua_gettop (self->lua);
  948. }
  949. quat = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatQuaternion), LISCR_SCRIPT_QUATERNION);
  950. if (quat != NULL)
  951. {
  952. *((LIMatQuaternion*) quat->data) = *value;
  953. lua_setfield (self->lua, self->output_table, name);
  954. }
  955. }
  956. }
  957. void
  958. liscr_args_sets_stack (LIScrArgs* self,
  959. const char* name)
  960. {
  961. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  962. {
  963. if (!self->output_table)
  964. {
  965. lua_newtable (self->lua);
  966. lua_pushvalue (self->lua, -2);
  967. lua_remove (self->lua, -3);
  968. self->output_table = lua_gettop (self->lua) - 1;
  969. }
  970. lua_setfield (self->lua, self->output_table, name);
  971. }
  972. }
  973. void
  974. liscr_args_sets_string (LIScrArgs* self,
  975. const char* name,
  976. const char* value)
  977. {
  978. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  979. {
  980. if (!self->output_table)
  981. {
  982. lua_newtable (self->lua);
  983. self->output_table = lua_gettop (self->lua);
  984. }
  985. lua_pushstring (self->lua, value);
  986. lua_setfield (self->lua, self->output_table, name);
  987. }
  988. }
  989. void liscr_args_sets_vector (
  990. LIScrArgs* self,
  991. const char* name,
  992. const LIMatVector* value)
  993. {
  994. LIScrData* vector;
  995. if (self->output_mode == LISCR_ARGS_OUTPUT_TABLE)
  996. {
  997. if (!self->output_table)
  998. {
  999. lua_newtable (self->lua);
  1000. self->output_table = lua_gettop (self->lua);
  1001. }
  1002. vector = liscr_data_new_alloc (self->script, self->lua, sizeof (LIMatVector), LISCR_SCRIPT_VECTOR);
  1003. if (vector != NULL)
  1004. {
  1005. *((LIMatVector*) vector->data) = *value;
  1006. lua_setfield (self->lua, self->output_table, name);
  1007. }
  1008. }
  1009. }
  1010. /*****************************************************************************/
  1011. int liscr_marshal_CLASS (lua_State* lua)
  1012. {
  1013. LIScrArgs args;
  1014. LIScrScript* script;
  1015. void (*func)(LIScrArgs*);
  1016. script = lua_touserdata (lua, lua_upvalueindex (1));
  1017. func = lua_touserdata (lua, lua_upvalueindex (2));
  1018. liscr_args_init_func (&args, lua, script, NULL);
  1019. func (&args);
  1020. if (args.output_table)
  1021. return 1;
  1022. else
  1023. return args.ret;
  1024. }
  1025. int liscr_marshal_DATA (lua_State* lua)
  1026. {
  1027. LIScrArgs args;
  1028. LIScrData* data;
  1029. LIScrScript* script;
  1030. const char* type;
  1031. void (*func)(LIScrArgs*);
  1032. script = lua_touserdata (lua, lua_upvalueindex (1));
  1033. type = lua_tostring (lua, lua_upvalueindex (2));
  1034. func = lua_touserdata (lua, lua_upvalueindex (3));
  1035. data = liscr_isdata (lua, 1, type);
  1036. if (data == NULL)
  1037. return 0;
  1038. liscr_args_init_func (&args, lua, script, data);
  1039. func (&args);
  1040. if (args.output_table)
  1041. return 1;
  1042. else
  1043. return args.ret;
  1044. }
  1045. /** @} */
  1046. /** @} */