6 extern "C" void GetGameDir(char *buffer, int maxlength);
7 extern "C" void *GetThisPointer();
11 #define MMPATH "|gameinfo_path|addons\\metamod\\bin"
12 #define WINDOWS_LEAN_AND_MEAN
14 #elif defined __linux__
16 #define MMPATH "|gameinfo_path|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()
33 GetGameDir(gamedir, sizeof(gamedir));
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);
41 FILE *fp = fopen(old_path, "rt");
48 FILE *op = fopen(new_path, "wt");
64 ParseState ps = Parse_Root;
69 while (!feof(fp) && fgets(input, sizeof(input), fp) != NULL)
71 UTIL_Format(backup, sizeof(backup), "%s", input);
77 /* Strip beginning whitespace */
78 while (*inbuf != '\0' && s_isspace(*inbuf))
83 /* Strip ending whitespace */
84 size_t len = strlen(inbuf);
85 for (size_t i = len - 1;
89 if (s_isspace(inbuf[i]))
98 /* Strip quotation marks */
100 && inbuf[len-1] == '"')
102 inbuf[len - 1] = '\0';
108 if (ps == Parse_Root && strcmp(inbuf, "GameInfo") == 0)
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");
119 if (strstr(game, "GameBin") != NULL
120 && strstr(game, MMPATH) != NULL)
124 break; /* Nothing more to do! */
126 fputs("\t\t\t\tGameBin\t\t" MMPATH "\n", op);
138 /* Well, we can't really do anything else. Give up. */
143 /* Close all streams */
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);
151 if (!RenameFile(old_path, backup_name))
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!
159 if (!RenameFile(new_path, old_path))
161 /* Since this failed, we really have no choice.
162 * Try and rename the old back.
164 RenameFile(backup_name, old_path);
174 bool s_isspace(char c)
176 if ((unsigned)c & 0x80)
180 return isspace(c) ? true : false;
184 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
189 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
192 if (len >= maxlength)
201 bool RenameFile(const char *old, const char *newf)
203 #if defined __linux__
204 return (rename(old, newf) == 0);
206 return (MoveFileA(old, newf) != 0);
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)
216 if (strcmp(iface, "ISERVERPLUGINCALLBACKS001") == 0)
222 return GetThisPointer();