2004-08-24 22:41:42 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "ttd.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "gui.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "variables.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "console.h"
|
|
|
|
|
|
|
|
// ** main console ** //
|
|
|
|
static bool _iconsole_inited;
|
|
|
|
static byte* _iconsole_buffer[80];
|
|
|
|
static byte _iconsole_cbuffer[80];
|
|
|
|
static byte _iconsole_cmdline[255];
|
|
|
|
static byte _iconsole_cmdpos;
|
2004-08-25 08:55:53 +00:00
|
|
|
static byte _iconsole_mode = ICONSOLE_CLOSED;
|
2004-08-24 22:41:42 +00:00
|
|
|
static Window *_iconsole_win = NULL;
|
|
|
|
static byte _iconsole_scroll;
|
|
|
|
|
|
|
|
// ** console cursor ** //
|
|
|
|
static bool _icursor_state;
|
|
|
|
static byte _icursor_rate;
|
|
|
|
static byte _icursor_counter;
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
// ** stdlib ** //
|
|
|
|
byte _stdlib_developer=1;
|
|
|
|
bool _stdlib_con_developer=false;
|
2004-09-14 16:10:20 +00:00
|
|
|
FILE * _iconsole_output_file;
|
2004-09-12 20:15:18 +00:00
|
|
|
|
|
|
|
// ** main console cmd buffer ** // sign_de: especialy for Celestar :D
|
|
|
|
static byte* _iconsole_cmdbuffer[20];
|
|
|
|
static byte _iconsole_cmdbufferpos;
|
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
// ** console window ** //
|
|
|
|
static void IConsoleWndProc(Window *w, WindowEvent *e);
|
2004-09-07 21:48:09 +00:00
|
|
|
static const Widget _iconsole_window_widgets[] = {{WIDGETS_END}};
|
2004-08-24 22:41:42 +00:00
|
|
|
static const WindowDesc _iconsole_window_desc = {
|
|
|
|
0, 0, 2, 2,
|
|
|
|
WC_CONSOLE,0,
|
|
|
|
WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
|
|
|
|
_iconsole_window_widgets,
|
|
|
|
IConsoleWndProc,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* *************** */
|
|
|
|
/* end of header */
|
|
|
|
/* *************** */
|
|
|
|
|
|
|
|
void IConsoleClearCommand()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<255; i++) _iconsole_cmdline[i]=0;
|
|
|
|
_iconsole_cmdpos=0;
|
|
|
|
SetWindowDirty(_iconsole_win);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void IConsoleWndProc(Window *w, WindowEvent *e)
|
|
|
|
{
|
2004-09-12 10:23:35 +00:00
|
|
|
// only do window events with the console
|
|
|
|
w = FindWindowById(WC_CONSOLE, 0);
|
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
switch(e->event) {
|
|
|
|
|
2004-09-06 21:27:26 +00:00
|
|
|
case WE_PAINT:
|
2004-08-24 22:41:42 +00:00
|
|
|
GfxFillRect(w->left,w->top,w->width,w->height-1,0);
|
|
|
|
{
|
|
|
|
int i=_iconsole_scroll;
|
|
|
|
int max=(w->height/12)-1;
|
|
|
|
while ((i>_iconsole_scroll-max) && (_iconsole_buffer[i]!=NULL)) {
|
|
|
|
DoDrawString(_iconsole_buffer[i],5,w->height-(((_iconsole_scroll+2)-i)*12),_iconsole_cbuffer[i]);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
DoDrawString("]",5,w->height-12,_iconsole_color_commands);
|
|
|
|
DoDrawString((char *)&_iconsole_cmdline,10,w->height-12,_iconsole_color_commands);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WE_TICK:
|
|
|
|
|
|
|
|
_icursor_counter++;
|
|
|
|
if (_icursor_counter>_icursor_rate) {
|
|
|
|
_icursor_state=!_icursor_state;
|
|
|
|
{
|
|
|
|
int posx;
|
|
|
|
int posy;
|
|
|
|
int color;
|
|
|
|
_cur_dpi=&_screen;
|
|
|
|
if (_icursor_state) color=14; else color=0;
|
|
|
|
posx=10+GetStringWidth((char *)&_iconsole_cmdline);
|
|
|
|
posy=w->height-3;
|
|
|
|
GfxFillRect(posx,posy,posx+5,posy+1,color);
|
|
|
|
_video_driver->make_dirty(posx,posy,5,1);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
_icursor_counter=0;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-09-06 21:27:26 +00:00
|
|
|
case WE_DESTROY:
|
|
|
|
_iconsole_win=NULL;
|
|
|
|
_iconsole_mode=ICONSOLE_CLOSED;
|
|
|
|
break;
|
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
case WE_KEYPRESS:
|
|
|
|
e->keypress.cont=false;
|
2004-08-25 22:11:41 +00:00
|
|
|
if (e->keypress.keycode == (WKC_UP))
|
|
|
|
{
|
|
|
|
IConsoleCmdBufferNavigate(+1);
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} else
|
|
|
|
if (e->keypress.keycode == (WKC_DOWN))
|
|
|
|
{
|
|
|
|
IConsoleCmdBufferNavigate(-1);
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} else
|
|
|
|
if (e->keypress.keycode == (WKC_SHIFT | WKC_PAGEUP))
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
if ((_iconsole_scroll - ((w->height/12)-1))<0) {
|
|
|
|
_iconsole_scroll = 0;
|
|
|
|
} else {
|
|
|
|
_iconsole_scroll -= (w->height/12)-1;
|
|
|
|
}
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} else
|
2004-08-25 22:11:41 +00:00
|
|
|
if (e->keypress.keycode == (WKC_SHIFT | WKC_PAGEDOWN))
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
if ((_iconsole_scroll + ((w->height/12)-1))>79) {
|
|
|
|
_iconsole_scroll = 79;
|
|
|
|
} else {
|
|
|
|
_iconsole_scroll += (w->height/12)-1;
|
|
|
|
}
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} else
|
2004-08-25 22:11:41 +00:00
|
|
|
if (e->keypress.keycode == (WKC_SHIFT | WKC_UP))
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
if ((_iconsole_scroll - 1)<0) {
|
|
|
|
_iconsole_scroll = 0;
|
|
|
|
} else {
|
|
|
|
_iconsole_scroll -= 1;
|
|
|
|
}
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} else
|
2004-08-25 22:11:41 +00:00
|
|
|
if (e->keypress.keycode == (WKC_SHIFT | WKC_DOWN))
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
if ((_iconsole_scroll + 1)>79) {
|
|
|
|
_iconsole_scroll = 79;
|
|
|
|
} else {
|
|
|
|
_iconsole_scroll += 1;
|
|
|
|
}
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} else
|
|
|
|
if (e->keypress.keycode == WKC_BACKQUOTE)
|
|
|
|
{
|
|
|
|
IConsoleSwitch();
|
|
|
|
} else
|
2004-09-10 19:02:27 +00:00
|
|
|
if (e->keypress.keycode == WKC_RETURN)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
IConsolePrintF(_iconsole_color_commands, "] %s", _iconsole_cmdline);
|
2004-08-25 22:11:41 +00:00
|
|
|
IConsoleCmdBufferAdd(_iconsole_cmdline);
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleCmdExec((byte *) _iconsole_cmdline);
|
|
|
|
IConsoleClearCommand();
|
|
|
|
} else
|
2004-09-10 19:02:27 +00:00
|
|
|
if (e->keypress.keycode == WKC_BACKSPACE)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
if (_iconsole_cmdpos!=0) _iconsole_cmdpos--;
|
|
|
|
_iconsole_cmdline[_iconsole_cmdpos]=0;
|
|
|
|
SetWindowDirty(w);
|
2004-08-25 22:11:41 +00:00
|
|
|
_iconsole_cmdbufferpos=19;
|
2004-08-24 22:41:42 +00:00
|
|
|
} else
|
|
|
|
if (IS_INT_INSIDE((e->keypress.ascii), 32, 256))
|
|
|
|
{
|
|
|
|
_iconsole_scroll=79;
|
|
|
|
_iconsole_cmdline[_iconsole_cmdpos]=e->keypress.ascii;
|
2004-09-10 19:02:27 +00:00
|
|
|
if (_iconsole_cmdpos!=255) _iconsole_cmdpos++;
|
2004-08-24 22:41:42 +00:00
|
|
|
SetWindowDirty(w);
|
2004-08-25 22:11:41 +00:00
|
|
|
_iconsole_cmdbufferpos=19;
|
2004-09-10 19:02:27 +00:00
|
|
|
} else e->keypress.cont=true;
|
2004-08-24 22:41:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleInit()
|
|
|
|
{
|
2004-09-12 20:15:18 +00:00
|
|
|
int i;
|
|
|
|
#if defined(WITH_REV)
|
|
|
|
extern char _openttd_revision[];
|
|
|
|
#endif
|
2004-09-14 16:10:20 +00:00
|
|
|
_iconsole_output_file = NULL;
|
2004-09-12 20:15:18 +00:00
|
|
|
_iconsole_color_default = 1;
|
2004-09-14 16:10:20 +00:00
|
|
|
_iconsole_color_error = 3;
|
2004-09-13 06:56:30 +00:00
|
|
|
_iconsole_color_warning = 13;
|
2004-09-12 20:15:18 +00:00
|
|
|
_iconsole_color_debug = 5;
|
|
|
|
_iconsole_color_commands = 2;
|
|
|
|
_iconsole_scroll=79;
|
|
|
|
_iconsole_cmdbufferpos=19;
|
|
|
|
_iconsole_inited=true;
|
|
|
|
_iconsole_mode=ICONSOLE_CLOSED;
|
|
|
|
_iconsole_win=NULL;
|
|
|
|
_icursor_state=false;
|
|
|
|
_icursor_rate=5;
|
|
|
|
_icursor_counter=0;
|
|
|
|
for (i=0;i<20;i++) {
|
|
|
|
_iconsole_cmdbuffer[i]=NULL;
|
|
|
|
}
|
|
|
|
for (i=0;i<80;i++) {
|
|
|
|
_iconsole_buffer[i]=NULL;
|
|
|
|
_iconsole_cbuffer[i]=0;
|
|
|
|
}
|
|
|
|
IConsoleStdLibRegister();
|
|
|
|
#if defined(WITH_REV)
|
2004-09-12 21:49:38 +00:00
|
|
|
IConsolePrintF(13,"OpenTTD Game Console Revision 4 - %s",_openttd_revision);
|
2004-09-12 20:15:18 +00:00
|
|
|
#else
|
2004-09-12 21:49:38 +00:00
|
|
|
IConsolePrint(13,"OpenTTD Game Console Revision 4");
|
2004-09-12 20:15:18 +00:00
|
|
|
#endif
|
|
|
|
IConsolePrint(12,"---------------------------------");
|
|
|
|
IConsolePrint(12,"use \"help\" for more info");
|
|
|
|
IConsolePrint(12,"");
|
|
|
|
IConsoleClearCommand();
|
|
|
|
IConsoleCmdBufferAdd("");
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleClear()
|
|
|
|
{
|
2004-09-12 20:15:18 +00:00
|
|
|
int i;
|
|
|
|
for (i=0;i<80;i++) if (_iconsole_buffer[i]!=NULL) {
|
|
|
|
free(_iconsole_buffer[i]);
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleFree()
|
|
|
|
{
|
2004-09-12 20:15:18 +00:00
|
|
|
_iconsole_inited=false;
|
|
|
|
IConsoleClear();
|
2004-09-14 16:10:20 +00:00
|
|
|
if (_iconsole_output_file!=NULL) fclose(_iconsole_output_file);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
void IConsoleResize()
|
|
|
|
{
|
2004-08-24 22:41:42 +00:00
|
|
|
if (_iconsole_win!=NULL) {
|
|
|
|
_iconsole_win->height = _screen.height / 3;
|
|
|
|
_iconsole_win->width= _screen.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleSwitch()
|
|
|
|
{
|
|
|
|
if (_iconsole_mode==ICONSOLE_CLOSED) {
|
|
|
|
_iconsole_win = AllocateWindowDesc(&_iconsole_window_desc);
|
|
|
|
_iconsole_win->height = _screen.height / 3;
|
|
|
|
_iconsole_win->width= _screen.width;
|
2004-08-25 08:55:53 +00:00
|
|
|
_iconsole_mode=ICONSOLE_OPENED;
|
2004-08-24 22:41:42 +00:00
|
|
|
} else
|
|
|
|
if (_iconsole_mode==ICONSOLE_OPENED) {
|
2004-09-12 10:23:35 +00:00
|
|
|
DeleteWindowById(WC_CONSOLE,0);
|
2004-08-24 22:41:42 +00:00
|
|
|
_iconsole_win=NULL;
|
|
|
|
_iconsole_mode=ICONSOLE_CLOSED;
|
|
|
|
}
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
MarkAllViewportsDirty(0,0,_screen.width,_screen.height);
|
|
|
|
_video_driver->make_dirty(0,0,_screen.width,_screen.height);
|
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
void IConsoleClose()
|
|
|
|
{
|
|
|
|
if (_iconsole_mode==ICONSOLE_OPENED) IConsoleSwitch();
|
|
|
|
_iconsole_mode=ICONSOLE_CLOSED;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
void IConsoleOpen()
|
|
|
|
{
|
|
|
|
if (_iconsole_mode==ICONSOLE_CLOSED) IConsoleSwitch();
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleCmdBufferAdd(const byte * cmd)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (_iconsole_cmdbufferpos != 19) return;
|
|
|
|
if (_iconsole_cmdbuffer[18]!=NULL) free(_iconsole_cmdbuffer[18]);
|
|
|
|
for (i=18; i>0; i--) _iconsole_cmdbuffer[i]=_iconsole_cmdbuffer[i-1];
|
2004-09-12 21:49:38 +00:00
|
|
|
i=strlen(cmd);
|
2004-09-12 20:15:18 +00:00
|
|
|
_iconsole_cmdbuffer[0]=malloc(i+1);
|
|
|
|
memset(((void *)_iconsole_cmdbuffer[0]),0,i+1);
|
2004-09-12 21:49:38 +00:00
|
|
|
memcpy(((void *)_iconsole_cmdbuffer[0]),cmd,i);
|
2004-09-12 20:15:18 +00:00
|
|
|
_iconsole_cmdbuffer[0][i]=0;
|
|
|
|
_iconsole_cmdbufferpos = 19;
|
2004-08-25 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
void IConsoleCmdBufferNavigate(signed char direction)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
i=_iconsole_cmdbufferpos + direction;
|
2004-08-25 22:11:41 +00:00
|
|
|
if (i<0) i=19;
|
2004-09-12 20:15:18 +00:00
|
|
|
if (i>19) i=0;
|
|
|
|
if (direction>0) while (_iconsole_cmdbuffer[i]==NULL) {
|
|
|
|
i++;
|
|
|
|
if (i>19) i=0;
|
|
|
|
}
|
|
|
|
if (direction<0) while (_iconsole_cmdbuffer[i]==NULL) {
|
|
|
|
i--;
|
|
|
|
if (i<0) i=19;
|
|
|
|
}
|
|
|
|
_iconsole_cmdbufferpos = i;
|
|
|
|
IConsoleClearCommand();
|
|
|
|
memcpy((void *)_iconsole_cmdline,(void *)_iconsole_cmdbuffer[i],strlen(_iconsole_cmdbuffer[i]));
|
|
|
|
_iconsole_cmdpos =strlen(_iconsole_cmdbuffer[i]);
|
2004-08-25 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsolePrint(byte color_code, const byte* string)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2004-09-12 20:15:18 +00:00
|
|
|
byte * _ex;
|
|
|
|
byte * _new;
|
|
|
|
byte _exc;
|
|
|
|
byte _newc;
|
|
|
|
int i,j;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
if (!_iconsole_inited) return;
|
|
|
|
|
|
|
|
_newc=color_code;
|
2004-09-12 21:49:38 +00:00
|
|
|
i=strlen(string);
|
2004-09-12 20:15:18 +00:00
|
|
|
_new=malloc(i+1);
|
|
|
|
memset(_new,0,i+1);
|
|
|
|
memcpy(_new,string,i);
|
|
|
|
|
|
|
|
for (j=0;j<i;j++) {
|
|
|
|
if (_new[j]<0x1F) _new[j]=0x20;
|
|
|
|
}
|
|
|
|
|
|
|
|
i=79;
|
|
|
|
while (i>=0) {
|
|
|
|
_ex=_iconsole_buffer[i];
|
|
|
|
_exc=_iconsole_cbuffer[i];
|
|
|
|
_iconsole_buffer[i]=_new;
|
|
|
|
_iconsole_cbuffer[i]=_newc;
|
|
|
|
_new=_ex;
|
|
|
|
_newc=_exc;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (_ex!=NULL) free(_ex);
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
if (_iconsole_win!=NULL) SetWindowDirty(_iconsole_win);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-06 21:27:26 +00:00
|
|
|
void CDECL IConsolePrintF(byte color_code, const char *s, ...)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char buf[1024];
|
2004-09-14 16:10:20 +00:00
|
|
|
int len;
|
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
va_start(va, s);
|
2004-09-14 16:10:20 +00:00
|
|
|
len = vsprintf(buf, s, va);
|
2004-08-24 22:41:42 +00:00
|
|
|
va_end(va);
|
2004-09-14 16:10:20 +00:00
|
|
|
|
|
|
|
if (_iconsole_output_file!=NULL) {
|
|
|
|
// if there is an console output file ... also print it there
|
|
|
|
fwrite((void *) &buf, len, 1, _iconsole_output_file);
|
|
|
|
buf[1023]='\n';
|
|
|
|
fwrite((void *)&buf[1023], 1, 1,_iconsole_output_file);
|
|
|
|
}
|
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsolePrint(color_code, (byte *) &buf);
|
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
void IConsoleDebug(byte* string)
|
|
|
|
{
|
|
|
|
if (_stdlib_developer>1) IConsolePrintF(_iconsole_color_debug, "DEBUG: %s", string);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleError(const byte* string)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_error, "ERROR: %s", string);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-14 16:10:20 +00:00
|
|
|
|
|
|
|
void IConsoleWarning(const byte* string)
|
|
|
|
{
|
|
|
|
if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_warning, "WARNING: %s", string);
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleCmdRegister(const byte * name, void * addr)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
byte * _new;
|
|
|
|
_iconsole_cmd * item;
|
|
|
|
_iconsole_cmd * item_new;
|
|
|
|
int i;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
i=strlen(name);
|
|
|
|
_new=malloc(i+1);
|
|
|
|
memset(_new,0,i+1);
|
|
|
|
memcpy(_new,name,i);
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item_new = malloc(sizeof(_iconsole_cmd));
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item_new->_next = NULL;
|
|
|
|
item_new->addr = addr;
|
|
|
|
item_new->name = _new;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item_new->hook_access = NULL;
|
|
|
|
item_new->hook_after_exec = NULL;
|
|
|
|
item_new->hook_before_exec = NULL;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item = _iconsole_cmds;
|
|
|
|
if (item == NULL) {
|
|
|
|
_iconsole_cmds = item_new;
|
|
|
|
} else {
|
|
|
|
while (item->_next != NULL) { item = item->_next; };
|
|
|
|
item->_next = item_new;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-13 06:56:30 +00:00
|
|
|
_iconsole_cmd * IConsoleCmdGet(const byte * name)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
_iconsole_cmd * item;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item = _iconsole_cmds;
|
|
|
|
while (item != NULL) {
|
|
|
|
if (strcmp(item->name,name)==0) return item;
|
|
|
|
item = item->_next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleVarRegister(const byte * name, void * addr, byte type)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
byte * _new;
|
|
|
|
_iconsole_var * item;
|
|
|
|
_iconsole_var * item_new;
|
|
|
|
int i;
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
i=strlen(name)+1;
|
|
|
|
_new=malloc(i+1);
|
|
|
|
memset(_new,0,i+1);
|
|
|
|
_new[0]='*';
|
|
|
|
memcpy(_new+1,name,i);
|
2004-09-12 20:15:18 +00:00
|
|
|
|
|
|
|
item_new = malloc(sizeof(_iconsole_var));
|
|
|
|
|
|
|
|
item_new->_next = NULL;
|
|
|
|
item_new->addr = addr;
|
|
|
|
item_new->name = _new;
|
|
|
|
item_new->type = type;
|
|
|
|
item_new->_malloc = false;
|
|
|
|
|
|
|
|
item_new->hook_access = NULL;
|
|
|
|
item_new->hook_after_change = NULL;
|
|
|
|
item_new->hook_before_change = NULL;
|
|
|
|
|
|
|
|
item = _iconsole_vars;
|
|
|
|
if (item == NULL) {
|
|
|
|
_iconsole_vars = item_new;
|
|
|
|
} else {
|
|
|
|
while (item->_next != NULL) { item = item->_next; };
|
|
|
|
item->_next = item_new;
|
|
|
|
}
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 23:35:01 +00:00
|
|
|
void IConsoleVarMemRegister(const byte * name, byte type)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
_iconsole_var * item;
|
|
|
|
item = IConsoleVarAlloc(type);
|
|
|
|
IConsoleVarInsert(item,name);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleVarInsert(_iconsole_var * var, const byte * name)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
byte * _new;
|
|
|
|
_iconsole_var * item;
|
|
|
|
_iconsole_var * item_new;
|
|
|
|
int i;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item_new = var;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
// dont allow to build variable rings
|
|
|
|
if (item_new->_next != NULL) return;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
i=strlen(name)+1;
|
|
|
|
_new=malloc(i+1);
|
|
|
|
memset(_new,0,i+1);
|
|
|
|
_new[0]='*';
|
|
|
|
memcpy(_new+1,name,i);
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item_new->name = _new;
|
|
|
|
|
|
|
|
item = _iconsole_vars;
|
|
|
|
if (item == NULL) {
|
|
|
|
_iconsole_vars = item_new;
|
2004-08-24 22:41:42 +00:00
|
|
|
} else {
|
2004-09-12 20:15:18 +00:00
|
|
|
while (item->_next != NULL) { item = item->_next; };
|
|
|
|
item->_next = item_new;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
_iconsole_var * IConsoleVarGet(const byte * name)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
_iconsole_var * item;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item = _iconsole_vars;
|
|
|
|
while (item != NULL) {
|
|
|
|
if (strcmp(item->name,name)==0) return item;
|
|
|
|
item = item->_next;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
return NULL;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
_iconsole_var * IConsoleVarAlloc(byte type)
|
|
|
|
{
|
|
|
|
_iconsole_var * item;
|
|
|
|
item=malloc(sizeof(_iconsole_var));
|
|
|
|
item->_next = NULL;
|
|
|
|
item->name = "";
|
|
|
|
item->type = type;
|
|
|
|
switch (item->type) {
|
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
|
|
|
{
|
|
|
|
item->addr=malloc(sizeof(bool));
|
|
|
|
memset(item->addr,0,sizeof(bool));
|
|
|
|
item->_malloc=true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
|
|
|
{
|
|
|
|
item->addr=malloc(sizeof(byte));
|
|
|
|
memset(item->addr,0,sizeof(byte));
|
|
|
|
item->_malloc=true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
|
|
|
{
|
|
|
|
item->addr=malloc(sizeof(unsigned short));
|
|
|
|
memset(item->addr,0,sizeof(unsigned short));
|
|
|
|
item->_malloc=true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT32:
|
|
|
|
{
|
|
|
|
item->addr=malloc(sizeof(unsigned int));
|
|
|
|
memset(item->addr,0,sizeof(unsigned int));
|
|
|
|
item->_malloc=true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
|
|
|
{
|
|
|
|
item->addr=malloc(sizeof(signed short));
|
|
|
|
memset(item->addr,0,sizeof(signed short));
|
|
|
|
item->_malloc=true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT32:
|
|
|
|
{
|
|
|
|
item->addr=malloc(sizeof(signed int));
|
|
|
|
memset(item->addr,0,sizeof(signed int));
|
|
|
|
item->_malloc=true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item->addr = NULL;
|
|
|
|
item->_malloc = false;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
item->hook_access = NULL;
|
|
|
|
item->hook_after_change = NULL;
|
|
|
|
item->hook_before_change = NULL;
|
|
|
|
return item;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
void IConsoleVarFree(_iconsole_var * var)
|
|
|
|
{
|
2004-09-12 21:49:38 +00:00
|
|
|
if (var->_malloc)
|
|
|
|
free(var->addr);
|
2004-09-12 20:15:18 +00:00
|
|
|
free(var);
|
|
|
|
}
|
2004-08-25 22:11:41 +00:00
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleVarSetString(_iconsole_var * var, const byte * string)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
int l;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
if (string == NULL) return;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
if (var->_malloc) {
|
|
|
|
free(var->addr);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
l=strlen(string);
|
2004-09-12 20:15:18 +00:00
|
|
|
var->addr=malloc(l+1);
|
|
|
|
var->_malloc=true;
|
|
|
|
memset(var->addr,0,l);
|
2004-09-12 21:49:38 +00:00
|
|
|
memcpy(var->addr, string, l);
|
2004-09-12 20:15:18 +00:00
|
|
|
((byte *)var->addr)[l]=0;
|
2004-09-12 21:49:38 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleVarSetValue(_iconsole_var * var, int value) {
|
2004-09-12 20:15:18 +00:00
|
|
|
switch (var->type) {
|
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
2004-09-12 21:49:38 +00:00
|
|
|
*(bool *)var->addr = (value != 0);
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
2004-09-12 21:49:38 +00:00
|
|
|
*(byte *)var->addr = value;
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
2004-09-12 21:49:38 +00:00
|
|
|
*(unsigned short *)var->addr = value;
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT32:
|
2004-09-12 21:49:38 +00:00
|
|
|
*(unsigned int *)var->addr = value;
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
2004-09-12 21:49:38 +00:00
|
|
|
*(signed short *)var->addr = value;
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT32:
|
2004-09-12 21:49:38 +00:00
|
|
|
*(signed int *)var->addr = value;
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2004-09-12 21:49:38 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleVarDump(_iconsole_var * var, const byte * dump_desc)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
byte var_b; // TYPE BYTE
|
|
|
|
unsigned short var_ui16; // TYPE UINT16
|
|
|
|
unsigned int var_ui32; // TYPE UINT32
|
|
|
|
signed short var_i16; // TYPE INT16
|
|
|
|
signed int var_i32; // TYPE INT32
|
|
|
|
byte * var_s; // TYPE STRING
|
2004-08-24 22:41:42 +00:00
|
|
|
|
|
|
|
if (dump_desc==NULL) dump_desc = var->name;
|
|
|
|
|
|
|
|
switch (var->type) {
|
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
|
|
|
{
|
|
|
|
if (*(bool *)var->addr) {
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = true",dump_desc);
|
|
|
|
} else {
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = false",dump_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
|
|
|
{
|
|
|
|
var_b=*(byte *)var->addr;
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_b);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
|
|
|
{
|
|
|
|
var_ui16=*(unsigned short *)var->addr;
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_ui16);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT32:
|
|
|
|
{
|
|
|
|
var_ui32=*(unsigned int *)var->addr;
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_ui32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
|
|
|
{
|
|
|
|
var_i16=*(signed short *)var->addr;
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_i16);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT32:
|
|
|
|
{
|
|
|
|
var_i32=*(signed int *)var->addr;
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_i32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_STRING:
|
|
|
|
{
|
|
|
|
var_s=(byte *)var->addr;
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = %s",dump_desc,var_s);
|
|
|
|
}
|
|
|
|
break;
|
2004-08-25 22:11:41 +00:00
|
|
|
case ICONSOLE_VAR_REFERENCE:
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = @%s",dump_desc,((_iconsole_var *)var->addr)->name);
|
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_UNKNOWN:
|
|
|
|
case ICONSOLE_VAR_POINTER:
|
|
|
|
{
|
|
|
|
var_i32=(signed int)((byte *)var->addr);
|
|
|
|
IConsolePrintF(_iconsole_color_default, "%s = @%i",dump_desc,var_i32);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
// * ************************* * //
|
|
|
|
// * hooking code * //
|
|
|
|
// * ************************* * //
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleVarHook(const byte * name, byte type, void * proc)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
_iconsole_var * hook_var;
|
|
|
|
hook_var = IConsoleVarGet(name);
|
|
|
|
if (hook_var == NULL) return;
|
|
|
|
switch (type) {
|
|
|
|
case ICONSOLE_HOOK_BEFORE_CHANGE:
|
|
|
|
hook_var->hook_after_change = proc;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_AFTER_CHANGE:
|
|
|
|
hook_var->hook_after_change = proc;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_ACCESS:
|
|
|
|
hook_var->hook_access = proc;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
bool IConsoleVarHookHandle(_iconsole_var * hook_var, byte type)
|
|
|
|
{
|
2004-09-12 21:49:38 +00:00
|
|
|
bool (*proc)(_iconsole_var * hook_var) = NULL;
|
2004-09-12 20:15:18 +00:00
|
|
|
switch (type) {
|
|
|
|
case ICONSOLE_HOOK_BEFORE_CHANGE:
|
|
|
|
proc = hook_var->hook_before_change;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_AFTER_CHANGE:
|
|
|
|
proc = hook_var->hook_after_change;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_ACCESS:
|
|
|
|
proc = hook_var->hook_access;
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 21:49:38 +00:00
|
|
|
|
2004-09-12 22:14:45 +00:00
|
|
|
if (proc == NULL) { return true;}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
return proc(hook_var);
|
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
void IConsoleCmdHook(const byte * name, byte type, void * proc)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
_iconsole_cmd * hook_cmd;
|
|
|
|
hook_cmd = IConsoleCmdGet(name);
|
|
|
|
if (hook_cmd == NULL) return;
|
|
|
|
switch (type) {
|
|
|
|
case ICONSOLE_HOOK_AFTER_EXEC:
|
|
|
|
hook_cmd->hook_after_exec = proc;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_BEFORE_EXEC:
|
|
|
|
hook_cmd->hook_before_exec = proc;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_ACCESS:
|
|
|
|
hook_cmd->hook_access = proc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IConsoleCmdHookHandle(_iconsole_cmd * hook_cmd, byte type)
|
|
|
|
{
|
2004-09-12 22:03:14 +00:00
|
|
|
bool (*proc)(_iconsole_cmd * hook_cmd) = NULL;
|
2004-09-12 20:15:18 +00:00
|
|
|
switch (type) {
|
|
|
|
case ICONSOLE_HOOK_AFTER_EXEC:
|
|
|
|
proc = hook_cmd->hook_after_exec;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_BEFORE_EXEC:
|
|
|
|
proc = hook_cmd->hook_before_exec;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_ACCESS:
|
|
|
|
proc = hook_cmd->hook_access;
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 22:03:14 +00:00
|
|
|
|
2004-09-12 22:14:45 +00:00
|
|
|
if (proc == NULL) { return true;}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
return proc(hook_cmd);
|
|
|
|
}
|
|
|
|
|
2004-09-14 16:30:33 +00:00
|
|
|
void IConsoleCmdExec(const byte* cmdstr)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
|
|
|
_iconsole_var * (*function)(byte argc, byte* argv[], byte argt[]);
|
|
|
|
byte * tokens[20];
|
|
|
|
byte tokentypes[20];
|
|
|
|
byte * tokenstream;
|
|
|
|
byte * tokenstream_s;
|
|
|
|
byte execution_mode;
|
|
|
|
_iconsole_var * var = NULL;
|
|
|
|
_iconsole_var * result = NULL;
|
|
|
|
_iconsole_cmd * cmd = NULL;
|
|
|
|
|
|
|
|
bool longtoken;
|
|
|
|
bool valid_token;
|
|
|
|
bool skip_lt_change;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
int i;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
//** clearing buffer **//
|
|
|
|
|
|
|
|
for (i=0;i<20;i++) { tokens[i]=NULL; tokentypes[i]=ICONSOLE_VAR_NONE; };
|
|
|
|
tokenstream_s=tokenstream=malloc(1024);
|
|
|
|
memset(tokenstream,0,1024);
|
|
|
|
|
|
|
|
//** parsing **//
|
|
|
|
|
|
|
|
longtoken=false;
|
|
|
|
valid_token=false;
|
|
|
|
skip_lt_change=false;
|
2004-09-14 16:30:33 +00:00
|
|
|
l=strlen(cmdstr);
|
2004-09-12 20:15:18 +00:00
|
|
|
i=0;
|
|
|
|
c=0;
|
|
|
|
tokens[c] = tokenstream;
|
|
|
|
while (i<l) {
|
|
|
|
if (cmdstr[i]=='"') {
|
|
|
|
if (longtoken) {
|
|
|
|
if (cmdstr[i+1]=='"') {
|
|
|
|
i++;
|
|
|
|
*tokenstream = '"';
|
|
|
|
tokenstream++;
|
|
|
|
skip_lt_change=true;
|
|
|
|
} else {
|
|
|
|
longtoken=!longtoken;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
} else {
|
|
|
|
longtoken=!longtoken;
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
if (!skip_lt_change) {
|
|
|
|
if (!longtoken) {
|
|
|
|
if (valid_token) {
|
|
|
|
c++;
|
|
|
|
*tokenstream = 0;
|
|
|
|
tokenstream++;
|
|
|
|
tokens[c] = tokenstream;
|
|
|
|
valid_token = false;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
skip_lt_change=false;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else if ((!longtoken) && (cmdstr[i]==' ')) {
|
|
|
|
if (valid_token) {
|
|
|
|
c++;
|
|
|
|
*tokenstream = 0;
|
|
|
|
tokenstream++;
|
|
|
|
tokens[c] = tokenstream;
|
|
|
|
valid_token = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
valid_token=true;
|
|
|
|
*tokenstream = cmdstr[i];
|
2004-08-24 22:41:42 +00:00
|
|
|
tokenstream++;
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
i++;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
|
|
|
|
tokenstream--;
|
|
|
|
if (!(*tokenstream==0)) {
|
|
|
|
c++;
|
2004-08-24 22:41:42 +00:00
|
|
|
tokenstream++;
|
2004-09-12 20:15:18 +00:00
|
|
|
*tokenstream = 0;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
//** interpreting **//
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
for (i=0; i<c; i++) {
|
|
|
|
tokentypes[i]=ICONSOLE_VAR_UNKNOWN;
|
|
|
|
if (tokens[i]!=NULL) if (i>0) if (strlen((char *) tokens[i])>0) {
|
|
|
|
if (tokens[i][0]=='*') {
|
|
|
|
if ((i==2) && (tokentypes[1]==ICONSOLE_VAR_UNKNOWN) && (strcmp(tokens[1],"<<")==0)) {
|
|
|
|
// dont change the variable to an pointer if execution_mode 4 is being prepared
|
|
|
|
// this is used to assign one variable the value of the other one [token 0 and 2]
|
|
|
|
} else {
|
|
|
|
var = IConsoleVarGet(tokens[i]);
|
|
|
|
if (var!=NULL) {
|
|
|
|
tokens[i]=(byte *)var->addr;
|
|
|
|
tokentypes[i]=var->type;
|
|
|
|
}
|
2004-08-25 22:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
if (tokens[i]!=NULL) if (tokens[i][0]=='@') if (tokens[i][1]=='*') {
|
|
|
|
var = IConsoleVarGet(tokens[i]+1);
|
|
|
|
if (var!=NULL) {
|
|
|
|
tokens[i]=(byte *)var;
|
|
|
|
tokentypes[i]=ICONSOLE_VAR_REFERENCE;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
execution_mode=0;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
function = NULL;
|
|
|
|
cmd = IConsoleCmdGet(tokens[0]);
|
|
|
|
if (cmd != NULL) function = cmd->addr;
|
|
|
|
|
|
|
|
if (function != NULL) {
|
|
|
|
execution_mode=1; // this is a command
|
|
|
|
} else {
|
|
|
|
var = IConsoleVarGet(tokens[0]);
|
|
|
|
if (var != NULL) {
|
|
|
|
execution_mode=2; // this is a variable
|
|
|
|
if (c>2) if (strcmp(tokens[1],"<<")==0) {
|
|
|
|
// this is command to variable mode [normal]
|
2004-09-12 21:49:38 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
function = NULL;
|
|
|
|
cmd = IConsoleCmdGet(tokens[2]);
|
|
|
|
if (cmd != NULL) function = cmd->addr;
|
|
|
|
|
|
|
|
if (function != NULL) {
|
|
|
|
execution_mode=3;
|
|
|
|
} else {
|
|
|
|
result = IConsoleVarGet(tokens[2]);
|
|
|
|
if (result != NULL) {
|
|
|
|
execution_mode=4;
|
|
|
|
}
|
2004-08-25 22:11:41 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
//** executing **//
|
|
|
|
if (_stdlib_con_developer) IConsolePrintF(_iconsole_color_debug,"CONDEBUG: execution_mode: %i",execution_mode);
|
|
|
|
switch (execution_mode) {
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
// not found
|
|
|
|
IConsoleError("command or variable not found");
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_ACCESS)) {
|
|
|
|
// execution with command syntax
|
|
|
|
IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_BEFORE_EXEC);
|
|
|
|
result = function(c,tokens,tokentypes);
|
|
|
|
if (result!=NULL) {
|
|
|
|
IConsoleVarDump(result,"result");
|
|
|
|
IConsoleVarFree(result);
|
|
|
|
}
|
|
|
|
IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_AFTER_EXEC);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
// execution with variable syntax
|
|
|
|
if (IConsoleVarHookHandle(var,ICONSOLE_HOOK_ACCESS)) if ((c==2) || (c==3)) {
|
|
|
|
// ** variable modifications ** //
|
|
|
|
IConsoleVarHookHandle(var,ICONSOLE_HOOK_BEFORE_CHANGE);
|
|
|
|
switch (var->type) {
|
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
*(bool *)var->addr=(atoi((char *) tokens[2])!=0);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
*(bool *)var->addr=false;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
*(bool *)var->addr=!*(bool *)var->addr;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
*(bool *)var->addr=!*(bool *)var->addr;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
*(byte *)var->addr=atoi((char *) tokens[2]);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
*(byte *)var->addr=0;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
(*(byte *)var->addr)++;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
(*(byte *)var->addr)--;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
*(unsigned short *)var->addr=atoi((char *) tokens[2]);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
*(unsigned short *)var->addr=0;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
(*(unsigned short *)var->addr)++;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
(*(unsigned short *)var->addr)--;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT32:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
*(unsigned int *)var->addr=atoi((char *) tokens[2]);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
*(unsigned int *)var->addr=0;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
(*(unsigned int *)var->addr)++;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
(*(unsigned int *)var->addr)--;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
*(signed short *)var->addr=atoi((char *) tokens[2]);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
*(signed short *)var->addr=0;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
(*(signed short *)var->addr)++;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
(*(signed short *)var->addr)--;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT32:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
*(signed int *)var->addr=atoi((char *) tokens[2]);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
*(signed int *)var->addr=0;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
(*(signed int *)var->addr)++;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
(*(signed int *)var->addr)--;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_STRING:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
IConsoleVarSetString(var, tokens[2]);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
} else {
|
|
|
|
IConsoleVarSetString(var, "");
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_POINTER:
|
|
|
|
{
|
|
|
|
if (strcmp(tokens[1],"=")==0) {
|
|
|
|
if (c==3) {
|
|
|
|
if (tokentypes[2]==ICONSOLE_VAR_UNKNOWN) {
|
|
|
|
var->addr = (void *)atoi(tokens[2]);
|
|
|
|
} else {
|
|
|
|
var->addr = (void *)tokens[2];
|
|
|
|
}
|
|
|
|
IConsoleVarDump(var,NULL);
|
2004-08-24 22:41:42 +00:00
|
|
|
} else {
|
2004-09-12 20:15:18 +00:00
|
|
|
var->addr = NULL;
|
|
|
|
IConsoleVarDump(var,NULL);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"++")==0) {
|
|
|
|
var->addr = ((char *)var->addr)+1;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(tokens[1],"--")==0) {
|
|
|
|
var->addr = ((char *)var->addr)-1;;
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
else { IConsoleError("operation not supported"); }
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
IConsoleVarHookHandle(var,ICONSOLE_HOOK_AFTER_CHANGE);
|
|
|
|
}
|
|
|
|
if (c==1) {
|
|
|
|
// ** variable output ** //
|
|
|
|
IConsoleVarDump(var,NULL);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
// execute command with result or assign a variable
|
2004-09-12 21:49:38 +00:00
|
|
|
if (execution_mode==3) {
|
|
|
|
if (IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_ACCESS)) {
|
|
|
|
int i;
|
|
|
|
int diff;
|
|
|
|
void * temp;
|
|
|
|
byte temp2;
|
|
|
|
|
|
|
|
// tokenshifting
|
|
|
|
for (diff=0; diff<2; diff++) {
|
|
|
|
temp=tokens[0];
|
|
|
|
temp2=tokentypes[0];
|
|
|
|
for (i=1; i<20; i++) {
|
|
|
|
tokens[i-1]=tokens[i];
|
|
|
|
tokentypes[i-1]=tokentypes[i];
|
|
|
|
}
|
|
|
|
tokens[19]=temp;
|
|
|
|
tokentypes[19]=temp2;
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2004-09-12 21:49:38 +00:00
|
|
|
IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_BEFORE_EXEC);
|
|
|
|
result = function(c,tokens,tokentypes);
|
|
|
|
IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_AFTER_EXEC);
|
|
|
|
} else
|
|
|
|
execution_mode=255;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-08-25 22:11:41 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
if (IConsoleVarHookHandle(var,ICONSOLE_HOOK_ACCESS)) if (result!=NULL) {
|
|
|
|
if (result ->type != var -> type) {
|
2004-08-24 22:41:42 +00:00
|
|
|
IConsoleError("variable type missmatch");
|
2004-09-12 20:15:18 +00:00
|
|
|
} else {
|
|
|
|
IConsoleVarHookHandle(var,ICONSOLE_HOOK_BEFORE_CHANGE);
|
|
|
|
switch (result->type) {
|
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
|
|
|
{
|
|
|
|
(*(bool *)var->addr)=(*(bool *)result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
|
|
|
{
|
|
|
|
(*(byte *)var->addr)=(*(byte *)result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
|
|
|
{
|
|
|
|
(*(unsigned short *)var->addr)=(*(unsigned short *)result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT32:
|
|
|
|
{
|
|
|
|
(*(unsigned int *)var->addr)=(*(unsigned int *)result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
|
|
|
{
|
|
|
|
(*(signed short *)var->addr)=(*(signed short *)result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT32:
|
|
|
|
{
|
|
|
|
(*(signed int *)var->addr)=(*(signed int *)result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_POINTER:
|
|
|
|
{
|
|
|
|
var->addr=result->addr;
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_STRING:
|
|
|
|
{
|
|
|
|
IConsoleVarSetString(var,result->addr);
|
|
|
|
IConsoleVarDump(var,NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
IConsoleError("variable type missmatch");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
IConsoleVarHookHandle(var,ICONSOLE_HOOK_AFTER_CHANGE);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
if (execution_mode==3) {
|
|
|
|
IConsoleVarFree(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
2004-08-25 22:11:41 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// execution mode invalid
|
|
|
|
IConsoleError("invalid execution mode");
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
//** freeing the tokens **//
|
|
|
|
for (i=0;i<20;i++) tokens[i]=NULL;
|
|
|
|
free(tokenstream_s);
|
2004-08-24 22:41:42 +00:00
|
|
|
|
|
|
|
}
|