8 #define MMPATH "addons\\metamod\\bin"
9 #define WINDOWS_LEAN_AND_MEAN
11 #elif defined __linux__
13 #define MMPATH "addons/metamod/bin"
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();
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);
29 /* This will be called by the thunk */
31 extern "C" void LoadFunction()
32 #elif defined __linux__
33 extern "C" void _LoadFunction()
36 ICvar *pCvar = GetICvar();
37 if (pCvar->FindVar("metamod_version") != NULL)
39 /* Already exists, bail out */
53 RestartMode mode = Restart_Error;
55 GetGameDir(gamedir, sizeof(gamedir));
58 UTIL_Format(mmpath, sizeof(mmpath), "|gameinfo_path|%s", MMPATH);
62 UTIL_Format(config, sizeof(config), "%s" SEPCHAR "sourcemm_updater.conf", gamedir);
63 FILE *fpCfg = fopen(config, "rt");
67 while (!feof(fpCfg) && fgets(cfgLine, sizeof(cfgLine), fpCfg) != NULL)
73 /* Strip whitespace */
74 char *input = cfgLine;
75 while (*input != '\0' && s_isspace(*input))
80 /* Strip ending whitespace */
81 size_t len = strlen(input);
82 for (size_t i = len - 1;
86 if (s_isspace(input[i]))
95 /* Eat stuff until we find a key */
96 while (*input != '\0' && !s_isspace(*input))
98 if (keyLen < sizeof(key))
100 key[keyLen++] = *input;
106 /* Eat spaces until we hit an = sign */
107 while (*input != '\0' && *input != '=')
117 /* Eat spaces again */
118 while (*input != '\0' && s_isspace(*input))
123 /* Ignore comments */
129 /* The rest is our key */
130 if (strcmp(key, "mmpath") == 0)
132 UTIL_Format(mmpath, sizeof(mmpath), "%s", input);
133 } else if (strcmp(key, "restart") == 0) {
134 if (strcmp(input, "never") == 0)
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_Never;
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);
153 FILE *fp = fopen(old_path, "rt");
160 FILE *op = fopen(new_path, "wt");
176 ParseState ps = Parse_Root;
181 bool bWroteOutput = false;
183 while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
185 UTIL_Format(backup, sizeof(backup), "%s", input);
187 if (ps != Parse_None)
191 /* Strip beginning whitespace */
192 while (*inbuf != '\0' && s_isspace(*inbuf))
197 /* Strip ending whitespace */
198 size_t len = strlen(inbuf);
199 for (size_t i = len - 1;
203 if (s_isspace(inbuf[i]))
212 /* Strip quotation marks */
214 && inbuf[len-1] == '"')
216 inbuf[len - 1] = '\0';
222 if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
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");
233 if (strstr(game, "GameBin") != NULL
234 && strstr(game, mmpath) != NULL)
238 break; /* Nothing more to do! */
240 fputs("\t\t\tGameBin\t\t\t", op);
255 /* Well, we can't really do anything else. Give up. */
260 /* Close all streams */
264 /* If we didn't change anything, abort here */
267 RemoveFile(new_path);
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);
275 if (!RenameFile(old_path, backup_name))
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!
283 if (!RenameFile(new_path, old_path))
285 /* Since this failed, we really have no choice.
286 * Try and rename the old back.
288 RenameFile(backup_name, old_path);
291 RemoveFile(new_path);
293 if (mode == Restart_Error)
295 Error("Server is restarting to load Metamod:Source");
296 } else if (mode == Restart_Quit) {
297 ServerCommand("quit\n");
301 bool RemoveFile(const char *file)
304 return (_unlink(file) == 0);
306 return (unlink(file) == 0);
310 bool s_isspace(char c)
312 if ((unsigned)c & 0x80)
316 return isspace(c) ? true : false;
320 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
325 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
328 if (len >= maxlength)
337 bool RenameFile(const char *old, const char *newf)
339 #if defined __linux__
340 return (rename(old, newf) == 0);
342 return (MoveFileA(old, newf) != 0);
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)
352 if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
358 return GetThisPointer();