Class Resource
Namespace: ValveResourceFormat
Assembly: ValveResourceFormat.dll
Represents a compiled Source 2 resource file, such as a .vtex_c or .vmdl_c.
public class Resource : IDisposableInheritance
object ← Resource
Implements
Remarks
Create an instance, then call Resource.Read(string, ReadMode) or Resource.Read(Stream, bool, ReadMode). Check Resource.ResourceType and cast Resource.DataBlock to the matching block class.
Blocks and the data they expose are read from the input stream when first accessed, so the resource must stay undisposed and its input stream open for as long as its blocks are used.
Constructors
Resource()
Initializes a new instance of the Resource class.
public Resource()Resource(ResourceType, ushort)
Initializes a new instance of the Resource class for creating new resources.
public Resource(ResourceType resourceType, ushort version = 0)Parameters
resourceType ResourceType
version ushort
Fields
KnownHeaderVersion
The known and expected header version for resource files.
public const ushort KnownHeaderVersion = 12Field Value
ushort
Properties
Blocks
Gets the blocks this resource contains, each parsed by the time it is returned.
public IReadOnlyList<Block> Blocks { get; }Property Value
DataBlock
Gets the DATA block, which holds the main contents of the resource. Its class depends on Resource.ResourceType, for example Texture for textures. It is null when the resource has no DATA block.
public Block? DataBlock { get; }Property Value
EditInfo
Gets the ResourceEditInfo block.
public ResourceEditInfo? EditInfo { get; }Property Value
ExternalReferences
Gets the ResourceExtRefList block.
public ResourceExtRefList? ExternalReferences { get; }Property Value
FileName
Gets or sets the file name this resource was parsed from.
public string? FileName { get; set; }Property Value
string?
FileSize
Gets the resource size.
public uint FileSize { get; }Property Value
uint
FullFileSize
Resource files have a Resource.FileSize in the metadata, however certain file types such as sounds have streaming audio data come after the resource file, and the size is specified within the DATA block. This property attempts to return the correct size.
public uint FullFileSize { get; }Property Value
uint
HeaderVersion
Gets the version of this resource, should be 12.
public ushort HeaderVersion { get; }Property Value
ushort
Reader
Gets the binary reader. USE AT YOUR OWN RISK! It is exposed publicly to ease reading the same file.
public BinaryReader? Reader { get; }Property Value
ResourceType
Gets the type of the resource, which decides the class of Resource.DataBlock. It comes from the file extension of Resource.FileName when set, otherwise from the edit info block, and is ResourceType.Unknown when neither identifies it.
public ResourceType ResourceType { get; }Property Value
Version
Gets the file type version.
public ushort Version { get; }Property Value
ushort
Methods
ContainsBlockType(BlockType)
Determines whether the resource contains a block of the specified type.
public bool ContainsBlockType(BlockType type)Parameters
type BlockType
The type of block to check for.
Returns
bool
True if a block of the specified type exists; otherwise, false.
Dispose()
Releases binary reader.
public void Dispose()Dispose(bool)
Releases the resources used by the Resource.
protected virtual void Dispose(bool disposing)Parameters
disposing bool
True to release both managed and unmanaged resources; false to release only unmanaged resources.
GetBlockByIndex(int)
Gets a block by its index in the blocks list.
public Block GetBlockByIndex(int index)Parameters
index int
The zero-based index of the block.
Returns
The block at the specified index.
Exceptions
Thrown when the index is out of range.
GetBlockByType(BlockType)
Gets the first block of the specified type.
public Block? GetBlockByType(BlockType type)Parameters
type BlockType
The type of block to retrieve.
Returns
The first block of the specified type, or null if not found.
Read(string, ReadMode)
Opens and reads the given filename. The file is held open until the object is disposed.
public void Read(string filename, ReadMode mode = ReadMode.Runtime)Parameters
filename string
The file to open and read.
mode ReadMode
When the blocks are parsed.
Read(Stream, bool, ReadMode)
Reads the given Stream.
public void Read(Stream input, bool leaveOpen = false, ReadMode mode = ReadMode.Runtime)Parameters
input Stream
The input Stream to read from.
leaveOpen bool
Whether to leave the stream open after the object is disposed.
mode ReadMode
When the blocks are parsed.
Remarks
The input stream must remain open while accessing data from this resource, as some operations may perform reads lazily from the stream at call time.
Serialize(Stream)
Serialize resource to binary.
public void Serialize(Stream stream)Parameters
stream Stream
Stream to write to. The stream must support seeking.
Remarks
NOT PRODUCTION READY! Not all blocks support serialization and will throw. The total file size must not exceed uint.

