@@ -211,6 +211,30 @@ static void action_reload(Engine* e, Instance* instance, char* args) {
211211 action_close (e , NULL , NULL );
212212}
213213
214+ /* Run a Lua function. See action documentation. */
215+ static void action_lua (Engine * e , Instance * instance , char * args ) {
216+ char * function = args ;
217+ lua_getglobal (e -> L , function );
218+ lua_pushlightuserdata (e -> L , e );
219+ lua_pushlightuserdata (e -> L , instance );
220+ lua_pcall (e -> L , 2 , 0 , 0 );
221+ }
222+
223+ static int thread_function (void * L ) {
224+ lua_pcall (L , 2 , 0 , 0 );
225+ return 0 ;
226+ }
227+
228+ /* Run a Lua function in another thread. See action documentation. */
229+ static void action_lua_async (Engine * e , Instance * instance , char * args ) {
230+ char * function = args ;
231+ /*lua_getglobal(e->L, function);
232+ lua_pushlightuserdata(e->L, e);
233+ lua_pushlightuserdata(e->L, instance);
234+ SDL_CreateThread(thread_function, function, e->L);*/
235+ SDL_Log ("Not implemented" );
236+ }
237+
214238/* Fill in memory with specified structure.
215239 * Variadic arguments must be in pairs of two.
216240 * STRING is string, INTEGER is number, and REAL is bool.
@@ -274,6 +298,7 @@ static char* args_generator(zpl_json_object* json, size_t n, ...) {
274298/* Based on the type provided, setup args so calling an action is faster. */
275299static int action_handler (Action * action , zpl_json_object * json , Asset * assets , const size_t assets_num ) {
276300 char * type = args_generator (json , 1 , "type" , ZPL_ADT_TYPE_STRING );
301+ if (type == NULL ) return -1 ;
277302
278303 if (strcmp (type , "spawn" ) == 0 ) {
279304 action -> args =
@@ -388,6 +413,18 @@ static int action_handler(Action* action, zpl_json_object* json, Asset* assets,
388413 action -> args = NULL ;
389414 action -> run = & action_reload ;
390415
416+ } else if (strcmp (type , "lua" ) == 0 ) {
417+ action -> args =
418+ args_generator (json , 1 , "function" , ZPL_ADT_TYPE_STRING );
419+ if (action -> args == NULL ) return ERROR ("Args generator failed" );
420+ action -> run = & action_lua ;
421+
422+ } else if (strcmp (type , "lua_async" ) == 0 ) {
423+ action -> args =
424+ args_generator (json , 1 , "function" , ZPL_ADT_TYPE_STRING );
425+ if (action -> args == NULL ) return ERROR ("Args generator failed" );
426+ action -> run = & action_lua_async ;
427+
391428 } else return ERROR ("Invalid action type found" );
392429 return 0 ;
393430}
@@ -403,3 +440,20 @@ int action_init(Action* action, zpl_json_object* json, Asset* assets, const size
403440void action_cleanup (Action * action ) {
404441 free (action -> args );
405442}
443+
444+ int action_api (lua_State * L ) {
445+ Engine * e = lua_touserdata (L , 1 );
446+ Instance * instance = lua_touserdata (L , 2 );
447+ const char * string = luaL_checkstring (L , 3 );
448+ char * str = malloc (strlen (string )+ 1 );
449+ strcpy (str , string );
450+ Action action ;
451+ zpl_json_object json ;
452+ zpl_json_parse (& json , str , zpl_heap ());
453+ if (action_init (& action , & json , e -> assets , e -> assets_num ) < 0 ) return 0 ; //should improve error handling later
454+ free (str );
455+ zpl_json_free (& json );
456+ action .run (e , instance , action .args );
457+ action_cleanup (& action );
458+ return 0 ;
459+ }
0 commit comments