core/GameDataFetcher.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 14 09:18:30 2008 -0600 (2008-11-14)
changeset 2400 76b7b4575e21
parent 2371 323fbf1ab608
child 2479 44e8e76d4e53
permissions -rw-r--r--
Added preliminary support for Left 4 Dead; some things may not yet work.
     1 /**
     2 * vim: set ts=4 :
     3 * =============================================================================
     4 * SourceMod
     5 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6 * =============================================================================
     7 *
     8 * This program is free software; you can redistribute it and/or modify it under
     9 * the terms of the GNU General Public License, version 3.0, as published by the
    10 * Free Software Foundation.
    11 * 
    12 * This program is distributed in the hope that it will be useful, but WITHOUT
    13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15 * details.
    16 *
    17 * You should have received a copy of the GNU General Public License along with
    18 * this program.  If not, see <http://www.gnu.org/licenses/>.
    19 *
    20 * As a special exception, AlliedModders LLC gives you permission to link the
    21 * code of this program (as well as its derivative works) to "Half-Life 2," the
    22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23 * by the Valve Corporation.  You must obey the GNU General Public License in
    24 * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25 * this exception to all derivative works.  AlliedModders LLC defines further
    26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27 * or <http://www.sourcemod.net/license.php>.
    28 *
    29 * Version: $Id$
    30 */
    31 
    32 #include "GameDataFetcher.h"
    33 #include "bitbuf.h"
    34 
    35 #ifdef PLATFORM_WINDOWS
    36 #include <winsock2.h>
    37 #else
    38 #include <unistd.h>
    39 #include <sys/types.h>
    40 #include <sys/socket.h>
    41 #include <netinet/in.h>
    42 #include <netdb.h>
    43 #include <arpa/inet.h>
    44 
    45 #define INVALID_SOCKET -1
    46 #define closesocket close
    47 #endif
    48 
    49 #include "sh_vector.h"
    50 #include "sh_string.h"
    51 #include "sm_version.h"
    52 
    53 #if SOURCE_ENGINE == SE_LEFT4DEAD
    54 #include "convar_sm_l4d.h"
    55 #elif SOURCE_ENGINE == SE_ORANGEBOX
    56 #include "convar_sm_ob.h"
    57 #else
    58 #include "convar_sm.h"
    59 #endif
    60 
    61 #include "sourcemm_api.h"
    62 #include "time.h"
    63 #include "TimerSys.h"
    64 #include "compat_wrappers.h"
    65 #include "sm_stringutil.h"
    66 
    67 #define QUERY_MAX_LENGTH 1024
    68 
    69 BuildMD5ableBuffer g_MD5Builder;
    70 FetcherThread g_FetchThread;
    71 
    72 FILE *logfile = NULL;
    73 
    74 bool g_disableGameDataUpdate = false;
    75 bool g_restartAfterUpdate = false;
    76 
    77 int g_serverPort = 6500;
    78 char g_serverAddress[100] = "hayate.alliedmods.net";
    79 
    80 void FetcherThread::RunThread( IThreadHandle *pHandle )
    81 {
    82 	char lock_path[PLATFORM_MAX_PATH];
    83 	g_SourceMod.BuildPath(Path_SM, lock_path, sizeof(lock_path), "data/temp");
    84 	g_LibSys.CreateFolder(lock_path);
    85 
    86 	g_SourceMod.BuildPath(Path_SM, lock_path, sizeof(lock_path), "data/temp/gamedata.lock");
    87 
    88 	g_Logger.LogMessage("Starting Gamedata update fetcher... please report problems to bugs.alliedmods.net");
    89 
    90 	char log_path[PLATFORM_MAX_PATH];
    91 	g_SourceMod.BuildPath(Path_SM, log_path, sizeof(log_path), "logs/gamedata");
    92 
    93 	g_LibSys.CreateFolder(log_path);
    94 
    95 	time_t t;
    96 	GetAdjustedTime(&t);
    97 	tm *curtime = localtime(&t);
    98 
    99 	g_SourceMod.BuildPath(Path_SM, log_path, sizeof(log_path), "logs/gamedata/L%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
   100 
   101 	logfile = fopen(log_path, "a");
   102 
   103 	if (!logfile)
   104 	{
   105 		g_Logger.LogError("Failed to create GameData log file");
   106 		return;
   107 	}
   108 
   109 	//Create a blank lock file
   110 	FILE *fp = fopen(lock_path, "w");
   111 	if (fp)
   112 	{
   113 		fclose(fp);
   114 	}
   115 
   116 	char query[QUERY_MAX_LENGTH];
   117 
   118 	/* Check for updated gamedata files */
   119 	int len = BuildGameDataQuery(query, QUERY_MAX_LENGTH);
   120 
   121 	if (len == 0)
   122 	{
   123 		g_Logger.LogToOpenFile(logfile, "Query Writing failed");
   124 
   125 		fclose(logfile);
   126 		unlink(lock_path);
   127 		return;
   128 	}
   129 
   130 	if (g_disableGameDataUpdate)
   131 	{
   132 #ifdef DEBUG
   133 		g_Logger.LogMessage("Skipping GameData Query due to DisableAutoUpdate being set to true");
   134 #endif
   135 
   136 		fclose(logfile);
   137 		unlink(lock_path);
   138 		return;
   139 	}
   140 
   141 	/* Create a new socket for this connection */
   142 	int socketDescriptor = ConnectSocket();
   143 
   144 	if (socketDescriptor == INVALID_SOCKET)
   145 	{
   146 		fclose(logfile);
   147 		unlink(lock_path);
   148 		return;
   149 	}
   150 
   151 	int sent = SendData(socketDescriptor, query, len);
   152 
   153 #ifdef DEBUG
   154 	g_Logger.LogToOpenFile(logfile, "Sent Query!");
   155 #endif
   156 
   157 	if (sent == 0)
   158 	{
   159 		g_Logger.LogToOpenFile(logfile, "Failed to send gamedata query data to remote host");
   160 
   161 		closesocket(socketDescriptor);
   162 		fclose(logfile);
   163 		unlink(lock_path);
   164 		return;
   165 	}
   166 
   167 	ProcessGameDataQuery(socketDescriptor);
   168 
   169 	/* And we're done! */
   170 	closesocket(socketDescriptor);
   171 	fclose(logfile);
   172 	unlink(lock_path);
   173 }
   174 
   175 void FetcherThread::OnTerminate( IThreadHandle *pHandle, bool cancel )
   176 {
   177 	g_blockGameDataLoad = false;
   178 }
   179 
   180 int FetcherThread::BuildGameDataQuery( char *buffer, int maxlen )
   181 {
   182 	char gamedata_path[PLATFORM_MAX_PATH];
   183 	g_SourceMod.BuildPath(Path_SM, gamedata_path, sizeof(gamedata_path), "gamedata");
   184 
   185 	IDirectory *dir = g_LibSys.OpenDirectory(gamedata_path);
   186 
   187 	if (dir == NULL)
   188 	{
   189 		return 0;
   190 	}
   191 
   192 	bf_write Writer = bf_write("GameDataQuery", buffer, maxlen);
   193 
   194 	Writer.WriteByte('A'); //Generic Header char
   195 	Writer.WriteByte('G'); //G for gamedata query, or green, like my hat.
   196 
   197 	short build[4] = { SVN_FILE_VERSION };
   198 
   199 	Writer.WriteBytes(&build[0], 8);
   200 
   201 	Writer.WriteByte(0); // Initialize the file counter - Index 10
   202 
   203 	while (dir->MoreFiles())
   204 	{
   205 		if (dir->IsEntryFile())
   206 		{
   207 			const char *name = dir->GetEntryName();
   208 			size_t len = strlen(name);
   209 			if (len >= 4
   210 				&& strcmp(&name[len-4], ".txt") == 0)
   211 			{
   212 				char file[PLATFORM_MAX_PATH];
   213 
   214 				g_LibSys.PathFormat(file, sizeof(file), "%s/%s", gamedata_path, name);
   215 
   216 				SMCStates states;
   217 				if (g_TextParser.ParseFile_SMC(file, &g_MD5Builder, &states) == SMCError_Okay)
   218 				{
   219 					unsigned char *md5 = g_MD5Builder.GetMD5();
   220 					if (md5 != NULL)
   221 					{
   222 						(uint8_t)buffer[10]++; //Increment the file counter
   223 						Writer.WriteBytes(md5, 16);
   224 
   225 						g_Logger.LogToOpenFile(logfile, "%s - \"%s\"", file, g_MD5Builder.GetMD5String());
   226 
   227 						FileData *data = new FileData();
   228 						data->filename = new SourceHook::String(file);
   229 						memcpy(data->checksum, g_MD5Builder.GetMD5String(), 33);
   230 						filenames.push_back(data);
   231 					}
   232 					else
   233 					{
   234 #ifdef DEBUG
   235 						g_Logger.LogToOpenFile(logfile, "%s no md5?", file);
   236 #endif
   237 					}
   238 				}
   239 				else
   240 				{
   241 #ifdef DEBUG
   242 					g_Logger.LogToOpenFile(logfile, "%s failed!", file);
   243 #endif
   244 				}
   245 			}
   246 		}
   247 
   248 		dir->NextEntry();
   249 	}
   250 
   251 	return Writer.GetNumBytesWritten();
   252 }
   253 
   254 int FetcherThread::ConnectSocket()
   255 {
   256 	struct protoent *ptrp;
   257 	ptrp = getprotobyname("tcp");
   258 
   259 #ifdef WIN32
   260 	WSADATA wsaData;
   261 	WSAStartup(0x0101, &wsaData);
   262 #endif
   263 
   264 	if (ptrp == NULL) 
   265 	{
   266 		g_Logger.LogToOpenFile(logfile, "Failed to find TCP");
   267 		return INVALID_SOCKET;
   268 	}
   269 
   270 	int socketDescriptor = socket(AF_INET, SOCK_STREAM, ptrp->p_proto);
   271 
   272 	if (socketDescriptor == INVALID_SOCKET)
   273 	{
   274 		//bugger aye?
   275 #ifdef WIN32
   276 		g_Logger.LogToOpenFile(logfile, "Failed to create a new socket - Error %i", WSAGetLastError());
   277 #else
   278 		g_Logger.LogToOpenFile(logfile, "Failed to create a new socket - Error %i", errno);
   279 #endif
   280 		closesocket(socketDescriptor);
   281 		return INVALID_SOCKET;
   282 	}
   283 
   284 	struct hostent *he;
   285 	struct sockaddr_in local_addr;
   286 
   287 	local_addr.sin_family = AF_INET;
   288 	local_addr.sin_port = htons((u_short)g_serverPort);
   289 
   290 	he = gethostbyname(g_serverAddress);
   291 
   292 	if (!he)
   293 	{
   294 		if ((local_addr.sin_addr.s_addr = inet_addr(g_serverAddress)) == INADDR_NONE)
   295 		{
   296 			g_Logger.LogToOpenFile(logfile, "Couldn't locate address");
   297 			closesocket(socketDescriptor);
   298 			return INVALID_SOCKET;
   299 		}
   300 	} 
   301 	else 
   302 	{
   303 		memcpy(&local_addr.sin_addr, (struct in_addr *)he->h_addr, he->h_length);
   304 	}
   305 
   306 	if (connect(socketDescriptor, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0)
   307 	{
   308 #ifdef WIN32
   309 		g_Logger.LogToOpenFile(logfile, "Couldn't connect - Error %i", WSAGetLastError());
   310 #else
   311 		g_Logger.LogToOpenFile(logfile, "Couldn't connect - Error %i", errno);
   312 #endif
   313 		closesocket(socketDescriptor);
   314 		return INVALID_SOCKET;
   315 	}
   316 
   317 	return socketDescriptor;
   318 }
   319 
   320 void FetcherThread::ProcessGameDataQuery(int socketDescriptor)
   321 {
   322 	char buffer[50];
   323 #ifdef DEBUG
   324 	g_Logger.LogToOpenFile(logfile, "Waiting for reply!");
   325 #endif
   326 
   327 	//Read in the header bytes
   328 	int returnLen = RecvData(socketDescriptor, buffer, 12);
   329 
   330 #ifdef DEBUG
   331 	g_Logger.LogToOpenFile(logfile, "Recv Completed");
   332 #endif
   333 
   334 	if (returnLen == 0)
   335 	{
   336 #ifdef DEBUG
   337 		g_Logger.LogToOpenFile(logfile, ",but it failed.");
   338 #endif
   339 		/* Timeout or fail? */
   340 		return;
   341 	}
   342 
   343 #ifdef DEBUG
   344 	g_Logger.LogToOpenFile(logfile, "Received Header!");
   345 #endif
   346 
   347 	bf_read Reader = bf_read("GameDataQuery", buffer, 12);
   348 
   349 	if (Reader.ReadByte() != 'A' || Reader.ReadByte() != 'G')
   350 	{
   351 		g_Logger.LogToOpenFile(logfile, "Unknown Query Response");
   352 		return;
   353 	}
   354 
   355 	UpdateStatus updateStatus = (UpdateStatus)Reader.ReadByte();
   356 
   357 	short build[4] = {0,0,0,0};
   358 
   359 	build[0] = Reader.ReadShort();
   360 	build[1] = Reader.ReadShort();
   361 	build[2] = Reader.ReadShort();
   362 	build[3] = Reader.ReadShort();
   363 
   364 #ifdef DEBUG
   365 	g_Logger.LogToOpenFile(logfile, "Update Status: %i - Latest %i.%i.%i.%i", updateStatus, build[0], build[1], build[2], build[3]);
   366 #endif
   367 
   368 	HandleUpdateStatus(updateStatus, build);
   369 
   370 	int changedFiles = Reader.ReadByte();
   371 
   372 #ifdef DEBUG
   373 	g_Logger.LogToOpenFile(logfile, "Files to download: %i", changedFiles);
   374 #endif
   375 
   376 	for (int i=0; i<changedFiles; i++)
   377 	{
   378 		//Read in the file index and byte count
   379 		returnLen = RecvData(socketDescriptor, buffer, 5);
   380 
   381 		if (returnLen == 0)
   382 		{
   383 			/* Timeout or fail? */
   384 			return;
   385 		}
   386 
   387 		Reader.StartReading(buffer, 5);
   388 
   389 		int index = Reader.ReadByte();
   390 		int tempLen = Reader.ReadUBitLong(32);
   391 
   392 #ifdef DEBUG
   393 		g_Logger.LogToOpenFile(logfile, "File index %i and length %i", index, tempLen);
   394 #endif
   395 
   396 		void *memPtr;
   397 		memtable->CreateMem(tempLen+1, &memPtr);
   398 
   399 		//Read the contents of our file into the memtable
   400 		returnLen = RecvData(socketDescriptor, (char *)memPtr, tempLen);
   401 
   402 #ifdef DEBUG
   403 		g_Logger.LogToOpenFile(logfile, "Recieved %i bytes", returnLen);
   404 #endif
   405 
   406 		if (returnLen == 0)
   407 		{
   408 			/* Timeout or fail? */
   409 			return;
   410 		}
   411 
   412 		((unsigned char *)memPtr)[tempLen] = '\0';
   413 
   414 		FileData *data = filenames.at(index);
   415 		const char* filename;
   416 		if (data != NULL)
   417 		{
   418 			filename = data->filename->c_str();
   419 
   420 			FILE *fp = fopen(filename, "w");
   421 
   422 			if (fp)
   423 			{
   424 				fprintf(fp, (const char *)memPtr);
   425 				fclose(fp);
   426 			}
   427 			else
   428 			{
   429 				g_Logger.LogToOpenFile(logfile, "Failed to open file \"%s\"", filename);
   430 			}
   431 		}
   432 		else
   433 		{
   434 			filename = "";
   435 		}
   436 
   437 		memtable->Reset();
   438 
   439 #ifdef DEBUG
   440 		g_Logger.LogToOpenFile(logfile, "Updated File %s", filename);
   441 #endif
   442 	}
   443 
   444 #ifdef DEBUG
   445 	g_Logger.LogToOpenFile(logfile, "File Downloads Completed!");
   446 #endif
   447 
   448 	bool needsRestart = false;
   449 
   450 	if (changedFiles > 0)
   451 	{
   452 		needsRestart = true;
   453 		g_Logger.LogMessage("New GameData Files have been downloaded to your gamedata directory. Please restart your server for these to take effect");
   454 	}
   455 
   456 	//Read changed file count
   457 	returnLen = RecvData(socketDescriptor, buffer, 1);
   458 
   459 	if (returnLen == 0)
   460 	{
   461 		/* Timeout or fail? */
   462 		g_Logger.LogToOpenFile(logfile, "Failed to receive unknown count");
   463 		return;
   464 	}
   465 
   466 	Reader.StartReading(buffer, 1);
   467 
   468 	changedFiles = Reader.ReadByte();
   469 
   470 	if (changedFiles == 0)
   471 	{
   472 #ifdef DEBUG
   473 		g_Logger.LogToOpenFile(logfile, "No unknown files. We're all done");
   474 #endif
   475 		return;
   476 	}
   477 
   478 	char *changedFileIndexes = new char[changedFiles];
   479 
   480 #ifdef DEBUG
   481 	g_Logger.LogToOpenFile(logfile, "%i Files were unknown", changedFiles);
   482 #endif
   483 
   484 	returnLen = RecvData(socketDescriptor, changedFileIndexes, changedFiles);
   485 
   486 	if (returnLen == 0)
   487 	{
   488 		/* Timeout or fail? */
   489 #ifdef DEBUG
   490 		g_Logger.LogToOpenFile(logfile, "Failed to receive unknown list");
   491 #endif
   492 		return;
   493 	}
   494 
   495 	Reader.StartReading(changedFileIndexes, changedFiles);
   496 
   497 	for (int i=0; i<changedFiles; i++)
   498 	{
   499 		int index = Reader.ReadByte();
   500 		char fileName[30];
   501 
   502 		FileData *data = filenames.at(index);
   503 		const char* pathname;
   504 		if (data != NULL)
   505 		{
   506 			pathname = data->filename->c_str();
   507 		}
   508 		else
   509 		{
   510 			pathname = "";
   511 		}
   512 
   513 		g_LibSys.GetFileFromPath(fileName, sizeof(fileName), pathname);
   514 #ifdef DEBUG
   515 		g_Logger.LogToOpenFile(logfile, "Unknown File %i : %s", index, fileName);
   516 #endif
   517 	}
   518 
   519 	delete [] changedFileIndexes;
   520 
   521 	if (needsRestart && g_restartAfterUpdate)
   522 	{
   523 		g_Logger.LogMessage("Automatically restarting server after a successful gamedata update!");
   524 		engine->ServerCommand("quit\n");
   525 	}
   526 }
   527 
   528 int FetcherThread::RecvData( int socketDescriptor, char *buffer, int len )
   529 {
   530 	fd_set fds;
   531 	struct timeval tv;
   532 
   533 	/* Create a 10 Second Timeout */
   534 	tv.tv_sec = 10;
   535 	tv.tv_usec = 0;
   536 
   537 	int bytesReceivedTotal = 0;
   538 
   539 	while (bytesReceivedTotal < len)
   540 	{
   541 		/* Add our socket to a socket set */
   542 		FD_ZERO(&fds);
   543 		FD_SET(socketDescriptor, &fds);
   544 
   545 		/* Wait max of 10 seconds for recv to become available */
   546 		select(socketDescriptor+1, &fds, NULL, NULL, &tv);
   547 
   548 		int bytesReceived = 0;
   549 
   550 		/* Is there a limit on how much we can receive? Some site said 1024 bytes, which will be well short of a file */
   551 		if (FD_ISSET(socketDescriptor, &fds))
   552 		{
   553 			bytesReceived = recv(socketDescriptor, buffer+bytesReceivedTotal, len-bytesReceivedTotal, 0);
   554 		}
   555 
   556 		if (bytesReceived == 0 || bytesReceived == -1)
   557 		{
   558 			return 0;
   559 		}
   560 
   561 		bytesReceivedTotal += bytesReceived;
   562 	}
   563 
   564 	return bytesReceivedTotal;	
   565 }
   566 
   567 int FetcherThread::SendData( int socketDescriptor, char *buffer, int len )
   568 {
   569 	fd_set fds;
   570 	struct timeval tv;
   571 
   572 	tv.tv_sec = 10;
   573 	tv.tv_usec = 0;
   574 
   575 	int sentBytesTotal = 0;
   576 
   577 	while (sentBytesTotal < len)
   578 	{
   579 		FD_ZERO(&fds);
   580 		FD_SET(socketDescriptor, &fds);
   581 
   582 		select(socketDescriptor+1, NULL, &fds, NULL, &tv);
   583 
   584 		int sentBytes = 0;
   585 
   586 		if (FD_ISSET(socketDescriptor, &fds))
   587 		{
   588 			sentBytes = send(socketDescriptor, buffer+sentBytesTotal, len-sentBytesTotal, 0);
   589 		}
   590 
   591 		if (sentBytes == 0 || sentBytes == -1)
   592 		{
   593 			return 0;
   594 		}		
   595 
   596 		sentBytesTotal += sentBytes;
   597 	}
   598 
   599 	return sentBytesTotal;
   600 }
   601 
   602 void FetcherThread::HandleUpdateStatus( UpdateStatus status, short version[4] )
   603 {
   604 	switch (status)
   605 	{
   606 		case Update_Unknown:
   607 		case Update_Current:
   608 		{
   609 			break;
   610 		}
   611 
   612 		case Update_NewBuild:
   613 		{
   614 			g_Logger.LogMessage("SourceMod Update: A new Mercurial build is available from sourcemod.net");
   615 			g_Logger.LogMessage("Current Version: %i.%i.%i.%i Available: %i.%i.%i.%i", version[0], version[1], version[2], version[3], version[0], version[1], version[2], version[3]);
   616 			break;
   617 		}
   618 
   619 		case Update_MinorAvailable:
   620 		{
   621 			g_Logger.LogMessage("SourceMod Update: An incremental minor release of SourceMod is now available from sourcemod.net");
   622 			g_Logger.LogMessage("Current Version: %i.%i.%i Available: %i.%i.%i", version[0], version[1], version[2], version[0], version[1], version[2]);
   623 			break;
   624 		}
   625 
   626 		case Update_MajorAvailable:
   627 		{
   628 			g_Logger.LogMessage("SourceMod Update: An major release of SourceMod is now available from sourcemod.net");
   629 			g_Logger.LogMessage("Current Version: %i.%i.%i Available: %i.%i.%i", version[0], version[1], version[2], version[0], version[1], version[2]);
   630 			break;
   631 		}
   632 
   633 		case Update_CriticalAvailable:
   634 		{
   635 			g_Logger.LogError("SourceMod Update: A new critical release of SourceMod is now available from sourcemod.net. It is strongly recommended that you update");
   636 			g_Logger.LogMessage("Current Version: %i.%i.%i.%i Available: %i.%i.%i.%i", version[0], version[1], version[2], version[3], version[0], version[1], version[2], version[3]);
   637 			break;
   638 		}
   639 	}
   640 }
   641 
   642 bool g_blockGameDataLoad = false;
   643 
   644 class InitFetch : public SMGlobalClass
   645 {
   646 public:
   647 	void OnSourceModAllInitialized_Post()
   648 	{
   649 		char lock_path[PLATFORM_MAX_PATH];
   650 		g_SourceMod.BuildPath(Path_SM, lock_path, sizeof(lock_path), "data/temp/gamedata.lock");
   651 
   652 		if (g_LibSys.IsPathFile(lock_path) && g_LibSys.PathExists(lock_path))
   653 		{
   654 			g_Logger.LogError("sourcemod/data/temp/gamedata.lock file detected. This is most likely due to a crash during GameData updating - Blocking GameData loading");
   655 			g_Logger.LogError("If this error persists delete the file manually");
   656 			g_blockGameDataLoad = true;
   657 		}
   658 
   659 		ThreadParams fetchThreadParams = ThreadParams();
   660 		fetchThreadParams.prio = ThreadPrio_Low;
   661 		g_pThreader->MakeThread(&g_FetchThread, &fetchThreadParams);
   662 	}
   663 
   664 	ConfigResult OnSourceModConfigChanged(const char *key, 
   665 		const char *value, 
   666 		ConfigSource source,
   667 		char *error, 
   668 		size_t maxlength)
   669 	{
   670 		if (strcmp(key, "DisableAutoUpdate") == 0)
   671 		{
   672 			if (strcmp(value, "yes") == 0)
   673 			{
   674 				g_disableGameDataUpdate = true;
   675 				return ConfigResult_Accept;
   676 			}
   677 			else if (strcmp(value, "no") == 0)
   678 			{
   679 				g_disableGameDataUpdate = false;
   680 				return ConfigResult_Accept;
   681 			}
   682 
   683 			return ConfigResult_Reject;
   684 		}
   685 
   686 		if (strcmp(key, "ForceRestartAfterUpdate") == 0)
   687 		{
   688 			if (strcmp(value, "yes") == 0)
   689 			{
   690 				g_restartAfterUpdate = true;
   691 				return ConfigResult_Accept;
   692 			}
   693 			else if (strcmp(value, "no") == 0)
   694 			{
   695 				g_restartAfterUpdate = false;
   696 				return ConfigResult_Accept;
   697 			}
   698 
   699 			return ConfigResult_Reject;
   700 		}
   701 
   702 		if (strcmp(key, "AutoUpdateServer") == 0)
   703 		{
   704 			UTIL_Format(g_serverAddress, sizeof(g_serverAddress), "%s", value);
   705 
   706 			return ConfigResult_Accept;
   707 		}
   708 
   709 		if (strcmp(key, "AutoUpdatePort") == 0)
   710 		{
   711 			int port = atoi(value);
   712 
   713 			if (!port)
   714 			{
   715 				return ConfigResult_Reject;
   716 			}
   717 
   718 			g_serverPort = port;
   719 
   720 			return ConfigResult_Accept;
   721 		}
   722 
   723 		return ConfigResult_Ignore;
   724 	}
   725 } g_InitFetch;
   726 
   727 CON_COMMAND(sm_gamedata_md5, "Checks the MD5 sum for a given gamedata file")
   728 {
   729 #if SOURCE_ENGINE == SE_EPISODEONE
   730 	CCommand args;
   731 #endif
   732 
   733 	if (args.ArgC() < 2)
   734 	{
   735 		g_SMAPI->ConPrint("Usage: sm_gamedata_md5 <file>\n");
   736 		return;
   737 	}
   738 
   739 	const char *file = args.Arg(1);
   740 	if (!file || file[0] == '\0')
   741 	{
   742 		g_SMAPI->ConPrint("Usage: sm_gamedata_md5 <file>\n");
   743 		return;
   744 	}
   745 
   746 	SourceHook::CVector<FileData *>::iterator iter = g_FetchThread.filenames.begin();
   747 
   748 	FileData *curData;
   749 
   750 	while (iter != g_FetchThread.filenames.end())
   751 	{
   752 		curData = (*iter);
   753 
   754 		char fileName[30];
   755 
   756 		g_LibSys.GetFileFromPath(fileName, sizeof(fileName), curData->filename->c_str());
   757 
   758 		if (strcmpi(fileName, file) == 0)
   759 		{
   760 			g_SMAPI->ConPrintf("MD5 Sum: %s\n", curData->checksum);
   761 			return;
   762 		}
   763 
   764 		iter++;
   765 	}
   766 
   767 	g_SMAPI->ConPrint("File not found!\n");
   768 }