Advertisement
Guest User

PSP Lua main.c

a guest
Feb 12th, 2017
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. // include psp stuff
  2. #include <pspkernel.h>
  3. #include <pspdebug.h>
  4. #include <pspdisplay.h>
  5. #include <pspctrl.h>
  6.  
  7.  
  8. // include lua stuff
  9. #include "lua.h"
  10. #include "lualib.h"
  11. #include "lauxlib.h"
  12.  
  13. #include "lua_lib.h"
  14.  
  15.  
  16. // configure PSP stuff
  17. #define VERS    1
  18. #define REVS    0
  19.  
  20. PSP_MODULE_INFO("LUATEST", PSP_MODULE_USER, VERS, REVS);
  21. PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | THREAD_ATTR_VFPU);
  22.  
  23. // make printing easier on us
  24. #define printf pspDebugScreenPrintf
  25.  
  26.  
  27.  
  28. // basic callback stuff
  29. static int exitRequest  = 0;
  30.  
  31. int isRunning()
  32. {
  33.     return !exitRequest;
  34. }
  35.  
  36. int exitCallback(int arg1, int arg2, void *common)
  37. {
  38.     exitRequest = 1;
  39.     return 0;
  40. }
  41.  
  42. int callbackThread(SceSize args, void *argp)
  43. {
  44.     int callbackID;
  45.  
  46.     callbackID = sceKernelCreateCallback("Exit Callback", exitCallback, NULL);
  47.     sceKernelRegisterExitCallback(callbackID);
  48.  
  49.     sceKernelSleepThreadCB();
  50.  
  51.     return 0;
  52. }
  53.  
  54. int setupExitCallback()
  55. {
  56.     int threadID = 0;
  57.  
  58.     threadID = sceKernelCreateThread("Callback Update Thread", callbackThread, 0x11, 0xFA0, THREAD_ATTR_USER, 0);
  59.      
  60.     if(threadID >= 0)
  61.     {
  62.         sceKernelStartThread(threadID, 0, 0);
  63.     }
  64.  
  65.     return threadID;
  66. }
  67.  
  68.  
  69.  
  70. static void stackDump(lua_State *L) {
  71.     int i=lua_gettop(L);
  72.     printf(" ----------------  Stack Dump ----------------\n" );
  73.     while(i) {
  74.         int t = lua_type(L, i);
  75.         switch (t) {
  76.         case LUA_TSTRING:
  77.             printf("%d:`%s'\n", i, lua_tostring(L, i));
  78.             break;
  79.         case LUA_TBOOLEAN:
  80.             printf("%d: %s\n",i,lua_toboolean(L, i) ? "true" : "false");
  81.         break;
  82.         case LUA_TNUMBER:
  83.             printf("%d: %g\n",  i, lua_tonumber(L, i));
  84.         break;
  85.         default: printf("%d: %s\n", i, lua_typename(L, t)); break;
  86.         }
  87.         i--;
  88.     }
  89.     printf("--------------- Stack Dump Finished ---------------\n" );
  90. }
  91.  
  92. int main(int argc, char** argv)
  93. {
  94.     // basic init
  95.     setupExitCallback();
  96.     pspDebugScreenInit();
  97.    
  98.    
  99.     // script location
  100.     const char* scriptPath = "umd0:/script.lua";
  101.    
  102.    
  103.     // init Lua, its libraries, and do a dry run for initialization
  104.     lua_State *L = lua_lib_newstate();
  105.    
  106.     int status = 0;
  107.    
  108.     // cache the file in lua registry
  109.     status = luaL_loadfile(L, scriptPath);
  110.     if(status != 0) {
  111.         // break for alerting we have a compilation error
  112.         while(isRunning()) {
  113.             pspDebugScreenSetXY(0, 0);
  114.             stackDump(L);
  115.             printf("error code %d: %s", status, lua_tostring(L, -1));
  116.             sceDisplayWaitVblankStart();
  117.         }
  118.     }
  119.    
  120.     int lua_script = luaL_ref(L, LUA_REGISTRYINDEX);
  121.    
  122.    
  123.     // run main logic, call script, check for errors
  124.     while (isRunning()) {
  125.         pspDebugScreenSetXY(0, 0);
  126.         lua_rawgeti(L, LUA_REGISTRYINDEX, lua_script); // get script into stack
  127.         status = lua_pcall(L, 0, 0, 0); // run the script
  128.         if(status != 0) {
  129.             // alerting that we have a runtime error
  130.             stackDump(L); // a dirty stack with error string
  131.             printf(lua_tostring(L, -1)); // print last push, ie the error
  132.             lua_pop(L, 1); // pop the error arg
  133.             stackDump(L); // a clean stack
  134.         }
  135.         sceDisplayWaitVblankStart();
  136.     }
  137.    
  138.    
  139.     // clean up
  140.     lua_close(L);
  141.    
  142.     sceKernelExitGame();   
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement