author | truelight |
Tue, 24 Aug 2004 20:54:40 +0000 | |
changeset 133 | 321532e90bc8 |
parent 129 | df1a60bc0d70 |
child 135 | 638fb31434eb |
permissions | -rw-r--r-- |
126 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "window.h" |
|
4 |
#include "gui.h" |
|
5 |
#include "gfx.h" |
|
6 |
#include "player.h" |
|
7 |
#include "variables.h" |
|
8 |
#include "hal.h" |
|
9 |
#include <stdarg.h> |
|
10 |
#include "console.h" |
|
11 |
||
12 |
||
13 |
// ** main console ** // |
|
14 |
static bool _iconsole_inited; |
|
15 |
static byte* _iconsole_buffer[80]; |
|
16 |
static byte _iconsole_cbuffer[80]; |
|
17 |
static byte _iconsole_cmdline[255]; |
|
18 |
static byte _iconsole_cmdpos; |
|
19 |
static byte _iconsole_mode; |
|
20 |
static byte _iconsole_color_default = 1; |
|
21 |
static byte _iconsole_color_error = 3; |
|
22 |
static byte _iconsole_color_debug = 5; |
|
23 |
static byte _iconsole_color_commands = 2; |
|
24 |
static Window *_iconsole_win = NULL; |
|
25 |
||
26 |
// ** console cursor ** // |
|
27 |
static bool _icursor_state; |
|
28 |
static byte _icursor_rate; |
|
29 |
static byte _icursor_counter; |
|
30 |
||
31 |
// ** console window ** // |
|
32 |
||
33 |
static void IConsoleWndProc(Window *w, WindowEvent *e); |
|
34 |
static const Widget _iconsole_window_widgets[] = {{WWT_LAST}}; |
|
35 |
static const WindowDesc _iconsole_window_desc = { |
|
36 |
0, 0, 2, 2, |
|
37 |
WC_CONSOLE,0, |
|
38 |
WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, |
|
39 |
_iconsole_window_widgets, |
|
40 |
IConsoleWndProc, |
|
41 |
}; |
|
42 |
||
43 |
// ** console parser ** // |
|
44 |
||
45 |
static _iconsole_cmd * _iconsole_cmds; // list of registred commands |
|
46 |
static _iconsole_var * _iconsole_vars; // list of registred vars |
|
47 |
||
48 |
// ** console std lib ** // |
|
49 |
static byte _stdlib_developer=0; |
|
50 |
static void IConsoleStdLibRegister(); |
|
51 |
static byte * _stdlib_temp_string; |
|
52 |
static byte * _stdlib_temp_pointer; |
|
53 |
static uint32 _stdlib_temp_uint32; |
|
54 |
static bool _stdlib_temp_bool; |
|
55 |
||
56 |
/* *************** */ |
|
57 |
/* end of header */ |
|
58 |
/* *************** */ |
|
59 |
||
60 |
void IConsoleClearCommand() |
|
61 |
{ |
|
62 |
int i; |
|
63 |
for (i=0; i<255; i++) _iconsole_cmdline[i]=0; |
|
64 |
_iconsole_cmdpos=0; |
|
65 |
SetWindowDirty(_iconsole_win); |
|
66 |
} |
|
67 |
||
68 |
static void IConsoleWndProc(Window *w, WindowEvent *e) |
|
69 |
{ |
|
70 |
switch(e->event) { |
|
71 |
||
72 |
case WE_PAINT: |
|
73 |
GfxFillRect(w->left,w->top,w->width,w->height-1,0); |
|
74 |
{ |
|
75 |
int i=79; |
|
76 |
int max=(w->height/12)-1; |
|
77 |
while ((i>79-max) && (_iconsole_buffer[i]!=NULL)) { |
|
78 |
DoDrawString(_iconsole_buffer[i],5,w->height-((81-i)*12),_iconsole_cbuffer[i]); |
|
79 |
i--; |
|
80 |
} |
|
81 |
DoDrawString((char *)&_iconsole_cmdline,5,w->height-12,_iconsole_color_commands); |
|
82 |
} |
|
83 |
break; |
|
84 |
||
85 |
case WE_TICK: |
|
86 |
||
87 |
if (_iconsole_mode==ICONSOLE_OPENING) { |
|
88 |
_iconsole_mode=ICONSOLE_OPENED; |
|
89 |
} |
|
90 |
||
91 |
_icursor_counter++; |
|
92 |
if (_icursor_counter>_icursor_rate) { |
|
93 |
_icursor_state=!_icursor_state; |
|
94 |
{ |
|
95 |
int posx; |
|
96 |
int posy; |
|
97 |
int color; |
|
98 |
_cur_dpi=&_screen; |
|
99 |
if (_icursor_state) color=14; else color=0; |
|
100 |
posx=5+GetStringWidth((char *)&_iconsole_cmdline); |
|
101 |
posy=w->height-3; |
|
102 |
GfxFillRect(posx,posy,posx+5,posy+1,color); |
|
103 |
_video_driver->make_dirty(posx,posy,5,1); |
|
104 |
} |
|
105 |
_icursor_counter=0; |
|
106 |
} |
|
107 |
break; |
|
108 |
||
109 |
case WE_KEYPRESS: |
|
110 |
e->keypress.cont=false; |
|
129
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
126
diff
changeset
|
111 |
if (e->keypress.keycode == WKC_BACKQUOTE) |
126 | 112 |
{ |
113 |
IConsoleSwitch(); |
|
114 |
} else |
|
115 |
if (e->keypress.keycode == WKC_RETURN) |
|
116 |
{ |
|
117 |
IConsolePrintF(_iconsole_color_commands, "] %s", _iconsole_cmdline); |
|
118 |
IConsoleCmdExec((byte *) _iconsole_cmdline); |
|
119 |
IConsoleClearCommand(); |
|
120 |
} else |
|
121 |
if (e->keypress.keycode == WKC_BACKSPACE) |
|
122 |
{ |
|
123 |
if (_iconsole_cmdpos!=0) _iconsole_cmdpos--; |
|
124 |
_iconsole_cmdline[_iconsole_cmdpos]=0; |
|
125 |
SetWindowDirty(w); |
|
126 |
} else |
|
127 |
if (IS_INT_INSIDE((e->keypress.ascii), 32, 256)) |
|
128 |
{ |
|
129 |
_iconsole_cmdline[_iconsole_cmdpos]=e->keypress.ascii; |
|
130 |
if (_iconsole_cmdpos!=255) _iconsole_cmdpos++; |
|
131 |
SetWindowDirty(w); |
|
132 |
e->keypress.keycode=0; |
|
133 |
} else e->keypress.cont=true; |
|
134 |
break; |
|
135 |
||
136 |
} |
|
137 |
} |
|
138 |
||
139 |
void IConsoleInit() |
|
140 |
{ |
|
141 |
int i; |
|
142 |
_iconsole_inited=true; |
|
143 |
_iconsole_mode=ICONSOLE_CLOSED; |
|
144 |
_iconsole_win=NULL; |
|
145 |
_icursor_state=false; |
|
146 |
_icursor_rate=5; |
|
147 |
_icursor_counter=0; |
|
148 |
for (i=0;i<80;i++) _iconsole_buffer[i]=NULL; |
|
149 |
IConsoleStdLibRegister(); |
|
150 |
IConsolePrint(12,"OpenTTD Game Console Build #"); |
|
151 |
IConsolePrint(12,"---------------------------------"); |
|
152 |
IConsoleClearCommand(); |
|
153 |
} |
|
154 |
||
155 |
void IConsoleClear() |
|
156 |
{ |
|
157 |
int i; |
|
158 |
for (i=0;i<80;i++) if (_iconsole_buffer[i]!=NULL) { |
|
159 |
free(_iconsole_buffer[i]); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
void IConsoleFree() |
|
164 |
{ |
|
165 |
_iconsole_inited=false; |
|
166 |
IConsoleClear(); |
|
167 |
} |
|
168 |
||
169 |
void IConsoleResize() { |
|
170 |
if (_iconsole_win!=NULL) { |
|
171 |
_iconsole_win->height = _screen.height / 3; |
|
172 |
_iconsole_win->width= _screen.width; |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
void IConsoleSwitch() |
|
177 |
{ |
|
178 |
if (_iconsole_mode==ICONSOLE_CLOSED) { |
|
179 |
_iconsole_win = AllocateWindowDesc(&_iconsole_window_desc); |
|
180 |
_iconsole_win->height = _screen.height / 3; |
|
181 |
_iconsole_win->width= _screen.width; |
|
182 |
_iconsole_mode=ICONSOLE_OPENING; |
|
183 |
} else |
|
184 |
if (_iconsole_mode==ICONSOLE_OPENED) { |
|
185 |
DeleteWindow(_iconsole_win); |
|
186 |
_iconsole_win=NULL; |
|
187 |
_iconsole_mode=ICONSOLE_CLOSED; |
|
188 |
} |
|
189 |
MarkWholeScreenDirty(); |
|
190 |
MarkAllViewportsDirty(0,0,_screen.width,_screen.height); |
|
191 |
_video_driver->make_dirty(0,0,_screen.width,_screen.height); |
|
192 |
} |
|
193 |
||
194 |
void IConsoleClose() { |
|
195 |
if (_iconsole_mode==ICONSOLE_OPENED) IConsoleSwitch(); |
|
196 |
} |
|
197 |
||
198 |
void IConsoleOpen() { |
|
199 |
if (_iconsole_mode==ICONSOLE_CLOSED) IConsoleSwitch(); |
|
200 |
} |
|
201 |
||
202 |
void IConsolePrint(byte color_code, byte* string) |
|
203 |
{ |
|
204 |
byte * _ex; |
|
205 |
byte * _new; |
|
206 |
byte _exc; |
|
207 |
byte _newc; |
|
208 |
int i,j; |
|
209 |
||
210 |
if (!_iconsole_inited) return; |
|
211 |
||
212 |
_newc=color_code; |
|
213 |
||
214 |
i=strlen((char *)string); |
|
215 |
_new=malloc(i+1); |
|
216 |
memset(_new,0,i+1); |
|
217 |
memcpy(_new,string,i); |
|
218 |
||
219 |
for (j=0;j<i;j++) { |
|
220 |
if (_new[j]<0x1F) _new[j]=0x20; |
|
221 |
} |
|
222 |
||
223 |
i=79; |
|
224 |
while (i>=0) { |
|
225 |
_ex=_iconsole_buffer[i]; |
|
226 |
_exc=_iconsole_cbuffer[i]; |
|
227 |
_iconsole_buffer[i]=_new; |
|
228 |
_iconsole_cbuffer[i]=_newc; |
|
229 |
_new=_ex; |
|
230 |
_newc=_exc; |
|
231 |
i--; |
|
232 |
} |
|
233 |
if (_ex!=NULL) free(_ex); |
|
234 |
||
235 |
if (_iconsole_win!=NULL) SetWindowDirty(_iconsole_win); |
|
236 |
} |
|
237 |
||
238 |
||
239 |
void IConsolePrintF(byte color_code, const char *s, ...) |
|
240 |
{ |
|
241 |
va_list va; |
|
242 |
char buf[1024]; |
|
243 |
va_start(va, s); |
|
244 |
vsprintf(buf, s, va); |
|
245 |
va_end(va); |
|
246 |
IConsolePrint(color_code, (byte *) &buf); |
|
247 |
} |
|
248 |
||
249 |
void IConsoleDebug(byte* string) { |
|
250 |
if (_stdlib_developer>1) IConsolePrintF(_iconsole_color_debug, "DEBUG: %s", string); |
|
251 |
} |
|
252 |
||
253 |
void IConsoleError(byte* string) { |
|
254 |
if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_error, "ERROR: %s", string); |
|
255 |
} |
|
256 |
||
257 |
void IConsoleCmdRegister(byte * name, void * addr) { |
|
258 |
byte * _new; |
|
259 |
_iconsole_cmd * item; |
|
260 |
_iconsole_cmd * item_new; |
|
261 |
int i; |
|
262 |
||
263 |
i=strlen((char *)name); |
|
264 |
_new=malloc(i+1); |
|
265 |
memset(_new,0,i+1); |
|
266 |
memcpy(_new,name,i); |
|
267 |
||
268 |
item_new = malloc(sizeof(_iconsole_cmd)); |
|
269 |
||
270 |
item_new->_next = NULL; |
|
271 |
item_new->addr = addr; |
|
272 |
item_new->name = _new; |
|
273 |
||
274 |
item = _iconsole_cmds; |
|
275 |
if (item == NULL) { |
|
276 |
_iconsole_cmds = item_new; |
|
277 |
} else { |
|
278 |
while (item->_next != NULL) { item = item->_next; }; |
|
279 |
item->_next = item_new; |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
static void* IConsoleCmdGetAddr(byte * name) { |
|
284 |
_iconsole_cmd * item; |
|
285 |
||
286 |
item = _iconsole_cmds; |
|
287 |
while (item != NULL) { |
|
288 |
if (strcmp(item->name,name)==0) return item->addr; |
|
289 |
item = item->_next; |
|
290 |
} |
|
291 |
return NULL; |
|
292 |
} |
|
293 |
||
294 |
void IConsoleVarRegister(byte * name, void * addr, byte type) { |
|
295 |
byte * _new; |
|
296 |
_iconsole_var * item; |
|
297 |
_iconsole_var * item_new; |
|
298 |
int i; |
|
299 |
||
300 |
i=strlen((char *)name)+1; |
|
301 |
_new=malloc(i+1); |
|
302 |
memset(_new,0,i+1); |
|
303 |
_new[0]='*'; |
|
304 |
memcpy(_new+1,name,i); |
|
305 |
||
306 |
item_new = malloc(sizeof(_iconsole_var)); |
|
307 |
||
308 |
item_new->_next = NULL; |
|
309 |
item_new->addr = addr; |
|
310 |
item_new->name = _new; |
|
311 |
item_new->type = type; |
|
312 |
||
313 |
item = _iconsole_vars; |
|
314 |
if (item == NULL) { |
|
315 |
_iconsole_vars = item_new; |
|
316 |
} else { |
|
317 |
while (item->_next != NULL) { item = item->_next; }; |
|
318 |
item->_next = item_new; |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
_iconsole_var * IConsoleVarGet(byte * name) { |
|
323 |
_iconsole_var * item; |
|
324 |
||
325 |
item = _iconsole_vars; |
|
326 |
while (item != NULL) { |
|
327 |
if (strcmp(item->name,name)==0) return item; |
|
328 |
item = item->_next; |
|
329 |
} |
|
330 |
return NULL; |
|
331 |
} |
|
332 |
||
333 |
static void IConsoleVarStringSet(_iconsole_var * var, byte * string) { |
|
334 |
int l; |
|
335 |
||
336 |
if (strlen((byte *) var->addr)!=0) { |
|
337 |
free(var->addr); |
|
338 |
} |
|
339 |
l=strlen((char *) string); |
|
340 |
var->addr=malloc(l+1); |
|
341 |
memset(var->addr,0,l); |
|
342 |
memcpy((void *) var->addr,(void *) string, l); |
|
343 |
((byte *)var->addr)[l]=0; |
|
344 |
} |
|
345 |
||
346 |
void IConsoleCmdExec(byte * cmdstr) { |
|
347 |
void (*function)(byte argc, byte* argv[], byte argt[]); |
|
348 |
byte * tokens[20]; |
|
349 |
byte tokentypes[20]; |
|
350 |
byte * tokenstream; |
|
351 |
byte * tokenstream_s; |
|
352 |
byte execution_mode; |
|
353 |
_iconsole_var * var = NULL; |
|
354 |
||
355 |
byte var_b; // TYPE BYTE |
|
356 |
unsigned short var_ui16; // TYPE UINT16 |
|
357 |
unsigned int var_ui32; // TYPE UINT32 |
|
358 |
signed short var_i16; // TYPE INT16 |
|
359 |
signed int var_i32; // TYPE INT32 |
|
360 |
byte * var_s; // TYPE STRING |
|
361 |
||
362 |
bool longtoken; |
|
363 |
bool valid_token; |
|
364 |
bool skip_lt_change; |
|
365 |
||
366 |
int c; |
|
367 |
int i; |
|
368 |
int l; |
|
369 |
||
370 |
//** clearing buffer **// |
|
371 |
||
372 |
for (i=0;i<20;i++) { tokens[i]=NULL; tokentypes[i]=ICONSOLE_VAR_NONE; }; |
|
373 |
tokenstream_s=tokenstream=malloc(1024); |
|
374 |
memset(tokenstream,0,1024); |
|
375 |
||
376 |
//** parsing **// |
|
377 |
||
378 |
longtoken=false; |
|
379 |
valid_token=false; |
|
380 |
skip_lt_change=false; |
|
381 |
l=strlen((char *) cmdstr); |
|
382 |
i=0; |
|
383 |
c=0; |
|
384 |
tokens[c] = tokenstream; |
|
385 |
while (i<l) { |
|
386 |
if (cmdstr[i]=='"') { |
|
387 |
if (longtoken) { |
|
388 |
if (cmdstr[i+1]=='"') { |
|
389 |
i++; |
|
390 |
*tokenstream = '"'; |
|
391 |
tokenstream++; |
|
392 |
skip_lt_change=true; |
|
393 |
} else { |
|
394 |
longtoken=!longtoken; |
|
395 |
} |
|
396 |
} else { |
|
397 |
longtoken=!longtoken; |
|
398 |
} |
|
399 |
if (!skip_lt_change) { |
|
400 |
if (!longtoken) { |
|
401 |
if (valid_token) { |
|
402 |
c++; |
|
403 |
*tokenstream = 0; |
|
404 |
tokenstream++; |
|
405 |
tokens[c] = tokenstream; |
|
406 |
valid_token = false; |
|
407 |
} |
|
408 |
} |
|
409 |
skip_lt_change=false; |
|
410 |
} |
|
411 |
} |
|
412 |
else if ((!longtoken) && (cmdstr[i]==' ')) { |
|
413 |
if (valid_token) { |
|
414 |
c++; |
|
415 |
*tokenstream = 0; |
|
416 |
tokenstream++; |
|
417 |
tokens[c] = tokenstream; |
|
418 |
valid_token = false; |
|
419 |
} |
|
420 |
} |
|
421 |
else { |
|
422 |
valid_token=true; |
|
423 |
*tokenstream = cmdstr[i]; |
|
424 |
tokenstream++; |
|
425 |
} |
|
426 |
i++; |
|
427 |
} |
|
428 |
||
429 |
tokenstream--; |
|
430 |
if (!(*tokenstream==0)) { |
|
431 |
c++; |
|
432 |
tokenstream++; |
|
433 |
*tokenstream = 0; |
|
434 |
} |
|
435 |
||
436 |
//** interpreting **// |
|
437 |
||
438 |
for (i=0; i<c; i++) { |
|
439 |
if (i>0) if (strlen((char *) tokens[i])>0) { |
|
440 |
if (tokens[i][0]=='*') { |
|
441 |
var = IConsoleVarGet(tokens[i]); |
|
442 |
if (var!=NULL) { |
|
443 |
tokens[i]=(byte *)var->addr; |
|
444 |
tokentypes[i]=var->type; |
|
445 |
} |
|
446 |
} else { |
|
447 |
tokentypes[i]=ICONSOLE_VAR_STRING; |
|
448 |
} |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
execution_mode=0; |
|
453 |
||
454 |
function = IConsoleCmdGetAddr(tokens[0]); |
|
455 |
if (function != NULL) { |
|
456 |
execution_mode=1; // this is a command |
|
457 |
} else { |
|
458 |
var = IConsoleVarGet(tokens[0]); |
|
459 |
if (var != NULL) { |
|
460 |
execution_mode=2; // this is a variable |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
//** executing **// |
|
465 |
||
466 |
switch (execution_mode) { |
|
467 |
case 0: |
|
468 |
{ |
|
469 |
// not found |
|
470 |
IConsoleError("command or variable not found"); |
|
471 |
} |
|
472 |
break; |
|
473 |
case 1: |
|
474 |
{ |
|
475 |
// execution with command syntax |
|
476 |
function(c,tokens,tokentypes); |
|
477 |
} |
|
478 |
break; |
|
479 |
case 2: |
|
480 |
{ |
|
481 |
// execution with variable syntax |
|
482 |
if ((c==2) || (c==3)) { |
|
483 |
// ** variable modifications ** // |
|
484 |
switch (var->type) { |
|
485 |
case ICONSOLE_VAR_BOOLEAN: |
|
486 |
{ |
|
487 |
if (strcmp(tokens[1],"=")==0) { |
|
488 |
if (c==3) { |
|
489 |
*(bool *)var->addr=(atoi((char *) tokens[2])==1); |
|
490 |
c=1; |
|
491 |
} else { |
|
492 |
*(bool *)var->addr=false; |
|
493 |
c=1; |
|
494 |
} |
|
495 |
} |
|
496 |
else if (strcmp(tokens[1],"++")==0) { |
|
497 |
*(bool *)var->addr=!*(bool *)var->addr; |
|
498 |
c=1; |
|
499 |
} |
|
500 |
else if (strcmp(tokens[1],"--")==0) { |
|
501 |
*(bool *)var->addr=!*(bool *)var->addr; |
|
502 |
c=1; |
|
503 |
} |
|
504 |
else { IConsoleError("operation not supported"); } |
|
505 |
} |
|
506 |
break; |
|
507 |
case ICONSOLE_VAR_BYTE: |
|
508 |
{ |
|
509 |
if (strcmp(tokens[1],"=")==0) { |
|
510 |
if (c==3) { |
|
511 |
*(byte *)var->addr=atoi((char *) tokens[2]); |
|
512 |
c=1; |
|
513 |
} else { |
|
514 |
*(byte *)var->addr=0; |
|
515 |
c=1; |
|
516 |
} |
|
517 |
} |
|
518 |
else if (strcmp(tokens[1],"++")==0) { |
|
519 |
(*(byte *)var->addr)++; |
|
520 |
c=1; |
|
521 |
} |
|
522 |
else if (strcmp(tokens[1],"--")==0) { |
|
523 |
(*(byte *)var->addr)--; |
|
524 |
c=1; |
|
525 |
} |
|
526 |
else { IConsoleError("operation not supported"); } |
|
527 |
} |
|
528 |
break; |
|
529 |
case ICONSOLE_VAR_UINT16: |
|
530 |
{ |
|
531 |
if (strcmp(tokens[1],"=")==0) { |
|
532 |
if (c==3) { |
|
533 |
*(unsigned short *)var->addr=atoi((char *) tokens[2]); |
|
534 |
c=1; |
|
535 |
} else { |
|
536 |
*(unsigned short *)var->addr=0; |
|
537 |
c=1; |
|
538 |
} |
|
539 |
} |
|
540 |
else if (strcmp(tokens[1],"++")==0) { |
|
541 |
(*(unsigned short *)var->addr)++; |
|
542 |
c=1; |
|
543 |
} |
|
544 |
else if (strcmp(tokens[1],"--")==0) { |
|
545 |
(*(unsigned short *)var->addr)--; |
|
546 |
c=1; |
|
547 |
} |
|
548 |
else { IConsoleError("operation not supported"); } |
|
549 |
} |
|
550 |
break; |
|
551 |
case ICONSOLE_VAR_UINT32: |
|
552 |
{ |
|
553 |
if (strcmp(tokens[1],"=")==0) { |
|
554 |
if (c==3) { |
|
555 |
*(unsigned int *)var->addr=atoi((char *) tokens[2]); |
|
556 |
c=1; |
|
557 |
} else { |
|
558 |
*(unsigned int *)var->addr=0; |
|
559 |
c=1; |
|
560 |
} |
|
561 |
} |
|
562 |
else if (strcmp(tokens[1],"++")==0) { |
|
563 |
(*(unsigned int *)var->addr)++; |
|
564 |
c=1; |
|
565 |
} |
|
566 |
else if (strcmp(tokens[1],"--")==0) { |
|
567 |
(*(unsigned int *)var->addr)--; |
|
568 |
c=1; |
|
569 |
} |
|
570 |
else { IConsoleError("operation not supported"); } |
|
571 |
} |
|
572 |
break; |
|
573 |
case ICONSOLE_VAR_INT16: |
|
574 |
{ |
|
575 |
if (strcmp(tokens[1],"=")==0) { |
|
576 |
if (c==3) { |
|
577 |
*(signed short *)var->addr=atoi((char *) tokens[2]); |
|
578 |
c=1; |
|
579 |
} else { |
|
580 |
*(signed short *)var->addr=0; |
|
581 |
c=1; |
|
582 |
} |
|
583 |
} |
|
584 |
else if (strcmp(tokens[1],"++")==0) { |
|
585 |
(*(signed short *)var->addr)++; |
|
586 |
c=1; |
|
587 |
} |
|
588 |
else if (strcmp(tokens[1],"--")==0) { |
|
589 |
(*(signed short *)var->addr)--; |
|
590 |
c=1; |
|
591 |
} |
|
592 |
else { IConsoleError("operation not supported"); } |
|
593 |
} |
|
594 |
break; |
|
595 |
case ICONSOLE_VAR_INT32: |
|
596 |
{ |
|
597 |
if (strcmp(tokens[1],"=")==0) { |
|
598 |
if (c==3) { |
|
599 |
*(signed int *)var->addr=atoi((char *) tokens[2]); |
|
600 |
c=1; |
|
601 |
} else { |
|
602 |
*(signed int *)var->addr=0; |
|
603 |
c=1; |
|
604 |
} |
|
605 |
} |
|
606 |
else if (strcmp(tokens[1],"++")==0) { |
|
607 |
(*(signed int *)var->addr)++; |
|
608 |
c=1; |
|
609 |
} |
|
610 |
else if (strcmp(tokens[1],"--")==0) { |
|
611 |
(*(signed int *)var->addr)--; |
|
612 |
c=1; |
|
613 |
} |
|
614 |
else { IConsoleError("operation not supported"); } |
|
615 |
} |
|
616 |
break; |
|
617 |
case ICONSOLE_VAR_STRING: |
|
618 |
{ |
|
619 |
if (strcmp(tokens[1],"=")==0) { |
|
620 |
if (c==3) { |
|
621 |
IConsoleVarStringSet(var, tokens[2]); |
|
622 |
c=1; |
|
623 |
} else { |
|
624 |
IConsoleVarStringSet(var, ""); |
|
625 |
c=1; |
|
626 |
} |
|
627 |
} |
|
628 |
else { IConsoleError("operation not supported"); } |
|
629 |
} |
|
630 |
break; |
|
631 |
case ICONSOLE_VAR_POINTER: |
|
632 |
{ |
|
633 |
if (strcmp(tokens[1],"=")==0) { |
|
634 |
if (c==3) { |
|
635 |
var->addr = tokens[2]; |
|
636 |
c=1; |
|
637 |
} else { |
|
638 |
var->addr = NULL; |
|
639 |
c=1; |
|
640 |
} |
|
641 |
} |
|
642 |
else if (strcmp(tokens[1],"++")==0) { |
|
643 |
var->addr = ((char *)var->addr)+1; |
|
644 |
c=1; |
|
645 |
} |
|
646 |
else if (strcmp(tokens[1],"--")==0) { |
|
647 |
var->addr = ((char *)var->addr)-1;; |
|
648 |
c=1; |
|
649 |
} |
|
650 |
else { IConsoleError("operation not supported"); } |
|
651 |
} |
|
652 |
break; |
|
653 |
} |
|
654 |
} |
|
655 |
if (c==1) { |
|
656 |
// ** variable output ** // |
|
657 |
switch (var->type) { |
|
658 |
case ICONSOLE_VAR_BOOLEAN: |
|
659 |
{ |
|
660 |
if (*(bool *)var->addr) { |
|
661 |
IConsolePrintF(_iconsole_color_default, "%s = true",var->name); |
|
662 |
} else { |
|
663 |
IConsolePrintF(_iconsole_color_default, "%s = false",var->name); |
|
664 |
} |
|
665 |
} |
|
666 |
break; |
|
667 |
case ICONSOLE_VAR_BYTE: |
|
668 |
{ |
|
669 |
var_b=*(byte *)var->addr; |
|
670 |
IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_b); |
|
671 |
} |
|
672 |
break; |
|
673 |
case ICONSOLE_VAR_UINT16: |
|
674 |
{ |
|
675 |
var_ui16=*(unsigned short *)var->addr; |
|
676 |
IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_ui16); |
|
677 |
} |
|
678 |
break; |
|
679 |
case ICONSOLE_VAR_UINT32: |
|
680 |
{ |
|
681 |
var_ui32=*(unsigned int *)var->addr; |
|
682 |
IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_ui32); |
|
683 |
} |
|
684 |
break; |
|
685 |
case ICONSOLE_VAR_INT16: |
|
686 |
{ |
|
687 |
var_i16=*(signed short *)var->addr; |
|
688 |
IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_i16); |
|
689 |
} |
|
690 |
break; |
|
691 |
case ICONSOLE_VAR_INT32: |
|
692 |
{ |
|
693 |
var_i32=*(signed int *)var->addr; |
|
694 |
IConsolePrintF(_iconsole_color_default, "%s = %i",var->name,var_i32); |
|
695 |
} |
|
696 |
break; |
|
697 |
case ICONSOLE_VAR_STRING: |
|
698 |
{ |
|
699 |
var_s=(byte *)var->addr; |
|
700 |
IConsolePrintF(_iconsole_color_default, "%s = %s",var->name,var_s); |
|
701 |
} |
|
702 |
break; |
|
703 |
case ICONSOLE_VAR_UNKNOWN: |
|
704 |
case ICONSOLE_VAR_VARPTR: |
|
705 |
case ICONSOLE_VAR_POINTER: |
|
706 |
{ |
|
707 |
var_i32=(signed int)((byte *)var->addr); |
|
708 |
IConsolePrintF(_iconsole_color_default, "%s = @%i",var->name,var_i32); |
|
709 |
} |
|
710 |
break; |
|
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
break; |
|
715 |
default: |
|
716 |
{ |
|
717 |
// execution mode invalid |
|
718 |
IConsoleError("invalid execution mode"); |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
//** freeing the tokens **// |
|
723 |
for (i=0;i<20;i++) tokens[i]=NULL; |
|
724 |
free(tokenstream_s); |
|
725 |
||
726 |
} |
|
727 |
||
728 |
/* **************************** */ |
|
729 |
/* default console commands */ |
|
730 |
/* **************************** */ |
|
731 |
||
732 |
||
733 |
static void IConsoleStdLibEcho(byte argc, byte * argv[], byte argt[]) { |
|
734 |
if (argc<2) return; |
|
735 |
IConsolePrint(_iconsole_color_default, argv[1]); |
|
736 |
} |
|
737 |
||
738 |
static void IConsoleStdLibEchoC(byte argc, byte * argv[], byte argt[]) { |
|
739 |
if (argc<3) return; |
|
740 |
IConsolePrint(atoi(argv[1]), argv[2]); |
|
741 |
} |
|
742 |
||
743 |
static void IConsoleStdLibPrintF(byte argc, byte * argv[], byte argt[]) { |
|
744 |
if (argc<3) return; |
|
745 |
IConsolePrintF(_iconsole_color_default, argv[1] ,argv[2],argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9],argv[10],argv[11],argv[12],argv[13],argv[14],argv[15],argv[16],argv[17],argv[18],argv[19]); |
|
746 |
} |
|
747 |
||
748 |
static void IConsoleStdLibPrintFC(byte argc, byte * argv[], byte argt[]) { |
|
749 |
if (argc<3) return; |
|
750 |
IConsolePrintF(atoi(argv[1]), argv[2] ,argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9],argv[10],argv[11],argv[12],argv[13],argv[14],argv[15],argv[16],argv[17],argv[18],argv[19]); |
|
751 |
} |
|
752 |
||
753 |
static void IConsoleStdLibScreenShot(byte argc, byte * argv[], byte argt[]) { |
|
754 |
||
755 |
if (argc<2) { |
|
756 |
_make_screenshot=1; |
|
757 |
} else { |
|
758 |
if (strcmp(argv[1],"big")==0) { |
|
759 |
_make_screenshot=2; |
|
760 |
} |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
static void IConsoleStdLibDebugLevel(byte argc, byte * argv[], byte argt[]) { |
|
765 |
if (argc<2) return; |
|
766 |
SetDebugString(argv[1]); |
|
767 |
} |
|
768 |
||
769 |
static void IConsoleStdLibExit(byte argc, byte * argv[], byte argt[]) { |
|
770 |
_exit_game = true; |
|
771 |
} |
|
772 |
||
773 |
static void IConsoleStdLibListCommands(byte argc, byte * argv[], byte argt[]) { |
|
774 |
_iconsole_cmd * item; |
|
775 |
int l = 0; |
|
776 |
||
777 |
if (argv[1]!=NULL) l = strlen((char *) argv[1]); |
|
778 |
||
779 |
item = _iconsole_cmds; |
|
780 |
while (item != NULL) { |
|
781 |
if (argv[1]!=NULL) { |
|
782 |
||
783 |
if (memcmp((void *) item->name, (void *) argv[1],l)==0) |
|
784 |
IConsolePrintF(_iconsole_color_default,"%s",item->name); |
|
785 |
||
786 |
} else { |
|
787 |
||
788 |
IConsolePrintF(_iconsole_color_default,"%s",item->name); |
|
789 |
||
790 |
} |
|
791 |
item = item->_next; |
|
792 |
} |
|
793 |
} |
|
794 |
||
795 |
static void IConsoleStdLibListVariables(byte argc, byte * argv[], byte argt[]) { |
|
796 |
_iconsole_var * item; |
|
797 |
int l = 0; |
|
798 |
||
799 |
if (argv[1]!=NULL) l = strlen((char *) argv[1]); |
|
800 |
||
801 |
item = _iconsole_vars; |
|
802 |
while (item != NULL) { |
|
803 |
if (argv[1]!=NULL) { |
|
804 |
||
805 |
if (memcmp((void *) item->name, (void *) argv[1],l)==0) |
|
806 |
IConsolePrintF(_iconsole_color_default,"%s",item->name); |
|
807 |
||
808 |
} else { |
|
809 |
||
810 |
IConsolePrintF(_iconsole_color_default,"%s",item->name); |
|
811 |
||
812 |
} |
|
813 |
item = item->_next; |
|
814 |
} |
|
815 |
} |
|
816 |
||
817 |
static void IConsoleStdLibRegister() { |
|
818 |
IConsoleCmdRegister("echo",IConsoleStdLibEcho); |
|
819 |
IConsoleCmdRegister("echoc",IConsoleStdLibEchoC); |
|
820 |
IConsoleCmdRegister("printf",IConsoleStdLibPrintF); |
|
821 |
IConsoleCmdRegister("printfc",IConsoleStdLibPrintFC); |
|
822 |
IConsoleCmdRegister("list_cmds",IConsoleStdLibListCommands); |
|
823 |
IConsoleCmdRegister("list_vars",IConsoleStdLibListVariables); |
|
824 |
IConsoleCmdRegister("screenshot",IConsoleStdLibScreenShot); |
|
825 |
IConsoleCmdRegister("debug_level",IConsoleStdLibDebugLevel); |
|
826 |
IConsoleCmdRegister("exit",IConsoleStdLibExit); |
|
827 |
IConsoleVarRegister("developer",(void *) &_stdlib_developer,ICONSOLE_VAR_BYTE); |
|
828 |
IConsoleVarRegister("cursor_rate",(void *) &_icursor_rate,ICONSOLE_VAR_BYTE); |
|
829 |
IConsoleVarRegister("temp_string",(void *) &_stdlib_temp_string,ICONSOLE_VAR_STRING); |
|
830 |
IConsoleVarRegister("temp_pointer",(void *) &_stdlib_temp_pointer,ICONSOLE_VAR_POINTER); |
|
831 |
IConsoleVarRegister("temp_uint32",(void *) &_stdlib_temp_uint32,ICONSOLE_VAR_UINT32); |
|
832 |
IConsoleVarRegister("temp_bool",(void *) &_stdlib_temp_bool,ICONSOLE_VAR_BOOLEAN); |
|
833 |
} |
|
834 |
||
835 |