Esempio n. 1
0
int icelua_init(void)
{
	int i, argct;
	
	// create states
	lstate_client = (boot_mode & 1 ? luaL_newstate() : NULL);
	lstate_server = (boot_mode & 2 ? luaL_newstate() : NULL);
	
	// create tables
	if(lstate_client != NULL)
	{
		lua_newtable(lstate_client);
		lua_setglobal(lstate_client, "client");
		lua_newtable(lstate_client);
		lua_setglobal(lstate_client, "common");
		lua_pushvalue(lstate_client, LUA_GLOBALSINDEX);
		lua_setglobal(lstate_client, "_G");
	}
	
	if(lstate_server != NULL)
	{
		lua_newtable(lstate_server);
		lua_setglobal(lstate_server, "server");
		lua_newtable(lstate_server);
		lua_setglobal(lstate_server, "common");
		lua_pushvalue(lstate_server, LUA_GLOBALSINDEX);
		lua_setglobal(lstate_server, "_G");
	}
	
	// load stuff into them
#ifndef DEDI
	icelua_loadfuncs(lstate_client, "client", icelua_client);
	icelua_loadfuncs(lstate_client, "client", icelua_common);
	icelua_loadfuncs(lstate_client, "common", icelua_common);
	icelua_loadfuncs(lstate_client, "client", icelua_common_client);
	icelua_loadfuncs(lstate_client, "common", icelua_common_client);
#endif
	icelua_loadfuncs(lstate_server, "server", icelua_server);
	icelua_loadfuncs(lstate_server, "server", icelua_common);
	icelua_loadfuncs(lstate_server, "common", icelua_common);
	icelua_loadfuncs(lstate_server, "server", icelua_common_server);
	icelua_loadfuncs(lstate_server, "common", icelua_common_server);
	
	// load some lua base libraries
	icelua_loadbasefuncs(lstate_client);
	icelua_loadbasefuncs(lstate_server);
	
	// shove some pathnames / versions in
	if(lstate_server != NULL)
	{
		lua_getglobal(lstate_server, "common");
		lua_getglobal(lstate_server, "server");
		lua_pushstring(lstate_server, mod_basedir+4);
		lua_setfield(lstate_server, -2, "base_dir");
		lua_pop(lstate_server, 1);
		lua_pushstring(lstate_server, mod_basedir+4);
		lua_setfield(lstate_server, -2, "base_dir");
		lua_pop(lstate_server, 1);
		
		icelua_pushversion(lstate_server, "common");
		icelua_pushversion(lstate_server, "server");
	}
	
	if(lstate_client != NULL)
	{
		icelua_pushversion(lstate_client, "common");
		icelua_pushversion(lstate_client, "client");
	}
	
	/*
	NOTE:
	to call stuff, use lua_pcall.
	DO NOT use lua_call! if it fails, it will TERMINATE the program!
	*/
	
	// quick test
	// TODO: set up a "convert/filter file path" function
	// TODO: split the client/server inits
	char xpath[128];
	snprintf(xpath, 128, "%s/main_server.lua", mod_basedir);
	
	if((lstate_server != NULL) && luaL_loadfile(lstate_server, xpath) != 0)
	{
		printf("ERROR loading server Lua: %s\n", lua_tostring(lstate_server, -1));
		return 1;
	}
	
	argct = (main_largstart == -1 || (main_largstart >= main_argc)
		? 0
		: main_argc - main_largstart);
	
	if(lstate_server != NULL)
	{
		for(i = 0; i < argct; i++)
			lua_pushstring(lstate_server, main_argv[i+main_largstart]);
		if(lua_pcall(lstate_server, argct, 0, 0) != 0)
		{
			printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
			lua_pop(lstate_server, 1);
			return 1;
		}
	}
	
	if(lstate_client != NULL && mod_basedir != NULL)
		if(icelua_initfetch())
			return 1;
	
	// dispatch initial connect
	if(lstate_server != NULL && lstate_client != NULL)
	{
		lua_getglobal(lstate_server, "server");
		lua_getfield(lstate_server, -1, "hook_connect");
		lua_remove(lstate_server, -2);
		if(!lua_isnil(lstate_server, -1))
		{
			lua_pushboolean(lstate_server, 1);
			lua_newtable(lstate_server);
			
			lua_pushstring(lstate_server, "local");
			lua_setfield(lstate_server, -2, "proto");
			lua_pushnil(lstate_server);
			lua_setfield(lstate_server, -2, "addr");
			
			if(lua_pcall(lstate_server, 2, 0, 0) != 0)
			{
				printf("ERROR running server Lua (hook_connect): %s\n", lua_tostring(lstate_server, -1));
				lua_pop(lstate_server, 2);
				return 1;
			}
		} else {
			lua_pop(lstate_server, 1);
		}
	}
	
	return 0;
}
Esempio n. 2
0
int icelua_init(void)
{
	int i, argct;
	
	// create states
	lstate_client = (boot_mode & 1 ? luaL_newstate() : NULL);
	lstate_server = (boot_mode & 2 ? luaL_newstate() : NULL);
	
	if(lstate_client != NULL)
	{
		// create temp state for loading config
		lua_State *Lc = luaL_newstate();
		int v;
		float f;

		// load config
#ifndef DEDI
		if(!json_load(Lc, "clsave/config.json"))
		{
			// set video stuff 
			lua_getfield(Lc, -1, "video");

			lua_getfield(Lc, -1, "width");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) screen_width = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "height");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) screen_height = v;
			lua_pop(Lc, 1);
			
			lua_getfield(Lc, -1, "cubeshift");
			v = lua_tointeger(Lc, -1);
			if(v != 0) screen_cubeshift = -v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "antialiasinglevel");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) screen_antialiasing_level = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "smoothlighting");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) screen_smooth_lighting = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "vbo");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) gl_use_vbo = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_expand_textures");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) gl_expand_textures = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_chunk_size");
			v = lua_tointeger(Lc, -1);
			if(v > 0) gl_chunk_size = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_visible_chunks");
			v = lua_tointeger(Lc, -1);
			if(v > 0) gl_visible_chunks = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "gl_chunks_tesselated_per_frame");
			v = lua_tointeger(Lc, -1);
			if(v > 0) gl_chunks_tesselated_per_frame = v;
			lua_pop(Lc, 1);

			lua_getfield(Lc, -1, "fullscreen");
			v = lua_toboolean(Lc, -1);
			if(!lua_isnil(Lc, -1)) screen_fullscreen = v;
			lua_pop(Lc, 1);
			
			// drop table
			lua_pop(Lc, 1);

			// set audio stuff 
			lua_getfield(Lc, -1, "audio");

			lua_getfield(Lc, -1, "freq");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) wav_mfreq = v;
			lua_pop(Lc, 1);
			
			lua_getfield(Lc, -1, "bufsize");
			v = lua_tointeger(Lc, -1);
			if(v >= 0) wav_bufsize = v;
			lua_pop(Lc, 1);
			
			lua_getfield(Lc, -1, "volume");
			f = lua_tonumber(Lc, -1);
			if(!lua_isnil(Lc, -1)) wav_gvol = f;
			lua_pop(Lc, 1);
			
			// drop table
			lua_pop(Lc, 1);
		}
