Skip to content

Commit 58ee777

Browse files
committed
Add support for passing arguments to the new init functions
1 parent c48e30c commit 58ee777

File tree

5 files changed

+84
-145
lines changed

5 files changed

+84
-145
lines changed

include/common.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,8 @@ extern int restart_boot_mode;
608608
extern int boot_mode;
609609
extern int quitflag;
610610

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

617614
int run_game_cont1(void);
618615
int run_game_cont2(void);
@@ -621,7 +618,7 @@ int error_sdl(char *msg);
621618
int error_perror(char *msg);
622619

623620
#ifndef DEDI
624-
void ib_create_launcher(int port, const char *pkg);
621+
void ib_create_launcher(const char *pkg);
625622
void ib_create_server(int port, const char *pkg);
626623
void ib_join_server(const char *address, int port);
627624
#endif

pkg/iceball/config/main_client.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ 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.create_launcher(0, "pkg/iceball/launch")
321+
client.create_launcher("pkg/iceball/launch")
322322
-- client.mk_sys_execv()
323323
elseif field[4] == "action_exit" then
324-
client.create_launcher(0, "pkg/iceball/launch")
324+
client.create_launcher("pkg/iceball/launch")
325325
-- client.mk_sys_execv()
326326
end
327327
elseif key == SDLK_UP then

pkg/iceball/launch/main_client.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ 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.join_server(sv.address, sv.port)
87+
client.join_server(sv.address, sv.port, arg_closure(argv))
8888
-- client.mk_sys_execv("-c", sv.address, sv.port, arg_closure(argv))
8989
end
9090
end
@@ -93,10 +93,10 @@ end
9393
function client.hook_key(key, state, modif, uni)
9494
if not state then
9595
if key == SDLK_l then
96-
client.create_server(20737, "pkg/base")
96+
client.create_server(20737, "pkg/base", arg_closure(argv))
9797
-- client.mk_sys_execv("-s", "20737", "pkg/base", arg_closure(argv))
9898
elseif key == SDLK_c then
99-
client.create_launcher(0, "pkg/iceball/config")
99+
client.create_launcher("pkg/iceball/config")
100100
-- client.mk_sys_execv("-l", "pkg/iceball/config")
101101
elseif key == SDLK_ESCAPE then
102102
client.hook_tick = nil

src/lua.c

Lines changed: 41 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,56 @@ int icelua_fn_common_mk_compat_disable(lua_State *L)
8181
return 0;
8282
}
8383
#ifndef DEDI
84+
static void update_lua_args(lua_State *L)
85+
{
86+
int top = lua_gettop(L);
87+
88+
if (lua_args) {
89+
for (int i = 0; i < lua_args_len; ++i) {
90+
free(lua_args[i]);
91+
}
92+
93+
free(lua_args);
94+
lua_args = NULL;
95+
}
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));
102+
}
103+
}
104+
}
105+
84106
int icelua_fn_client_join_server(lua_State *L)
85107
{
86108
if (!(boot_mode & IB_LAUNCHER))
87109
return luaL_error(L, "join_server called when not in -l mode");
88110

89-
int top = lua_gettop(L);
111+
update_lua_args(L);
90112

91-
const char *address = lua_tostring(L, -2);
92-
int port = lua_tonumber(L, -1);
113+
const char *address = lua_tostring(L, 1);
114+
int port = lua_tonumber(L, 2);
93115

94116
ib_join_server(address, port);
95117

96-
return 2;
118+
return 0;
97119
}
98120

99121
int icelua_fn_client_create_server(lua_State *L)
100122
{
101123
if (!(boot_mode & IB_LAUNCHER))
102124
return luaL_error(L, "create_server called when not in -l mode");
103125

104-
int top = lua_gettop(L);
126+
update_lua_args(L);
105127

106-
int port = lua_tonumber(L, -2);
107-
const char *pkg = lua_tostring(L, -1);
128+
int port = lua_tonumber(L, 1);
129+
const char *pkg = lua_tostring(L, 2);
108130

109131
ib_create_server(port, pkg);
110132

111-
return 2;
133+
return 0;
112134
}
113135

114136
int icelua_fn_client_create_launcher(lua_State *L)
@@ -118,95 +140,10 @@ int icelua_fn_client_create_launcher(lua_State *L)
118140

119141
int top = lua_gettop(L);
120142

121-
int port = lua_tonumber(L, -2);
122143
const char *pkg = lua_tostring(L, -1);
123144

124-
ib_create_launcher(port, pkg);
145+
ib_create_launcher(pkg);
125146

