Merge.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "GameDataFetcher.h"
35 #ifdef PLATFORM_WINDOWS
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #define INVALID_SOCKET -1
46 #define closesocket close
49 #include "sh_vector.h"
50 #include "sh_string.h"
51 #include "sm_version.h"
53 #if SOURCE_ENGINE == SE_LEFT4DEAD
54 #include "convar_sm_l4d.h"
55 #elif SOURCE_ENGINE == SE_ORANGEBOX
56 #include "convar_sm_ob.h"
58 #include "convar_sm.h"
61 #include "sourcemm_api.h"
64 #include "compat_wrappers.h"
65 #include "sm_stringutil.h"
67 #define QUERY_MAX_LENGTH 1024
69 BuildMD5ableBuffer g_MD5Builder;
70 FetcherThread g_FetchThread;
74 bool g_disableGameDataUpdate = false;
75 bool g_restartAfterUpdate = false;
77 int g_serverPort = 6500;
78 char g_serverAddress[100] = "hayate.alliedmods.net";
80 void FetcherThread::RunThread( IThreadHandle *pHandle )
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);
86 g_SourceMod.BuildPath(Path_SM, lock_path, sizeof(lock_path), "data/temp/gamedata.lock");
88 g_Logger.LogMessage("Starting Gamedata update fetcher... please report problems to bugs.alliedmods.net");
90 char log_path[PLATFORM_MAX_PATH];
91 g_SourceMod.BuildPath(Path_SM, log_path, sizeof(log_path), "logs/gamedata");
93 g_LibSys.CreateFolder(log_path);
97 tm *curtime = localtime(&t);
99 g_SourceMod.BuildPath(Path_SM, log_path, sizeof(log_path), "logs/gamedata/L%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
101 logfile = fopen(log_path, "a");
105 g_Logger.LogError("Failed to create GameData log file");
109 //Create a blank lock file
110 FILE *fp = fopen(lock_path, "w");
116 char query[QUERY_MAX_LENGTH];
118 /* Check for updated gamedata files */
119 int len = BuildGameDataQuery(query, QUERY_MAX_LENGTH);
123 g_Logger.LogToOpenFile(logfile, "Query Writing failed");
130 if (g_disableGameDataUpdate)
133 g_Logger.LogMessage("Skipping GameData Query due to DisableAutoUpdate being set to true");
141 /* Create a new socket for this connection */
142 int socketDescriptor = ConnectSocket();
144 if (socketDescriptor == INVALID_SOCKET)
151 int sent = SendData(socketDescriptor, query, len);
154 g_Logger.LogToOpenFile(logfile, "Sent Query!");
159 g_Logger.LogToOpenFile(logfile, "Failed to send gamedata query data to remote host");
161 closesocket(socketDescriptor);
167 ProcessGameDataQuery(socketDescriptor);
169 /* And we're done! */
170 closesocket(socketDescriptor);
175 void FetcherThread::OnTerminate( IThreadHandle *pHandle, bool cancel )
177 g_blockGameDataLoad = false;
180 int FetcherThread::BuildGameDataQuery( char *buffer, int maxlen )
182 char gamedata_path[PLATFORM_MAX_PATH];
183 g_SourceMod.BuildPath(Path_SM, gamedata_path, sizeof(gamedata_path), "gamedata");
185 IDirectory *dir = g_LibSys.OpenDirectory(gamedata_path);
192 bf_write Writer = bf_write("GameDataQuery", buffer, maxlen);
194 Writer.WriteByte('A'); //Generic Header char
195 Writer.WriteByte('G'); //G for gamedata query, or green, like my hat.
197 short build[4] = { SVN_FILE_VERSION };
199 Writer.WriteBytes(&build[0], 8);
201 Writer.WriteByte(0); // Initialize the file counter - Index 10
203 while (dir->MoreFiles())
205 if (dir->IsEntryFile())
207 const char *name = dir->GetEntryName();
208 size_t len = strlen(name);
210 && strcmp(&name[len-4], ".txt") == 0)
212 char file[PLATFORM_MAX_PATH];
214 g_LibSys.PathFormat(file, sizeof(file), "%s/%s", gamedata_path, name);
217 if (g_TextParser.ParseFile_SMC(file, &g_MD5Builder, &states) == SMCError_Okay)
219 unsigned char *md5 = g_MD5Builder.GetMD5();
222 (uint8_t)buffer[10]++; //Increment the file counter
223 Writer.WriteBytes(md5, 16);
225 g_Logger.LogToOpenFile(logfile, "%s - \"%s\"", file, g_MD5Builder.GetMD5String());
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);
235 g_Logger.LogToOpenFile(logfile, "%s no md5?", file);
242 g_Logger.LogToOpenFile(logfile, "%s failed!", file);
251 return Writer.GetNumBytesWritten();
254 int FetcherThread::ConnectSocket()
256 struct protoent *ptrp;
257 ptrp = getprotobyname("tcp");
261 WSAStartup(0x0101, &wsaData);
266 g_Logger.LogToOpenFile(logfile, "Failed to find TCP");
267 return INVALID_SOCKET;
270 int socketDescriptor = socket(AF_INET, SOCK_STREAM, ptrp->p_proto);
272 if (socketDescriptor == INVALID_SOCKET)
276 g_Logger.LogToOpenFile(logfile, "Failed to create a new socket - Error %i", WSAGetLastError());
278 g_Logger.LogToOpenFile(logfile, "Failed to create a new socket - Error %i", errno);
280 closesocket(socketDescriptor);
281 return INVALID_SOCKET;
285 struct sockaddr_in local_addr;
287 local_addr.sin_family = AF_INET;
288 local_addr.sin_port = htons((u_short)g_serverPort);
290 he = gethostbyname(g_serverAddress);
294 if ((local_addr.sin_addr.s_addr = inet_addr(g_serverAddress)) == INADDR_NONE)
296 g_Logger.LogToOpenFile(logfile, "Couldn't locate address");
297 closesocket(socketDescriptor);
298 return INVALID_SOCKET;
303 memcpy(&local_addr.sin_addr, (struct in_addr *)he->h_addr, he->h_length);
306 if (connect(socketDescriptor, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0)
309 g_Logger.LogToOpenFile(logfile, "Couldn't connect - Error %i", WSAGetLastError());
311 g_Logger.LogToOpenFile(logfile, "Couldn't connect - Error %i", errno);
313 closesocket(socketDescriptor);
314 return INVALID_SOCKET;
317 return socketDescriptor;
320 void FetcherThread::ProcessGameDataQuery(int socketDescriptor)
324 g_Logger.LogToOpenFile(logfile, "Waiting for reply!");
327 //Read in the header bytes
328 int returnLen = RecvData(socketDescriptor, buffer, 12);
331 g_Logger.LogToOpenFile(logfile, "Recv Completed");
337 g_Logger.LogToOpenFile(logfile, ",but it failed.");
339 /* Timeout or fail? */
344 g_Logger.LogToOpenFile(logfile, "Received Header!");
347 bf_read Reader = bf_read("GameDataQuery", buffer, 12);
349 if (Reader.ReadByte() != 'A' || Reader.ReadByte() != 'G')
351 g_Logger.LogToOpenFile(logfile, "Unknown Query Response");
355 UpdateStatus updateStatus = (UpdateStatus)Reader.ReadByte();
357 short build[4] = {0,0,0,0};
359 build[0] = Reader.ReadShort();
360 build[1] = Reader.ReadShort();
361 build[2] = Reader.ReadShort();
362 build[3] = Reader.ReadShort();
365 g_Logger.LogToOpenFile(logfile, "Update Status: %i - Latest %i.%i.%i.%i", updateStatus, build[0], build[1], build[2], build[3]);
368 HandleUpdateStatus(updateStatus, build);
370 int changedFiles = Reader.ReadByte();
373 g_Logger.LogToOpenFile(logfile, "Files to download: %i", changedFiles);
376 for (int i=0; i<changedFiles; i++)
378 //Read in the file index and byte count
379 returnLen = RecvData(socketDescriptor, buffer, 5);
383 /* Timeout or fail? */
387 Reader.StartReading(buffer, 5);
389 int index = Reader.ReadByte();
390 int tempLen = Reader.ReadUBitLong(32);
393 g_Logger.LogToOpenFile(logfile, "File index %i and length %i", index, tempLen);
397 memtable->CreateMem(tempLen+1, &memPtr);
399 //Read the contents of our file into the memtable
400 returnLen = RecvData(socketDescriptor, (char *)memPtr, tempLen);
403 g_Logger.LogToOpenFile(logfile, "Recieved %i bytes", returnLen);
408 /* Timeout or fail? */
412 ((unsigned char *)memPtr)[tempLen] = '\0';
414 FileData *data = filenames.at(index);
415 const char* filename;
418 filename = data->filename->c_str();
420 FILE *fp = fopen(filename, "w");
424 fprintf(fp, (const char *)memPtr);
429 g_Logger.LogToOpenFile(logfile, "Failed to open file \"%s\"", filename);
440 g_Logger.LogToOpenFile(logfile, "Updated File %s", filename);
445 g_Logger.LogToOpenFile(logfile, "File Downloads Completed!");
448 bool needsRestart = false;
450 if (changedFiles > 0)
453 g_Logger.LogMessage("New GameData Files have been downloaded to your gamedata directory. Please restart your server for these to take effect");
456 //Read changed file count
457 returnLen = RecvData(socketDescriptor, buffer, 1);
461 /* Timeout or fail? */
462 g_Logger.LogToOpenFile(logfile, "Failed to receive unknown count");
466 Reader.StartReading(buffer, 1);
468 changedFiles = Reader.ReadByte();
470 if (changedFiles == 0)
473 g_Logger.LogToOpenFile(logfile, "No unknown files. We're all done");
478 char *changedFileIndexes = new char[changedFiles];
481 g_Logger.LogToOpenFile(logfile, "%i Files were unknown", changedFiles);
484 returnLen = RecvData(socketDescriptor, changedFileIndexes, changedFiles);
488 /* Timeout or fail? */
490 g_Logger.LogToOpenFile(logfile, "Failed to receive unknown list");
495 Reader.StartReading(changedFileIndexes, changedFiles);
497 for (int i=0; i<changedFiles; i++)
499 int index = Reader.ReadByte();
502 FileData *data = filenames.at(index);
503 const char* pathname;
506 pathname = data->filename->c_str();
513 g_LibSys.GetFileFromPath(fileName, sizeof(fileName), pathname);
515 g_Logger.LogToOpenFile(logfile, "Unknown File %i : %s", index, fileName);
519 delete [] changedFileIndexes;
521 if (needsRestart && g_restartAfterUpdate)
523 g_Logger.LogMessage("Automatically restarting server after a successful gamedata update!");
524 engine->ServerCommand("quit\n");
528 int FetcherThread::RecvData( int socketDescriptor, char *buffer, int len )
533 /* Create a 10 Second Timeout */
537 int bytesReceivedTotal = 0;
539 while (bytesReceivedTotal < len)
541 /* Add our socket to a socket set */
543 FD_SET(socketDescriptor, &fds);
545 /* Wait max of 10 seconds for recv to become available */
546 select(socketDescriptor+1, &fds, NULL, NULL, &tv);
548 int bytesReceived = 0;
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))
553 bytesReceived = recv(socketDescriptor, buffer+bytesReceivedTotal, len-bytesReceivedTotal, 0);
556 if (bytesReceived == 0 || bytesReceived == -1)
561 bytesReceivedTotal += bytesReceived;
564 return bytesReceivedTotal;
567 int FetcherThread::SendData( int socketDescriptor, char *buffer, int len )
575 int sentBytesTotal = 0;
577 while (sentBytesTotal < len)
580 FD_SET(socketDescriptor, &fds);
582 select(socketDescriptor+1, NULL, &fds, NULL, &tv);
586 if (FD_ISSET(socketDescriptor, &fds))
588 sentBytes = send(socketDescriptor, buffer+sentBytesTotal, len-sentBytesTotal, 0);
591 if (sentBytes == 0 || sentBytes == -1)
596 sentBytesTotal += sentBytes;
599 return sentBytesTotal;
602 void FetcherThread::HandleUpdateStatus( UpdateStatus status, short version[4] )
612 case Update_NewBuild:
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]);
619 case Update_MinorAvailable:
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]);
626 case Update_MajorAvailable:
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]);
633 case Update_CriticalAvailable:
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]);
642 bool g_blockGameDataLoad = false;
644 class InitFetch : public SMGlobalClass
647 void OnSourceModAllInitialized_Post()
649 char lock_path[PLATFORM_MAX_PATH];
650 g_SourceMod.BuildPath(Path_SM, lock_path, sizeof(lock_path), "data/temp/gamedata.lock");
652 if (g_LibSys.IsPathFile(lock_path) && g_LibSys.PathExists(lock_path))
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;
659 ThreadParams fetchThreadParams = ThreadParams();
660 fetchThreadParams.prio = ThreadPrio_Low;
661 g_pThreader->MakeThread(&g_FetchThread, &fetchThreadParams);
664 ConfigResult OnSourceModConfigChanged(const char *key,
670 if (strcmp(key, "DisableAutoUpdate") == 0)
672 if (strcmp(value, "yes") == 0)
674 g_disableGameDataUpdate = true;
675 return ConfigResult_Accept;
677 else if (strcmp(value, "no") == 0)
679 g_disableGameDataUpdate = false;
680 return ConfigResult_Accept;
683 return ConfigResult_Reject;
686 if (strcmp(key, "ForceRestartAfterUpdate") == 0)
688 if (strcmp(value, "yes") == 0)
690 g_restartAfterUpdate = true;
691 return ConfigResult_Accept;
693 else if (strcmp(value, "no") == 0)
695 g_restartAfterUpdate = false;
696 return ConfigResult_Accept;
699 return ConfigResult_Reject;
702 if (strcmp(key, "AutoUpdateServer") == 0)
704 UTIL_Format(g_serverAddress, sizeof(g_serverAddress), "%s", value);
706 return ConfigResult_Accept;
709 if (strcmp(key, "AutoUpdatePort") == 0)
711 int port = atoi(value);
715 return ConfigResult_Reject;
720 return ConfigResult_Accept;
723 return ConfigResult_Ignore;
727 CON_COMMAND(sm_gamedata_md5, "Checks the MD5 sum for a given gamedata file")
729 #if SOURCE_ENGINE == SE_EPISODEONE
735 g_SMAPI->ConPrint("Usage: sm_gamedata_md5 <file>\n");
739 const char *file = args.Arg(1);
740 if (!file || file[0] == '\0')
742 g_SMAPI->ConPrint("Usage: sm_gamedata_md5 <file>\n");
746 SourceHook::CVector<FileData *>::iterator iter = g_FetchThread.filenames.begin();
750 while (iter != g_FetchThread.filenames.end())
756 g_LibSys.GetFileFromPath(fileName, sizeof(fileName), curData->filename->c_str());
758 if (strcmpi(fileName, file) == 0)
760 g_SMAPI->ConPrintf("MD5 Sum: %s\n", curData->checksum);
767 g_SMAPI->ConPrint("File not found!\n");