update_tool/update_tool.cpp
author David Anderson <dvander@alliedmods.net>
Sat Mar 31 20:23:59 2007 +0000 (2007-03-31)
changeset 302 a6556cd5cbaf
parent 300 807620eb4f4e
child 305 539f25c00858
permissions -rw-r--r--
config file support now
     1 #include <stdio.h>
     2 #include <stdarg.h>
     3 #include <string.h>
     4 #include <ctype.h>
     5 
     6 extern "C" void GetGameDir(char *buffer, int maxlength);
     7 extern "C" void *GetThisPointer();
     8 
     9 #if defined _MSC_VER
    10 #define SEPCHAR			"\\"
    11 #define MMPATH			"addons\\metamod\\bin"
    12 #define WINDOWS_LEAN_AND_MEAN
    13 #include <windows.h>
    14 #elif defined __linux__
    15 #define SEPCHAR			"/"
    16 #define MMPATH			"addons/metamod/bin"
    17 #include <unistd.h>
    18 #endif
    19 
    20 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
    21 bool s_isspace(char c);
    22 bool RenameFile(const char *old, const char *newf);
    23 
    24 /* This will be called by the thunk */
    25 #if defined _MSC_VER
    26 extern "C" void LoadFunction()
    27 #elif defined __linux__
    28 extern "C" void _LoadFunction()
    29 #endif
    30 {
    31 	char gamedir[260];
    32 	char mmpath[260];
    33 
    34 	GetGameDir(gamedir, sizeof(gamedir));
    35 
    36 	/* Defaults */
    37 	UTIL_Format(mmpath, sizeof(mmpath), "|gameinfo_path|%s", MMPATH);
    38 
    39 	/* Read config */
    40 	char config[260];
    41 	UTIL_Format(config, sizeof(config), "%s" SEPCHAR "sourcemm_updater.conf", gamedir);
    42 	FILE *fpCfg = fopen(config, "rt");
    43 	if (fpCfg)
    44 	{
    45 		char cfgLine[512];
    46 		while (!feof(fpCfg) && fgets(cfgLine, sizeof(cfgLine), fpCfg) != NULL)
    47 		{
    48 			char key[255];
    49 
    50 			size_t keyLen = 0;
    51 
    52 			/* Strip whitespace */
    53 			char *input = cfgLine;
    54 			while (*input != '\0' && s_isspace(*input))
    55 			{
    56 				input++;
    57 			}
    58 
    59 			/* Strip ending whitespace */
    60 			size_t len = strlen(input);
    61 			for (size_t i = len - 1;
    62 				 i >= 0 && i < len;
    63 				 i--)
    64 			{
    65 				if (s_isspace(input[i]))
    66 				{
    67 					input[i] = '\0';
    68 					len--;
    69 				} else {
    70 					break;
    71 				}
    72 			}
    73 			
    74 			/* Eat stuff until we find a key */
    75 			while (*input != '\0' && !s_isspace(*input))
    76 			{
    77 				if (keyLen < sizeof(key))
    78 				{
    79 					key[keyLen++] = *input;
    80 				}
    81 				input++;
    82 			}
    83 			key[keyLen] = '\0';
    84 
    85 			/* Eat spaces until we hit an = sign */
    86 			while (*input != '\0' && *input != '=')
    87 			{
    88 				input++;
    89 			}
    90 
    91 			if (*input == '=')
    92 			{
    93 				input++;
    94 			}
    95 
    96 			/* Eat spaces again */
    97 			while (*input != '\0' && s_isspace(*input))
    98 			{
    99 				input++;
   100 			}
   101 
   102 			/* The rest is our key */
   103 			if (strcmp(key, "mmpath") == 0)
   104 			{
   105 				UTIL_Format(mmpath, sizeof(mmpath), "%s", input);
   106 			}
   107 		}
   108 		fclose(fpCfg);
   109 	}
   110 
   111 	char old_path[260];
   112 	char new_path[260];
   113 
   114 	UTIL_Format(old_path, sizeof(old_path), "%s" SEPCHAR "gameinfo.txt", gamedir);
   115 	UTIL_Format(new_path, sizeof(new_path), "%s" SEPCHAR "gameinfo.new.txt", gamedir);
   116 
   117 	FILE *fp = fopen(old_path, "rt");
   118 	
   119 	if (!fp)
   120 	{
   121 		return;
   122 	}
   123 
   124 	FILE *op = fopen(new_path, "wt");
   125 	if (!op)
   126 	{
   127 		fclose(fp);
   128 		return;
   129 	}
   130 
   131 	enum ParseState
   132 	{
   133 		Parse_None,
   134 		Parse_Root,
   135 		Parse_GameInfo,
   136 		Parse_FileSystem,
   137 		Parse_SearchPaths,
   138 	};
   139 
   140 	ParseState ps = Parse_Root;
   141 
   142 	char input[1024];
   143 	char backup[1024];
   144 	
   145 	while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
   146 	{
   147 		UTIL_Format(backup, sizeof(backup), "%s", input);
   148 
   149 		if (ps != Parse_None)
   150 		{
   151 			char *inbuf = input;
   152 			
   153 			/* Strip beginning whitespace */
   154 			while (*inbuf != '\0' && s_isspace(*inbuf))
   155 			{
   156 				inbuf++;
   157 			}
   158 
   159 			/* Strip ending whitespace */
   160 			size_t len = strlen(inbuf);
   161 			for (size_t i = len - 1;
   162 				 i >= 0 && i < len;
   163 				 i--)
   164 			{
   165 				if (s_isspace(inbuf[i]))
   166 				{
   167 					inbuf[i] = '\0';
   168 					len--;
   169 				} else {
   170 					break;
   171 				}
   172 			}
   173 
   174 			/* Strip quotation marks */
   175 			if (inbuf[0] == '"'
   176 				&& inbuf[len-1] == '"')
   177 			{
   178 				inbuf[len - 1] = '\0';
   179 				inbuf = &inbuf[1];
   180 				len -= 2;
   181 			}
   182 
   183 			/* Do tests */
   184 			if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
   185 			{
   186 				ps = Parse_GameInfo;
   187 			} else if (ps == Parse_GameInfo && strcmp(inbuf, "FileSystem") == 0) {
   188 				ps = Parse_FileSystem;
   189 			} else if (ps == Parse_FileSystem && strcmp(inbuf, "SearchPaths") == 0) {
   190 				ps = Parse_SearchPaths;
   191 			} else if (ps == Parse_SearchPaths) {
   192 				const char *game = strstr(inbuf, "Game");
   193 				if (game)
   194 				{
   195 					if (strstr(game, "GameBin") != NULL
   196 						&& strstr(game, mmpath) != NULL)
   197 					{
   198 						fclose(op);
   199 						op = NULL;
   200 						break;			/* Nothing more to do! */
   201 					} else {
   202 						fputs("\t\t\tGameBin\t\t\t|gameinfo_path|", op);
   203 						fputs(mmpath, op);
   204 						fputs("\n", op);
   205 						ps = Parse_None;
   206 					}
   207 				}
   208 			}
   209 		}
   210 
   211 		fputs(backup, op);
   212 	}
   213 
   214 	if (!op)
   215 	{
   216 		/* Well, we can't really do anything else.  Give up. */
   217 		fclose(fp);
   218 		return;
   219 	}
   220 
   221 	/* Close all streams */
   222 	fclose(op);
   223 	fclose(fp);
   224 
   225 	/* Move the old file to a backup name */
   226 	char backup_name[260];
   227 	UTIL_Format(backup_name, sizeof(backup_name), "%s" SEPCHAR "gameinfo.backup.txt", gamedir);
   228 
   229 	if (!RenameFile(old_path, backup_name))
   230 	{
   231 		/* If we can't rename, just bail out.
   232 		 * We don't want to overwrite the client's default
   233 		 * without backing it up first!
   234 		 */
   235 		return;
   236 	}
   237 	if (!RenameFile(new_path, old_path))
   238 	{
   239 		/* Since this failed, we really have no choice.
   240 		 * Try and rename the old back.
   241 		 */
   242 		RenameFile(backup_name, old_path);
   243 		return;
   244 	}
   245 #if defined _MSC_VER
   246 	_unlink(new_path);
   247 #else
   248 	unlink(new_path);
   249 #endif
   250 }
   251 
   252 bool s_isspace(char c)
   253 {
   254 	if ((unsigned)c & 0x80)
   255 	{
   256 		return false;
   257 	} else {
   258 		return isspace(c) ? true : false;
   259 	}
   260 }
   261 
   262 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   263 {
   264 	va_list ap;
   265 
   266 	va_start(ap, fmt);
   267 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   268 	va_end(ap);
   269 
   270 	if (len >= maxlength)
   271 	{
   272 		len = maxlength - 1;
   273 		buffer[len] = '\0';
   274 	}
   275 
   276 	return len;
   277 }
   278 
   279 bool RenameFile(const char *old, const char *newf)
   280 {
   281 #if defined __linux__
   282 	return (rename(old, newf) == 0);
   283 #elif defined WIN32
   284 	return (MoveFileA(old, newf) != 0);
   285 #endif
   286 }
   287 
   288 #if defined _MSC_VER
   289 extern "C" __declspec(dllexport) void *CreateInterface(const char *iface, int *ret)
   290 #elif defined __linux__
   291 extern "C" __attribute__((visibility("default"))) void *CreateInterface(const char *iface, int *ret)
   292 #endif
   293 {
   294 	if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
   295 	{
   296 		if (ret)
   297 		{
   298 			*ret = 0;
   299 		}
   300 		return GetThisPointer();
   301 	}
   302 
   303 	if (ret)
   304 	{
   305 		*ret = 1;
   306 	}
   307 	return NULL;
   308 }