metamod/game_support.cpp
author darope
Tue Feb 12 19:01:13 2013 +0000 (2013-02-12)
branchB-work
changeset 230 bdf8171073e8
parent 229 585571487b0d
permissions -rw-r--r--
Edited DSO lookup: replace everything after the last '_', not just
_i386.so.
     1 // vi: set ts=4 sw=4 :
     2 // vim: set tw=75 :
     3 
     4 // game_support.cpp - info to recognize different HL mod "games"
     5 
     6 /*
     7  * Copyright (c) 2001-2003 Will Day <[email protected]>
     8  *
     9  *    This file is part of Metamod.
    10  *
    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.
    15  *
    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.
    20  *
    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
    24  *
    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
    33  *    version.
    34  *
    35  */
    36 #include <fcntl.h>          // open, write
    37 
    38 #include <extdll.h>			// always
    39 
    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
    44 
    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
    51 	//
    52 	// Previously enumerated in this sourcefile, the list is now kept in a
    53 	// separate file, generated based on game information stored in a 
    54 	// convenient db.
    55 	//
    56 #include "games.h"
    57 	// End of list terminator:
    58 	{NULL, NULL, NULL, NULL}
    59 };
    60 
    61 // Find a modinfo corresponding to the given game name.
    62 game_modinfo_t *lookup_game(const char *name) {
    63 	game_modinfo_t *imod;
    64 	int i;
    65 	for(i=0; known_games[i].name; i++) {
    66 		imod=&known_games[i];
    67 		if(strcasematch(imod->name, name))
    68 			return(imod);
    69 	}
    70 	// no match found
    71 	return(NULL);
    72 }
    73 
    74 
    75 
    76 mBOOL install_gamedll(char *from, const char *to) {
    77 	int length_in;
    78 	int length_out;
    79 
    80 	if ( NULL == from ) return mFALSE;
    81 	if ( NULL == to ) to = from;
    82 
    83 	byte* cachefile = LOAD_FILE_FOR_ME(from, &length_in);
    84 
    85 	// If the file seems to exist in the chache.
    86 	if ( NULL != cachefile ) {
    87 
    88 		int fd=open(to, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
    89 		if(fd < 0) {
    90 			META_DEBUG(3, ("Installing gamedll from cache: Failed to create file %s: %s\n", to, strerror(errno)) );
    91 			FREE_FILE(cachefile);
    92 			return mFALSE;
    93 		}
    94 	
    95 		length_out=write(fd, cachefile, length_in);
    96 		FREE_FILE(cachefile);
    97 		close(fd);
    98 	
    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);
   104 			return mFALSE;
   105 		}
   106 
   107 		META_LOG("Installed gamedll %s from cache.\n", to);
   108 
   109 	} else {
   110 		META_DEBUG(3, ("Failed to install gamedll from cache: file %s not found in cache.\n", from) );
   111 		return mFALSE;
   112 	}
   113 
   114 	return mTRUE;
   115 }
   116 
   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.
   120 //
   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;
   126 	const char *cp;
   127 	char *knownfn = NULL;
   128 	int override=0;
   129 
   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);
   133 
   134 	// First, look for a known game, based on gamedir.
   135 	if((known=lookup_game(gamedll->name))) {
   136 #ifdef _WIN32
   137 		knownfn=strdup(known->win_dll);
   138 #elif defined(linux)
   139 		knownfn=strdup(known->linux_so);
   140 #else
   141 #error "OS unrecognized"
   142 #endif /* _WIN32 */
   143 	}
   144 
   145 	// Neither override nor auto-detect found a gamedll.
   146 	if(!known && !Config->gamedll)
   147 			RETURN_ERRNO(mFALSE, ME_NOTFOUND);
   148 
   149 	// Use override-dll if specified.
   150 	if(Config->gamedll) {
   151 		STRNCPY(gamedll->pathname, Config->gamedll, 
   152 				sizeof(gamedll->pathname));
   153 		override=1;
   154 
   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));
   168 		}
   169 	}
   170 	// Else use Auto-detect dll.
   171 	else {
   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", 
   177 				knownfn);
   178 		// Check if the gamedll file exists. If not, try to install it from
   179 		// the cache.
   180 		mBOOL ok = mTRUE;
   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);
   185 		}
   186 
   187 		// If the file still doesn't exist, and knownfn has an "_*.so"
   188 		// in it, try to strip the _* out.
   189 		if (!ok) {
   190 			if (char *loc = strrchr(knownfn, '_')) {
   191 				strcpy(loc, ".so");
   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);
   196 			}
   197 		}
   198 
   199 		// Now make an absolute path
   200 		snprintf(gamedll->pathname, sizeof(gamedll->pathname), "%s/dlls/%s", 
   201 				gamedll->gamedir, knownfn);
   202 	}
   203 
   204 	// get filename from pathname
   205 	cp=strrchr(gamedll->pathname, '/');
   206 	if(cp) cp++;
   207 	else cp=gamedll->pathname;
   208 	gamedll->file=cp;
   209 
   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));
   218 	
   219 	if(override) {
   220 		// generate a desc
   221 		snprintf(override_desc_buf, sizeof(override_desc_buf), 
   222 				"%s (override)", gamedll->file);
   223 		gamedll->desc=override_desc_buf;
   224 		// log result
   225 		META_LOG("Overriding game '%s' with dllfile '%s'", gamedll->name, 
   226 				gamedll->file);
   227 	}
   228 	else {
   229 		gamedll->desc=known->desc;
   230 		META_LOG("Recognized game '%s'; using dllfile '%s'", gamedll->name, 
   231 				gamedll->file);
   232 	}
   233 
   234 	free(knownfn);
   235 
   236 	return(mTRUE);
   237 }