aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/l_jin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin-lua/l_jin.cpp')
-rw-r--r--src/libjin-lua/l_jin.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/libjin-lua/l_jin.cpp b/src/libjin-lua/l_jin.cpp
new file mode 100644
index 0000000..6a0809a
--- /dev/null
+++ b/src/libjin-lua/l_jin.cpp
@@ -0,0 +1,187 @@
+#include "common/je_lua.h"
+#include "common/l_common.h"
+#include "modules/l_modules.h"
+//#include "l_embed.h"
+#include "l_jin.h"
+
+namespace JinEngine
+{
+ namespace Lua
+ {
+
+ LUA_IMPLEMENT int l_getversion(lua_State* L)
+ {
+ luax_pushstring(L, VERSION);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_getAuthor(lua_State* L)
+ {
+ luax_pushstring(L, AUTHOR);
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_getOS(lua_State* L)
+ {
+ #ifdef _WIN32
+ luax_pushstring(L, "windows");
+ #elif defined __unix__
+ luax_pushstring(L, "unix");
+ #elif defined __APPLE__
+ luax_pushstring(L, "macos");
+ #endif
+ return 1;
+ }
+
+ LUA_IMPLEMENT int l_revision(lua_State* L)
+ {
+ luax_pushnumber(L, REVISION);
+ return 1;
+ }
+
+ // Register jin module, keep it on the top of stack.
+ // The reason why not using luaL_register is because we want register some variables into jin module.
+ LUA_EXPORT void open(lua_State* L)
+ {
+ jin_log_info("Openning jin library.");
+
+ luax_globaltable(L, MODULE_NAME);
+
+ const luax_Str s[] = {
+ { "version", VERSION },
+ { "author", AUTHOR },
+ { "codename", CODE_NAME },
+ { 0, 0 }
+ };
+ const luax_Num n[] = {
+ { "revision", REVISION },
+ { 0, 0 }
+ };
+ // Register values.
+ luax_setfieldstrings(L, s);
+ luax_setfieldnumbers(L, n);
+
+ luax_Reg modules[] = {
+ { "core", luaopen_core },
+ { "event", luaopen_event },
+ { "graphics", luaopen_graphics },
+ { "time", luaopen_time },
+ { "mouse", luaopen_mouse },
+ { "keyboard", luaopen_keyboard },
+ { "filesystem", luaopen_filesystem },
+ { "net", luaopen_net },
+ { "audio", luaopen_audio },
+ { "joypad", luaopen_joypad },
+ { "math", luaopen_math },
+ { "thread", luaopen_thread },
+ { "bit", luaopen_bit },
+ //{"ai", luaopen_ai },
+ { 0, 0 }
+ };
+
+ // Register sub modules.
+ for (int i = 0; modules[i].name; ++i)
+ {
+ modules[i].func(L);
+ luax_setfield(L, -2, modules[i].name);
+ }
+
+ // Pop jin table.
+ luax_pop(L, 1);
+
+ jin_log_info("Done.");
+ }
+
+ // Embed structure.
+ struct jin_Embed
+ {
+ const char* file;
+ const char* source;
+ };
+
+ // Embed scripts.
+ #include "scripts/ai/state_machine.lua.h"
+ #include "scripts/graphics/graphics.lua.h"
+ #include "scripts/keyboard/keyboard.lua.h"
+ #include "scripts/mouse/mouse.lua.h"
+ #include "scripts/path/path.lua.h"
+ #include "scripts/time/time.lua.h"
+ #include "scripts/utils/json.lua.h"
+ #include "scripts/utils/xml.lua.h"
+ #include "scripts/log.lua.h"
+ #include "scripts/tiledmap/tiledmap.lua.h"
+ #include "scripts/physics/physics.lua.h"
+ #include "scripts/audio/audio.lua.h"
+
+ #include "scripts/app.lua.h"
+
+ // In order.
+ static const jin_Embed modules[] = {
+ // ai
+ { "state_machine.lua", state_machine_lua },
+ // keyboard
+ { "keyboard.lua", keyboard_lua },
+ { "mouse.lua", mouse_lua },
+ { "graphics.lua", graphics_lua },
+ { "path.lua", path_lua },
+ { "time.lua", time_lua },
+ { "json.lua", json_lua },
+ { "xml.lua", xml_lua },
+ { "tiledmap.lua", tiledmap_lua },
+ { "physics.lua", physics_lua },
+ { "audio.lua", audio_lua },
+ //
+ { "log.lua", log_lua },
+ { 0, 0 }
+ };
+
+ static const jin_Embed bootscript = { "app.lua", app_lua };
+
+ static void embed(lua_State* L)
+ {
+ jin_log_info("Load embeded scripts.");
+ const char* file, *source;
+ for (int i = 0; modules[i].file != 0; ++i)
+ {
+ file = modules[i].file;
+ source = modules[i].source;
+ jin_log_info("Embed script \"%s\".", file);
+ luax_clearstack(L);
+ if (luax_loadbuffer(L, source, strlen(source), file) == 0)
+ {
+ luax_call(L, 0, 0);
+ }
+ else
+ {
+ jin_log_error("Embed script \"%s\" failed.", file);
+ }
+ }
+ }
+
+ static void run(lua_State* L)
+ {
+ const char* file, *source;
+ file = bootscript.file;
+ source = bootscript.source;
+ jin_log_info("Run boot script \"%s\".", file);
+ luax_clearstack(L);
+ if (luax_loadbuffer(L, source, strlen(source), file) == 0)
+ {
+ luax_call(L, 0, 0);
+ }
+ }
+
+ LUA_EXPORT void boot(lua_State* L, const char* cwd)
+ {
+ luax_clearstack(L);
+ luax_getglobal(L, MODULE_NAME);
+ luax_newtable(L);
+ luax_setfieldstring(L, "cwd", cwd);
+ luax_setfield(L, -2, "args");
+ luax_clearstack(L);
+ embed(L);
+ run(L);
+ }
+
+ } // namespace Lua
+} // namespace JinEngine \ No newline at end of file