scenemanager_tools.cpp

来自「hl2 source code. Do not use it illegal.」· C++ 代码 · 共 780 行 · 第 1/2 页

CPP
780
字号
		int len = Q_snprintf( path, maxpath, "\\%s", dir );
   }
   else
   {
	   path[0] = 0;
   }
   Q_snprintf( filename, maxfilename, "%s%s", fname, ext );
}


void VSS_Checkout( char const *name, bool updatestaticons /*= true*/ )
{
	CWorkspace *ws = GetWorkspaceManager()->GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		return;
	}

	if ( filesystem->IsFileWritable( name ) )
	{
		return;
	}

	char path[ 256 ];
	char filename[ 256 ];

	SplitFileName( name, path, sizeof( path ), filename, sizeof( filename ) );

	Con_ColorPrintf( 200, 200, 100, "VSS Checkout:  '%s'\n", name );

	SceneManager_VSSCheckout( 
		ws->GetVSSUserName(),
		ws->GetVSSProject(),
		path,
		va( "%s%s", gamedir, path ),
		filename );

	if ( updatestaticons )
	{
		GetWorkspaceManager()->RefreshBrowsers();
	}
}

void VSS_Checkin( char const *name, bool updatestaticons /*= true*/ )
{
	CWorkspace *ws = GetWorkspaceManager()->GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		return;
	}

	if ( !filesystem->IsFileWritable( name ) )
	{
		return;
	}

	char path[ 256 ];
	char filename[ 256 ];

	SplitFileName( name, path, sizeof( path ), filename, sizeof( filename ) );

	Con_ColorPrintf( 200, 200, 100, "VSS Checkin:  '%s'\n", name );

	SceneManager_VSSCheckin( 
		ws->GetVSSUserName(),
		ws->GetVSSProject(),
		path,
		va( "%s%s", gamedir, path ),
		filename );

	if ( updatestaticons )
	{
		GetWorkspaceManager()->RefreshBrowsers();
	}
}


//-----------------------------------------------------------------------------
// Purpose: Implements the RIFF i/o interface on stdio
//-----------------------------------------------------------------------------
class StdIOReadBinary : public IFileReadBinary
{
public:
	int open( const char *pFileName )
	{
		return (int)filesystem->Open( pFileName, "rb" );
	}

	int read( void *pOutput, int size, int file )
	{
		if ( !file )
			return 0;

		return filesystem->Read( pOutput, size, (FileHandle_t)file );
	}

	void seek( int file, int pos )
	{
		if ( !file )
			return;

		filesystem->Seek( (FileHandle_t)file, pos, FILESYSTEM_SEEK_HEAD );
	}

	unsigned int tell( int file )
	{
		if ( !file )
			return 0;

		return filesystem->Tell( (FileHandle_t)file );
	}

	unsigned int size( int file )
	{
		if ( !file )
			return 0;

		return filesystem->Size( (FileHandle_t)file );
	}

	void close( int file )
	{
		if ( !file )
			return;

		filesystem->Close( (FileHandle_t)file );
	}
};

class StdIOWriteBinary : public IFileWriteBinary
{
public:
	int create( const char *pFileName )
	{
		return (int)filesystem->Open( pFileName, "wb" );
	}

	int write( void *pData, int size, int file )
	{
		return filesystem->Write( pData, size, (FileHandle_t)file );
	}

	void close( int file )
	{
		filesystem->Close( (FileHandle_t)file );
	}

	void seek( int file, int pos )
	{
		filesystem->Seek( (FileHandle_t)file, pos, FILESYSTEM_SEEK_HEAD );
	}

	unsigned int tell( int file )
	{
		return filesystem->Tell( (FileHandle_t)file );
	}
};

static StdIOReadBinary io_in;
static StdIOWriteBinary io_out;

#define RIFF_WAVE			MAKEID('W','A','V','E')
#define WAVE_FMT			MAKEID('f','m','t',' ')
#define WAVE_DATA			MAKEID('d','a','t','a')
#define WAVE_FACT			MAKEID('f','a','c','t')
#define WAVE_CUE			MAKEID('c','u','e',' ')

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &walk - 
//-----------------------------------------------------------------------------
static void SceneManager_ParseSentence( CSentence& sentence, IterateRIFF &walk )
{
	CUtlBuffer buf( 0, 0, true );

	buf.EnsureCapacity( walk.ChunkSize() );
	walk.ChunkRead( buf.Base() );
	buf.SeekPut( CUtlBuffer::SEEK_HEAD, walk.ChunkSize() );

	sentence.InitFromDataChunk( buf.Base(), buf.TellPut() );
}

bool SceneManager_LoadSentenceFromWavFileUsingIO( char const *wavfile, CSentence& sentence, IFileReadBinary& io )
{
	sentence.Reset();

	InFileRIFF riff( wavfile, io );

	// UNDONE: Don't use printf to handle errors
	if ( riff.RIFFName() != RIFF_WAVE )
	{
		return false;
	}

	// set up the iterator for the whole file (root RIFF is a chunk)
	IterateRIFF walk( riff, riff.RIFFSize() );

	// This chunk must be first as it contains the wave's format
	// break out when we've parsed it
	bool found = false;
	while ( walk.ChunkAvailable() && !found )
	{
		switch( walk.ChunkName() )
		{
		case WAVE_VALVEDATA:
			{
				found = true;
				SceneManager_ParseSentence( sentence, walk );
			}
			break;
		}
		walk.ChunkNext();
	}

	return true;
}

