An all in one Open Source .NET Core library for effectively reading from and writing to VMF(Valve Map Format) files.
A library which has a Reader and Writer, alongside all other required types for reading and editing all of the classes which can be found in map files generated by Source 1 Hammer.
This class gives you a simple and extensible way to read VMFs, it works like most other readers and writers. You initialize it by giving it the path to the VMF, then you can call ReadClass() which will return whichever class it was able to read.
An example of how VClassReader can be used to effectively read VMFs can be found in VMfTest. VClassReader itself can be used like any other Reader in C#, by either calling it with a using loop
using (VClassReader reader = new VClassReader(File.OpenText(@"path\to\file.vmf")))
{
// This will fetch the first class in the file, so versioninfo, and return it to us with all its properties.
BaseVClass currentClass = classReader.ReadClass();
// This loop will break once we have read the entire file, since VClassReader returns null when it cannot find a class
while (currentClass != null)
{
// This will read the next class in the file, and because of the while loop continue reading until there are no classes left
currentClass = classReader.ReadClass()
}
}
Or initializing VClassReader directly with a path to the file
VClassReader classReader = new VClassReader(@"path\to\file.vmf");
// This will fetch the first class in the file, so versioninfo, and return it to us with all its properties.
BaseVClass currentClass = classReader.ReadClass();
// This loop will break once we have read the entire file, since VClassReader returns null when it cannot find a class
while (currentClass != null)
{
// This will read the next class in the file, and because of the while loop continue reading until there are no classes left
currentClass = classReader.ReadClass()
}
// Always remember to do this! Otherwise the file will be locked as read-only
// (until the program is closed that is)
classReader.Dispose();
This class will allow you to write the C# representations of these classes into something hammer can read. Keep in mind this will not write them in the correct order for you, nor will it fix your mistakes. Use it wisely, or risk corrupting your VMFs!
Like VClassReader, you can write to files with this with a "using" loop or initializing it directly.
// Using loop
using (VClassWriter writer = new VClassWriter(File.CreateText(@"path\to\file.vmf")))
{
// The order this list is in is important. Things like versioninfo and the world should be first, cameras and cordons last.
foreach (BaseVClass vClass in VClasses)
{
writer.WriteClass(vClass); // This will write the class into text.
}
}
// initializing directly
VClassWriter writer = new VClassWriter(@"path\to\file.vmf")
// The order this list is in is important. Things like versioninfo and the world should be first, cameras and cordons last.
foreach (BaseVClass vClass in VClasses)
{
writer.WriteClass(vClass); // This will write the class into text.
}
// Always remember to do this! Otherwise the file will be marked as used until the program is closed
writer.Dispose();
An example console application to show what the library is capable of and how to use it.
I didn't specifically design VMfTest to be used though if you wish to use it(for some reason) or want to understand its functionality, thats what this part is for.
Commands for VMfTest are always formatted as commandname "params"
. For example, read "path\to\file.vmf"
will read a file at the specified path
Macros for VMfTest allow the user to input multiple command with a single prompt, they are formatted as macro 'commanda "params",commandb "params"'
with the '
indicating a macros commands, and a ,
separating them.
This is a list of all of the commands avaliable in VMfTest
read "path\to\file.vmf"
This command will read a vmf and print its results, then once its finished reading will throw away its findings.readadd "path\to\file.vmf"
This command is identical to read, only instead of throwing away its results it saves them allowing the classes to be manipulated.clear
Throw out the currently saved classes and start overprintall
This will print out all of the currently loaded classes and the index they currently occupy.select "{integer}"
This will select a class at the specified index. Useprintall
to see which classes occupy which indexesprintprop
This will print all of the properties of the selected classedit "EditCommand.Name.Value"
Offers a variety of ways of editing the selected classes properties. UseAddProperty
(in place ofEditCommand
) to add a completely new property of the specified name and value, andEditProperty
to edit a property.save
Will overwrite the current file with these classes. USE WITH CAUTION!!!saveas "path\to\file.vmf"
Save the classes into a new VMF file.