update_tool/update_tool.cpp
author David Anderson <dvander@alliedmods.net>
Sat Mar 31 16:23:19 2007 +0000 (2007-03-31)
changeset 300 807620eb4f4e
child 302 a6556cd5cbaf
permissions -rw-r--r--
initial import of the updater tool
     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			"|gameinfo_path|addons\\metamod\\bin"
    12 #define WINDOWS_LEAN_AND_MEAN
    13 #include <windows.h>
    14 #elif defined __linux__
    15 #define SEPCHAR			"/"
    16 #define MMPATH			"|gameinfo_path|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 
    33 	GetGameDir(gamedir, sizeof(gamedir));
    34 
    35 	char old_path[260];
    36 	char new_path[260];
    37 
    38 	UTIL_Format(old_path, sizeof(old_path), "%s" SEPCHAR "gameinfo.txt", gamedir);
    39 	UTIL_Format(new_path, sizeof(new_path), "%s" SEPCHAR "gameinfo.new.txt", gamedir);
    40 
    41 	FILE *fp = fopen(old_path, "rt");
    42 	
    43 	if (!fp)
    44 	{
    45 		return;
    46 	}
    47 
    48 	FILE *op = fopen(new_path, "wt");
    49 	if (!op)
    50 	{
    51 		fclose(fp);
    52 		return;
    53 	}
    54 
    55 	enum ParseState
    56 	{
    57 		Parse_None,
    58 		Parse_Root,
    59 		Parse_GameInfo,
    60 		Parse_FileSystem,
    61 		Parse_SearchPaths,
    62 	};
    63 
    64 	ParseState ps = Parse_Root;
    65 
    66 	char input[1024];
    67 	char backup[1024];
    68 	
    69 	while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
    70 	{
    71 		UTIL_Format(backup, sizeof(backup), "%s", input);
    72 
    73 		if (ps != Parse_None)
    74 		{
    75 			char *inbuf = input;
    76 			
    77 			/* Strip beginning whitespace */
    78 			while (*inbuf != '\0' && s_isspace(*inbuf))
    79 			{
    80 				inbuf++;
    81 			}
    82 
    83 			/* Strip ending whitespace */
    84 			size_t len = strlen(inbuf);
    85 			for (size_t i = len - 1;
    86 				 i >= 0 && i < len;
    87 				 i--)
    88 			{
    89 				if (s_isspace(inbuf[i]))
    90 				{
    91 					inbuf[i] = '\0';
    92 					len--;
    93 				} else {
    94 					break;
    95 				}
    96 			}
    97 
    98 			/* Strip quotation marks */
    99 			if (inbuf[0] == '"'
   100 				&& inbuf[len-1] == '"')
   101 			{
   102 				inbuf[len - 1] = '\0';
   103 				inbuf = &inbuf[1];
   104 				len -= 2;
   105 			}
   106 
   107 			/* Do tests */
   108 			if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
   109 			{
   110 				ps = Parse_GameInfo;
   111 			} else if (ps == Parse_GameInfo && strcmp(inbuf, "FileSystem") == 0) {
   112 				ps = Parse_FileSystem;
   113 			} else if (ps == Parse_FileSystem && strcmp(inbuf, "SearchPaths") == 0) {
   114 				ps = Parse_SearchPaths;
   115 			} else if (ps == Parse_SearchPaths) {
   116 				const char *game = strstr(inbuf, "Game");
   117 				if (game)
   118 				{
   119 					if (strstr(game, "GameBin") != NULL
   120 						&& strstr(game, MMPATH) != NULL)
   121 					{
   122 						fclose(op);
   123 						op = NULL;
   124 						break;			/* Nothing more to do! */
   125 					} else {
   126 						fputs("\t\t\t\tGameBin\t\t" MMPATH "\n", op);
   127 						ps = Parse_None;
   128 					}
   129 				}
   130 			}
   131 		}
   132 
   133 		fputs(backup, op);
   134 	}
   135 
   136 	if (!op)
   137 	{
   138 		/* Well, we can't really do anything else.  Give up.  */
   139 		fclose(fp);
   140 		return;
   141 	}
   142 
   143 	/* Close all streams */
   144 	fclose(op);
   145 	fclose(fp);
   146 
   147 	/* Move the old file to a backup name */
   148 	char backup_name[260];
   149 	UTIL_Format(backup_name, sizeof(backup_name), "%s" SEPCHAR "gameinfo.backup.txt", gamedir);
   150 
   151 	if (!RenameFile(old_path, backup_name))
   152 	{
   153 		/* If we can't rename, just bail out.
   154 		 * We don't want to overwrite the client's default
   155 		 * without backing it up first!
   156 		 */
   157 		return;
   158 	}
   159 	if (!RenameFile(new_path, old_path))
   160 	{
   161 		/* Since this failed, we really have no choice.
   162 		 * Try and rename the old back.
   163 		 */
   164 		RenameFile(backup_name, old_path);
   165 		return;
   166 	}
   167 #if defined _MSC_VER
   168 	_unlink(new_path);
   169 #else
   170 	unlink(new_path);
   171 #endif
   172 }
   173 
   174 bool s_isspace(char c)
   175 {
   176 	if ((unsigned)c & 0x80)
   177 	{
   178 		return false;
   179 	} else {
   180 		return isspace(c) ? true : false;
   181 	}
   182 }
   183 
   184 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   185 {
   186 	va_list ap;
   187 
   188 	va_start(ap, fmt);
   189 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   190 	va_end(ap);
   191 
   192 	if (len >= maxlength)
   193 	{
   194 		len = maxlength - 1;
   195 		buffer[len] = '\0';
   196 	}
   197 
   198 	return len;
   199 }
   200 
   201 bool RenameFile(const char *old, const char *newf)
   202 {
   203 #if defined __linux__
   204 	return (rename(old, newf) == 0);
   205 #elif defined WIN32
   206 	return (MoveFileA(old, newf) != 0);
   207 #endif
   208 }
   209 
   210 #if defined _MSC_VER
   211 extern "C" __declspec(dllexport) void *CreateInterface(const char *iface, int *ret)
   212 #elif defined __linux__
   213 extern "C" __attribute__((visibility("default"))) void *CreateInterface(const char *iface, int *ret)
   214 #endif
   215 {
   216 	if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
   217 	{
   218 		if (ret)
   219 		{
   220 			*ret = 0;
   221 		}
   222 		return GetThisPointer();
   223 	}
   224 
   225 	if (ret)
   226 	{
   227 		*ret = 1;
   228 	}
   229 	return NULL;
   230 }