(svn r248) -Feature: console script files "exec myscript.file"
authordarkvater
Tue, 14 Sep 2004 16:10:20 +0000
changeset 247 1cbc32ff06eb
parent 246 de179e5f7c3d
child 248 bd894287e3f2
(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
console.c
console_cmds.c
network.c
scripts/on_client.scr
scripts/on_server.scr
--- a/console.c	Tue Sep 14 15:46:21 2004 +0000
+++ b/console.c	Tue Sep 14 16:10:20 2004 +0000
@@ -27,6 +27,7 @@
 // ** 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 @@
 	#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 @@
 {
 	_iconsole_inited=false;
 	IConsoleClear();
+	if (_iconsole_output_file!=NULL) fclose(_iconsole_output_file);
 }
 
 void IConsoleResize()
@@ -344,9 +347,19 @@
 {
 	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 @@
 {
 	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)
 {
--- a/console_cmds.c	Tue Sep 14 15:46:21 2004 +0000
+++ b/console_cmds.c	Tue Sep 14 16:10:20 2004 +0000
@@ -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 @@
 
 #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 @@
 		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 @@
 
 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 @@
 {
 	// stdlib
 	extern byte _stdlib_developer;
-	extern bool _stdlib_con_developer;
 
 #ifdef _DEBUG
 	IConsoleDebugLibRegister();
@@ -364,32 +434,34 @@
 	(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);
--- a/network.c	Tue Sep 14 15:46:21 2004 +0000
+++ b/network.c	Tue Sep 14 16:10:20 2004 +0000
@@ -1730,6 +1730,7 @@
 	_networking = NetworkConnect(b, port);
 	if (_networking) {
 		NetworkLobbyShutdown();
+		IConsoleCmdExec("exec scripts/on_client.scr 0");
 	} else {
 		if (_networking_override)
 			NetworkLobbyShutdown();
@@ -1760,6 +1761,8 @@
 	_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;
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/on_client.scr	Tue Sep 14 16:10:20 2004 +0000
@@ -0,0 +1,2 @@
+echo "Setting default network client settings..."
+*net_ready_ahead = 1
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/on_server.scr	Tue Sep 14 16:10:20 2004 +0000
@@ -0,0 +1,3 @@
+echo "Setting default network server settings..."
+*net_sync_freq = 4
+*net_client_timeout = 300;
\ No newline at end of file