(svn r954) -Fix: [Console] Hook fixes (sign_de)
authortruelight
Sun, 05 Dec 2004 12:25:25 +0000
changeset 554 670b089d7772
parent 553 ee002e9b6155
child 555 eec6c0294435
(svn r954) -Fix: [Console] Hook fixes (sign_de)
-Add: [Console] Auto sort commands and variables (sign_de)
console.c
console.h
console_cmds.c
--- a/console.c	Sun Dec 05 11:40:58 2004 +0000
+++ b/console.c	Sun Dec 05 12:25:25 2004 +0000
@@ -439,6 +439,7 @@
 	char* _new;
 	_iconsole_cmd* item;
 	_iconsole_cmd* item_new;
+	_iconsole_cmd* item_before;
 
 	_new = strdup(name);
 
@@ -452,13 +453,39 @@
 	item_new->hook_after_exec = NULL;
 	item_new->hook_before_exec = NULL;
 
+	item_before = NULL;
 	item = _iconsole_cmds;
+
 	if (item == NULL) {
 		_iconsole_cmds = item_new;
 	} else {
-		while (item->_next != NULL) item = item->_next;
-		item->_next = item_new;
+		while ((item->_next != NULL) && (strcmp(item->name,item_new->name)<=0)) {
+			item_before = item;
+			item = item->_next;
+			}
+// insertion sort
+		if (item_before==NULL) {
+			if (strcmp(item->name,item_new->name)<=0) {
+				// appending
+				item ->_next = item_new;
+			} else {
+				// inserting as startitem
+				_iconsole_cmds = item_new;
+				item_new ->_next = item;
+			}
+		} else {
+			if (strcmp(item->name,item_new->name)<=0) {
+				// appending
+				item ->_next = item_new;
+			} else {
+				// inserting
+				item_new ->_next = item_before->_next;
+				item_before ->_next = item_new;
+			}
+		}
+// insertion sort end
 	}
+
 }
 
 _iconsole_cmd* IConsoleCmdGet(const char* name)
@@ -473,17 +500,58 @@
 	return NULL;
 }
 
+void IConsoleVarInsert(_iconsole_var* item_new, const char* name)
+{
+	_iconsole_var* item;
+	_iconsole_var* item_before;
+
+	item_new->_next = NULL;
+
+	item_new->name = malloc(strlen(name) + 2); /* XXX unchecked malloc */
+	sprintf(item_new->name, "%s", name);
+
+	item_before = NULL;
+	item = _iconsole_vars;
+
+	if (item == NULL) {
+		_iconsole_vars = item_new;
+	} else {
+		while ((item->_next != NULL) && (strcmp(item->name,item_new->name)<=0)) {
+			item_before = item;
+			item = item->_next;
+			}
+// insertion sort
+		if (item_before==NULL) {
+			if (strcmp(item->name,item_new->name)<=0) {
+				// appending
+				item ->_next = item_new;
+			} else {
+				// inserting as startitem
+				_iconsole_vars = item_new;
+				item_new ->_next = item;
+			}
+		} else {
+			if (strcmp(item->name,item_new->name)<=0) {
+				// appending
+				item ->_next = item_new;
+			} else {
+				// inserting
+				item_new ->_next = item_before->_next;
+				item_before ->_next = item_new;
+			}
+		}
+// insertion sort end
+	}
+}
+
 void IConsoleVarRegister(const char* name, void* addr, _iconsole_var_types type)
 {
-	_iconsole_var* item;
 	_iconsole_var* item_new;
 
 	item_new = malloc(sizeof(_iconsole_var)); /* XXX unchecked malloc */
 
-	item_new->name = malloc(strlen(name) + 2); /* XXX unchecked malloc */
-	sprintf(item_new->name, "%s", name);
+	item_new->_next = NULL;
 
-	item_new->_next = NULL;
 	switch (type) {
 		case ICONSOLE_VAR_BOOLEAN:
 			item_new->data.bool_ = addr;
@@ -511,20 +579,16 @@
 			error("unknown console variable type");
 			break;
 	}
+
+	IConsoleVarInsert(item_new, name);
+
 	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;
-	}
+	
 }
 
 void IConsoleVarMemRegister(const char* name, _iconsole_var_types type)
@@ -534,27 +598,6 @@
 	IConsoleVarInsert(item, name);
 }
 
