Class KVObjectExtensions
Namespace: ValveResourceFormat.Serialization.KeyValues
Assembly: ValveResourceFormat.dll
VRF-specific extension methods for KVObject. These provide the same behavior as the old VRF KVObject property getters.
public static class KVObjectExtensionsInheritance
object ← KVObjectExtensions
Methods
DeserializeWithoutRoot(KVSerializer, Stream)
Deserializes KeyValues1 text that keeps several keys at its top level rather than a single root.
public static KVObject DeserializeWithoutRoot(this KVSerializer serializer, Stream stream)Parameters
serializer KVSerializer
A KeyValues1 text serializer.
stream Stream
The text to read.
Returns
KVObject
An object holding every top level key.
GetArray(KVObject, string)
Gets the values of an array child by name. Returns the underlying collection without copying.
public static IReadOnlyList<KVObject> GetArray(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
IReadOnlyList<KVObject>
GetArray<T>(KVObject, string)
Gets a typed array of primitive values by name. Also handles binary blobs.
public static T[] GetArray<T>(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
T[]
Type Parameters
T
GetBooleanProperty(KVObject, string, bool)
Gets a boolean property from the key-value object.
public static bool GetBooleanProperty(this KVObject obj, string name, bool defaultValue = false)Parameters
obj KVObject
name string
defaultValue bool
Returns
bool
GetByteProperty(KVObject, string, byte)
Gets a byte property from the key-value object.
public static byte GetByteProperty(this KVObject obj, string name, byte defaultValue = 0)Parameters
obj KVObject
name string
defaultValue byte
Returns
byte
GetDoubleProperty(KVObject, string, double)
Gets a double property from the key-value object.
public static double GetDoubleProperty(this KVObject obj, string name, double defaultValue = 0)Parameters
obj KVObject
name string
defaultValue double
Returns
double
GetEnumValue<TEnum>(KVObject, string, bool, string)
Gets an enum value from the key-value object.
public static TEnum GetEnumValue<TEnum>(this KVObject obj, string name, bool normalize = false, string stripExtension = "Flags") where TEnum : struct, EnumParameters
obj KVObject
name string
normalize bool
stripExtension string
Returns
TEnum
Type Parameters
TEnum
GetFloatArray(KVObject, string)
Gets an array of floats by name.
public static float[] GetFloatArray(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
float[]
GetFloatProperty(KVObject, string, float)
Gets a float property from the key-value object.
public static float GetFloatProperty(this KVObject obj, string name, float defaultValue = 0)Parameters
obj KVObject
name string
defaultValue float
Returns
float
GetInt32Property(KVObject, string, int)
Gets an Int32 property from the key-value object.
public static int GetInt32Property(this KVObject obj, string name, int defaultValue = 0)Parameters
obj KVObject
name string
defaultValue int
Returns
int
GetIntegerArray(KVObject, string)
Gets an array of long integers by name.
public static long[] GetIntegerArray(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
long[]
GetIntegerProperty(KVObject, string, long)
Gets a 64-bit integer property from the key-value object. Text is read as the integer it starts with, a typed number is cast the way C does and a boolean is 0 or 1. Text that holds no integer is 0.
public static long GetIntegerProperty(this KVObject obj, string name, long defaultValue = 0)Parameters
obj KVObject
name string
defaultValue long
Returns
long
GetNullableFloatProperty(KVObject, string)
Gets a float property, or null when the key is absent.
public static float? GetNullableFloatProperty(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
float?
GetNullableInt32Property(KVObject, string)
Gets an Int32 property, or null when the key is absent.
public static int? GetNullableInt32Property(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
int?
GetParticleBooleanProperty(KVObject, string, bool)
Gets a particle boolean field, or defaultValue when the key is absent. See KVObjectExtensions.ToParticleBoolean.
public static bool GetParticleBooleanProperty(this KVObject obj, string name, bool defaultValue = false)Parameters
obj KVObject
name string
defaultValue bool
Returns
bool
GetStringProperty(KVObject, string, string?)
Gets a string property from the key-value object. A number is returned as its invariant text, and any other value that is not a string gives defaultValue.
public static string GetStringProperty(this KVObject obj, string name, string? defaultValue = null)Parameters
obj KVObject
name string
defaultValue string?
Returns
string
GetSubCollection(KVObject, string)
Gets a child KVObject (sub-collection) by name.
public static KVObject GetSubCollection(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
KVObject
GetUInt32Property(KVObject, string, uint)
Gets a UInt32 property from the key-value object.
public static uint GetUInt32Property(this KVObject obj, string name, uint defaultValue = 0)Parameters
obj KVObject
name string
defaultValue uint
Returns
uint
GetUnsignedIntegerArray(KVObject, string)
Gets an unsigned integer array of 64-bit bit patterns, with unchecked int-to-ulong conversion for binary KV3 compatibility.
public static ulong[] GetUnsignedIntegerArray(this KVObject obj, string name)Parameters
obj KVObject
name string
Returns
ulong[]
GetUnsignedIntegerArray<T>(KVObject, string)
Gets an unsigned integer array whose elements are bit patterns, such as flags, masks or hashes. Elements are truncated to T instead of range checked, so negative values keep their bits.
public static T[] GetUnsignedIntegerArray<T>(this KVObject obj, string name) where T : unmanaged, IBinaryInteger<T>, IUnsignedNumber<T>Parameters
obj KVObject
name string
Returns
T[]
Type Parameters
T
GetUnsignedIntegerProperty(KVObject, string, ulong)
Gets an unsigned integer property, with unchecked int-to-ulong conversion for binary KV3 compatibility.
public static ulong GetUnsignedIntegerProperty(this KVObject obj, string name, ulong defaultValue = 0)Parameters
obj KVObject
name string
defaultValue ulong
Returns
ulong
GetVector2Property(KVObject, string, Vector2)
Gets a two component vector property. Text is split on spaces and each part is read as a whole float, with extra parts ignored. An array must have exactly two elements. Text with fewer than two parts, empty or white space text, an array of any other length and a value of any other kind give defaultValue.
public static Vector2 GetVector2Property(this KVObject obj, string name, Vector2 defaultValue = default)Parameters
obj KVObject
name string
defaultValue Vector2
Returns
Remarks
A wrong count uses the default so a malformed "scales" does not collapse a model.
GetVector3Property(KVObject, string, Vector3)
Gets a three component vector property. See KVObjectExtensions.ToVector3OrDefault(KVObject, Vector3).
public static Vector3 GetVector3Property(this KVObject obj, string name, Vector3 defaultValue = default)Parameters
obj KVObject
name string
defaultValue Vector3
Returns
IsNotBlobType(KVObject, string)
Determines whether the specified key contains an array (not a blob type).
public static bool IsNotBlobType(this KVObject obj, string key)Parameters
obj KVObject
key string
Returns
bool
NormalizeEnumName<TEnum>(string, string)
Normalize C Style VALVE_ENUM_SOME_VALUE to C# ValveEnum.SomeValue (underscores and digits are stripped)
public static string NormalizeEnumName<TEnum>(string name, string stripExtension = "") where TEnum : EnumParameters
name string
stripExtension string
Returns
string
Type Parameters
TEnum
ToBooleanOrDefault(KVObject, bool)
Converts a value to a boolean. Text goes through ValueParsing.ParseBool(ReadOnlySpan<char>, bool), an integer is true when it is not zero and a float is true when its value truncated to an integer is not zero, so 0.5 is false.
public static bool ToBooleanOrDefault(this KVObject obj, bool defaultValue = false)Parameters
obj KVObject
The value.
defaultValue bool
The result for empty or white space text, for text that is not a boolean and for any other kind of value.
Returns
bool
ToColor32(KVObject)
Reads a color from text of three or four numbers, or from an array of exactly three or four numbers. Where the fourth is left out the color is opaque. Each number is truncated to an integer and wraps to a byte.
public static Color32? ToColor32(this KVObject obj)Parameters
obj KVObject
Returns
The color, or null when the value is not text of three or four numbers or an array of three or four.
ToFloatOrDefault(KVObject, float)
Converts a value to a float. Text is read as a whole number and is 0 when it is not one, a typed number is cast and a boolean is 0 or 1.
public static float ToFloatOrDefault(this KVObject obj, float defaultValue = 0)Parameters
obj KVObject
The value.
defaultValue float
The result for empty or white space text and for any kind of value that is not text, a number or a boolean.
Returns
float
ToInt32OrDefault(KVObject, int)
Converts a value to an integer. Text is read as the integer it starts with, a typed number is cast the way C does and a boolean is 0 or 1. Text that holds no integer is 0.
public static int ToInt32OrDefault(this KVObject obj, int defaultValue = 0)Parameters
obj KVObject
The value.
defaultValue int
The result for empty or white space text and for any kind of value that is not text, a number or a boolean.
Returns
int
ToInt64OrDefault(KVObject, long)
Converts a value to a 64-bit integer. Text is read as the integer it starts with, a typed number is cast the way C does and a boolean is 0 or 1. Text that holds no integer is 0.
public static long ToInt64OrDefault(this KVObject obj, long defaultValue = 0)Parameters
obj KVObject
The value.
defaultValue long
The result for empty or white space text and for any kind of value that is not text, a number or a boolean.
Returns
long
ToMatrix4x4(IReadOnlyList<KVObject>)
Converts an array of key-value objects to a Matrix4x4.
public static Matrix4x4 ToMatrix4x4(this IReadOnlyList<KVObject> array)Parameters
array IReadOnlyList<KVObject>
Returns
ToMatrix4x4(KVObject)
Converts the key-value object to a Matrix4x4.
public static Matrix4x4 ToMatrix4x4(this KVObject array)Parameters
array KVObject
Returns
ToQuaternion(KVObject)
Converts the key-value object to a Quaternion.
public static Quaternion ToQuaternion(this KVObject obj)Parameters
obj KVObject
Returns
ToTransform(KVObject)
Converts the key-value object to a tuple containing Position, Uniform Scale, and Rotation.
public static (Vector3 Position, float Scale, Quaternion Rotation) ToTransform(this KVObject obj)Parameters
obj KVObject
Returns
(Vector3 Position, float Scale, Quaternion Rotation)
ToVector2(KVObject)
Converts the key-value object to a Vector2.
public static Vector2 ToVector2(this KVObject obj)Parameters
obj KVObject
Returns
ToVector3(KVObject)
Converts the key-value object to a Vector3.
public static Vector3 ToVector3(this KVObject obj)Parameters
obj KVObject
Returns
ToVector3OrDefault(KVObject, Vector3)
Converts a value to a three component vector. Text is split on spaces and each part is read as a whole float, with extra parts ignored, so a color with its alpha reads as its color. An array must have exactly three elements. Text with fewer than three parts, empty or white space text, an array of any other length and a value of any other kind give defaultValue.
public static Vector3 ToVector3OrDefault(this KVObject value, Vector3 defaultValue = default)Parameters
value KVObject
defaultValue Vector3
Returns
Remarks
A wrong count uses the default so a malformed "scales" does not collapse a model.
ToVector4(KVObject)
Converts the key-value object to a Vector4.
public static Vector4 ToVector4(this KVObject obj)Parameters
obj KVObject