bool SceneManager_LoadSentenceFromWavFile( char const *wavfile, CSentence& sentence )
{
	return SceneManager_LoadSentenceFromWavFileUsingIO( wavfile, sentence, io_in );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : store - 
//-----------------------------------------------------------------------------
static void SceneManager_StoreValveDataChunk( CSentence& sentence, IterateOutputRIFF& store )
{
	// Buffer and dump data
	CUtlBuffer buf( 0, 0, true );

	sentence.SaveToBuffer( buf );

	// Copy into store
	store.ChunkWriteData( buf.Base(), buf.TellPut() );
}

bool SceneManager_SaveSentenceToWavFile( char const *wavfile, CSentence& sentence )
{
	char tempfile[ 512 ];

	Q_strcpy( tempfile, wavfile );

	StripExtension( tempfile );
	DefaultExtension( tempfile, ".tmp" );

	if ( filesystem->FileExists( tempfile, NULL ) )
	{
		filesystem->RemoveFile( tempfile, NULL );
	}

	if ( !filesystem->IsFileWritable( wavfile ) )
	{
		int retval = MultipleRequest( va( "Check out '%s'?", wavfile ) );
		if ( retval != 0 )
			return false;

		VSS_Checkout( wavfile );
	}

	if ( !filesystem->IsFileWritable( wavfile ) )
	{
		Con_Printf( "%s is not writable, can't save sentence data to file\n", wavfile );
		return false;
	}
	
	// Rename original wavfile to temp
	filesystem->RenameFile( wavfile, tempfile, NULL );

	// NOTE:  Put this in it's own scope so that the destructor for outfileRFF actually closes the file!!!!
	{
		// Read from Temp
		InFileRIFF riff( tempfile, io_in );
		Assert( riff.RIFFName() == RIFF_WAVE );

		// set up the iterator for the whole file (root RIFF is a chunk)
		IterateRIFF walk( riff, riff.RIFFSize() );

		// And put data back into original wavfile by name
		OutFileRIFF riffout( wavfile, io_out );

		IterateOutputRIFF store( riffout );

		bool wordtrackwritten = false;

		// Walk input chunks and copy to output
		while ( walk.ChunkAvailable() )
		{
			store.ChunkStart( walk.ChunkName() );

			switch ( walk.ChunkName() )
			{
			case WAVE_VALVEDATA:
				{
					// Overwrite data
					SceneManager_StoreValveDataChunk( sentence, store );
					wordtrackwritten = true;
				}
				break;
			default:
				store.CopyChunkData( walk );
				break;
			}

			store.ChunkFinish();

			walk.ChunkNext();
		}

		// If we didn't write it above, write it now
		if ( !wordtrackwritten )
		{
			store.ChunkStart( WAVE_VALVEDATA );
			SceneManager_StoreValveDataChunk( sentence, store );
			store.ChunkFinish();
		}
	}

	// Remove temp file
	filesystem->RemoveFile( tempfile, NULL );

	return true;
}


void SceneManager_LoadWindowPositions( KeyValues *kv, mxWindow *wnd )
{
	bool zoomed = kv->GetInt( "zoomed", 0 ) ? true : false;
	int x = kv->GetInt( "x", 0 );
	int y = kv->GetInt( "y", 0 );
	int w = kv->GetInt( "w", 400 );
	int h = kv->GetInt( "h", 300 );

	wnd->setBounds( x, y, w, h );
	if ( zoomed )
	{
		ShowWindow( (HWND)wnd->getHandle(), SW_SHOWMAXIMIZED );
	}
}

static void Indent( CUtlBuffer& buf, int numtabs )
{
	for ( int i = 0 ; i < numtabs; i++ )
	{
		buf.Printf( "\t" );
	}
}

void SceneManager_SaveWindowPositions( CUtlBuffer& buf, int indent, mxWindow *wnd )
{
	int x, y, w, h;

	x = wnd->x();
	y = wnd->y();
	w = wnd->w();
	h = wnd->h();

	// xpos and ypos are screen space
	POINT pt;
	pt.x = x;
	pt.y = y;

	// Convert from screen space to relative to client area of parent window so
	//  the setBounds == MoveWindow call will offset to the same location
	if ( wnd->getParent() )
	{
		ScreenToClient( (HWND)wnd->getParent()->getHandle(), &pt );
		x = (short)pt.x;
		y = (short)pt.y;
	}


	Indent( buf, indent );
	buf.Printf( "\"x\"\t\"%i\"\n", x );

	Indent( buf, indent );
	buf.Printf( "\"y\"\t\"%i\"\n", y );

	Indent( buf, indent );
	buf.Printf( "\"w\"\t\"%i\"\n", w );

	Indent( buf, indent );
	buf.Printf( "\"h\"\t\"%i\"\n", h );

	bool zoomed = IsZoomed( (HWND)wnd->getHandle() ) ? true : false;

	Indent( buf, indent );
	buf.Printf( "\"zoomed\"\t\"%i\"\n", zoomed ? 1 : 0 );

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?