metamod/game_support.cpp
changeset 275 f62b46dcf019
parent 267 b75724ad6b58
     1.1 --- a/metamod/game_support.cpp	Fri Mar 15 14:59:27 2013 -0500
     1.2 +++ b/metamod/game_support.cpp	Fri Mar 15 15:01:19 2013 -0500
     1.3 @@ -4,7 +4,7 @@
     1.4  // game_support.cpp - info to recognize different HL mod "games"
     1.5  
     1.6  /*
     1.7 - * Copyright (c) 2001-2003 Will Day <[email protected]>
     1.8 + * Copyright (c) 2001-2013 Will Day <[email protected]>
     1.9   *
    1.10   *    This file is part of Metamod.
    1.11   *
    1.12 @@ -98,7 +98,8 @@
    1.13  	
    1.14  		// Writing the file was not successfull
    1.15  		if(length_out != length_in) {
    1.16 -			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)) );
    1.17 +			META_DEBUG(3,("Installing gamedll from chache: Failed to write all %d bytes to file, only %d written: %s\n",
    1.18 +							length_in, length_out, strerror(errno)) );
    1.19  			// Let's not leave a mess but clean up nicely.
    1.20  			if (length_out >= 0) unlink(to);
    1.21  			return mFALSE;
    1.22 @@ -124,7 +125,9 @@
    1.23  	static char override_desc_buf[256];
    1.24  	game_modinfo_t *known;
    1.25  	const char *cp;
    1.26 -	char *knownfn = NULL;
    1.27 +	const char *knownfn = 0;
    1.28 +	const char *usedfn = 0;
    1.29 +	char *strippedfn = 0;
    1.30  	int override=0;
    1.31  
    1.32  	// Check for old-style "metagame.ini" file and complain.
    1.33 @@ -171,36 +174,74 @@
    1.34  	}
    1.35  	// Else use Auto-detect dll.
    1.36  	else {
    1.37 -		// Again, as above, I abuse the real_pathname member to store the full pathname
    1.38 -		// and the pathname member to store the relative name to pass it to the 
    1.39 -		// install_gamedll function to save stack space. They are going
    1.40 -		// to get overwritten later on, so that's ok.
    1.41 -		snprintf(gamedll->pathname, sizeof(gamedll->pathname), "dlls/%s", 
    1.42 -				knownfn);
    1.43 -		// Check if the gamedll file exists. If not, try to install it from
    1.44 -		// the cache.
    1.45 -		mBOOL ok = mTRUE;
    1.46 -		if(!valid_gamedir_file(gamedll->pathname)) {
    1.47 -			snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname), "%s/dlls/%s", 
    1.48 -					gamedll->gamedir, knownfn);
    1.49 -			ok = install_gamedll(gamedll->pathname, gamedll->real_pathname);
    1.50 +#ifdef linux
    1.51 +		// The engine changed game dll lookup behaviour in that it strips
    1.52 +		// anything after the last '_' from the name and tries to load the
    1.53 +		// resulting name. The DSO names were changed and do not have the
    1.54 +		// '_i386' part in them anymore, so cs_i386.so became cs.so. We
    1.55 +		// have to adapt to that and try to load the DSO name without the
    1.56 +		// '_*' part first, to see if we have a new version file available.
    1.57 +
    1.58 +		strippedfn = strdup(knownfn);
    1.59 +
    1.60 +		char *loc = strrchr(strippedfn, '_');
    1.61 +
    1.62 +		// A small safety net here: make sure that we are dealing with
    1.63 +		// a file name at least four characters long and ending in
    1.64 +		// '.so'. This way we can be sure that we can safely overwrite
    1.65 +		// anything from the '_' on with '.so'.
    1.66 +		int size = 0;
    1.67 +		const char *ext;
    1.68 +		if(0 != loc) {
    1.69 +			size = strlen(strippedfn);
    1.70 +			ext = strippedfn + (size - 3);
    1.71  		}
    1.72  
    1.73 -		// If the file still doesn't exist, and knownfn has an "_i386.so"
    1.74 -		// in it, try to strip the _i386.so out.
    1.75 -		if (!ok) {
    1.76 -			if (char *loc = strstr(knownfn, "_i386.so")) {
    1.77 -				strcpy(loc, ".so");
    1.78 -				snprintf(gamedll->pathname, sizeof(gamedll->pathname), "dlls/%s", knownfn);
    1.79 -				snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname),
    1.80 +		if(0 != loc && size > 3 && 0 == strcasecmp(ext, ".so")) {
    1.81 +			
    1.82 +			strcpy(loc, ".so");
    1.83 +			META_DEBUG(4, ("Checking for new version game DLL name '%s'.\n", strippedfn) );
    1.84 +
    1.85 +			// Again, as above, I abuse the real_pathname member to store the full pathname
    1.86 +			// and the pathname member to store the relative name to pass it to the 
    1.87 +			// install_gamedll function to save stack space. They are going
    1.88 +			// to get overwritten later on, so that's ok.
    1.89 +			snprintf(gamedll->pathname, sizeof(gamedll->pathname), "dlls/%s", 
    1.90 +					 strippedfn);
    1.91 +			// Check if the gamedll file exists. If not, try to install it from
    1.92 +			// the cache.
    1.93 +			mBOOL ok = mTRUE;
    1.94 +			if(!valid_gamedir_file(gamedll->pathname)) {
    1.95 +				snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname), "%s/dlls/%s", 
    1.96 +						 gamedll->gamedir, strippedfn);
    1.97 +				ok = install_gamedll(gamedll->pathname, gamedll->real_pathname);
    1.98 +			}
    1.99 +			if(ok) usedfn = strippedfn;
   1.100 +		}
   1.101 +		else {
   1.102 +			META_DEBUG(4, ("Known game DLL name does not qualify for checking for a stripped version, skipping: '%s'.\n",
   1.103 +							strippedfn) );
   1.104 +		}
   1.105 +#endif /* linux */
   1.106 +
   1.107 +		// If no file to be used was found, try the old known DLL file
   1.108 +		// name.
   1.109 +		if (0 == usedfn) {
   1.110 +			META_DEBUG(4, ("Checking for old version game DLL name '%s'.\n", knownfn) );
   1.111 +			snprintf(gamedll->pathname, sizeof(gamedll->pathname), "dlls/%s", knownfn);
   1.112 +			// Check if the gamedll file exists. If not, try to install it from
   1.113 +			// the cache.
   1.114 +			if(!valid_gamedir_file(gamedll->pathname)) {
   1.115 +				snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname), "%s/dlls/%s", 
   1.116  						gamedll->gamedir, knownfn);
   1.117  				install_gamedll(gamedll->pathname, gamedll->real_pathname);
   1.118  			}
   1.119 +			usedfn = knownfn;
   1.120  		}
   1.121  
   1.122  		// Now make an absolute path
   1.123  		snprintf(gamedll->pathname, sizeof(gamedll->pathname), "%s/dlls/%s", 
   1.124 -				gamedll->gamedir, knownfn);
   1.125 +				gamedll->gamedir, usedfn);
   1.126  	}
   1.127  
   1.128  	// get filename from pathname
   1.129 @@ -213,7 +254,7 @@
   1.130  	// gamedir, in case it differs from the "override" dll path.
   1.131  	if(known && override)
   1.132  		snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname),
   1.133 -				"%s/dlls/%s", gamedll->gamedir, knownfn);
   1.134 +				"%s/dlls/%s", gamedll->gamedir, usedfn);
   1.135  	else // !known or !override
   1.136  		STRNCPY(gamedll->real_pathname, gamedll->pathname, 
   1.137  				sizeof(gamedll->real_pathname));
   1.138 @@ -233,7 +274,7 @@
   1.139  				gamedll->file);
   1.140  	}
   1.141  
   1.142 -	free(knownfn);
   1.143 +	if(0 != strippedfn) free(strippedfn);
   1.144  
   1.145  	return(mTRUE);
   1.146  }