This commit is contained in:
nephacks
2025-06-04 03:22:50 +02:00
parent f234f23848
commit f12416cffd
14243 changed files with 6446499 additions and 26 deletions

View File

@@ -0,0 +1,83 @@
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#ifndef IRESOURCECOMPILER_H
#define IRESOURCECOMPILER_H
#ifdef _WIN32
#pragma once
#endif
#include "appframework/IAppSystem.h"
#include "resourcefile/resourcetype.h"
#include "resourcefile/resourcedictionary.h"
#include "resourcefile/resourceintrospection.h"
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CDmElement;
class CResourceStream;
class CUtlBuffer;
//-----------------------------------------------------------------------------
// Used for resource registration
//-----------------------------------------------------------------------------
struct RegisterResourceInfo_t
{
ResourceType_t m_nType;
ResourceId_t m_nId;
uint32 m_nFlags;
// Describes cacheable data
uint32 m_nDataOffset;
ResourceCompressionType_t m_nCompressionType;
uint32 m_nDataSize;
uint32 m_nUncompressedDataSize;
// Describes permanent data
uint32 m_nPermanentDataOffset;
uint32 m_nPermanentDataSize;
};
//-----------------------------------------------------------------------------
// Resource compiler
//-----------------------------------------------------------------------------
abstract_class IResourceCompilerRegistry
{
public:
virtual void RegisterResource( const RegisterResourceInfo_t &info ) = 0;
virtual ResourceId_t RegisterResourceReference( ResourceType_t nType, const char *pFileName, const char *pResourceSubName ) = 0;
virtual void RegisterUsedType( const char *pStructName, bool bPermanentData ) = 0;
virtual void RegisterUsedType( ResourceStructureId_t id, bool bPermanentData ) = 0;
virtual bool CompileResource( ResourceType_t nResourceType, CUtlBuffer &buf, const char *pElementFileName, CResourceStream *pPermanentStream, CResourceStream *pDataStream ) = 0;
};
//-----------------------------------------------------------------------------
// Resource compiler
//-----------------------------------------------------------------------------
abstract_class IResourceCompiler : public IAppSystem
{
public:
// This version of compile resource will potentially read multiple files
// in a gather operation
virtual bool CompileResource( const char *pFullPath, IResourceCompilerRegistry *pRegistry, CResourceStream *pPermanentStream, CResourceStream *pDataStream ) = 0;
// This version of compile resource has already had the gather applied to it
// and the data in the utlbuffer is all the data it will ever need
virtual bool CompileResource( CUtlBuffer &buf, const char *pFullPath, IResourceCompilerRegistry *pRegistry, CResourceStream *pPermanentStream, CResourceStream *pDataStream ) = 0;
};
#define RESOURCE_COMPILER_INTERFACE_VERSION "RESOURCE_COMPILER_001"
#endif // IRESOURCECOMPILER_H

View File

