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
Creates a new empty Resource instance with ResourceType.Unknown.
Resource(ResourceType, ushort)
Creates a new Resource instance for creating new resources.The type of resource to create
Properties
The known and expected header version for resource files.
Gets the binary reader. USE AT YOUR OWN RISK! It is exposed publicly to ease reading the same file.
Gets or sets the file name this resource was parsed from.
Gets the resource size as specified in the file header.
Gets the version of this resource header, should be 12.
Gets the file type version.
Gets the list of blocks this resource contains. Use this to iterate through all blocks in the resource.
Gets the type of the resource (e.g., Model, Texture, Material, etc.).
Gets the ResourceEditInfo block containing dependency and compilation metadata.
Gets the ResourceExtRefList block containing external resource references.
Gets the generic DATA block, which contains the primary resource content.
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
Opens and reads the given filename. The file is held open until the object is disposed.The file to open and read
var resource = new Resource();
resource.Read("materials/example.vmat_c");
Console.WriteLine($"Resource type: {resource.ResourceType}");
resource.Dispose();
Reads the given Stream.The input Stream to read from
Whether to verify that the stream was correctly consumed
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 resource to binary.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
Gets a block by its index in the blocks list.The zero-based index of the block
The block at the specified index
Throws ArgumentOutOfRangeException when the index is out of range.
GetBlockByType(BlockType)
Gets the first block of the specified type.The type of block to retrieve
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)
Determines whether the resource contains a block of the specified type.The type of block to check for
True if a block of the specified type exists; otherwise, false
if (resource.ContainsBlockType(BlockType.VBIB))
{
Console.WriteLine("Resource contains vertex buffer data");
}
Dispose
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