Merge pull request #252 from fkaa/rewrite-init
Rewrite initialisation to not restart process
This commit is contained in:
commit
64e9121086
3
.gitignore
vendored
3
.gitignore
vendored
@ -62,4 +62,7 @@ ZERO_CHECK.vcproj
|
||||
*.ipr
|
||||
*.iws
|
||||
|
||||
# vim stuff
|
||||
.vimrc
|
||||
|
||||
# clsave/vol/fastload.tmp
|
||||
|
@ -32,12 +32,15 @@
|
||||
#define NET_HOST_SIZE 256
|
||||
#define NET_PATH_SIZE 256
|
||||
|
||||
#define IB_CLIENT 0x1
|
||||
#define IB_SERVER 0x2
|
||||
#define IB_LAUNCHER 0x4
|
||||
#define IB_MAIN_LOADED 0x10
|
||||
#define IB_MAIN_LOADING 0x20
|
||||
#define IB_ENET 0x80
|
||||
#define IB_CLIENT (0x1 << 0)
|
||||
#define IB_SERVER (0x1 << 1)
|
||||
#define IB_LAUNCHER (0x1 << 2)
|
||||
#define IB_MAIN_LOADED (0x1 << 3)
|
||||
#define IB_MAIN_LOADING (0x1 << 4)
|
||||
#define IB_ENET (0x1 << 5)
|
||||
|
||||
#define IB_QUIT_SHUTDOWN (0x1 << 0)
|
||||
#define IB_QUIT_RESTART (0x1 << 1)
|
||||
|
||||
#define MODEL_BONE_MAX 256
|
||||
#define MODEL_POINT_MAX 4096
|
||||
@ -604,13 +607,12 @@ extern char *net_address;
|
||||
extern char *net_path;
|
||||
|
||||
extern char *mod_basedir;
|
||||
extern int restart_boot_mode;
|
||||
extern int boot_mode;
|
||||
extern int quitflag;
|
||||
|
||||
extern int main_argc;
|
||||
extern char **main_argv;
|
||||
extern char *main_argv0;
|
||||
extern char *main_oldcwd;
|
||||
extern int main_largstart;
|
||||
extern char **lua_args;
|
||||
extern int lua_args_len;
|
||||
|
||||
int run_game_cont1(void);
|
||||
int run_game_cont2(void);
|
||||
@ -618,6 +620,12 @@ int run_game_cont2(void);
|
||||
int error_sdl(char *msg);
|
||||
int error_perror(char *msg);
|
||||
|
||||
#ifndef DEDI
|
||||
void ib_create_launcher(const char *pkg);
|
||||
void ib_create_server(int port, const char *pkg);
|
||||
void ib_join_server(const char *address, int port);
|
||||
#endif
|
||||
|
||||
// map.c
|
||||
map_t *map_parse_aos(int len, const char *data);
|
||||
map_t *map_parse_icemap(int len, const char *data);
|
||||
|
@ -318,9 +318,11 @@ function client.hook_key(key, state, modif, uni)
|
||||
if field[4] == "action_save" then
|
||||
common.json_write("clsave/pub/user.json", profile)
|
||||
common.json_write("clsave/config.json", config)
|
||||
client.mk_sys_execv()
|
||||
client.create_launcher("pkg/iceball/launch")
|
||||
-- client.mk_sys_execv()
|
||||
elseif field[4] == "action_exit" then
|
||||
client.mk_sys_execv()
|
||||
client.create_launcher("pkg/iceball/launch")
|
||||
-- client.mk_sys_execv()
|
||||
end
|
||||
elseif key == SDLK_UP then
|
||||
selected = selected - 1
|
||||
|
@ -84,7 +84,8 @@ local function join_server(sid)
|
||||
local idx = (page * 9) + sid
|
||||
if idx <= #server_list then
|
||||
local sv = server_list[idx]
|
||||
client.mk_sys_execv("-c", sv.address, sv.port, arg_closure(argv))
|
||||
client.join_server(sv.address, sv.port, arg_closure(argv))
|
||||
-- client.mk_sys_execv("-c", sv.address, sv.port, arg_closure(argv))
|
||||
end
|
||||
end
|
||||
|
||||
@ -92,9 +93,11 @@ end
|
||||
function client.hook_key(key, state, modif, uni)
|
||||
if not state then
|
||||
if key == SDLK_l then
|
||||
client.mk_sys_execv("-s", "20737", "pkg/base", arg_closure(argv))
|
||||
client.create_server(20737, "pkg/base", arg_closure(argv))
|
||||
-- client.mk_sys_execv("-s", "20737", "pkg/base", arg_closure(argv))
|
||||
elseif key == SDLK_c then
|
||||
client.mk_sys_execv("-l", "pkg/iceball/config")
|
||||
client.create_launcher("pkg/iceball/config")
|
||||
-- client.mk_sys_execv("-l", "pkg/iceball/config")
|
||||
elseif key == SDLK_ESCAPE then
|
||||
client.hook_tick = nil
|
||||
elseif key >= SDLK_1 and key <= SDLK_9 then
|
||||
|
166
src/lua.c
166
src/lua.c
@ -81,87 +81,69 @@ int icelua_fn_common_mk_compat_disable(lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
#ifndef DEDI
|
||||
int icelua_fn_client_mk_sys_execv(lua_State *L)
|
||||
static void update_lua_args(lua_State *L)
|
||||
{
|
||||
if(!(boot_mode & IB_LAUNCHER))
|
||||
return luaL_error(L, "mk_sys_execv called when not in -l mode");
|
||||
int top = lua_gettop(L);
|
||||
|
||||
if (lua_args) {
|
||||
for (int i = 0; i < lua_args_len; ++i) {
|
||||
free(lua_args[i]);
|
||||
}
|
||||
|
||||
free(lua_args);
|
||||
lua_args = NULL;
|
||||
}
|
||||
|
||||
if (top > 2) {
|
||||
lua_args = malloc(sizeof(*lua_args) * (top - 3));
|
||||
|
||||
for(int i = 3; i <= top; i++) {
|
||||
lua_args[i - 3] = strdup(lua_tostring(L, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int icelua_fn_client_join_server(lua_State *L)
|
||||
{
|
||||
if (!(boot_mode & IB_LAUNCHER))
|
||||
return luaL_error(L, "join_server called when not in -l mode");
|
||||
|
||||
update_lua_args(L);
|
||||
|
||||
const char *address = lua_tostring(L, 1);
|
||||
int port = lua_tonumber(L, 2);
|
||||
|
||||
ib_join_server(address, port);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int icelua_fn_client_create_server(lua_State *L)
|
||||
{
|
||||
if (!(boot_mode & IB_LAUNCHER))
|
||||
return luaL_error(L, "create_server called when not in -l mode");
|
||||
|
||||
update_lua_args(L);
|
||||
|
||||
int port = lua_tonumber(L, 1);
|
||||
const char *pkg = lua_tostring(L, 2);
|
||||
|
||||
ib_create_server(port, pkg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int icelua_fn_client_create_launcher(lua_State *L)
|
||||
{
|
||||
if (!(boot_mode & IB_LAUNCHER))
|
||||
return luaL_error(L, "create_launcher called when not in -l mode");
|
||||
|
||||
int top = lua_gettop(L);
|
||||
char **arglist = malloc(sizeof(char *) * (top+2));
|
||||
int i;
|
||||
|
||||
for(i = 1; i <= top; i++)
|
||||
arglist[i] = strdup(lua_tostring(L, i));
|
||||
const char *pkg = lua_tostring(L, -1);
|
||||
|
||||
arglist[0] = strdup(main_argv0);
|
||||
arglist[top+1] = NULL;
|
||||
ib_create_launcher(pkg);
|
||||
|
||||
SDL_Quit();
|
||||
#ifdef WIN32
|
||||
#if 1
|
||||
//if(main_oldcwd != NULL)
|
||||
// _chdir(main_oldcwd);
|
||||
|
||||
char cwd[2048] = "";
|
||||
GetModuleFileName(NULL, cwd, 2047);
|
||||
char *v = cwd + strlen(cwd) - 1;
|
||||
while (v >= cwd)
|
||||
{
|
||||
if (*v == '\\')
|
||||
{
|
||||
v++;
|
||||
break;
|
||||
}
|
||||
v--;
|
||||
}
|
||||
arglist[0] = v;
|
||||
|
||||
for (i = 0; i <= top; i++)
|
||||
{
|
||||
int new_size = strlen(arglist[i]) + 3;
|
||||
int j;
|
||||
for (j = 0; j < strlen(arglist[i]); j++)
|
||||
{
|
||||
if (arglist[i][j] == '"' || arglist[i][j] == '\\')
|
||||
new_size++;
|
||||
}
|
||||
char *new_arg = malloc(new_size);
|
||||
char *k = new_arg;
|
||||
char *l = arglist[i];
|
||||
*(k++) = '"';
|
||||
while (*l != 0)
|
||||
{
|
||||
if (*l == '"' || *l == '\\')
|
||||
*(k++) = '\\';
|
||||
*(k++) = *(l++);
|
||||
}
|
||||
*(k++) = '"';
|
||||
*(k++) = 0;
|
||||
arglist[i] = new_arg;
|
||||
}
|
||||
/*FILE *fp = fopen("FUCK.txt", "w");
|
||||
for (i = 0; i <= top; i++)
|
||||
{
|
||||
fprintf(fp, "%s ", arglist[i]);
|
||||
fflush(fp);
|
||||
}*/
|
||||
#else
|
||||
char *v = strdup("iceball.exe");
|
||||
arglist[0] = v;
|
||||
#endif
|
||||
#endif
|
||||
printf("argv0: [%s]\n", main_argv0);
|
||||
fflush(stdout);
|
||||
|
||||
execv(main_argv0, arglist);
|
||||
|
||||
printf("WORK YOU FUCKASS: %s\n", strerror(errno));
|
||||
fflush(stdout);
|
||||
|
||||
// DOES NOT RETURN.
|
||||
fprintf(stderr, "ABORT: sys_execv must not return!\n");
|
||||
fflush(stderr);
|
||||
abort();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -208,8 +190,10 @@ int icelua_fn_client_mk_set_title(lua_State *L)
|
||||
|
||||
#ifndef DEDI
|
||||
struct icelua_entry icelua_client[] = {
|
||||
{icelua_fn_client_create_launcher, "create_launcher"},
|
||||
{icelua_fn_client_create_server, "create_server"},
|
||||
{icelua_fn_client_join_server, "join_server"},
|
||||
{icelua_fn_client_mk_set_title, "mk_set_title"},
|
||||
{icelua_fn_client_mk_sys_execv, "mk_sys_execv"},
|
||||
|
||||
{icelua_fn_client_text_input_start, "text_input_start"},
|
||||
{icelua_fn_client_text_input_stop, "text_input_stop"},
|
||||
@ -402,11 +386,7 @@ void icelua_loadbasefuncs(lua_State *L)
|
||||
|
||||
int icelua_initfetch(void)
|
||||
{
|
||||
int i;
|
||||
char xpath[128+1];
|
||||
int argct = (main_largstart == -1 || (main_largstart >= main_argc)
|
||||
? 0
|
||||
: main_argc - main_largstart);
|
||||
|
||||
if(to_client_local.sockfd == -1)
|
||||
to_client_local.sockfd = SOCKFD_LOCAL;
|
||||
@ -439,8 +419,10 @@ int icelua_initfetch(void)
|
||||
}
|
||||
|
||||
printf("Client loaded! Initialising...\n");
|
||||
for(i = 0; i < argct; i++)
|
||||
lua_pushstring(lstate_client, main_argv[i+main_largstart]);
|
||||
for(int i = 0; i < lua_args_len; i++)
|
||||
lua_pushstring(lstate_client, lua_args[i]);
|
||||
|
||||
int argct = lua_args_len;
|
||||
if((boot_mode & IB_CLIENT) && net_path[1] != '\x00')
|
||||
{
|
||||
lua_pushstring(lstate_client, net_path);
|
||||
@ -506,8 +488,6 @@ void icelua_pushversion(lua_State *L, const char *tabname)
|
||||
|
||||
int icelua_init(void)
|
||||
{
|
||||
int i, argct;
|
||||
|
||||
// create states
|
||||
if(boot_mode & IB_CLIENT)
|
||||
{
|
||||
@ -681,7 +661,7 @@ int icelua_init(void)
|
||||
raw_whitelist = malloc(sizeof(struct icelua_whitelist)*raw_whitelist_len);
|
||||
|
||||
// read each entry
|
||||
for(i = 0; i < raw_whitelist_len; i++)
|
||||
for(int i = 0; i < raw_whitelist_len; i++)
|
||||
{
|
||||
printf("entry %i/%i\n", i+1, raw_whitelist_len);
|
||||
// get entry
|
||||
@ -818,15 +798,12 @@ int icelua_init(void)
|
||||
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)
|
||||
for(int i = 0; i < lua_args_len; i++)
|
||||
lua_pushstring(lstate_server, lua_args[i]);
|
||||
|
||||
if(lua_pcall(lstate_server, lua_args_len, 0, 0) != 0)
|
||||
{
|
||||
printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
|
||||
lua_pop(lstate_server, 1);
|
||||
@ -870,6 +847,11 @@ int icelua_init(void)
|
||||
|
||||
void icelua_deinit(void)
|
||||
{
|
||||
if (lstate_client)
|
||||
lua_close(lstate_client);
|
||||
|
||||
if (lstate_server)
|
||||
lua_close(lstate_server);
|
||||
// TODO!
|
||||
}
|
||||
|
||||
|
283
src/main.c
283
src/main.c
@ -54,18 +54,17 @@ int force_redraw = 1;
|
||||
|
||||
// bit 0 = client, bit 1 = server, bit 2 = main_client.lua has been loaded,
|
||||
// bit 3 = currently loading main_client.lua and co, bit 4 == use ENet for client
|
||||
int restart_boot_mode = 0;
|
||||
int boot_mode = 0;
|
||||
int quitflag = 0;
|
||||
|
||||
char *mod_basedir = NULL;
|
||||
char *net_address;
|
||||
char *net_path;
|
||||
int net_port;
|
||||
|
||||
int main_argc;
|
||||
char **main_argv;
|
||||
char *main_argv0;
|
||||
char *main_oldcwd;
|
||||
int main_largstart = -1;
|
||||
char **lua_args;
|
||||
int lua_args_len;
|
||||
|
||||
int64_t frame_prev = 0;
|
||||
int64_t frame_now = 0;
|
||||
@ -237,10 +236,14 @@ int video_init(void)
|
||||
|
||||
void video_deinit(void)
|
||||
{
|
||||
if (gl_context)
|
||||
if (gl_context) {
|
||||
SDL_GL_DeleteContext(gl_context);
|
||||
if (window)
|
||||
gl_context = NULL;
|
||||
}
|
||||
if (window) {
|
||||
SDL_DestroyWindow(window);
|
||||
window = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void platform_deinit(void)
|
||||
@ -403,6 +406,65 @@ int64_t platform_get_time_usec(void)
|
||||
}
|
||||
|
||||
#ifndef DEDI
|
||||
void ib_create_launcher(const char *pkg)
|
||||
{
|
||||
if (net_address) {
|
||||
free(net_address);
|
||||
}
|
||||
|
||||
net_address = NULL;
|
||||
net_port = 0;
|
||||
|
||||
if (mod_basedir) {
|
||||
free(mod_basedir);
|
||||
}
|
||||
|
||||
mod_basedir = malloc(PATH_LEN_MAX);
|
||||
mod_basedir = strncpy(mod_basedir, pkg, PATH_LEN_MAX);
|
||||
|
||||
restart_boot_mode = IB_LAUNCHER | IB_CLIENT | IB_SERVER;
|
||||
quitflag |= IB_QUIT_RESTART;
|
||||
}
|
||||
|
||||
void ib_create_server(int port, const char *pkg)
|
||||
{
|
||||
if (net_address) {
|
||||
free(net_address);
|
||||
}
|
||||
|
||||
net_address = NULL;
|
||||
net_port = port;
|
||||
|
||||
if (mod_basedir) {
|
||||
free(mod_basedir);
|
||||
}
|
||||
|
||||
mod_basedir = malloc(PATH_LEN_MAX);
|
||||
mod_basedir = strncpy(mod_basedir, pkg, PATH_LEN_MAX);
|
||||
|
||||
restart_boot_mode = IB_CLIENT | IB_SERVER;
|
||||
quitflag |= IB_QUIT_RESTART;
|
||||
}
|
||||
|
||||
void ib_join_server(const char *address, int port)
|
||||
{
|
||||
if (mod_basedir)
|
||||
free(mod_basedir);
|
||||
|
||||
if (!net_address)
|
||||
net_address = malloc(NET_HOST_SIZE);
|
||||
|
||||
mod_basedir = NULL;
|
||||
net_address[0] = '\0';
|
||||
net_path[0] = '/';
|
||||
net_path[1] = '\0';
|
||||
net_port = port;
|
||||
|
||||
net_address = strncpy(net_address, address, NET_HOST_SIZE);
|
||||
restart_boot_mode = IB_CLIENT | IB_ENET;
|
||||
quitflag |= IB_QUIT_RESTART;
|
||||
}
|
||||
|
||||
static int ib_client_tick_hook(void) {
|
||||
lua_getglobal(lstate_client, "client");
|
||||
lua_getfield(lstate_client, -1, "hook_tick");
|
||||
@ -423,7 +485,7 @@ static int ib_client_tick_hook(void) {
|
||||
{
|
||||
printf("Lua Client Error (tick): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
//if(!(boot_mode & IB_SERVER))
|
||||
sec_wait += lua_tonumber(lstate_client, -1);
|
||||
@ -442,7 +504,7 @@ static int ib_client_render_hook(void) {
|
||||
{
|
||||
printf("Lua Client Error (render): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@ -471,7 +533,7 @@ static int ib_client_key_hook(SDL_Event ev) {
|
||||
if (lua_pcall(lstate_client, 4, 0, 0) != 0) {
|
||||
printf("Lua Client Error (key): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -494,7 +556,7 @@ static int ib_client_text_hook(SDL_Event ev) {
|
||||
if (lua_pcall(lstate_client, 1, 0, 0) != 0) {
|
||||
printf("Lua Client Error (text): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -515,7 +577,7 @@ static int ib_client_mouse_press_hook(SDL_Event ev) {
|
||||
if (lua_pcall(lstate_client, 2, 0, 0) != 0) {
|
||||
printf("Lua Client Error (mouse_button): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -548,7 +610,7 @@ static int ib_client_mouse_motion_hook(SDL_Event ev)
|
||||
if (lua_pcall(lstate_client, 4, 0, 0) != 0) {
|
||||
printf("Lua Client Error (mouse_motion): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -568,16 +630,14 @@ static int ib_client_window_focus_hook(SDL_Event ev) {
|
||||
if (lua_pcall(lstate_client, 1, 0, 0) != 0) {
|
||||
printf("Lua Client Error (window_activate): %s\n", lua_tostring(lstate_client, -1));
|
||||
lua_pop(lstate_client, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int update_fps_counter(void)
|
||||
void update_fps_counter(void)
|
||||
{
|
||||
int quitflag = 0;
|
||||
|
||||
// update FPS counter
|
||||
frame_now = platform_get_time_usec();
|
||||
fps++;
|
||||
@ -590,14 +650,11 @@ int update_fps_counter(void)
|
||||
fps = 0;
|
||||
frame_prev = platform_get_time_usec();
|
||||
}
|
||||
|
||||
return quitflag;
|
||||
}
|
||||
|
||||
int render_client(void)
|
||||
{
|
||||
int quitflag = 0;
|
||||
|
||||
int quit = 0;
|
||||
// skip while still loading
|
||||
if(mod_basedir == NULL || (boot_mode & IB_MAIN_LOADING))
|
||||
return 0;
|
||||
@ -640,7 +697,7 @@ int render_client(void)
|
||||
}
|
||||
|
||||
// apply Lua HUD / model stuff
|
||||
quitflag = quitflag || ib_client_render_hook();
|
||||
quit = ib_client_render_hook();
|
||||
|
||||
// clean up stuff that may have happened in the scene
|
||||
glDepthMask(GL_TRUE);
|
||||
@ -663,29 +720,29 @@ int render_client(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
return quitflag;
|
||||
return quit;
|
||||
}
|
||||
|
||||
int poll_events(void)
|
||||
{
|
||||
int quitflag = 0;
|
||||
int quit = 0;
|
||||
|
||||
SDL_Event ev;
|
||||
while(SDL_PollEvent(&ev)) {
|
||||
while(SDL_PollEvent(&ev) && !quit) {
|
||||
switch (ev.type) {
|
||||
case SDL_KEYUP:
|
||||
case SDL_KEYDOWN:
|
||||
quitflag = ib_client_key_hook(ev);
|
||||
quit |= ib_client_key_hook(ev);
|
||||
break;
|
||||
case SDL_TEXTINPUT:
|
||||
quitflag = ib_client_text_hook(ev);
|
||||
quit |= ib_client_text_hook(ev);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
quitflag = ib_client_mouse_press_hook(ev);
|
||||
quit |= ib_client_mouse_press_hook(ev);
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
quitflag = ib_client_mouse_motion_hook(ev);
|
||||
quit |= ib_client_mouse_motion_hook(ev);
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (ev.window.event) {
|
||||
@ -702,26 +759,27 @@ int poll_events(void)
|
||||
}
|
||||
}
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
quitflag = ib_client_window_focus_hook(ev);
|
||||
quit |= ib_client_window_focus_hook(ev);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
quitflag = 1;
|
||||
quit |= IB_QUIT_SHUTDOWN;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return quitflag;
|
||||
return quit;
|
||||
}
|
||||
|
||||
int update_client(void)
|
||||
{
|
||||
int quitflag = update_fps_counter();
|
||||
int quit = 0;
|
||||
update_fps_counter();
|
||||
|
||||
if(mod_basedir == NULL)
|
||||
{
|
||||
@ -735,21 +793,19 @@ int update_client(void)
|
||||
|
||||
boot_mode &= ~IB_MAIN_LOADING;
|
||||
} else {
|
||||
quitflag = quitflag || ib_client_tick_hook();
|
||||
quit |= ib_client_tick_hook();
|
||||
}
|
||||
|
||||
quitflag = quitflag || render_client();
|
||||
quit |= render_client();
|
||||
quit |= poll_events();
|
||||
|
||||
quitflag = quitflag || poll_events();
|
||||
|
||||
return quitflag;
|
||||
return quit;
|
||||
}
|
||||
#endif
|
||||
|
||||
int update_server(void)
|
||||
{
|
||||
// TODO: respect time returned
|
||||
int quitflag = 0;
|
||||
|
||||
lua_getglobal(lstate_server, "server");
|
||||
lua_getfield(lstate_server, -1, "hook_tick");
|
||||
@ -757,7 +813,7 @@ int update_server(void)
|
||||
if(lua_isnil(lstate_server, -1))
|
||||
{
|
||||
lua_pop(lstate_server, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
lua_pushnumber(lstate_server, sec_curtime);
|
||||
@ -766,7 +822,7 @@ int update_server(void)
|
||||
{
|
||||
printf("Lua Server Error (tick): %s\n", lua_tostring(lstate_server, -1));
|
||||
lua_pop(lstate_server, 1);
|
||||
return 1;
|
||||
return IB_QUIT_SHUTDOWN;
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
@ -795,10 +851,10 @@ int update_server(void)
|
||||
#ifndef DEDI
|
||||
int run_game_cont1(void)
|
||||
{
|
||||
int quitflag = render_client();
|
||||
int quit = render_client();
|
||||
net_flush();
|
||||
if(boot_mode & IB_SERVER)
|
||||
quitflag = quitflag || update_server();
|
||||
quit |= update_server();
|
||||
net_flush();
|
||||
|
||||
// update time
|
||||
@ -807,16 +863,16 @@ int run_game_cont1(void)
|
||||
sec_curtime = ((double)usec_curtime)/1000000.0;
|
||||
|
||||
// update client/server
|
||||
quitflag = quitflag || update_fps_counter();
|
||||
update_fps_counter();
|
||||
|
||||
return quitflag;
|
||||
return quit;
|
||||
}
|
||||
|
||||
int run_game_cont2(void)
|
||||
{
|
||||
int quitflag = 0;
|
||||
int quit = 0;
|
||||
if(boot_mode & IB_SERVER)
|
||||
quitflag = quitflag || update_server();
|
||||
quit |= update_server();
|
||||
net_flush();
|
||||
|
||||
// update time
|
||||
@ -824,7 +880,7 @@ int run_game_cont2(void)
|
||||
int64_t usec_curtime = platform_get_time_usec() - usec_basetime;
|
||||
sec_curtime = ((double)usec_curtime)/1000000.0;
|
||||
|
||||
return quitflag;
|
||||
return quit;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -849,8 +905,6 @@ static void run_game(void)
|
||||
|
||||
//render_vxl_redraw(&tcam, clmap);
|
||||
|
||||
int quitflag = 0;
|
||||
|
||||
usec_basetime = platform_get_time_usec();
|
||||
|
||||
while(!quitflag)
|
||||
@ -863,11 +917,11 @@ static void run_game(void)
|
||||
// update client/server
|
||||
#ifndef DEDI
|
||||
if(boot_mode & IB_CLIENT)
|
||||
quitflag = quitflag || update_client();
|
||||
quitflag |= update_client();
|
||||
net_flush();
|
||||
#endif
|
||||
if(boot_mode & IB_SERVER)
|
||||
quitflag = quitflag || update_server();
|
||||
quitflag |= update_server();
|
||||
net_flush();
|
||||
}
|
||||
map_free(clmap);
|
||||
@ -929,29 +983,34 @@ struct cli_args {
|
||||
|
||||
char *basedir;
|
||||
int boot_mode;
|
||||
|
||||
char **extra_args;
|
||||
int extra_args_len;
|
||||
int used_args;
|
||||
};
|
||||
|
||||
static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
args->net_host = malloc(NET_HOST_SIZE);
|
||||
args->net_path = malloc(NET_PATH_SIZE);
|
||||
args->basedir = malloc(PATH_LEN_MAX);
|
||||
|
||||
// we set the initial value to a leading slash in order to play nice with
|
||||
// sscanf later on
|
||||
args->net_path[0] = '/';
|
||||
args->net_path[1] = '\0';
|
||||
|
||||
int used_args = 0;
|
||||
|
||||
#ifdef DEDI
|
||||
if (argc <= 1)
|
||||
return 1;
|
||||
#else
|
||||
if (argc <= 1) {
|
||||
args->net_port = 0;
|
||||
args->basedir = "pkg/iceball/launch";
|
||||
// TODO: Just make IB_LAUNCHER == IB_LAUNCHER | IB_CLIENT | IB_SERVER? It piggybacks on the existing
|
||||
// (client | server) setup stuff anyway, so IB_LAUNCHER without (IB_CLIENT | IB_SERVER) doesn't make sense.
|
||||
args->boot_mode = IB_LAUNCHER | IB_CLIENT | IB_SERVER;
|
||||
args->used_args = 4;
|
||||
args->basedir = strncpy(args->basedir, "pkg/iceball/launch", PATH_LEN_MAX);
|
||||
args->boot_mode = IB_CLIENT | IB_SERVER | IB_LAUNCHER;
|
||||
|
||||
used_args = 1;
|
||||
} else
|
||||
#endif
|
||||
|
||||
@ -963,10 +1022,10 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
args->net_port = 0;
|
||||
args->boot_mode = IB_LAUNCHER | IB_CLIENT | IB_SERVER;
|
||||
// TODO: Ensure used_args values are correct
|
||||
args->used_args = 2;
|
||||
used_args = 2;
|
||||
if (argc >= 3) {
|
||||
args->basedir = argv[2];
|
||||
args->used_args = 3;
|
||||
args->basedir = strncpy(args->basedir, argv[2], PATH_LEN_MAX);
|
||||
used_args = 3;
|
||||
}
|
||||
} else if (!strcmp(argv[1], "-c")) {
|
||||
if (argc <= 2 || (argc <= 3 && memcmp(argv[2], "iceball://", 10))) {
|
||||
@ -974,7 +1033,7 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
}
|
||||
|
||||
args->net_port = 20737;
|
||||
args->used_args = 3;
|
||||
used_args = 3;
|
||||
|
||||
if (sscanf(argv[2], "iceball://%[^:]:%i/%s", args->net_host, &args->net_port, &args->net_path[1]) < 1) {
|
||||
if (argc <= 3) {
|
||||
@ -983,10 +1042,10 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
|
||||
args->net_host = strncpy(args->net_host, argv[2], NET_HOST_SIZE);
|
||||
args->net_port = atoi(argv[3]);
|
||||
args->used_args = 4;
|
||||
used_args = 4;
|
||||
}
|
||||
|
||||
args->basedir = NULL;
|
||||
args->basedir[0] = '\0';
|
||||
args->boot_mode = IB_CLIENT | IB_ENET;
|
||||
|
||||
printf("Connecting to \"%s\" port %i (ENet mode)\n", args->net_host, args->net_port);
|
||||
@ -996,7 +1055,7 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
}
|
||||
|
||||
args->net_port = 20737;
|
||||
args->used_args = 3;
|
||||
used_args = 3;
|
||||
|
||||
if (sscanf(argv[2], "iceball://%[^:]:%i/%s", args->net_host, &args->net_port, &args->net_path[1]) < 1) {
|
||||
if (argc <= 3) {
|
||||
@ -1005,10 +1064,10 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
|
||||
args->net_host = strncpy(args->net_host, argv[2], NET_HOST_SIZE);
|
||||
args->net_port = atoi(argv[3]);
|
||||
args->used_args = 4;
|
||||
used_args = 4;
|
||||
}
|
||||
|
||||
args->basedir = NULL;
|
||||
args->basedir[0] = '\0';
|
||||
args->boot_mode = IB_CLIENT;
|
||||
|
||||
printf("Connecting to \"%s\" port %i (TCP mode)\n", args->net_host, args->net_port);
|
||||
@ -1018,9 +1077,9 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
}
|
||||
|
||||
args->net_port = atoi(argv[2]);
|
||||
args->basedir = argv[3];
|
||||
args->basedir = strncpy(args->basedir, argv[3], PATH_LEN_MAX);
|
||||
args->boot_mode = IB_CLIENT | IB_SERVER;
|
||||
args->used_args = 4;
|
||||
used_args = 4;
|
||||
|
||||
printf("Starting server on port %i, mod \"%s\" (local mode client)\n", args->net_port, args->basedir);
|
||||
} else
|
||||
@ -1031,9 +1090,9 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
}
|
||||
|
||||
args->net_port = atoi(argv[2]);
|
||||
args->basedir = argv[3];
|
||||
args->basedir = strncpy(args->basedir, argv[3], PATH_LEN_MAX);
|
||||
args->boot_mode = IB_SERVER;
|
||||
args->used_args = 4;
|
||||
used_args = 4;
|
||||
|
||||
printf("Starting headless/dedicated server on port %i, mod \"%s\"\n", args->net_port, args->basedir);
|
||||
} else {
|
||||
@ -1054,6 +1113,12 @@ static int parse_args(int argc, char *argv[], struct cli_args *args) {
|
||||
}
|
||||
}
|
||||
|
||||
args->extra_args_len = argc - used_args;
|
||||
args->extra_args = malloc(sizeof(*args->extra_args) * args->extra_args_len);
|
||||
for (int i = 0; i < args->extra_args_len; ++i) {
|
||||
args->extra_args[i] = strdup(argv[used_args + i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1061,34 +1126,11 @@ static void free_args(struct cli_args *args)
|
||||
{
|
||||
free(args->net_host);
|
||||
free(args->net_path);
|
||||
free(args->basedir);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int start_game()
|
||||
{
|
||||
struct cli_args args = {0};
|
||||
int parse_status = parse_args(argc, argv, &args);
|
||||
|
||||
if (parse_status) {
|
||||
print_usage(argv[0]);
|
||||
|
||||
free_args(&args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: minimize usage of globals
|
||||
main_argc = argc;
|
||||
main_argv = argv;
|
||||
main_argv0 = argv[0];
|
||||
main_oldcwd = NULL;
|
||||
|
||||
net_address = args.net_host;
|
||||
net_port = args.net_port;
|
||||
net_path = args.net_path;
|
||||
|
||||
boot_mode = args.boot_mode;
|
||||
mod_basedir = args.basedir;
|
||||
main_largstart = args.used_args;
|
||||
|
||||
#ifdef DEDI
|
||||
if (net_init()) goto cleanup;
|
||||
if (icelua_init()) goto cleanup;
|
||||
@ -1106,9 +1148,6 @@ cleanup:
|
||||
net_deinit();
|
||||
|
||||
#else
|
||||
if (boot_mode & IB_CLIENT)
|
||||
if (platform_init()) goto cleanup;
|
||||
|
||||
if (net_init()) goto cleanup;
|
||||
if (icelua_init()) goto cleanup;
|
||||
|
||||
@ -1137,7 +1176,53 @@ cleanup:
|
||||
|
||||
icelua_deinit();
|
||||
net_deinit();
|
||||
#endif
|
||||
|
||||
boot_mode = restart_boot_mode;
|
||||
|
||||
return quitflag;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct cli_args args = {0};
|
||||
int parse_status = parse_args(argc, argv, &args);
|
||||
|
||||
if (parse_status) {
|
||||
print_usage(argv[0]);
|
||||
|
||||
free_args(&args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: minimize usage of globals
|
||||
|
||||
lua_args = args.extra_args;
|
||||
lua_args_len = args.extra_args_len;
|
||||
|
||||
net_address = args.net_host;
|
||||
net_port = args.net_port;
|
||||
net_path = args.net_path;
|
||||
|
||||
boot_mode = args.boot_mode;
|
||||
|
||||
if (args.basedir && *args.basedir == '\0') {
|
||||
free(args.basedir);
|
||||
args.basedir = NULL;
|
||||
}
|
||||
|
||||
mod_basedir = args.basedir;
|
||||
|
||||
#ifndef DEDI
|
||||
if (boot_mode & IB_CLIENT)
|
||||
if (platform_init()) goto cleanup;
|
||||
#endif
|
||||
|
||||
while (start_game() != IB_QUIT_SHUTDOWN)
|
||||
quitflag = 0;
|
||||
|
||||
#ifndef DEDI
|
||||
cleanup:
|
||||
if (boot_mode & IB_CLIENT)
|
||||
platform_deinit();
|
||||
#endif
|
||||
@ -1145,7 +1230,5 @@ cleanup:
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
free_args(&args);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user