aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/l_jin.cpp
blob: 997268460f9e66798076dc051b5ab72a5d184d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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