Skip to main content
The Resource class is the main entry point for working with Source 2 resource files. It handles reading, parsing, and providing access to the blocks and data contained within compiled resource files.

Constructor

Resource()
constructor
Creates a new empty Resource instance with ResourceType.Unknown.
Resource(ResourceType, ushort)
constructor
Creates a new Resource instance for creating new resources.
resourceType
ResourceType
required
The type of resource to create
version
ushort
default:"0"
The file type version

Properties

KnownHeaderVersion
const ushort
default:"12"
The known and expected header version for resource files.
Reader
BinaryReader?
Gets the binary reader. USE AT YOUR OWN RISK! It is exposed publicly to ease reading the same file.
FileName
string?
Gets or sets the file name this resource was parsed from.
FileSize
uint
Gets the resource size as specified in the file header.
HeaderVersion
ushort
Gets the version of this resource header, should be 12.
Version
ushort
Gets the file type version.
Blocks
List<Block>
Gets the list of blocks this resource contains. Use this to iterate through all blocks in the resource.
ResourceType
ResourceType
Gets the type of the resource (e.g., Model, Texture, Material, etc.).
EditInfo
ResourceEditInfo?
Gets the ResourceEditInfo block containing dependency and compilation metadata.
ExternalReferences
ResourceExtRefList?
Gets the ResourceExtRefList block containing external resource references.
DataBlock
Block?
Gets the generic DATA block, which contains the primary resource content.
FullFileSize
uint
Gets the full file size including streaming data. Resource files have a 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.

Methods

Read

Read(string)
void
Opens and reads the given filename. The file is held open until the object is disposed.
filename
string
required
The file to open and read
var resource = new Resource();
resource.Read("materials/example.vmat_c");
Console.WriteLine($"Resource type: {resource.ResourceType}");
resource.Dispose();
Read(Stream, bool, bool)
void
Reads the given Stream.
input
Stream
required
The input Stream to read from
verifyFileSize
bool
default:"true"
Whether to verify that the stream was correctly consumed
leaveOpen
bool
default:"false"
Whether to leave the stream open after the object is disposed
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.
using var stream = File.OpenRead("models/example.vmdl_c");
var resource = new Resource();
resource.Read(stream, verifyFileSize: true, leaveOpen: false);

Serialize

Serialize(Stream)
void
Serialize resource to binary.
stream
Stream
required
Stream to write to. The stream must support seeking.
NOT PRODUCTION READY! Not all blocks support serialization and will throw. The total file size must not exceed uint.MaxValue.
var resource = new Resource(ResourceType.Material, version: 2);
// Add blocks to resource...
using var outputStream = File.Create("output.vmat_c");
resource.Serialize(outputStream);

Block Access Methods

GetBlockByIndex(int)
Block
Gets a block by its index in the blocks list.
index
int
required
The zero-based index of the block
returns
Block
The block at the specified index
Throws ArgumentOutOfRangeException when the index is out of range.
GetBlockByType(BlockType)
Block?
Gets the first block of the specified type.
type
BlockType
required
The type of block to retrieve
returns
Block?
The first block of the specified type, or null if not found
var dataBlock = resource.GetBlockByType(BlockType.DATA);
if (dataBlock != null)
{
    Console.WriteLine($"Found DATA block at offset {dataBlock.Offset}");
}
ContainsBlockType(BlockType)
bool
Determines whether the resource contains a block of the specified type.
type
BlockType
required
The type of block to check for
returns
bool
True if a block of the specified type exists; otherwise, false
if (resource.ContainsBlockType(BlockType.VBIB))
{
    Console.WriteLine("Resource contains vertex buffer data");
}

Dispose

Dispose()
void
Releases the binary reader and closes any open file streams. Always call this when done with a Resource.
using var resource = new Resource();
resource.Read("example.vmdl_c");
// Resource is automatically disposed when leaving scope

Usage Examples

Reading a Resource File

using ValveResourceFormat;

using var resource = new Resource();
resource.Read("materials/metal.vmat_c");

Console.WriteLine($"File: {resource.FileName}");
Console.WriteLine($"Type: {resource.ResourceType}");
Console.WriteLine($"Version: {resource.Version}");
Console.WriteLine($"Block Count: {resource.Blocks.Count}");

foreach (var block in resource.Blocks)
{
    Console.WriteLine($"  Block: {block.Type} (Size: {block.Size} bytes)");
}

Accessing Resource Data

using var resource = new Resource();
resource.Read("models/props/barrel.vmdl_c");

if (resource.ResourceType == ResourceType.Model)
{
    var dataBlock = resource.DataBlock;
    if (dataBlock is Model model)
    {
        Console.WriteLine($"Model has {model.GetMeshes().Count()} meshes");
    }
}

Working with External References

using var resource = new Resource();
resource.Read("materials/example.vmat_c");

if (resource.ExternalReferences != null)
{
    Console.WriteLine("External References:");
    foreach (var reference in resource.ExternalReferences.ResourceRefInfoList)
    {
        Console.WriteLine($"  {reference.Name}");
    }
}

See Also

  • Block - Base class for all resource blocks
  • ResourceType - Enum of all supported resource types
  • Blocks - Learn about block types

Build docs developers (and LLMs) love