#endif
	}
	
	// create tables
	if(lstate_client != NULL)
	{
		lua_newtable(lstate_client);
		lua_setglobal(lstate_client, "client");
		lua_newtable(lstate_client);
		lua_setglobal(lstate_client, "common");
		lua_pushvalue(lstate_client, LUA_GLOBALSINDEX);
		lua_setglobal(lstate_client, "_G");
	}
	
	if(lstate_server != NULL)
	{
		lua_newtable(lstate_server);
		lua_setglobal(lstate_server, "server");
		lua_newtable(lstate_server);
		lua_setglobal(lstate_server, "common");
		lua_pushvalue(lstate_server, LUA_GLOBALSINDEX);
		lua_setglobal(lstate_server, "_G");
	}
	
	// load stuff into them
#ifndef DEDI
	icelua_loadfuncs(lstate_client, "client", icelua_client);
	icelua_loadfuncs(lstate_client, "client", icelua_common);
	icelua_loadfuncs(lstate_client, "common", icelua_common);
	icelua_loadfuncs(lstate_client, "client", icelua_common_client);
	icelua_loadfuncs(lstate_client, "common", icelua_common_client);
#endif
	icelua_loadfuncs(lstate_server, "server", icelua_server);
	icelua_loadfuncs(lstate_server, "server", icelua_common);
	icelua_loadfuncs(lstate_server, "common", icelua_common);
	icelua_loadfuncs(lstate_server, "server", icelua_common_server);
	icelua_loadfuncs(lstate_server, "common", icelua_common_server);
	
	// load some lua base libraries
	icelua_loadbasefuncs(lstate_client);
	icelua_loadbasefuncs(lstate_server);
	
	// shove some pathnames / versions in
	if(lstate_server != NULL)
	{
		lua_getglobal(lstate_server, "common");
		lua_getglobal(lstate_server, "server");
		lua_pushstring(lstate_server, mod_basedir+4);
		lua_setfield(lstate_server, -2, "base_dir");
		lua_pop(lstate_server, 1);
		lua_pushstring(lstate_server, mod_basedir+4);
		lua_setfield(lstate_server, -2, "base_dir");
		lua_pop(lstate_server, 1);
		
		icelua_pushversion(lstate_server, "common");
		icelua_pushversion(lstate_server, "server");
	}
	
	if(lstate_client != NULL)
	{
		icelua_pushversion(lstate_client, "common");
		icelua_pushversion(lstate_client, "client");
	}
	
	/*
	NOTE:
	to call stuff, use lua_pcall.
	DO NOT use lua_call! if it fails, it will TERMINATE the program!
	*/
	
	// quick test
	// TODO: set up a "convert/filter file path" function
	// TODO: split the client/server inits
	char xpath[128];
	snprintf(xpath, 128, "%s/main_server.lua", mod_basedir);
	
	if((lstate_server != NULL) && luaL_loadfile(lstate_server, xpath) != 0)
	{
		printf("ERROR loading server Lua: %s\n", lua_tostring(lstate_server, -1));
		return 1;
	}
	
	argct = (main_largstart == -1 || (main_largstart >= main_argc)
		? 0
		: main_argc - main_largstart);
	
	if(lstate_server != NULL)
	{
		for(i = 0; i < argct; i++)
			lua_pushstring(lstate_server, main_argv[i+main_largstart]);
		if(lua_pcall(lstate_server, argct, 0, 0) != 0)
		{
			printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
			lua_pop(lstate_server, 1);
			return 1;
		}
	}
	
	if(lstate_client != NULL && mod_basedir != NULL)
		if(icelua_initfetch())
			return 1;
	
	// dispatch initial connect
	if(lstate_server != NULL && lstate_client != NULL)
	{
		lua_getglobal(lstate_server, "server");
		lua_getfield(lstate_server, -1, "hook_connect");
		lua_remove(lstate_server, -2);
		if(!lua_isnil(lstate_server, -1))
		{
			lua_pushboolean(lstate_server, 1);
			lua_newtable(lstate_server);
			
			lua_pushstring(lstate_server, "local");
			lua_setfield(lstate_server, -2, "proto");
			lua_pushnil(lstate_server);
			lua_setfield(lstate_server, -2, "addr");
			
			if(lua_pcall(lstate_server, 2, 0, 0) != 0)
			{
				printf("ERROR running server Lua (hook_connect): %s\n", lua_tostring(lstate_server, -1));
				lua_pop(lstate_server, 2);
				return 1;
			}
		} else {
			lua_pop(lstate_server, 1);
		}
	}
	
	return 0;
}