4 // game_support.cpp - info to recognize different HL mod "games"
9 * This file is part of Metamod.
11 * Metamod is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * Metamod is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Metamod; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * In addition, as a special exception, the author gives permission to
26 * link the code of this program with the Half-Life Game Engine ("HL
27 * Engine") and Modified Game Libraries ("MODs") developed by Valve,
28 * L.L.C ("Valve"). You must obey the GNU General Public License in all
29 * respects for all of the code used other than the HL Engine and MODs
30 * from Valve. If you modify this file, you may extend this exception
31 * to your version of the file, but you are not obligated to do so. If
32 * you do not wish to do so, delete this exception statement from your
36 #include <fcntl.h> // open, write
38 #include <extdll.h> // always
40 #include "game_support.h" // me
41 #include "log_meta.h" // META_LOG, etc
42 #include "types_meta.h" // mBOOL
43 #include "osdep.h" // win32 snprintf, etc
45 // Adapted from adminmod h_export.cpp:
46 //! this structure contains a list of supported mods and their dlls names
47 //! To add support for another mod add an entry here, and add all the
48 //! exported entities to link_func.cpp
49 game_modlist_t known_games = {
50 // name/gamedir linux_so win_dll desc
52 // Previously enumerated in this sourcefile, the list is now kept in a
53 // separate file, generated based on game information stored in a
57 // End of list terminator:
58 {NULL, NULL, NULL, NULL}
61 // Find a modinfo corresponding to the given game name.
62 game_modinfo_t *lookup_game(const char *name) {
65 for(i=0; known_games[i].name; i++) {
67 if(strcasematch(imod->name, name))
76 mBOOL install_gamedll(char *from, const char *to) {
80 if ( NULL == from ) return mFALSE;
81 if ( NULL == to ) to = from;
83 byte* cachefile = LOAD_FILE_FOR_ME(from, &length_in);
85 // If the file seems to exist in the chache.
86 if ( NULL != cachefile ) {
88 int fd=open(to, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
90 META_DEBUG(3, ("Installing gamedll from cache: Failed to create file %s: %s\n", to, strerror(errno)) );
95 length_out=write(fd, cachefile, length_in);
99 // Writing the file was not successfull
100 if(length_out != length_in) {
101 META_DEBUG(3,("Installing gamedll from chache: Failed to write all %d bytes to file, only %d written: %s\n", length_in, length_out, strerror(errno)) );
102 // Let's not leave a mess but clean up nicely.
103 if (length_out >= 0) unlink(to);
107 META_LOG("Installed gamedll %s from cache.\n", to);
110 META_DEBUG(3, ("Failed to install gamedll from cache: file %s not found in cache.\n", from) );
117 // Set all the fields in the gamedll struct, - based either on an entry in
118 // known_games matching the current gamedir, or on one specified manually
119 // by the server admin.
121 // meta_errno values:
122 // - ME_NOTFOUND couldn't recognize game
123 mBOOL setup_gamedll(gamedll_t *gamedll) {
124 static char override_desc_buf[256];
125 game_modinfo_t *known;
127 char *knownfn = NULL;
130 // Check for old-style "metagame.ini" file and complain.
131 if(valid_gamedir_file(OLD_GAMEDLL_TXT))
132 META_ERROR("File '%s' is no longer supported; instead, specify override gamedll in %s or with '+localinfo mm_gamedll <dllfile>'", OLD_GAMEDLL_TXT, CONFIG_INI);
134 // First, look for a known game, based on gamedir.
135 if((known=lookup_game(gamedll->name))) {
137 knownfn=strdup(known->win_dll);
139 knownfn=strdup(known->linux_so);
141 #error "OS unrecognized"
145 // Neither override nor auto-detect found a gamedll.
146 if(!known && !Config->gamedll)
147 RETURN_ERRNO(mFALSE, ME_NOTFOUND);
149 // Use override-dll if specified.
150 if(Config->gamedll) {
151 STRNCPY(gamedll->pathname, Config->gamedll,
152 sizeof(gamedll->pathname));
155 // If the path is relative, the gamedll file will be missing and
156 // it might be found in the cache file.
157 if(!is_absolute_path(gamedll->pathname)) {
158 // I abuse the real_pathname member here to pass a full pathname to
159 // the install_gamedll function. I am somewhat opposed to be pushing
160 // another MAX_PATH sized array on the stack, that's why.
161 snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname),
162 "%s/%s", gamedll->gamedir, gamedll->pathname);
163 // If we could successfully install the gamedll from the cache we
164 // rectify the pathname to be a full pathname.
165 if(install_gamedll(gamedll->pathname, gamedll->real_pathname))
166 STRNCPY(gamedll->pathname, gamedll->real_pathname,
167 sizeof(gamedll->pathname));
170 // Else use Auto-detect dll.
172 // Again, as above, I abuse the real_pathname member to store the full pathname
173 // and the pathname member to store the relative name to pass it to the
174 // install_gamedll function to save stack space. They are going
175 // to get overwritten later on, so that's ok.
176 snprintf(gamedll->pathname, sizeof(gamedll->pathname), "dlls/%s",
178 // Check if the gamedll file exists. If not, try to install it from
181 if(!valid_gamedir_file(gamedll->pathname)) {
182 snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname), "%s/dlls/%s",
183 gamedll->gamedir, knownfn);
184 ok = install_gamedll(gamedll->pathname, gamedll->real_pathname);
187 // If the file still doesn't exist, and knownfn has an "_*.so"
188 // in it, try to strip the _* out.
190 if (char *loc = strrchr(knownfn, '_')) {
192 snprintf(gamedll->pathname, sizeof(gamedll->pathname), "dlls/%s", knownfn);
193 snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname),
194 gamedll->gamedir, knownfn);
195 install_gamedll(gamedll->pathname, gamedll->real_pathname);
199 // Now make an absolute path
200 snprintf(gamedll->pathname, sizeof(gamedll->pathname), "%s/dlls/%s",
201 gamedll->gamedir, knownfn);
204 // get filename from pathname
205 cp=strrchr(gamedll->pathname, '/');
207 else cp=gamedll->pathname;
210 // If found, store also the supposed "real" dll path based on the
211 // gamedir, in case it differs from the "override" dll path.
212 if(known && override)
213 snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname),
214 "%s/dlls/%s", gamedll->gamedir, knownfn);
215 else // !known or !override
216 STRNCPY(gamedll->real_pathname, gamedll->pathname,
217 sizeof(gamedll->real_pathname));
221 snprintf(override_desc_buf, sizeof(override_desc_buf),
222 "%s (override)", gamedll->file);
223 gamedll->desc=override_desc_buf;
225 META_LOG("Overriding game '%s' with dllfile '%s'", gamedll->name,
229 gamedll->desc=known->desc;
230 META_LOG("Recognized game '%s'; using dllfile '%s'", gamedll->name,