-
-void IConsoleVarInsert(_iconsole_var* var, const char* name)
-{
-	_iconsole_var* item;
-
-	// disallow building variable rings
-	if (var->_next != NULL) return;
-
-	var->name = malloc(strlen(name) + 2); /* XXX unchecked malloc */
-	sprintf(var->name, "%s", name);
-
-	item = _iconsole_vars;
-	if (item == NULL) {
-		_iconsole_vars = var;
-	} else {
-		while (item->_next != NULL) item = item->_next;
-		item->_next = var;
-	}
-}
-
-
 _iconsole_var* IConsoleVarGet(const char* name)
 {
 	_iconsole_var* item;
--- a/console.h	Sun Dec 05 11:40:58 2004 +0000
+++ b/console.h	Sun Dec 05 12:25:25 2004 +0000
@@ -123,7 +123,7 @@
 
 void IConsoleVarRegister(const char* name, void* addr, _iconsole_var_types type);
 void IConsoleVarMemRegister(const char* name, _iconsole_var_types type);
-void IConsoleVarInsert(_iconsole_var* var, const char* name);
+void IConsoleVarInsert(_iconsole_var* item_new, const char* name);
 _iconsole_var* IConsoleVarGet(const char* name);
 _iconsole_var* IConsoleVarAlloc(_iconsole_var_types type);
 void IConsoleVarFree(_iconsole_var* var);
--- a/console_cmds.c	Sun Dec 05 11:40:58 2004 +0000
+++ b/console_cmds.c	Sun Dec 05 12:25:25 2004 +0000
@@ -52,6 +52,10 @@
 
 DEF_CONSOLE_VAR_HOOK(ConVarHookNoNetClient)
 {
+	if (!_network_available) {
+		IConsoleError("You can not use this command because there is no network available.");
+		return false;
+	}
 	if (!_network_server) {
 		IConsoleError("This variable only makes sense for a network server.");
 		return false;
@@ -61,7 +65,11 @@
 
 DEF_CONSOLE_CMD_HOOK(ConCmdHookNoNetClient)
 {
-	if (!_networking || !_network_server) {
+	if (!_network_available) {
+		IConsoleError("You can not use this command because there is no network available.");
+		return false;
+	}
+	if (!_network_server) {
 		IConsoleError("This command is only available for a network server.");
 		return false;
 	}
@@ -70,8 +78,12 @@
 
 DEF_CONSOLE_CMD_HOOK(ConCmdHookNoNetServer)
 {
-	if (!_networking || _network_server) {
-		IConsoleError("You can not use this command for you are a network-server.");
+	if (!_network_available) {
+		IConsoleError("You can not use this command because there is no network available.");
+		return false;
+	}
+	if (_network_server) {
+		IConsoleError("You can not use this command because you are a network-server.");
 		return false;
 	}
 	return true;
@@ -79,6 +91,10 @@
 
 DEF_CONSOLE_CMD_HOOK(ConCmdHookNeedNetwork)
 {
+	if (!_network_available) {
+		IConsoleError("You can not use this command because there is no network available.");
+		return false;
+	}
 	if (!_networking) {
 		IConsoleError("Not connected. Multiplayer only command.");
 		return false;
@@ -618,10 +634,12 @@
 
 void IConsoleDebugLibRegister()
 {
-	// stdlib
+	// debugging variables and functions
 	extern bool _stdlib_con_developer; /* XXX extern in .c */
 
 	IConsoleVarRegister("con_developer", &_stdlib_con_developer, ICONSOLE_VAR_BOOLEAN);
+	IConsoleVarMemRegister("temp_string", ICONSOLE_VAR_STRING);
+	IConsoleVarMemRegister("temp_string2", ICONSOLE_VAR_STRING);
 	IConsoleVarMemRegister("temp_bool", ICONSOLE_VAR_BOOLEAN);
 	IConsoleVarMemRegister("temp_int16", ICONSOLE_VAR_INT16);
 	IConsoleVarMemRegister("temp_int32", ICONSOLE_VAR_INT32);
@@ -629,8 +647,6 @@
 	IConsoleVarMemRegister("temp_uint16", ICONSOLE_VAR_UINT16);
 	IConsoleVarMemRegister("temp_uint16_2", ICONSOLE_VAR_UINT16);
 	IConsoleVarMemRegister("temp_uint32", ICONSOLE_VAR_UINT32);
-	IConsoleVarMemRegister("temp_string", ICONSOLE_VAR_STRING);
-	IConsoleVarMemRegister("temp_string2", ICONSOLE_VAR_STRING);
 	IConsoleCmdRegister("resettile", ConResetTile);
 }
 #endif
@@ -644,16 +660,7 @@
 	// stdlib
 	extern byte _stdlib_developer; /* XXX extern in .c */
 
-#ifdef _DEBUG
-	IConsoleDebugLibRegister();
-#endif
-
-	// functions [please add them alphabetically]
-#ifdef ENABLE_NETWORK
-	IConsoleCmdRegister("connect", ConNetworkConnect);
-	IConsoleCmdHook("connect", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetServer);
-	IConsoleCmdRegister("clients", ConNetworkClients);
-#endif /* ENABLE_NETWORK */
+	// default variables and functions
 	IConsoleCmdRegister("debug_level",  ConDebugLevel);
 	IConsoleCmdRegister("dump_vars",    ConListDumpVariables);
 	IConsoleCmdRegister("echo",         ConEcho);
@@ -665,22 +672,20 @@
 	IConsoleCmdRegister("info_var",     ConInfoVar);
 	IConsoleCmdRegister("list_cmds",    ConListCommands);
 	IConsoleCmdRegister("list_vars",    ConListVariables);
-#ifdef ENABLE_NETWORK
-	IConsoleCmdRegister("kick",         ConKick);
-	IConsoleCmdHook("kick", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
-	IConsoleCmdRegister("protect", ConProtect);
-	IConsoleCmdRegister("name",         ConClientName);
-#endif
 	IConsoleCmdRegister("newgame",         ConNewGame);
 	IConsoleCmdRegister("printf",       ConPrintF);
 	IConsoleCmdRegister("printfc",      ConPrintFC);
 	IConsoleCmdRegister("quit",         ConExit);
 	IConsoleCmdRegister("random",       ConRandom);
 	IConsoleCmdRegister("resetengines", ConResetEngines);
-#ifdef ENABLE_NETWORK
-	IConsoleCmdHook("resetengines", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetwork);
-#endif /* ENABLE_NETWORK */
 	IConsoleCmdRegister("return",     ConReturn);
+	IConsoleCmdRegister("screenshot", ConScreenShot);
+	IConsoleCmdRegister("script",     ConScript);
+	IConsoleCmdRegister("scrollto",   ConScrollToTile);
+
+	IConsoleVarRegister("developer", &_stdlib_developer, ICONSOLE_VAR_BYTE);
+
+	// networking variables and functions
 #ifdef ENABLE_NETWORK
 	IConsoleCmdRegister("say",        ConSay);
 	IConsoleCmdHook("say", ICONSOLE_HOOK_ACCESS, ConCmdHookNeedNetwork);
@@ -688,28 +693,31 @@
 	IConsoleCmdHook("say_player", ICONSOLE_HOOK_ACCESS, ConCmdHookNeedNetwork);
 	IConsoleCmdRegister("say_client", ConSayClient);
 	IConsoleCmdHook("say_client", ICONSOLE_HOOK_ACCESS, ConCmdHookNeedNetwork);
-#endif /* ENABLE_NETWORK */
-	IConsoleCmdRegister("screenshot", ConScreenShot);
-	IConsoleCmdRegister("script",     ConScript);
-	IConsoleCmdRegister("scrollto",   ConScrollToTile);
-#ifdef ENABLE_NETWORK
+	IConsoleCmdRegister("kick",         ConKick);
+	IConsoleCmdHook("kick", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
+	IConsoleCmdRegister("protect", ConProtect);
+	IConsoleCmdRegister("name",         ConClientName);
+	IConsoleCmdRegister("connect", ConNetworkConnect);
+	IConsoleCmdHook("connect", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetServer);
+	IConsoleCmdRegister("clients", ConNetworkClients);
 	IConsoleCmdRegister("setservername", ConSetServerName);
 	IConsoleCmdHook("setservername", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
 	IConsoleCmdRegister("setpassword", ConSetPassword);
 	IConsoleCmdHook("setpassword", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
 	IConsoleCmdRegister("status",   ConStatus);
 	IConsoleCmdHook("status", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
+	IConsoleCmdHook("resetengines", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetwork);
+
+	IConsoleVarRegister("net_frame_freq", &_network_frame_freq, ICONSOLE_VAR_UINT8);
+	IConsoleVarHook("net_frame_freq", ICONSOLE_HOOK_ACCESS, ConVarHookNoNetClient);
+	IConsoleVarRegister("net_sync_freq", &_network_sync_freq, ICONSOLE_VAR_UINT16);
+	IConsoleVarHook("net_sync_freq", ICONSOLE_HOOK_ACCESS, ConVarHookNoNetClient);
 #endif /* ENABLE_NETWORK */
 
-	// variables [please add them alphabeticaly]
-	IConsoleVarRegister("developer", &_stdlib_developer, ICONSOLE_VAR_BYTE);
-#ifdef ENABLE_NETWORK
-	IConsoleVarRegister("net_frame_freq", &_network_frame_freq, ICONSOLE_VAR_UINT8);
-	IConsoleVarHook("*net_frame_freq", ICONSOLE_HOOK_ACCESS, ConVarHookNoNetClient);
-	IConsoleVarRegister("net_sync_freq", &_network_sync_freq, ICONSOLE_VAR_UINT16);
-	IConsoleVarHook("*net_sync_freq", ICONSOLE_HOOK_ACCESS, ConVarHookNoNetClient);
-#endif /* ENABLE_NETWORK */
-
+	// debugging stuff
+#ifdef _DEBUG
+	IConsoleDebugLibRegister();
+#endif
 
 }
 /* -------------------- don't cross this line --------------------- */