forked from M-itch/libcod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsc_exec.cpp
416 lines (343 loc) · 8.07 KB
/
gsc_exec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "gsc_exec.hpp"
#if COMPILE_EXEC == 1
#include <pthread.h>
enum
{
INT_VALUE,
FLOAT_VALUE,
STRING_VALUE,
VECTOR_VALUE,
OBJECT_VALUE
};
struct exec_outputline
{
char content[COD2_MAX_STRINGLENGTH];
exec_outputline *next;
};
struct exec_async_task
{
exec_async_task *prev;
exec_async_task *next;
char command[COD2_MAX_STRINGLENGTH];
int callback;
bool done;
bool save;
bool error;
exec_outputline *output;
unsigned int levelId;
bool hasargument;
int valueType;
int intValue;
float floatValue;
char stringValue[COD2_MAX_STRINGLENGTH];
vec3_t vectorValue;
unsigned int objectValue;
};
exec_async_task *first_exec_async_task = NULL;
void gsc_exec()
{
char *command;
if (!stackGetParamString(0, &command))
{
stackError("gsc_exec() argument is undefined or has wrong type");
stackPushUndefined();
return;
}
Com_DPrintf("gsc_exec() executing: %s\n", command);
FILE *fp;
fp = popen(command, "r");
if (fp == NULL)
{
stackPushUndefined();
return;
}
char c;
int curpos = 0;
char content[COD2_MAX_STRINGLENGTH];
stackPushArray();
while ((c = getc(fp)) != EOF)
{
if (c == '\n' || curpos == COD2_MAX_STRINGLENGTH - 1)
{
content[curpos] = '\0';
stackPushString(content);
stackPushArrayLast();
curpos = 0;
}
else
{
content[curpos] = c;
curpos++;
}
}
content[curpos] = '\0';
stackPushString(content);
stackPushArrayLast();
pclose(fp);
}
void *exec_async(void *input_c)
{
exec_async_task *task = (exec_async_task*)input_c;
FILE *fp;
fp = popen(task->command, "r");
if (fp == NULL)
{
task->error = true;
return NULL;
}
if (task->save)
{
exec_outputline *output = new exec_outputline;
task->output = output;
output->next = NULL;
char c;
int curpos = 0;
while ((c = getc(fp)) != EOF)
{
if (c == '\n' || curpos == COD2_MAX_STRINGLENGTH - 1)
{
output->content[curpos] = '\0';
output->next = new exec_outputline;
output = output->next;
output->next = NULL;
curpos = 0;
}
else
{
output->content[curpos] = c;
curpos++;
}
}
output->content[curpos] = '\0';
}
else
while(getc(fp) != EOF); //make thread wait for function to finish
pclose(fp);
task->done = true;
return NULL;
}
void gsc_exec_async_create()
{
char *command;
int callback;
if (!stackGetParamString(0, &command))
{
stackError("gsc_exec_async_create() argument is undefined or has wrong type");
stackPushUndefined();
return;
}
Com_DPrintf("gsc_exec_async_create() executing: %s\n", command);
exec_async_task *current = first_exec_async_task;
while (current != NULL && current->next != NULL)
current = current->next;
exec_async_task *newtask = new exec_async_task;
strncpy(newtask->command, command, COD2_MAX_STRINGLENGTH - 1);
newtask->command[COD2_MAX_STRINGLENGTH - 1] = '\0';
newtask->output = NULL;
newtask->prev = current;
newtask->next = NULL;
if (!stackGetParamFunction(1, &callback))
newtask->callback = 0;
else
newtask->callback = callback;
newtask->done = false;
newtask->save = true;
newtask->error = false;
newtask->levelId = scrVarPub.levelId;
newtask->hasargument = true;
int valueInt;
float valueFloat;
char *valueString;
vec3_t valueVector;
unsigned int valueObject;
if (stackGetParamInt(2, &valueInt))
{
newtask->valueType = INT_VALUE;
newtask->intValue = valueInt;
}
else if (stackGetParamFloat(2, &valueFloat))
{
newtask->valueType = FLOAT_VALUE;
newtask->floatValue = valueFloat;
}
else if (stackGetParamString(2, &valueString))
{
newtask->valueType = STRING_VALUE;
strcpy(newtask->stringValue, valueString);
}
else if (stackGetParamVector(2, valueVector))
{
newtask->valueType = VECTOR_VALUE;
newtask->vectorValue[0] = valueVector[0];
newtask->vectorValue[1] = valueVector[1];
newtask->vectorValue[2] = valueVector[2];
}
else if (stackGetParamObject(2, &valueObject))
{
newtask->valueType = OBJECT_VALUE;
newtask->objectValue = valueObject;
}
else
newtask->hasargument = false;
if (current != NULL)
current->next = newtask;
else
first_exec_async_task = newtask;
pthread_t exec_doer;
if (pthread_create(&exec_doer, NULL, exec_async, newtask) != 0)
{
stackError("gsc_exec_async_create() error creating exec async handler thread!");
stackPushUndefined();
return;
}
if (pthread_detach(exec_doer) != 0)
{
stackError("gsc_exec_async_create() error detaching exec async handler thread!");
stackPushUndefined();
return;
}
stackPushInt(1);
}
void gsc_exec_async_create_nosave()
{
char *command;
int callback;
if (!stackGetParamString(0, &command))
{
stackError("gsc_exec_async_create_nosave() argument is undefined or has wrong type");
stackPushUndefined();
return;
}
Com_DPrintf("gsc_exec_async_create_nosave() executing: %s\n", command);
exec_async_task *current = first_exec_async_task;
while (current != NULL && current->next != NULL)
current = current->next;
exec_async_task *newtask = new exec_async_task;
strncpy(newtask->command, command, COD2_MAX_STRINGLENGTH - 1);
newtask->command[COD2_MAX_STRINGLENGTH - 1] = '\0';
newtask->output = NULL;
newtask->prev = current;
newtask->next = NULL;
if (!stackGetParamFunction(1, &callback))
newtask->callback = 0;
else
newtask->callback = callback;
newtask->done = false;
newtask->save = false;
newtask->error = false;
newtask->levelId = scrVarPub.levelId;
newtask->hasargument = true;
int valueInt;
float valueFloat;
char *valueString;
vec3_t valueVector;
unsigned int valueObject;
if (stackGetParamInt(2, &valueInt))
{
newtask->valueType = INT_VALUE;
newtask->intValue = valueInt;
}
else if (stackGetParamFloat(2, &valueFloat))
{
newtask->valueType = FLOAT_VALUE;
newtask->floatValue = valueFloat;
}
else if (stackGetParamString(2, &valueString))
{
newtask->valueType = STRING_VALUE;
strcpy(newtask->stringValue, valueString);
}
else if (stackGetParamVector(2, valueVector))
{
newtask->valueType = VECTOR_VALUE;
newtask->vectorValue[0] = valueVector[0];
newtask->vectorValue[1] = valueVector[1];
newtask->vectorValue[2] = valueVector[2];
}
else if (stackGetParamObject(2, &valueObject))
{
newtask->valueType = OBJECT_VALUE;
newtask->objectValue = valueObject;
}
else
newtask->hasargument = false;
if (current != NULL)
current->next = newtask;
else
first_exec_async_task = newtask;
pthread_t exec_doer;
if (pthread_create(&exec_doer, NULL, exec_async, newtask) != 0)
{
stackError("gsc_exec_async_create_nosave() error creating exec async handler thread!");
stackPushUndefined();
return;
}
if (pthread_detach(exec_doer) != 0)
{
stackError("gsc_exec_async_create_nosave() error detaching exec async handler thread!");
stackPushUndefined();
return;
}
stackPushInt(1);
}
void gsc_exec_async_checkdone()
{
exec_async_task *current = first_exec_async_task;
while (current != NULL)
{
exec_async_task *task = current;
current = current->next;
if (task->done)
{
//push to cod
if (Scr_IsSystemActive() && task->save && task->callback && !task->error && (scrVarPub.levelId == task->levelId))
{
if (task->hasargument)
{
switch(task->valueType)
{
case INT_VALUE:
stackPushInt(task->intValue);
break;
case FLOAT_VALUE:
stackPushFloat(task->floatValue);
break;
case STRING_VALUE:
stackPushString(task->stringValue);
break;
case VECTOR_VALUE:
stackPushVector(task->vectorValue);
break;
case OBJECT_VALUE:
stackPushObject(task->objectValue);
break;
default:
stackPushUndefined();
break;
}
}
stackPushArray();
exec_outputline *output = task->output;
while (output != NULL)
{
exec_outputline *next = output->next;
stackPushString(output->content);
stackPushArrayLast();
delete output;
output = next;
}
short ret = Scr_ExecThread(task->callback, task->save + task->hasargument);
Scr_FreeThread(ret);
}
//free task
if (task->next != NULL)
task->next->prev = task->prev;
if (task->prev != NULL)
task->prev->next = task->next;
else
first_exec_async_task = task->next;
delete task;
}
}
}
#endif