summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordarkvater <darkvater@openttd.org>2004-09-14 16:10:20 +0000
committerdarkvater <darkvater@openttd.org>2004-09-14 16:10:20 +0000
commitd48ce392b2569c3d856caf4f069b9bffbf0de9cc (patch)
tree6a9cad412cda5d812b392d390d583b0f6bcf7e39
parent6d55489368c8f2324d0ff43d4caad60478902b58 (diff)
downloadopenttd-d48ce392b2569c3d856caf4f069b9bffbf0de9cc.tar.xz
(svn r248) -Feature: console script files "exec myscript.file"
-Feature: console logging (of debug messages with *developer = 2 and debug_level #) to text-files "script test.txt" -Feature: server and client are auto-executing "on_server.scr" and "on_client.scr" scripts
-rw-r--r--console.c27
-rw-r--r--console_cmds.c140
-rw-r--r--network.c3
-rw-r--r--scripts/on_client.scr2
-rw-r--r--scripts/on_server.scr3
5 files changed, 134 insertions, 41 deletions
diff --git a/console.c b/console.c
index f39ba552d..612c6dbf0 100644
--- a/console.c
+++ b/console.c
@@ -27,6 +27,7 @@ static byte _icursor_counter;
// ** stdlib ** //
byte _stdlib_developer=1;
bool _stdlib_con_developer=false;
+FILE * _iconsole_output_file;
// ** main console cmd buffer ** // sign_de: especialy for Celestar :D
static byte* _iconsole_cmdbuffer[20];
@@ -186,8 +187,9 @@ void IConsoleInit()
#if defined(WITH_REV)
extern char _openttd_revision[];
#endif
+ _iconsole_output_file = NULL;
_iconsole_color_default = 1;
- _iconsole_color_error = 3;
+ _iconsole_color_error = 3;
_iconsole_color_warning = 13;
_iconsole_color_debug = 5;
_iconsole_color_commands = 2;
@@ -231,6 +233,7 @@ void IConsoleFree()
{
_iconsole_inited=false;
IConsoleClear();
+ if (_iconsole_output_file!=NULL) fclose(_iconsole_output_file);
}
void IConsoleResize()
@@ -344,9 +347,19 @@ void CDECL IConsolePrintF(byte color_code, const char *s, ...)
{
va_list va;
char buf[1024];
+ int len;
+
va_start(va, s);
- vsprintf(buf, s, va);
+ len = vsprintf(buf, s, va);
va_end(va);
+
+ 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);
+ }
+
IConsolePrint(color_code, (byte *) &buf);
}
@@ -359,11 +372,11 @@ void IConsoleError(const byte* string)
{
if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_error, "ERROR: %s", string);
}
-
-void IConsoleWarning(const byte* string)
-{
- if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_warning, "WARNING: %s", string);
-}
+
+void IConsoleWarning(const byte* string)
+{
+ if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_warning, "WARNING: %s", string);
+}
void IConsoleCmdRegister(const byte * name, void * addr)
{
diff --git a/console_cmds.c b/console_cmds.c
index ca8873b60..2d336b52e 100644
--- a/console_cmds.c
+++ b/console_cmds.c
@@ -10,11 +10,20 @@
# define ENABLE_NETWORK
#endif
+
+// ** scriptfile handling ** //
+static FILE * _script_file;
+static bool _script_running;
+
// ** console command / variable defines ** //
+
#define DEF_CONSOLE_CMD(yyyy) static _iconsole_var * yyyy(byte argc, byte* argv[], byte argt[])
#define DEF_CONSOLE_CMD_HOOK(yyyy) static bool yyyy(_iconsole_cmd * hookcmd)
#define DEF_CONSOLE_VAR_HOOK(yyyy) static bool yyyy(_iconsole_var * hookvar)
+
+// ** supporting functions ** //
+
static int32 GetArgumentInteger(byte *arg)
{
int32 result;
@@ -125,10 +134,68 @@ DEF_CONSOLE_CMD(ConNetworkConnect)
#endif
+/* ******************************** */
+/* script file console commands */
+/* ******************************** */
+
+DEF_CONSOLE_CMD(ConExec)
+{
+ char cmd[1024];
+ bool doerror;
+
+ if (argc<2) return NULL;
+
+ doerror = true;
+ _script_file = fopen(argv[1],"rb");
+
+ if (_script_file == NULL) {
+ if (argc>2) if (atoi(argv[2])==0) doerror=false;
+ if (doerror) IConsoleError("script file not found");
+ return NULL;
+ }
+
+ _script_running = true;
+
+ while (!feof(_script_file) && _script_running) {
+
+ fgets((char *)&cmd, 1024, _script_file);
+
+ IConsoleCmdExec((byte *) &cmd);
+
+ }
+
+ _script_running = false;
+ fclose(_script_file);
+ return NULL;
+}
+
+DEF_CONSOLE_CMD(ConReturn)
+{
+ _script_running = false;
+ return NULL;
+}
+
/* **************************** */
/* default console commands */
/* **************************** */
+DEF_CONSOLE_CMD(ConScript)
+{
+ extern FILE* _iconsole_output_file;
+
+ if (_iconsole_output_file!=NULL) {
+ if (argc<2) return NULL;
+ IConsolePrintF(_iconsole_color_default,"file output complete");
+ fclose(_iconsole_output_file);
+ } else {
+ IConsolePrintF(_iconsole_color_default,"file output started to: %s",argv[1]);
+ _iconsole_output_file = fopen(argv[1],"ab");
+ if (_iconsole_output_file == NULL) IConsoleError("could not open file");
+ }
+ return NULL;
+}
+
+
DEF_CONSOLE_CMD(ConEcho)
{
if (argc<2) return NULL;
@@ -184,35 +251,35 @@ DEF_CONSOLE_CMD(ConInfoVar)
IConsolePrintF(_iconsole_color_default,"var_name: %s",item->name);
IConsolePrintF(_iconsole_color_default,"var_type: %i",item->type);
IConsolePrintF(_iconsole_color_default,"var_addr: %i",item->addr);
- if (item->_malloc) IConsolePrintF(_iconsole_color_default,"var_malloc: internal");
- else IConsolePrintF(_iconsole_color_default, "var_malloc: external");
- if (item->hook_access) IConsoleWarning("var_access hooked");
- if (item->hook_before_change) IConsoleWarning("var_before_change hooked");
+ if (item->_malloc) IConsolePrintF(_iconsole_color_default,"var_malloc: internal");
+ else IConsolePrintF(_iconsole_color_default, "var_malloc: external");
+ if (item->hook_access) IConsoleWarning("var_access hooked");
+ if (item->hook_before_change) IConsoleWarning("var_before_change hooked");
if (item->hook_after_change) IConsoleWarning("var_after_change hooked");
}
return NULL;
-}
-
-
-DEF_CONSOLE_CMD(ConInfoCmd)
-{
- if (argc<2) return NULL;
- if (argt[1]!=ICONSOLE_VAR_UNKNOWN) {
- IConsoleError("first argument has to be a command name");
- } else {
- _iconsole_cmd * item;
- item = IConsoleCmdGet(argv[1]);
- if (item==NULL) {
- IConsoleError("the given command was not found");
- return NULL;
- }
- IConsolePrintF(_iconsole_color_default,"cmd_name: %s",item->name);
- IConsolePrintF(_iconsole_color_default,"cmd_addr: %i",item->addr);
- if (item->hook_access) IConsoleWarning("cmd_access hooked");
- if (item->hook_before_exec) IConsoleWarning("cmd_before_exec hooked");
- if (item->hook_after_exec) IConsoleWarning("cmd_after_exec hooked");
- }
- return NULL;
+}
+
+
+DEF_CONSOLE_CMD(ConInfoCmd)
+{
+ if (argc<2) return NULL;
+ if (argt[1]!=ICONSOLE_VAR_UNKNOWN) {
+ IConsoleError("first argument has to be a command name");
+ } else {
+ _iconsole_cmd * item;
+ item = IConsoleCmdGet(argv[1]);
+ if (item==NULL) {
+ IConsoleError("the given command was not found");
+ return NULL;
+ }
+ IConsolePrintF(_iconsole_color_default,"cmd_name: %s",item->name);
+ IConsolePrintF(_iconsole_color_default,"cmd_addr: %i",item->addr);
+ if (item->hook_access) IConsoleWarning("cmd_access hooked");
+ if (item->hook_before_exec) IConsoleWarning("cmd_before_exec hooked");
+ if (item->hook_after_exec) IConsoleWarning("cmd_after_exec hooked");
+ }
+ return NULL;
}
DEF_CONSOLE_CMD(ConDebugLevel)
@@ -335,6 +402,10 @@ DEF_CONSOLE_CMD(ConListDumpVariables)
void IConsoleDebugLibRegister()
{
+ // stdlib
+ extern bool _stdlib_con_developer;
+
+ IConsoleVarRegister("con_developer",(void *) &_stdlib_con_developer,ICONSOLE_VAR_BOOLEAN);
IConsoleVarMemRegister("temp_bool",ICONSOLE_VAR_BOOLEAN);
IConsoleVarMemRegister("temp_int16",ICONSOLE_VAR_INT16);
IConsoleVarMemRegister("temp_int32",ICONSOLE_VAR_INT32);
@@ -356,7 +427,6 @@ void IConsoleStdLibRegister()
{
// stdlib
extern byte _stdlib_developer;
- extern bool _stdlib_con_developer;
#ifdef _DEBUG
IConsoleDebugLibRegister();
@@ -364,32 +434,34 @@ void IConsoleStdLibRegister()
(void)ConResetTile; // Silence warning, this is only used in _DEBUG
#endif
- // functions [please add them alphabeticaly]
+ // functions [please add them alphabeticaly]
#ifdef ENABLE_NETWORK
IConsoleCmdRegister("connect",ConNetworkConnect);
IConsoleCmdHook("connect",ICONSOLE_HOOK_ACCESS,ConCmdHookNoNetwork);
-#endif
+#endif
IConsoleCmdRegister("debug_level",ConDebugLevel);
IConsoleCmdRegister("dump_vars",ConListDumpVariables);
IConsoleCmdRegister("echo",ConEcho);
IConsoleCmdRegister("echoc",ConEchoC);
+ IConsoleCmdRegister("exec",ConExec);
IConsoleCmdRegister("exit",ConExit);
- IConsoleCmdRegister("help",ConHelp);
- IConsoleCmdRegister("info_cmd",ConInfoCmd);
+ IConsoleCmdRegister("help",ConHelp);
+ IConsoleCmdRegister("info_cmd",ConInfoCmd);
IConsoleCmdRegister("info_var",ConInfoVar);
- IConsoleCmdRegister("list_cmds",ConListCommands);
- IConsoleCmdRegister("list_vars",ConListVariables);
+ IConsoleCmdRegister("list_cmds",ConListCommands);
+ IConsoleCmdRegister("list_vars",ConListVariables);
IConsoleCmdRegister("printf",ConPrintF);
IConsoleCmdRegister("printfc",ConPrintFC);
IConsoleCmdRegister("quit",ConExit);
IConsoleCmdRegister("random",ConRandom);
IConsoleCmdRegister("resetengines",ConResetEngines);
IConsoleCmdHook("resetengines",ICONSOLE_HOOK_ACCESS,ConCmdHookNoNetwork);
+ IConsoleCmdRegister("return",ConReturn);
IConsoleCmdRegister("screenshot",ConScreenShot);
+ IConsoleCmdRegister("script",ConScript);
IConsoleCmdRegister("scrollto",ConScrollToTile);
// variables [please add them alphabeticaly]
- IConsoleVarRegister("con_developer",(void *) &_stdlib_con_developer,ICONSOLE_VAR_BOOLEAN);
IConsoleVarRegister("developer",(void *) &_stdlib_developer,ICONSOLE_VAR_BYTE);
#ifdef ENABLE_NETWORK
IConsoleVarRegister("net_client_timeout",&_network_client_timeout,ICONSOLE_VAR_UINT16);
diff --git a/network.c b/network.c
index 6d7cadab8..edfeeec12 100644
--- a/network.c
+++ b/network.c
@@ -1730,6 +1730,7 @@ bool NetworkCoreConnectGame(const byte* b, unsigned short port)
_networking = NetworkConnect(b, port);
if (_networking) {
NetworkLobbyShutdown();
+ IConsoleCmdExec("exec scripts/on_client.scr 0");
} else {
if (_networking_override)
NetworkLobbyShutdown();
@@ -1760,6 +1761,8 @@ bool NetworkCoreStartGame()
_networking = true;
NetworkGameFillDefaults(); // clears the network game info
_network_game.players_on++; // the serverplayer is online
+ // execute server initialization script
+ IConsoleCmdExec("exec scripts/on_server.scr 0");
return true;
}
diff --git a/scripts/on_client.scr b/scripts/on_client.scr
new file mode 100644
index 000000000..8a24f40b7
--- /dev/null
+++ b/scripts/on_client.scr
@@ -0,0 +1,2 @@
+echo "Setting default network client settings..."
+*net_ready_ahead = 1 \ No newline at end of file
diff --git a/scripts/on_server.scr b/scripts/on_server.scr
new file mode 100644
index 000000000..930ee6e46
--- /dev/null
+++ b/scripts/on_server.scr
@@ -0,0 +1,3 @@
+echo "Setting default network server settings..."
+*net_sync_freq = 4
+*net_client_timeout = 300; \ No newline at end of file