.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   Half-Life 1 SDK (http://forums.bots-united.com/forumdisplay.php?f=33)
-   -   [CZ] Changing model directory "on the fly"? (http://forums.bots-united.com/showthread.php?t=4158)

Rusty Le Cyborg 03-07-2005 22:55

Re: [CZ] Changing model directory "on the fly"?
 
Okay. Thanks for going to all this trouble, it's much appreciated :)

Lazy 04-07-2005 09:26

Re: [CZ] Changing model directory "on the fly"?
 
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.

Rusty Le Cyborg 04-07-2005 20:33

Re: [CZ] Changing model directory "on the fly"?
 
Hmm.. not being knowledgable about these things I had a look at Steampowered about this...and there was a particular support query about this...

Does this help?

http://steampowered.custhelp.com/cgi...i=&p_topview=1

Lazy 04-07-2005 20:42

Re: [CZ] Changing model directory "on the fly"?
 
Thanks but the server is running on the same machine I play/develop on and the problem goes away when my code is disabled.

I'm going to take another look at it now but I can't see anything wrong.

Pierre-Marie Baty 04-07-2005 21:10

Re: [CZ] Changing model directory "on the fly"?
 
Lazy, for the player models, you will certainly need to set them in the client's key/value data.

Another tip, don't bother converting forward slashes to backwards slashes, even on Windows. Always use forward slashes, everywhere.

And at least one big mistake: define your local character arrays as static if you return pointers to them, else they are deallocated when the function exits.

Lazy 04-07-2005 21:30

Re: [CZ] Changing model directory "on the fly"?
 
I never return pointers to local variables, what I did is make the caller pass a pointer to it's own local variable which gets the new string copied into it.

And the only time I convert to backslashes is when I use fopen to check if the file exists or not, but I still have no idea why I'm getting this error.

OR, is hl keeping a pointer to szNewpath when I call PRECACHE_SOUND?
That would explain a lot but I'd assume it makes a copy first.

[Added]
Well, it looks like HL needs a copy of the string.
The problem was solved by changing szNewpath to STRING( ALLOC_STRING( szNewpath ) ).
Bah, I would never keep a string pointer like that.

Thanks PMB, now I can get back to finishing this thing http://forums.bots-united.org/images...es/biggrin.gif

CyKo74 05-07-2005 00:22

Re: [CZ] Changing model directory "on the fly"?
 
...And dont worry about register thingy. c & cpp copilers are "smart" enough to do it automaticaly. Check youreself by compiling & re-engeneering the output code. Btw why dont make those lil func's inline or macros?

Lazy 05-07-2005 00:36

Re: [CZ] Changing model directory "on the fly"?
 
The only time I read code output was when I was trying to make my function hooking class, took me a long time to get it right but I finally got that JMP instruction to work :)

Anyway, which functions are you talking about?
The BEGIN_xxx functions are just macros that handle setting up the dll automatically and hooking various APIs.

Rusty Le Cyborg 06-07-2005 18:26

Re: [CZ] Changing model directory "on the fly"?
 
Quote:

Originally Posted by Lazy

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

Just a quick question (again!)

Having the directory structure like this will mean that your mod root directory will be rather full of "extra" entries...

Would it be better to have a new directory to dump your map specific models/sounds into?

Something like "MOD ROOT / lazy / %s / weapons / ..." etc or is that a different kettle of fish entirely?

Cheers for now

Lazy 10-07-2005 01:34

Re: [CZ] Changing model directory "on the fly"?
 
I don't think its possible for sounds since hl will only play whats inside the sound directory and its subdirectories.
It used to be possible under won, in tfc I would have it speak sounds from cs under tfc by using something like ../../../../cstrike/sound/terwin and it worked for those who had cs installed.
But since steam that sort of thing wont work, which is why I chose that layout.


All times are GMT +2. The time now is 02:46.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.