Skip to content

Commit 64e9121

Browse files
authored
Merge pull request #252 from fkaa/rewrite-init
Rewrite initialisation to not restart process
2 parents d644ef6 + 58ee777 commit 64e9121

File tree

6 files changed

+285
-204
lines changed

6 files changed

+285
-204
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ ZERO_CHECK.vcproj
6262
*.ipr
6363
*.iws
6464

65+
# vim stuff
66+
.vimrc
67+
6568
# clsave/vol/fastload.tmp

include/common.h

+19-11
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
#define NET_HOST_SIZE 256
3333
#define NET_PATH_SIZE 256
3434

35-
#define IB_CLIENT 0x1
36-
#define IB_SERVER 0x2
37-
#define IB_LAUNCHER 0x4
38-
#define IB_MAIN_LOADED 0x10
39-
#define IB_MAIN_LOADING 0x20
40-
#define IB_ENET 0x80
35+
#define IB_CLIENT (0x1 << 0)
36+
#define IB_SERVER (0x1 << 1)
37+
#define IB_LAUNCHER (0x1 << 2)
38+
#define IB_MAIN_LOADED (0x1 << 3)
39+
#define IB_MAIN_LOADING (0x1 << 4)
40+
#define IB_ENET (0x1 << 5)
41+
42+
#define IB_QUIT_SHUTDOWN (0x1 << 0)
43+
#define IB_QUIT_RESTART (0x1 << 1)
4144

4245
#define MODEL_BONE_MAX 256
4346
#define MODEL_POINT_MAX 4096
@@ -604,20 +607,25 @@ extern char *net_address;
604607
extern char *net_path;
605608

606609
extern char *mod_basedir;
610+
extern int restart_boot_mode;
607611
extern int boot_mode;
612+
extern int quitflag;
608613

609-
extern int main_argc;
610-
extern char **main_argv;
611-
extern char *main_argv0;
612-
extern char *main_oldcwd;
613-
extern int main_largstart;
614+
extern char **lua_args;
615+
extern int lua_args_len;
614616

615617
int run_game_cont1(void);
616618
int run_game_cont2(void);
617619

618620
int error_sdl(char *msg);
619621
int error_perror(char *msg);
620622

623+
#ifndef DEDI
624+
void ib_create_launcher(const char *pkg);
625+
void ib_create_server(int port, const char *pkg);
626+
void ib_join_server(const char *address, int port);
627+
#endif
628+
621629
// map.c
622630
map_t *map_parse_aos(int len, const char *data);
623631
map_t *map_parse_icemap(int len, const char *data);

pkg/iceball/config/main_client.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ function client.hook_key(key, state, modif, uni)
318318
if field[4] == "action_save" then
319319
common.json_write("clsave/pub/user.json", profile)
320320
common.json_write("clsave/config.json", config)
321-
client.mk_sys_execv()
321+
client.create_launcher("pkg/iceball/launch")
322+
-- client.mk_sys_execv()
322323
elseif field[4] == "action_exit" then
323-
client.mk_sys_execv()
324+
client.create_launcher("pkg/iceball/launch")
325+
-- client.mk_sys_execv()
324326
end
325327
elseif key == SDLK_UP then
326328
selected = selected - 1

pkg/iceball/launch/main_client.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ local function join_server(sid)
8484
local idx = (page * 9) + sid
8585
if idx <= #server_list then
8686
local sv = server_list[idx]
87-
client.mk_sys_execv("-c", sv.address, sv.port, arg_closure(argv))
87+
client.join_server(sv.address, sv.port, arg_closure(argv))
88+
-- client.mk_sys_execv("-c", sv.address, sv.port, arg_closure(argv))
8889
end
8990
end
9091

9192
-- Some hooks
9293
function client.hook_key(key, state, modif, uni)
9394
if not state then
9495
if key == SDLK_l then
95-
client.mk_sys_execv("-s", "20737", "pkg/base", arg_closure(argv))
96+
client.create_server(20737, "pkg/base", arg_closure(argv))
97+
-- client.mk_sys_execv("-s", "20737", "pkg/base", arg_closure(argv))
9698
elseif key == SDLK_c then
97-
client.mk_sys_execv("-l", "pkg/iceball/config")
99+
client.create_launcher("pkg/iceball/config")
100+
-- client.mk_sys_execv("-l", "pkg/iceball/config")
98101
elseif key == SDLK_ESCAPE then
99102
client.hook_tick = nil
100103
elseif key >= SDLK_1 and key <= SDLK_9 then