@@ -0,0 +1,176 @@
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#ifndef IRESOURCESYSTEM_H
#define IRESOURCESYSTEM_H
#ifdef _WIN32
#pragma once
#endif
#include "appframework/iappsystem.h"
#include "resourcefile/resourceintrospection.h"
#include "resourcefile/resourcedictionary.h"
#include "resourcefile/resourcetype.h"
#include "tier2/tier2.h"
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class IResourceTypeManager;
//-----------------------------------------------------------------------------
// A method used to allow clients to construct + destruct resources based
// on raw data. Needs to be installed into resource type managers based on
// resource type
//-----------------------------------------------------------------------------
abstract_class IResourceTypeConstructor
{
public:
virtual bool Init( IResourceTypeManager *pTypeManager ) = 0;
virtual void Shutdown() = 0;
// Returns a structure type associated with the permanent data
virtual void *AllocatePermanentData( const void *pDataFromDisk, size_t nDataSize ) = 0;
virtual void DeallocatePermanentData( void *pData ) = 0;
// Returns the default memory limit
virtual size_t GetDefaultMemoryLimit() = 0;
// Allows the system to compute the actual size of the constructed class
// based on the permanent data in in the resource system. If the system
// doesn't know/care, return 0 here.
virtual size_t ComputeActualSize( void *pPermanentData ) = 0;
// NOTE: Resource references in pResourceData have been fixed up to
// be resource handles at this point
virtual void *Allocate( const void *pPermanentData, const void *pResourceData, size_t nDataSize, ResourceHandle_t hResourceHandle ) = 0;
virtual void Deallocate( void *pClass ) = 0;
// Fallbacks + errors..
virtual void *GetErrorResource( const void *pPermanentData ) = 0;
virtual void *GetFallback( const void *pPermanentData ) = 0;
};
//-----------------------------------------------------------------------------
// A resource dictionary to look up resources
//-----------------------------------------------------------------------------
abstract_class IResourceTypeManager
{
public:
virtual ResourceHandle_t FindOrCreateResource( const char *pFileName, const char *pSubResourceName ) = 0;
virtual ResourceHandle_t FindOrCreateProceduralResource( const char *pGroupName, const char *pResourceName, const void *pPermanentData, size_t nDataSize ) = 0;
virtual void DeleteResource( ResourceHandle_t hResource ) = 0;
virtual ResourceHandle_t FindResource( ResourceId_t nResourceId ) = 0;
virtual ResourceType_t GetResourceType() = 0;
virtual ResourceId_t GetResourceId( ResourceHandle_t hResource ) const = 0;
// To bring resources in or out of memory
virtual void CacheResource( ResourceHandle_t hResource ) = 0;
virtual void UncacheResource( ResourceHandle_t hResource ) = 0;
// Sets the amount of memory to restrict this resource type to using
virtual void SetMemoryLimit( int nMemoryLimit ) = 0;
// Returns the list of cached resources
virtual int GetResourceCount() const = 0;
virtual int GetResources( int nFirst, int nCount, ResourceHandle_t *pResources ) = 0;
};
//-----------------------------------------------------------------------------
// Methods related to managing resources
//-----------------------------------------------------------------------------
abstract_class IResourceSystem : public IAppSystem
{
public:
// Used to indicate globals to update to the current frame counter
// This counter is controlled by the resource system + set to the
// resource system's current frame
virtual void RegisterFrameCounter( uint32 *pFrameCounter ) = 0;
virtual void UnregisterFrameCounter( uint32 *pFrameCounter ) = 0;
// Allows various systems to hook in resource type constructors and class bindings
virtual void InstallResourceConstructor( ResourceType_t nType, const char *pResourceManagerType, IResourceTypeConstructor *pConstructor ) = 0;
virtual void RemoveResourceConstructor( IResourceTypeConstructor *pConstructor ) = 0;
// FIXME: Should this be in IResourceSystem or CResourceSystem?
virtual void InstallSchemaClassBinding( class CSchemaClassBindingBase *pBinding ) = 0;
// Indicates globals controlled by the client which specify the frame
// that the clients have finished with, thereby telling the resource system
// what resources it can free.
virtual void RegisterFinishedFrameCounter( uint32 *pFrameCounter ) = 0;
virtual void UnregisterFinishedFrameCounter( uint32 *pFrameCounter ) = 0;
void MarkFinishedFrameCounter( uint32 *pFrameCounter );
// Methods related to resource introspection. Should these go into a separate interface?
virtual const CResourceStructIntrospection* FindStructIntrospection( ResourceStructureId_t id ) const = 0;
virtual const CResourceStructIntrospection* FindStructIntrospection( const char *pStructName ) const = 0;
virtual const CResourceEnumIntrospection* FindEnumIntrospection( ResourceStructureId_t id ) const = 0;
virtual const CResourceEnumIntrospection* FindEnumIntrospection( const char *pEnumName ) const = 0;
virtual const CResourceTypedefIntrospection* FindTypedefIntrospection( ResourceStructureId_t id ) const = 0;
virtual const CResourceTypedefIntrospection* FindTypedefIntrospection( const char *pTypedefName ) const = 0;
// Returns the value associated with a particular enumeration
virtual bool FindEnumeratedValue( void *pValue, const char *pEnumName, const char *pEnumValueName, int nDefaultValue ) const = 0;
virtual const char *FindEnumerationName( const char *pEnumName, int nValue, const char *pDefaultName ) const = 0;
// TODO: Possibly change this to use a StructIntrospectionHandle_t
virtual const CResourceStructIntrospection* FindStructIntrospectionByBlockType( ResourceBlockId_t nBlockType ) const = 0;
virtual const CResourceStructIntrospection* FindStructIntrospectionForResourceType( ResourceType_t nType ) const = 0;
virtual const CResourceStructIntrospection* FindPermanentStructIntrospectionForResourceType( ResourceType_t nType ) const = 0;
virtual bool UnpackIntrospectedBlock( const void *pResourceData, size_t nSrcDataSize,
const CResourceIntrospection *pSrcIntro, const CResourceStructIntrospection* pSrcStructIntro,
const CResourceStructIntrospection* pDstStructIntro,
void const **pOutResult, int *pOutResultSize, IntrospectionCompatibilityType_t* pOutCompat ) const = 0;
// Utility methods
virtual void GetResourceMapping( const char *pDmeElementType, ResourceType_t *pType, ResourceStructureId_t *pId ) = 0;
virtual int GetFieldSize( ResourceFieldType_t nType ) const = 0;
virtual int GetFieldAlignment( ResourceFieldType_t nType ) const = 0;
virtual const char* GetFieldName( ResourceFieldType_t nType ) const = 0;
// Methods related to a resource dictionary
// NOTE: This will become obsolete at some point I believe? It loads a map
// of resource IDs and possible resource IDs from the specified files.
virtual void LoadResourceManifest( const char *pFileName ) = 0;
// Returns a resource manager for a particular resource type
virtual IResourceTypeManager *GetResourceManager( ResourceType_t nType ) = 0;
// Frame update.. resources are cached in or out here
virtual void FrameUpdate() = 0;
// Block until all type dictionaries are loaded.
// NOTE: This call is not necessary to make if you are running the game
// It's only necessary in tools that use IFileSystem instead of IAsyncFileSystem.
virtual void BlockUntilAllVTDsLoaded() = 0;
// Returns the resource manager associated with a particular resource data type
template < class T > IResourceTypeManager *GetResourceManagerForType( );
};
template < class T >
inline IResourceTypeManager *IResourceSystem::GetResourceManagerForType( )
{
return GetResourceManager( ResourceTypeInfo_t< T >::ResourceType() );
}
inline void IResourceSystem::MarkFinishedFrameCounter( uint32 *pFrameCounter )
{
*pFrameCounter = g_nResourceFrameCount;
}
#endif // IRESOURCESYSTEM_H

