From: havoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Sun, 24 Jan 2010 17:02:53 +0000 (+0000)
Subject: don't store pointers for serverlist_viewlist, because they become
X-Git-Tag: xonotic-v0.1.0preview~230^2~625
X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=6cdadc176fb4ec59e84b28c80e463fa035fca11b;p=xonotic%2Fdarkplaces.git

don't store pointers for serverlist_viewlist, because they become
invalid after serverlist_cache is reallocated


git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9844 d7cf8633-e32d-0410-b094-e92efae38249
---

diff --git a/menu.c b/menu.c
index 754152cc..5634d212 100644
--- a/menu.c
+++ b/menu.c
@@ -4400,9 +4400,10 @@ static void M_ServerList_Draw (void)
 	{
 		for (n = start;n < end;n++)
 		{
+			serverlist_entry_t *entry = ServerList_GetViewEntry(n);
 			DrawQ_Fill(menu_x, menu_y + y, 640, 16, n == slist_cursor ? (0.5 + 0.2 * sin(realtime * M_PI)) : 0, 0, 0, 0.5, 0);
-			M_PrintColored(0, y, serverlist_viewlist[n]->line1);y += 8;
-			M_PrintColored(0, y, serverlist_viewlist[n]->line2);y += 8;
+			M_PrintColored(0, y, entry->line1);y += 8;
+			M_PrintColored(0, y, entry->line2);y += 8;
 		}
 	}
 	else if (realtime - masterquerytime > 10)
@@ -4456,7 +4457,7 @@ static void M_ServerList_Key(int k, int ascii)
 	case K_ENTER:
 		S_LocalSound ("sound/misc/menu2.wav");
 		if (serverlist_viewcount)
-			Cbuf_AddText(va("connect \"%s\"\n", serverlist_viewlist[slist_cursor]->info.cname));
+			Cbuf_AddText(va("connect \"%s\"\n", ServerList_GetViewEntry(slist_cursor)->info.cname));
 		break;
 
 	default:
diff --git a/mvm_cmds.c b/mvm_cmds.c
index 208ac895..07424f80 100644
--- a/mvm_cmds.c
+++ b/mvm_cmds.c
@@ -436,7 +436,7 @@ void VM_M_getserverliststring(void)
 		Con_Print("VM_M_getserverliststring: bad hostnr passed!\n");
 		return;
 	}
-	cache = serverlist_viewlist[hostnr];
+	cache = ServerList_GetViewEntry(hostnr);
 	switch( (int) PRVM_G_FLOAT(OFS_PARM0) ) {
 		case SLIF_CNAME:
 			PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( cache->info.cname );
@@ -494,7 +494,7 @@ void VM_M_getserverlistnumber(void)
 		Con_Print("VM_M_getserverliststring: bad hostnr passed!\n");
 		return;
 	}
-	cache = serverlist_viewlist[hostnr];
+	cache = ServerList_GetViewEntry(hostnr);
 	switch( (int) PRVM_G_FLOAT(OFS_PARM0) ) {
 		case SLIF_MAXPLAYERS:
 			PRVM_G_FLOAT( OFS_RETURN ) = cache->info.maxplayers;
diff --git a/netconn.c b/netconn.c
index 28c2318f..1a709058 100755
--- a/netconn.c
+++ b/netconn.c
@@ -148,7 +148,7 @@ serverlist_infofield_t serverlist_sortbyfield;
 int serverlist_sortflags;
 
 int serverlist_viewcount = 0;
-serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
+unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
 
 int serverlist_maxcachecount = 0;
 int serverlist_cachecount = 0;
@@ -185,7 +185,7 @@ static void _ServerList_ViewList_Helper_InsertBefore( int index, serverlist_entr
 	for( ; i > index ; i-- )
 		serverlist_viewlist[ i ] = serverlist_viewlist[ i - 1 ];
 
-	serverlist_viewlist[index] = entry;
+	serverlist_viewlist[index] = (int)(entry - serverlist_cache);
 }
 
 /// we suppose serverlist_viewcount to be valid, ie > 0
@@ -431,11 +431,11 @@ static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
 
 	// two special cases
 	// check whether to insert it as new first item
-	if( _ServerList_Entry_Compare( entry, serverlist_viewlist[0] ) ) {
+	if( _ServerList_Entry_Compare( entry, ServerList_GetViewEntry(0) ) ) {
 		_ServerList_ViewList_Helper_InsertBefore( 0, entry );
 		return;
 	} // check whether to insert it as new last item
-	else if( !_ServerList_Entry_Compare( entry, serverlist_viewlist[serverlist_viewcount - 1] ) ) {
+	else if( !_ServerList_Entry_Compare( entry, ServerList_GetViewEntry(serverlist_viewcount - 1) ) ) {
 		_ServerList_ViewList_Helper_InsertBefore( serverlist_viewcount, entry );
 		return;
 	}
@@ -445,7 +445,7 @@ static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
 	{
 		mid = (start + end) / 2;
 		// test the item that lies in the middle between start and end
-		if( _ServerList_Entry_Compare( entry, serverlist_viewlist[mid] ) )
+		if( _ServerList_Entry_Compare( entry, ServerList_GetViewEntry(mid) ) )
 			// the item has to be in the upper half
 			end = mid;
 		else
@@ -460,7 +460,7 @@ static void ServerList_ViewList_Remove( serverlist_entry_t *entry )
 	int i;
 	for( i = 0; i < serverlist_viewcount; i++ )
 	{
-		if (serverlist_viewlist[i] == entry)
+		if (ServerList_GetViewEntry(i) == entry)
 		{
 			_ServerList_ViewList_Helper_Remove(i);
 			break;
diff --git a/netconn.h b/netconn.h
index 9d4a912f..b0eb6da2 100755
--- a/netconn.h
+++ b/netconn.h
@@ -348,16 +348,23 @@ typedef struct serverlist_mask_s
 	serverlist_info_t info;
 } serverlist_mask_t;
 
+#define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
+#define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
+
 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
 
 extern serverlist_infofield_t serverlist_sortbyfield;
 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
 
+#if SERVERLIST_TOTALSIZE > 65536
+#error too many servers, change type of index array
+#endif
 extern int serverlist_viewcount;
-extern serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
+extern unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
 
 extern int serverlist_cachecount;
+extern serverlist_entry_t *serverlist_cache;
 
 extern qboolean serverlist_consoleoutput;