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