update_tool/update_tool.cpp
branchsourcemm-1.6.0
changeset 428 40a9d2f61121
parent 370 4dc28ab1c722
child 429 cadecde4b802
     1.1 --- a/update_tool/update_tool.cpp	Thu Oct 04 19:46:28 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,366 +0,0 @@
     1.4 -#include <stdio.h>
     1.5 -#include <stdarg.h>
     1.6 -#include <string.h>
     1.7 -#include <ctype.h>
     1.8 -
     1.9 -#if defined _MSC_VER
    1.10 -#define SEPCHAR			"\\"
    1.11 -#define MMPATH			"addons\\metamod\\bin"
    1.12 -#define WINDOWS_LEAN_AND_MEAN
    1.13 -#include <windows.h>
    1.14 -#elif defined __linux__
    1.15 -#define SEPCHAR			"/"
    1.16 -#define MMPATH			"addons/metamod/bin"
    1.17 -#include <unistd.h>
    1.18 -#endif
    1.19 -
    1.20 -#include <icvar.h>
    1.21 -
    1.22 -extern "C" void GetGameDir(char *buffer, int maxlength);
    1.23 -extern "C" void *GetThisPointer();
    1.24 -extern "C" void ServerCommand(const char *command);
    1.25 -extern "C" ICvar *GetICvar();
    1.26 -
    1.27 -size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    1.28 -bool s_isspace(char c);
    1.29 -bool RenameFile(const char *old, const char *newf);
    1.30 -bool RemoveFile(const char *file);
    1.31 -
    1.32 -/* This will be called by the thunk */
    1.33 -#if defined _MSC_VER
    1.34 -extern "C" void LoadFunction()
    1.35 -#elif defined __linux__
    1.36 -extern "C" void _LoadFunction()
    1.37 -#endif
    1.38 -{
    1.39 -	ICvar *pCvar = GetICvar();
    1.40 -	if (pCvar->FindVar("metamod_version") != NULL)
    1.41 -	{
    1.42 -		/* Already exists, bail out */
    1.43 -		return;
    1.44 -	}
    1.45 -
    1.46 -	char gamedir[260];
    1.47 -	char mmpath[260];
    1.48 -
    1.49 -	enum RestartMode
    1.50 -	{
    1.51 -		Restart_Never,
    1.52 -		Restart_Error,
    1.53 -		Restart_Quit,
    1.54 -	};
    1.55 -
    1.56 -	RestartMode mode = Restart_Error;
    1.57 -
    1.58 -	GetGameDir(gamedir, sizeof(gamedir));
    1.59 -
    1.60 -	/* Defaults */
    1.61 -	UTIL_Format(mmpath, sizeof(mmpath), "|gameinfo_path|%s", MMPATH);
    1.62 -
    1.63 -	/* Read config */
    1.64 -	char config[260];
    1.65 -	UTIL_Format(config, sizeof(config), "%s" SEPCHAR "sourcemm_updater.conf", gamedir);
    1.66 -	FILE *fpCfg = fopen(config, "rt");
    1.67 -	if (fpCfg)
    1.68 -	{
    1.69 -		char cfgLine[512];
    1.70 -		while (!feof(fpCfg) && fgets(cfgLine, sizeof(cfgLine), fpCfg) != NULL)
    1.71 -		{
    1.72 -			char key[255];
    1.73 -
    1.74 -			size_t keyLen = 0;
    1.75 -
    1.76 -			/* Strip whitespace */
    1.77 -			char *input = cfgLine;
    1.78 -			while (*input != '\0' && s_isspace(*input))
    1.79 -			{
    1.80 -				input++;
    1.81 -			}
    1.82 -
    1.83 -			/* Strip ending whitespace */
    1.84 -			size_t len = strlen(input);
    1.85 -			for (size_t i = len - 1;
    1.86 -				 i >= 0 && i < len;
    1.87 -				 i--)
    1.88 -			{
    1.89 -				if (s_isspace(input[i]))
    1.90 -				{
    1.91 -					input[i] = '\0';
    1.92 -					len--;
    1.93 -				} else {
    1.94 -					break;
    1.95 -				}
    1.96 -			}
    1.97 -			
    1.98 -			/* Eat stuff until we find a key */
    1.99 -			while (*input != '\0' && !s_isspace(*input))
   1.100 -			{
   1.101 -				if (keyLen < sizeof(key))
   1.102 -				{
   1.103 -					key[keyLen++] = *input;
   1.104 -				}
   1.105 -				input++;
   1.106 -			}
   1.107 -			key[keyLen] = '\0';
   1.108 -
   1.109 -			/* Eat spaces until we hit an = sign */
   1.110 -			while (*input != '\0' && *input != '=')
   1.111 -			{
   1.112 -				input++;
   1.113 -			}
   1.114 -
   1.115 -			if (*input == '=')
   1.116 -			{
   1.117 -				input++;
   1.118 -			}
   1.119 -
   1.120 -			/* Eat spaces again */
   1.121 -			while (*input != '\0' && s_isspace(*input))
   1.122 -			{
   1.123 -				input++;
   1.124 -			}
   1.125 -
   1.126 -			/* Ignore comments */
   1.127 -			if (key[0] == ';')
   1.128 -			{
   1.129 -				continue;
   1.130 -			}
   1.131 -
   1.132 -			/* The rest is our key */
   1.133 -			if (strcmp(key, "mmpath") == 0)
   1.134 -			{
   1.135 -				UTIL_Format(mmpath, sizeof(mmpath), "%s", input);
   1.136 -			} else if (strcmp(key, "restart") == 0) {
   1.137 -				if (strcmp(input, "never") == 0)
   1.138 -				{
   1.139 -					mode = Restart_Never;
   1.140 -				} else if (strcmp(input, "error") == 0) {
   1.141 -					mode = Restart_Error;
   1.142 -				} else if (strcmp(input, "quit") == 0) {
   1.143 -					mode = Restart_Quit;
   1.144 -				}
   1.145 -			}
   1.146 -		}
   1.147 -		fclose(fpCfg);
   1.148 -	}
   1.149 -
   1.150 -	char old_path[260];
   1.151 -	char new_path[260];
   1.152 -
   1.153 -	UTIL_Format(old_path, sizeof(old_path), "%s" SEPCHAR "gameinfo.txt", gamedir);
   1.154 -	UTIL_Format(new_path, sizeof(new_path), "%s" SEPCHAR "gameinfo.new.txt", gamedir);
   1.155 -
   1.156 -	FILE *fp = fopen(old_path, "rt");
   1.157 -	
   1.158 -	if (!fp)
   1.159 -	{
   1.160 -		return;
   1.161 -	}
   1.162 -
   1.163 -	FILE *op = fopen(new_path, "wt");
   1.164 -	if (!op)
   1.165 -	{
   1.166 -		fclose(fp);
   1.167 -		return;
   1.168 -	}
   1.169 -
   1.170 -	enum ParseState
   1.171 -	{
   1.172 -		Parse_None,
   1.173 -		Parse_Root,
   1.174 -		Parse_GameInfo,
   1.175 -		Parse_FileSystem,
   1.176 -		Parse_SearchPaths,
   1.177 -	};
   1.178 -
   1.179 -	ParseState ps = Parse_Root;
   1.180 -
   1.181 -	char input[1024];
   1.182 -	char backup[1024];
   1.183 -
   1.184 -	bool bWroteOutput = false;
   1.185 -	
   1.186 -	while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
   1.187 -	{
   1.188 -		UTIL_Format(backup, sizeof(backup), "%s", input);
   1.189 -
   1.190 -		if (ps != Parse_None)
   1.191 -		{
   1.192 -			char *inbuf = input;
   1.193 -			
   1.194 -			/* Strip beginning whitespace */
   1.195 -			while (*inbuf != '\0' && s_isspace(*inbuf))
   1.196 -			{
   1.197 -				inbuf++;
   1.198 -			}
   1.199 -
   1.200 -			/* Strip ending whitespace */
   1.201 -			size_t len = strlen(inbuf);
   1.202 -			for (size_t i = len - 1;
   1.203 -				 i >= 0 && i < len;
   1.204 -				 i--)
   1.205 -			{
   1.206 -				if (s_isspace(inbuf[i]))
   1.207 -				{
   1.208 -					inbuf[i] = '\0';
   1.209 -					len--;
   1.210 -				} else {
   1.211 -					break;
   1.212 -				}
   1.213 -			}
   1.214 -
   1.215 -			/* Strip quotation marks */
   1.216 -			if (inbuf[0] == '"'
   1.217 -				&& inbuf[len-1] == '"')
   1.218 -			{
   1.219 -				inbuf[len - 1] = '\0';
   1.220 -				inbuf = &inbuf[1];
   1.221 -				len -= 2;
   1.222 -			}
   1.223 -
   1.224 -			/* Do tests */
   1.225 -			if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
   1.226 -			{
   1.227 -				ps = Parse_GameInfo;
   1.228 -			} else if (ps == Parse_GameInfo && strcmp(inbuf, "FileSystem") == 0) {
   1.229 -				ps = Parse_FileSystem;
   1.230 -			} else if (ps == Parse_FileSystem && strcmp(inbuf, "SearchPaths") == 0) {
   1.231 -				ps = Parse_SearchPaths;
   1.232 -			} else if (ps == Parse_SearchPaths) {
   1.233 -				const char *game = strstr(inbuf, "Game");
   1.234 -				if (game)
   1.235 -				{
   1.236 -					if (strstr(game, "GameBin") != NULL
   1.237 -						&& strstr(game, mmpath) != NULL)
   1.238 -					{
   1.239 -						fclose(op);
   1.240 -						op = NULL;
   1.241 -						break;			/* Nothing more to do! */
   1.242 -					} else {
   1.243 -						fputs("\t\t\tGameBin\t\t\t", op);
   1.244 -						fputs(mmpath, op);
   1.245 -						fputs("\n", op);
   1.246 -						ps = Parse_None;
   1.247 -						bWroteOutput = true;
   1.248 -					}
   1.249 -				}
   1.250 -			}
   1.251 -		}
   1.252 -
   1.253 -		fputs(backup, op);
   1.254 -	}
   1.255 -
   1.256 -	if (!op)
   1.257 -	{
   1.258 -		/* Well, we can't really do anything else.  Give up. */
   1.259 -		fclose(fp);
   1.260 -		return;
   1.261 -	}
   1.262 -
   1.263 -	/* Close all streams */
   1.264 -	fclose(op);
   1.265 -	fclose(fp);
   1.266 -
   1.267 -	/* If we didn't change anything, abort here */
   1.268 -	if (!bWroteOutput)
   1.269 -	{
   1.270 -		RemoveFile(new_path);
   1.271 -		return;
   1.272 -	}
   1.273 -
   1.274 -	/* Move the old file to a backup name */
   1.275 -	char backup_name[260];
   1.276 -	UTIL_Format(backup_name, sizeof(backup_name), "%s" SEPCHAR "gameinfo.backup.txt", gamedir);
   1.277 -
   1.278 -	if (!RenameFile(old_path, backup_name))
   1.279 -	{
   1.280 -		/* If we can't rename, just bail out.
   1.281 -		 * We don't want to overwrite the client's default
   1.282 -		 * without backing it up first!
   1.283 -		 */
   1.284 -		return;
   1.285 -	}
   1.286 -	if (!RenameFile(new_path, old_path))
   1.287 -	{
   1.288 -		/* Since this failed, we really have no choice.
   1.289 -		 * Try and rename the old back.
   1.290 -		 */
   1.291 -		RenameFile(backup_name, old_path);
   1.292 -		return;
   1.293 -	}
   1.294 -	RemoveFile(new_path);
   1.295 -
   1.296 -	if (mode == Restart_Error)
   1.297 -	{
   1.298 -		Error("Server is restarting to load Metamod:Source");
   1.299 -	} else if (mode == Restart_Quit) {
   1.300 -		ServerCommand("quit\n");
   1.301 -	}
   1.302 -}
   1.303 -
   1.304 -bool RemoveFile(const char *file)
   1.305 -{
   1.306 -#if defined _MSC_VER
   1.307 -	return (_unlink(file) == 0);
   1.308 -#else
   1.309 -	return (unlink(file) == 0);
   1.310 -#endif
   1.311 -}
   1.312 -
   1.313 -bool s_isspace(char c)
   1.314 -{
   1.315 -	if ((unsigned)c & 0x80)
   1.316 -	{
   1.317 -		return false;
   1.318 -	} else {
   1.319 -		return isspace(c) ? true : false;
   1.320 -	}
   1.321 -}
   1.322 -
   1.323 -size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   1.324 -{
   1.325 -	va_list ap;
   1.326 -
   1.327 -	va_start(ap, fmt);
   1.328 -	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   1.329 -	va_end(ap);
   1.330 -
   1.331 -	if (len >= maxlength)
   1.332 -	{
   1.333 -		len = maxlength - 1;
   1.334 -		buffer[len] = '\0';
   1.335 -	}
   1.336 -
   1.337 -	return len;
   1.338 -}
   1.339 -
   1.340 -bool RenameFile(const char *old, const char *newf)
   1.341 -{
   1.342 -#if defined __linux__
   1.343 -	return (rename(old, newf) == 0);
   1.344 -#elif defined WIN32
   1.345 -	return (MoveFileA(old, newf) != 0);
   1.346 -#endif
   1.347 -}
   1.348 -
   1.349 -#if defined _MSC_VER
   1.350 -extern "C" __declspec(dllexport) void *CreateInterface(const char *iface, int *ret)
   1.351 -#elif defined __linux__
   1.352 -extern "C" __attribute__((visibility("default"))) void *CreateInterface(const char *iface, int *ret)
   1.353 -#endif
   1.354 -{
   1.355 -	if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
   1.356 -	{
   1.357 -		if (ret)
   1.358 -		{
   1.359 -			*ret = 0;
   1.360 -		}
   1.361 -		return GetThisPointer();
   1.362 -	}
   1.363 -
   1.364 -	if (ret)
   1.365 -	{
   1.366 -		*ret = 1;
   1.367 -	}
   1.368 -	return NULL;
   1.369 -}