View File

@@ -0,0 +1,355 @@
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#ifndef STRONGHANDLE_H
#define STRONGHANDLE_H
#ifdef _WIN32
#pragma once
#endif
#include "tier2/tier2.h"
#include "resourcesystem/iresourcesystem.h"
//-----------------------------------------------------------------------------
// Resource pointer;
//-----------------------------------------------------------------------------
template< class T >
class CStrongHandle
{
public:
typedef const ResourceBinding_t< T > *ResourceHandleTyped_t;
// Constructors
CStrongHandle();
CStrongHandle( ResourceHandleTyped_t hResource );
CStrongHandle( ResourceId_t nResourceId );
CStrongHandle( const char *pFileName, const char *pSubResourceName );
CStrongHandle( const CStrongHandle< T > &src );
~CStrongHandle();
// Init, shutdown
void Init( ResourceHandleTyped_t nResourceId );
void Init( ResourceId_t nResourceId );
void Init( const char *pFileName, const char *pSubResourceName );
void Init( const CStrongHandle< T > &src );
void Shutdown();
// Assignment
CStrongHandle< T >& operator=( ResourceHandleTyped_t hResource );
// Is the resource actually in memory
bool IsCached() const;
// Forces the resource to be brought into or out of memory
void CacheResource();
void UncacheResource();
// Forces a reload of the resource
void ReloadResource();
// Cast operators
operator const T*() const;
const T* operator->() const;
operator ResourceHandleTyped_t() const;
operator ResourceHandle_t() const;
const ResourceBinding_t< T > *GetHandle() const;
// Comparison operators
bool operator==( ResourceHandle_t hResource ) const;
bool operator==( ResourceHandleTyped_t hResource ) const;
bool operator==( const CStrongHandle< T > &hResource ) const;
bool operator!=( ResourceHandle_t hResource ) const;
bool operator!=( ResourceHandleTyped_t hResource ) const;
bool operator!=( const CStrongHandle< T > &hResource ) const;
// Marks the resource as being used this frame
void MarkUsed();
private:
const ResourceBinding_t< T > *m_pBinding;
};
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
template< class T >
FORCEINLINE CStrongHandle< T >::CStrongHandle()
{
m_pBinding = NULL;
}
template< class T >
FORCEINLINE CStrongHandle< T >::CStrongHandle( const char *pFileName, const char *pSubResourceName )
{
m_pBinding = NULL;
Init( pFileName, pSubResourceName );
}
template< class T >
FORCEINLINE CStrongHandle< T >::CStrongHandle( ResourceId_t nResourceId )
{
m_pBinding = NULL;
Init( nResourceId );
}
// FIXME: Do I want typed resource handles?
template< class T >
FORCEINLINE CStrongHandle< T >::CStrongHandle( ResourceHandleTyped_t hResource )
{
m_pBinding = NULL;
Init( hResource );
}
template< class T >
CStrongHandle< T >::CStrongHandle( const CStrongHandle< T > &src )
{
m_pBinding = NULL;
Init( src );
}
template< class T >
CStrongHandle< T >::~CStrongHandle()
{
Shutdown();
}
//-----------------------------------------------------------------------------
// Init, shutdown
//-----------------------------------------------------------------------------
template< class T >
void CStrongHandle< T >::Init( ResourceId_t nResourceId )
{
if ( m_pBinding )
{
Shutdown();
}
IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
m_pBinding = (ResourceHandleTyped_t)pMgr->FindResource( nResourceId );
Assert( m_pBinding );
if ( m_pBinding )
{
++m_pBinding->m_nRefCount;
}
}
template< class T >
void CStrongHandle< T >::Init( const char *pFileName, const char *pSubResourceName )
{
if ( m_pBinding )
{
Shutdown();
}
IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
m_pBinding = (ResourceHandleTyped_t)pMgr->FindOrCreateResource( pFileName, pSubResourceName );
Assert( m_pBinding );
if ( m_pBinding )
{
++m_pBinding->m_nRefCount;
}
}
template< class T >
void CStrongHandle< T >::Init( ResourceHandleTyped_t hResource )
{
if ( m_pBinding )
{
Shutdown();
}
m_pBinding = hResource;
if ( m_pBinding )
{
++m_pBinding->m_nRefCount;
}
}
template< class T >
void CStrongHandle< T >::Init( const CStrongHandle< T > &src )
{
if ( m_pBinding )
{
Shutdown();
}
m_pBinding = src.m_pBinding;
if ( m_pBinding )
{
++m_pBinding->m_nRefCount;
}
}
template< class T >
void CStrongHandle< T >::Shutdown()
{
if ( m_pBinding )
{
--m_pBinding->m_nRefCount;
m_pBinding = NULL;
}
}
//-----------------------------------------------------------------------------
// Assignment
//-----------------------------------------------------------------------------
template< class T >
FORCEINLINE CStrongHandle< T >& CStrongHandle< T >::operator=( ResourceHandleTyped_t hResource )
{
Init( hResource );
return *this;
}
//-----------------------------------------------------------------------------
// Cast operators
//-----------------------------------------------------------------------------
template< class T >
FORCEINLINE const ResourceBinding_t< T > *CStrongHandle< T >::GetHandle() const
{
Assert( m_pBinding );
m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
return m_pBinding;
}
template< class T >
FORCEINLINE CStrongHandle< T >::operator const T*() const
{
Assert( m_pBinding );
m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
return ( const T* )m_pBinding->m_pData;
}
template< class T >
FORCEINLINE const T *CStrongHandle< T >::operator->() const
{
Assert( m_pBinding );
m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
return ( const T* )m_pBinding->m_pData;
}
template< class T >
FORCEINLINE CStrongHandle< T >::operator ResourceHandleTyped_t() const
{
Assert( m_pBinding );
m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
return m_pBinding;
}
template< class T >
FORCEINLINE CStrongHandle< T >::operator ResourceHandle_t() const
{
Assert( m_pBinding );
m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
return (ResourceHandle_t)m_pBinding;
}
//-----------------------------------------------------------------------------
// Comparison operators
//-----------------------------------------------------------------------------
template< class T >
inline bool CStrongHandle< T >::operator==( ResourceHandle_t hResource ) const
{
return m_pBinding == hResource;
}
template< class T >
inline bool CStrongHandle< T >::operator==( ResourceHandleTyped_t hResource ) const
{
return m_pBinding == hResource;
}
template< class T >
inline bool CStrongHandle< T >::operator==( const CStrongHandle< T > &hResource ) const
{
return m_pBinding == hResource.m_pBinding;
}
template< class T >
inline bool CStrongHandle< T >::operator!=( ResourceHandle_t hResource ) const
{
return m_pBinding != hResource;
}
template< class T >
inline bool CStrongHandle< T >::operator!=( ResourceHandleTyped_t hResource ) const
{
return m_pBinding != hResource;
}
template< class T >
inline bool CStrongHandle< T >::operator!=( const CStrongHandle< T > &hResource ) const
{
return m_pBinding != hResource.m_pBinding;
}
//-----------------------------------------------------------------------------
// Is the resource actually in memory
//-----------------------------------------------------------------------------
template< class T >
inline bool CStrongHandle< T >::IsCached() const
{
Assert( m_pBinding );
return ( m_pBinding->m_nFlags & RESOURCE_BINDING_CACHED ) != 0;
}
//-----------------------------------------------------------------------------
// Marks the resource as being used this frame
//-----------------------------------------------------------------------------
template< class T >
inline void CStrongHandle< T >::MarkUsed()
{
m_pBinding->m_nLastBindFrame = g_nResourceFrameCount;
}
//-----------------------------------------------------------------------------
// Forces the resource to be brought into memory
//-----------------------------------------------------------------------------
template< class T >
void CStrongHandle< T >::CacheResource()
{
Assert( m_pBinding );
IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
pMgr->CacheResource( (ResourceHandle_t)m_pBinding );
}
template< class T >
void CStrongHandle< T >::UncacheResource()
{
Assert( m_pBinding );
IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
pMgr->UncacheResource( (ResourceHandle_t)m_pBinding );
}
//-----------------------------------------------------------------------------
// Forces a reload of the resource
//-----------------------------------------------------------------------------
template< class T >
void CStrongHandle< T >::ReloadResource()
{
Assert( m_pBinding );
IResourceTypeManager *pMgr = g_pResourceSystem->GetResourceManagerForType< T >();
pMgr->UncacheResource( (ResourceHandle_t)m_pBinding );
pMgr->CacheResource( (ResourceHandle_t)m_pBinding );
}
#endif // STRONGHANDLE_H