View Single Post
Re: [CZ] Changing model directory "on the fly"?
Old
  (#22)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: [CZ] Changing model directory "on the fly"? - 04-07-2005

Np, though hl is fighting me along the way though lol.
I have it at the point where it should work but then I get a "bad character in client command" error when I join my server.

That makes absolutely no sense at all, the path only gets the mapname added to the start :/

What goes in: weapons/emp_1.wav
What comes out: 2fort/weapons/emp_1.wav

All is good, no precache errors, the file was checked to exist and I even unhooked EmitSound to be sure.

Heres it so far, I did some re-designing so only the sound stuff is in right now.
The plugin framework is turning out nice too, so much easier now
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mplugin.h"

// Converts forward slashes to backslashes so the paths
// work under win32
void LinuxToWinPath( char* pszPath, int nCount ) {
#ifdef _WIN32
   register int _nCount = nCount;   // I wonder if "register" actually does anything these days

   // Chose to use _nCount so hopefully the compiler would choose to
   // compare to registers rather than a memory location on each iteration.
   for ( register int i = 0; i < _nCount; i++ ) {
	  // Convert forward slashes to backslashes so
	  // this will work under windows
	  if ( pszPath[ i ] == '/' )
		 pszPath[ i ] = '\\';
   }
#endif
}

// NOTE:
// Make SURE pszSearchdir is set! ( ie. "sound" or "models" )
// Otherwise, fopen will fail and so will this function!
int CheckFileExists( const char* pszFile, const char* pszSearchdir ) {
   char szGamedir[ _MAX_PATH ];  // Mod root directory
   char szNewpath[ _MAX_PATH ];  // Where to look
   FILE* pFile = NULL;		   // Temporary file handle

   // Get the mod directory and setup our filename string
   GET_GAME_DIR( szGamedir );
   _snprintf( szNewpath, sizeof( szNewpath ), "%s\\%s\\%s", szGamedir, pszSearchdir, pszFile );

   // Make sure the slashes are right if running under win32
   // Note:
   // Under linux, the LinuxToWinPath function will do nothing. 
   LinuxToWinPath( szNewpath, strlen( szNewpath ) );

   // Attempt to open the file, if it exists pFile should be a valid pointer.
   if ( ( pFile = fopen( szNewpath, "rb" ) ) != NULL ) {
	  fclose( pFile );
	  return 1;
   }

   // Not found
   return 0;
}

// Old : weapons/dbarrel1.wav
// New : 2fort/weapons/dbarrel1.wav
int ChangeSearchPath( const char* pszFile, const char* pszSearchdir, char* pszNewpathbuf, size_t lMaxbuflen ) {
   char szNewpath[ _MAX_PATH ];

   // Add the mapname to the beginning of the string
   _snprintf( szNewpath, sizeof( szNewpath ), "%s/%s", STRING( gpGlobals->mapname ), pszFile );

   // If the file exists, give the caller the new path and return success
   if ( CheckFileExists( szNewpath, pszSearchdir ) == 1 ) {
	  strncpy( pszNewpathbuf, szNewpath, lMaxbuflen );
	  return 1;
   }

   // No updated version of this file
   return 0;
}

int PrecacheModel( char* pszModel ) {
   RETURN_META_VALUE( MRES_IGNORED, 1 );   
}
   
int PrecacheSound( char* pszSound ) {
   char szNewpath[ _MAX_PATH ];

 RETURN_META_VALUE( MRES_SUPERCEDE, ChangeSearchPath( pszSound, "sound", szNewpath, sizeof( szNewpath ) ) == 0 ? PRECACHE_SOUND( pszSound ) : PRECACHE_SOUND( szNewpath ) );
}
   
void SetModel( edict_t* pEntity, const char* pszModel ) {
   RETURN_META( MRES_IGNORED );
}

void EmitSound( edict_t* pEntity, int nChan, const char* pszSample, float flVolume, float flAttenuation, int iFlags, int iPitch ) {
   char szNewpath[ _MAX_PATH ];

 RETURN_META_VALUE( MRES_SUPERCEDE, ChangeSearchPath( pszSample, "sound", szNewpath, sizeof( szNewpath ) ) == 0 ? g_engfuncs.pfnEmitSound( pEntity, nChan, pszSample, flVolume, flAttenuation, iFlags, iPitch ) : g_engfuncs.pfnEmitSound( pEntity, nChan, szNewpath, flVolume, flAttenuation, iFlags, iPitch ) );
}

BEGIN_GIVEFNPTRSTODLL( )
END_GIVEFNPTRSTODLL( )

BEGIN_META_ATTACH( )
END_META_ATTACH( )

BEGIN_META_QUERY( )
END_META_QUERY( )

BEGIN_META_DETACH( )
END_META_DETACH( )

BEGIN_GETENGINEFUNCTIONS( )
   //HOOK_ENGINE_FN( pfnPrecacheModel, PrecacheModel );
   HOOK_ENGINE_FN( pfnPrecacheSound, PrecacheSound );
   //HOOK_ENGINE_FN( pfnEmitSound, EmitSound );
   //HOOK_ENGINE_FN( pfnSetModel, SetModel );
END_GETENGINEFUNCTIONS( )
It's 3:32am here and I'm just not seeing it, maybe someone knows why this is happening?

I put a breakpoint just before PrecacheSound returns, the updated path is normal with no unusual characters of any kind.
That is whats bothering me so much.

Only a small delay though, this will get fixed.
  
Reply With Quote