update_tool/update_tool.cpp
changeset 300 807620eb4f4e
child 302 a6556cd5cbaf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/update_tool/update_tool.cpp	Sat Mar 31 16:23:19 2007 +0000
     1.3 @@ -0,0 +1,230 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdarg.h>
     1.6 +#include <string.h>
     1.7 +#include <ctype.h>
     1.8 +
     1.9 +extern "C" void GetGameDir(char *buffer, int maxlength);
    1.10 +extern "C" void *GetThisPointer();
    1.11 +
    1.12 +#if defined _MSC_VER
    1.13 +#define SEPCHAR			"\\"
    1.14 +#define MMPATH			"|gameinfo_path|addons\\metamod\\bin"
    1.15 +#define WINDOWS_LEAN_AND_MEAN
    1.16 +#include <windows.h>
    1.17 +#elif defined __linux__
    1.18 +#define SEPCHAR			"/"
    1.19 +#define MMPATH			"|gameinfo_path|addons/metamod/bin"
    1.20 +#include <unistd.h>
    1.21 +#endif
    1.22 +
    1.23 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    1.24 +bool s_isspace(char c);
    1.25 +bool RenameFile(const char *old, const char *newf);
    1.26 +
    1.27 +/* This will be called by the thunk */
    1.28 +#if defined _MSC_VER
    1.29 +extern "C" void LoadFunction()
    1.30 +#elif defined __linux__
    1.31 +extern "C" void _LoadFunction()
    1.32 +#endif
    1.33 +{
    1.34 +	char gamedir[260];
    1.35 +
    1.36 +	GetGameDir(gamedir, sizeof(gamedir));
    1.37 +
    1.38 +	char old_path[260];
    1.39 +	char new_path[260];
    1.40 +
    1.41 +	UTIL_Format(old_path, sizeof(old_path), "%s" SEPCHAR "gameinfo.txt", gamedir);
    1.42 +	UTIL_Format(new_path, sizeof(new_path), "%s" SEPCHAR "gameinfo.new.txt", gamedir);
    1.43 +
    1.44 +	FILE *fp = fopen(old_path, "rt");
    1.45 +	
    1.46 +	if (!fp)
    1.47 +	{
    1.48 +		return;
    1.49 +	}
    1.50 +
    1.51 +	FILE *op = fopen(new_path, "wt");
    1.52 +	if (!op)
    1.53 +	{
    1.54 +		fclose(fp);
    1.55 +		return;
    1.56 +	}
    1.57 +
    1.58 +	enum ParseState
    1.59 +	{
    1.60 +		Parse_None,
    1.61 +		Parse_Root,
    1.62 +		Parse_GameInfo,
    1.63 +		Parse_FileSystem,
    1.64 +		Parse_SearchPaths,
    1.65 +	};
    1.66 +
    1.67 +	ParseState ps = Parse_Root;
    1.68 +
    1.69 +	char input[1024];
    1.70 +	char backup[1024];
    1.71 +	
    1.72 +	while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
    1.73 +	{
    1.74 +		UTIL_Format(backup, sizeof(backup), "%s", input);
    1.75 +
    1.76 +		if (ps != Parse_None)
    1.77 +		{
    1.78 +			char *inbuf = input;
    1.79 +			
    1.80 +			/* Strip beginning whitespace */
    1.81 +			while (*inbuf != '\0' && s_isspace(*inbuf))
    1.82 +			{
    1.83 +				inbuf++;
    1.84 +			}
    1.85 +
    1.86 +			/* Strip ending whitespace */
    1.87 +			size_t len = strlen(inbuf);
    1.88 +			for (size_t i = len - 1;
    1.89 +				 i >= 0 && i < len;
    1.90 +				 i--)
    1.91 +			{
    1.92 +				if (s_isspace(inbuf[i]))
    1.93 +				{
    1.94 +					inbuf[i] = '\0';
    1.95 +					len--;
    1.96 +				} else {
    1.97 +					break;
    1.98 +				}
    1.99 +			}
   1.100 +
   1.101 +			/* Strip quotation marks */
   1.102 +			if (inbuf[0] == '"'
   1.103 +				&& inbuf[len-1] == '"')
   1.104 +			{
   1.105 +				inbuf[len - 1] = '\0';
   1.106 +				inbuf = &inbuf[1];
   1.107 +				len -= 2;
   1.108 +			}
   1.109 +
   1.110 +			/* Do tests */
   1.111 +			if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
   1.112 +			{
   1.113 +				ps = Parse_GameInfo;
   1.114 +			} else if (ps == Parse_GameInfo && strcmp(inbuf, "FileSystem") == 0) {
   1.115 +				ps = Parse_FileSystem;
   1.116 +			} else if (ps == Parse_FileSystem && strcmp(inbuf, "SearchPaths") == 0) {
   1.117 +				ps = Parse_SearchPaths;
   1.118 +			} else if (ps == Parse_SearchPaths) {
   1.119 +				const char *game = strstr(inbuf, "Game");
   1.120 +				if (game)
   1.121 +				{
   1.122 +					if (strstr(game, "GameBin") != NULL
   1.123 +						&& strstr(game, MMPATH) != NULL)
   1.124 +					{
   1.125 +						fclose(op);
   1.126 +						op = NULL;
   1.127 +						break;			/* Nothing more to do! */
   1.128 +					} else {
   1.129 +						fputs("\t\t\t\tGameBin\t\t" MMPATH "\n", op);
   1.130 +						ps = Parse_None;
   1.131 +					}
   1.132 +				}
   1.133 +			}
   1.134 +		}
   1.135 +
   1.136 +		fputs(backup, op);
   1.137 +	}
   1.138 +
   1.139 +	if (!op)
   1.140 +	{
   1.141 +		/* Well, we can't really do anything else.  Give up.  */
   1.142 +		fclose(fp);
   1.143 +		return;
   1.144 +	}
   1.145 +
   1.146 +	/* Close all streams */
   1.147 +	fclose(op);
   1.148 +	fclose(fp);
   1.149 +
   1.150 +	/* Move the old file to a backup name */
   1.151 +	char backup_name[260];
   1.152 +	UTIL_Format(backup_name, sizeof(backup_name), "%s" SEPCHAR "gameinfo.backup.txt", gamedir);
   1.153 +
   1.154 +	if (!RenameFile(old_path, backup_name))
   1.155 +	{
   1.156 +		/* If we can't rename, just bail out.
   1.157 +		 * We don't want to overwrite the client's default
   1.158 +		 * without backing it up first!
   1.159 +		 */
   1.160 +		return;
   1.161 +	}
   1.162 +	if (!RenameFile(new_path, old_path))
   1.163 +	{
   1.164 +		/* Since this failed, we really have no choice.
   1.165 +		 * Try and rename the old back.
   1.166 +		 */
   1.167 +		RenameFile(backup_name, old_path);
   1.168 +		return;
   1.169 +	}
   1.170 +#if defined _MSC_VER
   1.171 +	_unlink(new_path);
   1.172 +#else
   1.173 +	unlink(new_path);
   1.174 +#endif
   1.175 +}
   1.176 +
   1.177 +bool s_isspace(char c)
   1.178 +{
   1.179 +	if ((unsigned)c & 0x80)
   1.180 +	{
   1.181 +		return false;
   1.182 +	} else {
   1.183 +		return isspace(c) ? true : false;
   1.184 +	}
   1.185 +}
   1.186 +
   1.187 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   1.188 +{
   1.189 +	va_list ap;
   1.190 +
   1.191 +	va_start(ap, fmt);
   1.192 +	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   1.193 +	va_end(ap);
   1.194 +
   1.195 +	if (len >= maxlength)
   1.196 +	{
   1.197 +		len = maxlength - 1;
   1.198 +		buffer[len] = '\0';
   1.199 +	}
   1.200 +
   1.201 +	return len;
   1.202 +}
   1.203 +
   1.204 +bool RenameFile(const char *old, const char *newf)
   1.205 +{
   1.206 +#if defined __linux__
   1.207 +	return (rename(old, newf) == 0);
   1.208 +#elif defined WIN32
   1.209 +	return (MoveFileA(old, newf) != 0);
   1.210 +#endif
   1.211 +}
   1.212 +
   1.213 +#if defined _MSC_VER
   1.214 +extern "C" __declspec(dllexport) void *CreateInterface(const char *iface, int *ret)
   1.215 +#elif defined __linux__
   1.216 +extern "C" __attribute__((visibility("default"))) void *CreateInterface(const char *iface, int *ret)
   1.217 +#endif
   1.218 +{
   1.219 +	if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
   1.220 +	{
   1.221 +		if (ret)
   1.222 +		{
   1.223 +			*ret = 0;
   1.224 +		}
   1.225 +		return GetThisPointer();
   1.226 +	}
   1.227 +
   1.228 +	if (ret)
   1.229 +	{
   1.230 +		*ret = 1;
   1.231 +	}
   1.232 +	return NULL;
   1.233 +}