src/lua.c

+70-88
Original file line numberDiff line numberDiff line change
@@ -81,87 +81,69 @@ int icelua_fn_common_mk_compat_disable(lua_State *L)
8181
return 0;
8282
}
8383
#ifndef DEDI
84-
int icelua_fn_client_mk_sys_execv(lua_State *L)
84+
static void update_lua_args(lua_State *L)
8585
{
86-
if(!(boot_mode & IB_LAUNCHER))
87-
return luaL_error(L, "mk_sys_execv called when not in -l mode");
88-
8986
int top = lua_gettop(L);
90-
char **arglist = malloc(sizeof(char *) * (top+2));
91-
int i;
9287

93-
for(i = 1; i <= top; i++)
94-
arglist[i] = strdup(lua_tostring(L, i));
95-
96-
arglist[0] = strdup(main_argv0);
97-
arglist[top+1] = NULL;
98-
99-
SDL_Quit();
100-
#ifdef WIN32
101-
#if 1
102-
//if(main_oldcwd != NULL)
103-
// _chdir(main_oldcwd);
104-
105-
char cwd[2048] = "";
106-
GetModuleFileName(NULL, cwd, 2047);
107-
char *v = cwd + strlen(cwd) - 1;
108-
while (v >= cwd)
109-
{
110-
if (*v == '\\')
111-
{
112-
v++;
113-
break;
88+
if (lua_args) {
89+
for (int i = 0; i < lua_args_len; ++i) {
90+
free(lua_args[i]);
11491
}
115-
v--;
92+
93+
free(lua_args);
94+
lua_args = NULL;
11695
}
117-
arglist[0] = v;
118-
119-
for (i = 0; i <= top; i++)
120-
{
121-
int new_size = strlen(arglist[i]) + 3;
122-
int j;
123-
for (j = 0; j < strlen(arglist[i]); j++)
124-
{
125-
if (arglist[i][j] == '"' || arglist[i][j] == '\\')
126-
new_size++;
127-
}
128-
char *new_arg = malloc(new_size);
129-
char *k = new_arg;
130-
char *l = arglist[i];
131-
*(k++) = '"';
132-
while (*l != 0)
133-
{
134-
if (*l == '"' || *l == '\\')
135-
*(k++) = '\\';
136-
*(k++) = *(l++);
96+
97+
if (top > 2) {
98+
lua_args = malloc(sizeof(*lua_args) * (top - 3));
99+
100+
for(int i = 3; i <= top; i++) {
101+
lua_args[i - 3] = strdup(lua_tostring(L, i));
137102
}
138-
*(k++) = '"';
139-
*(k++) = 0;
140-
arglist[i] = new_arg;
141103
}
142-
/*FILE *fp = fopen("FUCK.txt", "w");
143-
for (i = 0; i <= top; i++)
144-
{
145-
fprintf(fp, "%s ", arglist[i]);
146-
fflush(fp);
147-
}*/
148-
#else
149-
char *v = strdup("iceball.exe");
150-
arglist[0] = v;
151-
#endif
152-
#endif
153-
printf("argv0: [%s]\n", main_argv0);
154-
fflush(stdout);
104+
}
105+
106+
int icelua_fn_client_join_server(lua_State *L)
107+
{
108+
if (!(boot_mode & IB_LAUNCHER))
109+
return luaL_error(L, "join_server called when not in -l mode");
110+
111+
update_lua_args(L);
155112

156-
execv(main_argv0, arglist);
113+
const char *address = lua_tostring(L, 1);
114+
int port = lua_tonumber(L, 2);
157115

158-
printf("WORK YOU FUCKASS: %s\n", strerror(errno));
159-
fflush(stdout);
116+
ib_join_server(address, port);
117+
118+
return 0;
119+
}
120+
121+
int icelua_fn_client_create_server(lua_State *L)
122+
{
123+
if (!(boot_mode & IB_LAUNCHER))
124+
return luaL_error(L, "create_server called when not in -l mode");
125+
126+
update_lua_args(L);
127+
128+
int port = lua_tonumber(L, 1);
129+
const char *pkg = lua_tostring(L, 2);
130+
131+
ib_create_server(port, pkg);
132+
133+
return 0;
134+
}
135+
136+
int icelua_fn_client_create_launcher(lua_State *L)
137+
{
138+
if (!(boot_mode & IB_LAUNCHER))
139+
return luaL_error(L, "create_launcher called when not in -l mode");
140+
141+
int top = lua_gettop(L);
142+
143+
const char *pkg = lua_tostring(L, -1);
144+
145+
ib_create_launcher(pkg);
160146

161-
// DOES NOT RETURN.
162-
fprintf(stderr, "ABORT: sys_execv must not return!\n");
163-
fflush(stderr);
164-
abort();
165147
return 0;
166148
}
167149

@@ -208,8 +190,10 @@ int icelua_fn_client_mk_set_title(lua_State *L)
208190

209191
#ifndef DEDI
210192
struct icelua_entry icelua_client[] = {
193+
{icelua_fn_client_create_launcher, "create_launcher"},
194+
{icelua_fn_client_create_server, "create_server"},
195+
{icelua_fn_client_join_server, "join_server"},
211196
{icelua_fn_client_mk_set_title, "mk_set_title"},
212-
{icelua_fn_client_mk_sys_execv, "mk_sys_execv"},
213197

214198
{icelua_fn_client_text_input_start, "text_input_start"},
215199
{icelua_fn_client_text_input_stop, "text_input_stop"},
@@ -402,11 +386,7 @@ void icelua_loadbasefuncs(lua_State *L)
402386

403387
int icelua_initfetch(void)
404388
{
405-
int i;
406389
char xpath[128+1];
407-
int argct = (main_largstart == -1 || (main_largstart >= main_argc)
408-
? 0
409-
: main_argc - main_largstart);
410390

411391
if(to_client_local.sockfd == -1)
412392
to_client_local.sockfd = SOCKFD_LOCAL;
@@ -439,8 +419,10 @@ int icelua_initfetch(void)
439419
}
440420

441421
printf("Client loaded! Initialising...\n");
442-
for(i = 0; i < argct; i++)
443-
lua_pushstring(lstate_client, main_argv[i+main_largstart]);
422+
for(int i = 0; i < lua_args_len; i++)
423+
lua_pushstring(lstate_client, lua_args[i]);
424+
425+
int argct = lua_args_len;
444426
if((boot_mode & IB_CLIENT) && net_path[1] != '\x00')
445427
{
446428
lua_pushstring(lstate_client, net_path);
@@ -506,8 +488,6 @@ void icelua_pushversion(lua_State *L, const char *tabname)
506488

507489
int icelua_init(void)
508490
{
509-
int i, argct;
510-
511491
// create states
512492
if(boot_mode & IB_CLIENT)
513493
{
@@ -681,7 +661,7 @@ int icelua_init(void)
681661
raw_whitelist = malloc(sizeof(struct icelua_whitelist)*raw_whitelist_len);
682662

683663
// read each entry
684-
for(i = 0; i < raw_whitelist_len; i++)
664+
for(int i = 0; i < raw_whitelist_len; i++)
685665
{
686666
printf("entry %i/%i\n", i+1, raw_whitelist_len);
687667
// get entry
@@ -818,15 +798,12 @@ int icelua_init(void)
818798
return 1;
819799
}
820800

821-
argct = (main_largstart == -1 || (main_largstart >= main_argc)
822-
? 0
823-
: main_argc - main_largstart);
824-
825801
if(lstate_server != NULL)
826802
{
827-
for(i = 0; i < argct; i++)
828-
lua_pushstring(lstate_server, main_argv[i+main_largstart]);
829-
if(lua_pcall(lstate_server, argct, 0, 0) != 0)
803+
for(int i = 0; i < lua_args_len; i++)
804+
lua_pushstring(lstate_server, lua_args[i]);
805+
806+
if(lua_pcall(lstate_server, lua_args_len, 0, 0) != 0)
830807
{
831808
printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
832809
lua_pop(lstate_server, 1);
@@ -870,6 +847,11 @@ int icelua_init(void)
870847

871848
void icelua_deinit(void)
872849
{
850+
if (lstate_client)
851+
lua_close(lstate_client);
852+
853+
if (lstate_server)
854+
lua_close(lstate_server);
873855
// TODO!
874856
}
875857

0 commit comments

Comments
 (0)