126-
return 2;
127-
}
128-
129-
int icelua_fn_client_mk_sys_execv(lua_State *L)
130-
{
131-
if(!(boot_mode & IB_LAUNCHER))
132-
return luaL_error(L, "mk_sys_execv called when not in -l mode");
133-
134-
int top = lua_gettop(L);
135-
char **arglist = malloc(sizeof(char *) * (top+2));
136-
int i;
137-
138-
for(i = 1; i <= top; i++)
139-
arglist[i] = strdup(lua_tostring(L, i));
140-
141-
arglist[0] = strdup(main_argv0);
142-
arglist[top+1] = NULL;
143-
144-
SDL_Quit();
145-
#ifdef WIN32
146-
#if 1
147-
//if(main_oldcwd != NULL)
148-
// _chdir(main_oldcwd);
149-
150-
char cwd[2048] = "";
151-
GetModuleFileName(NULL, cwd, 2047);
152-
char *v = cwd + strlen(cwd) - 1;
153-
while (v >= cwd)
154-
{
155-
if (*v == '\\')
156-
{
157-
v++;
158-
break;
159-
}
160-
v--;
161-
}
162-
arglist[0] = v;
163-
164-
for (i = 0; i <= top; i++)
165-
{
166-
int new_size = strlen(arglist[i]) + 3;
167-
int j;
168-
for (j = 0; j < strlen(arglist[i]); j++)
169-
{
170-
if (arglist[i][j] == '"' || arglist[i][j] == '\\')
171-
new_size++;
172-
}
173-
char *new_arg = malloc(new_size);
174-
char *k = new_arg;
175-
char *l = arglist[i];
176-
*(k++) = '"';
177-
while (*l != 0)
178-
{
179-
if (*l == '"' || *l == '\\')
180-
*(k++) = '\\';
181-
*(k++) = *(l++);
182-
}
183-
*(k++) = '"';
184-
*(k++) = 0;
185-
arglist[i] = new_arg;
186-
}
187-
/*FILE *fp = fopen("FUCK.txt", "w");
188-
for (i = 0; i <= top; i++)
189-
{
190-
fprintf(fp, "%s ", arglist[i]);
191-
fflush(fp);
192-
}*/
193-
#else
194-
char *v = strdup("iceball.exe");
195-
arglist[0] = v;
196-
#endif
197-
#endif
198-
printf("argv0: [%s]\n", main_argv0);
199-
fflush(stdout);
200-
201-
execv(main_argv0, arglist);
202-
203-
printf("WORK YOU FUCKASS: %s\n", strerror(errno));
204-
fflush(stdout);
205-
206-
// DOES NOT RETURN.
207-
fprintf(stderr, "ABORT: sys_execv must not return!\n");
208-
fflush(stderr);
209-
abort();
210147
return 0;
211148
}
212149

@@ -257,7 +194,6 @@ struct icelua_entry icelua_client[] = {
257194
{icelua_fn_client_create_server, "create_server"},
258195
{icelua_fn_client_join_server, "join_server"},
259196
{icelua_fn_client_mk_set_title, "mk_set_title"},
260-
{icelua_fn_client_mk_sys_execv, "mk_sys_execv"},
261197

262198
{icelua_fn_client_text_input_start, "text_input_start"},
263199
{icelua_fn_client_text_input_stop, "text_input_stop"},
@@ -450,11 +386,7 @@ void icelua_loadbasefuncs(lua_State *L)
450386

451387
int icelua_initfetch(void)
452388
{
453-
int i;
454389
char xpath[128+1];
455-
int argct = (main_largstart == -1 || (main_largstart >= main_argc)
456-
? 0
457-
: main_argc - main_largstart);
458390

459391
if(to_client_local.sockfd == -1)
460392
to_client_local.sockfd = SOCKFD_LOCAL;
@@ -487,9 +419,11 @@ int icelua_initfetch(void)
487419
}
488420

489421
printf("Client loaded! Initialising...\n");
490-
for(i = 0; i < argct; i++)
491-
lua_pushstring(lstate_client, main_argv[i+main_largstart]);
492-
if((boot_mode & IB_CLIENT) && (net_path && net_path[1] != '\x00'))
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;
426+
if((boot_mode & IB_CLIENT) && net_path[1] != '\x00')
493427
{
494428
lua_pushstring(lstate_client, net_path);
495429
argct++;
@@ -554,8 +488,6 @@ void icelua_pushversion(lua_State *L, const char *tabname)
554488

555489
int icelua_init(void)
556490
{
557-
int i, argct;
558-
559491
// create states
560492
if(boot_mode & IB_CLIENT)
561493
{
@@ -729,7 +661,7 @@ int icelua_init(void)
729661
raw_whitelist = malloc(sizeof(struct icelua_whitelist)*raw_whitelist_len);
730662

731663
// read each entry
732-
for(i = 0; i < raw_whitelist_len; i++)
664+
for(int i = 0; i < raw_whitelist_len; i++)
733665
{
734666
printf("entry %i/%i\n", i+1, raw_whitelist_len);
735667
// get entry
@@ -866,15 +798,12 @@ int icelua_init(void)
866798
return 1;
867799
}
868800

869-
argct = (main_largstart == -1 || (main_largstart >= main_argc)
870-
? 0
871-
: main_argc - main_largstart);
872-
873801
if(lstate_server != NULL)
874802
{
875-
for(i = 0; i < argct; i++)
876-
lua_pushstring(lstate_server, main_argv[i+main_largstart]);
877-
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)
878807
{
879808
printf("ERROR running server Lua: %s\n", lua_tostring(lstate_server, -1));
880809
lua_pop(lstate_server, 1);

0 commit comments

Comments
 (0)