@@ -81,87 +81,69 @@ int icelua_fn_common_mk_compat_disable(lua_State *L)
81
81
return 0 ;
82
82
}
83
83
#ifndef DEDI
84
- int icelua_fn_client_mk_sys_execv (lua_State * L )
84
+ static void update_lua_args (lua_State * L )
85
85
{
86
- if (!(boot_mode & IB_LAUNCHER ))
87
- return luaL_error (L , "mk_sys_execv called when not in -l mode" );
88
-
89
86
int top = lua_gettop (L );
90
- char * * arglist = malloc (sizeof (char * ) * (top + 2 ));
91
- int i ;
92
87
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 ]);
114
91
}
115
- v -- ;
92
+
93
+ free (lua_args );
94
+ lua_args = NULL ;
116
95
}
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 ));
137
102
}
138
- * (k ++ ) = '"' ;
139
- * (k ++ ) = 0 ;
140
- arglist [i ] = new_arg ;
141
103
}
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 );
155
112
156
- execv (main_argv0 , arglist );
113
+ const char * address = lua_tostring (L , 1 );
114
+ int port = lua_tonumber (L , 2 );
157
115
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 );
160
146
161
- // DOES NOT RETURN.
162
- fprintf (stderr , "ABORT: sys_execv must not return!\n" );
163
- fflush (stderr );
164
- abort ();
165
147
return 0 ;
166
148
}
167
149
@@ -208,8 +190,10 @@ int icelua_fn_client_mk_set_title(lua_State *L)
208
190
209
191
#ifndef DEDI
210
192
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" },
211
196
{icelua_fn_client_mk_set_title , "mk_set_title" },
212
- {icelua_fn_client_mk_sys_execv , "mk_sys_execv" },
213
197
214
198
{icelua_fn_client_text_input_start , "text_input_start" },
215
199
{icelua_fn_client_text_input_stop , "text_input_stop" },
@@ -402,11 +386,7 @@ void icelua_loadbasefuncs(lua_State *L)
402
386
403
387
int icelua_initfetch (void )
404
388
{
405
- int i ;
406
389
char xpath [128 + 1 ];
407
- int argct = (main_largstart == -1 || (main_largstart >= main_argc )
408
- ? 0
409
- : main_argc - main_largstart );
410
390
411
391
if (to_client_local .sockfd == -1 )
412
392
to_client_local .sockfd = SOCKFD_LOCAL ;
@@ -439,8 +419,10 @@ int icelua_initfetch(void)
439
419
}
440
420
441
421
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 ;
444
426
if ((boot_mode & IB_CLIENT ) && net_path [1 ] != '\x00' )
445
427
{
446
428
lua_pushstring (lstate_client , net_path );
@@ -506,8 +488,6 @@ void icelua_pushversion(lua_State *L, const char *tabname)
506
488
507
489
int icelua_init (void )
508
490
{
509
- int i , argct ;
510
-
511
491
// create states
512
492
if (boot_mode & IB_CLIENT )
513
493
{
@@ -681,7 +661,7 @@ int icelua_init(void)
681
661
raw_whitelist = malloc (sizeof (struct icelua_whitelist )* raw_whitelist_len );
682
662
683
663
// read each entry
684
- for (i = 0 ; i < raw_whitelist_len ; i ++ )
664
+ for (int i = 0 ; i < raw_whitelist_len ; i ++ )
685
665
{
686
666
printf ("entry %i/%i\n" , i + 1 , raw_whitelist_len );
687
667
// get entry
@@ -818,15 +798,12 @@ int icelua_init(void)
818
798
return 1 ;
819
799
}
820
800
821
- argct = (main_largstart == -1 || (main_largstart >= main_argc )
822
- ? 0
823
- : main_argc - main_largstart );
824
-
825
801
if (lstate_server != NULL )
826
802
{
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 )
830
807
{
831
808
printf ("ERROR running server Lua: %s\n" , lua_tostring (lstate_server , -1 ));
832
809
lua_pop (lstate_server , 1 );
@@ -870,6 +847,11 @@ int icelua_init(void)
870
847
871
848
void icelua_deinit (void )
872
849
{
850
+ if (lstate_client )
851
+ lua_close (lstate_client );
852
+
853
+ if (lstate_server )
854
+ lua_close (lstate_server );
873
855
// TODO!
874
856
}
875
857
0 commit comments