6 extern "C" void GetGameDir(char *buffer, int maxlength);
7 extern "C" void *GetThisPointer();
11 #define MMPATH "addons\\metamod\\bin"
12 #define WINDOWS_LEAN_AND_MEAN
14 #elif defined __linux__
16 #define MMPATH "addons/metamod/bin"
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);
24 /* This will be called by the thunk */
26 extern "C" void LoadFunction()
27 #elif defined __linux__
28 extern "C" void _LoadFunction()
34 GetGameDir(gamedir, sizeof(gamedir));
37 UTIL_Format(mmpath, sizeof(mmpath), "|gameinfo_path|%s", MMPATH);
41 UTIL_Format(config, sizeof(config), "%s" SEPCHAR "sourcemm_updater.conf", gamedir);
42 FILE *fpCfg = fopen(config, "rt");
46 while (!feof(fpCfg) && fgets(cfgLine, sizeof(cfgLine), fpCfg) != NULL)
52 /* Strip whitespace */
53 char *input = cfgLine;
54 while (*input != '\0' && s_isspace(*input))
59 /* Strip ending whitespace */
60 size_t len = strlen(input);
61 for (size_t i = len - 1;
65 if (s_isspace(input[i]))
74 /* Eat stuff until we find a key */
75 while (*input != '\0' && !s_isspace(*input))
77 if (keyLen < sizeof(key))
79 key[keyLen++] = *input;
85 /* Eat spaces until we hit an = sign */
86 while (*input != '\0' && *input != '=')
96 /* Eat spaces again */
97 while (*input != '\0' && s_isspace(*input))
102 /* The rest is our key */
103 if (strcmp(key, "mmpath") == 0)
105 UTIL_Format(mmpath, sizeof(mmpath), "%s", input);
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);
117 FILE *fp = fopen(old_path, "rt");
124 FILE *op = fopen(new_path, "wt");
140 ParseState ps = Parse_Root;
145 while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
147 UTIL_Format(backup, sizeof(backup), "%s", input);
149 if (ps != Parse_None)
153 /* Strip beginning whitespace */
154 while (*inbuf != '\0' && s_isspace(*inbuf))
159 /* Strip ending whitespace */
160 size_t len = strlen(inbuf);
161 for (size_t i = len - 1;
165 if (s_isspace(inbuf[i]))
174 /* Strip quotation marks */
176 && inbuf[len-1] == '"')
178 inbuf[len - 1] = '\0';
184 if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
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");
195 if (strstr(game, "GameBin") != NULL
196 && strstr(game, mmpath) != NULL)
200 break; /* Nothing more to do! */
202 fputs("\t\t\tGameBin\t\t\t|gameinfo_path|", op);
216 /* Well, we can't really do anything else. Give up. */
221 /* Close all streams */
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);
229 if (!RenameFile(old_path, backup_name))
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!
237 if (!RenameFile(new_path, old_path))
239 /* Since this failed, we really have no choice.
240 * Try and rename the old back.
242 RenameFile(backup_name, old_path);
252 bool s_isspace(char c)
254 if ((unsigned)c & 0x80)
258 return isspace(c) ? true : false;
262 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
267 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
270 if (len >= maxlength)
279 bool RenameFile(const char *old, const char *newf)
281 #if defined __linux__
282 return (rename(old, newf) == 0);
284 return (MoveFileA(old, newf) != 0);
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)
294 if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
300 return GetThisPointer();