public void SetUp() { using (var stream = TestDataHelper.OpenResource("Text.list_of_values.vdf")) { _data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize <SerializedType>(stream); } }
public void SaveToBuffer(BinaryWriter writer) { // Write header types if we have a header if (_headerType != null) { writer.Write(Version); writer.Write((uint)UniverseType); } foreach (var child in this) { switch (child) { case IBinaryParseable parseable: parseable.SaveToBuffer(writer); break; case KvObject obj: KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Serialize(new NonClosingStream(writer.BaseStream), obj); break; } } // Write terminator if we have a header if (_headerType == typeof(BinaryVdfAppHeader)) { writer.Write(BinaryVdfAppHeader.Terminator); } if (_headerType == typeof(BinaryVdfPackageHeader)) { writer.Write(BinaryVdfPackageHeader.Terminator); } }
public void DeserializeWithNullStream(KvSerializationFormat format) { Assert.That( () => KvSerializer.Create(format).Deserialize(stream: null), Throws.Exception.TypeOf <ArgumentNullException>() .With.Property(nameof(ArgumentNullException.ParamName)).EqualTo("stream")); }
public void CreatesTextDocument() { var dataObject = new[] { new Dictionary <string, string> { ["description"] = "Dota 2 is a complex game where you get sworn at\nin Russian all the time.", ["developer"] = "Valve Software", ["name"] = "Dota 2" }, new Dictionary <string, string> { ["description"] = "Known as \"America's #1 war-themed hat simulator\", this game lets you wear stupid items while killing people.", ["developer"] = "Valve Software", ["name"] = "Team Fortress 2" }, }; string text; using (var ms = new MemoryStream()) { KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Serialize(ms, dataObject, "test data"); ms.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(ms)) text = reader.ReadToEnd(); } var expected = TestDataHelper.ReadTextResource("Text.serialization_expected.vdf"); Assert.That(text, Is.EqualTo(expected)); }
public void ThrowsException() { var dataObject1 = new DataObject { Name = "First" }; var dataObject2 = new DataObject { Name = "Second", Other = dataObject1 }; dataObject1.Other = dataObject2; Assert.That(dataObject1.Other, Is.SameAs(dataObject2), "Sanity check"); Assert.That(dataObject2.Other, Is.SameAs(dataObject1), "Sanity check"); using (var ms = new MemoryStream()) { Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Serialize(ms, dataObject1, "test data"), Throws.Exception.InstanceOf <KeyValueException>() .With.Message.EqualTo("Serialization failed - circular object reference detected.")); } }
public void SetUp() { using (var stream = TestDataHelper.OpenResource("Text.nested_object_graph.vdf")) { _data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize <ObjectGraph>(stream); } }
public void SetUp() { var data = new byte[] { 0x00, // object: FirstObject 0x46, 0x69, 0x72, 0x73, 0x74, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x01, // string: firstkey = firstvalue 0x66, 0x69, 0x72, 0x73, 0x74, 0x6b, 0x65, 0x79, 0x00, 0x66, 0x69, 0x72, 0x73, 0x74, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x00, 0x08, // end object 0x08, // end document 0x00, // object: SecondObject 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x01, // string: secondkey = secondvalue 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x6b, 0x65, 0x79, 0x00, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x00, 0x08, // end object 0x08, // end document }; using (var stream = new MemoryStream()) { stream.Write(data, 0, data.Length); stream.Seek(0, SeekOrigin.Begin); _firstObject = KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Deserialize <FirstObject>(stream); Assert.That(stream.Position, Is.EqualTo(36)); // ensure we read exactly 36 bytes _secondObject = KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Deserialize <SecondObject>(stream); Assert.That(stream.Position, Is.EqualTo(75)); // ensure we read exactly 39 bytes } }
private static string PerformNewLineTest(string value, bool hasEscapeSequences) { KvObject convertedKv; var kv = new KvObject("newLineTestCase", value); var options = new KvSerializerOptions { HasEscapeSequences = hasEscapeSequences }; string text; using (var ms = new MemoryStream()) { var serializer = KvSerializer.Create(KvSerializationFormat.KeyValues1Text); serializer.Serialize(ms, kv, options); ms.Seek(0, SeekOrigin.Begin); text = Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length); convertedKv = serializer.Deserialize(ms, options); } Assert.That((string)convertedKv.Value, Is.EqualTo(value)); return(text); }
public static KvObject Deserialize(this KvSerializer serializer, byte[] data, KvSerializerOptions options = null) { using (var ms = new MemoryStream(data)) { return(serializer.Deserialize(ms, options)); } }
public void SetUp() { var data = new byte[] { 0x00, // object: TestObject 0x54, 0x65, 0x73, 0x74, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x00, 0x01, // string: key = value 0x6B, 0x65, 0x79, 0x00, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00, 0x02, // int32: int = 0x01020304 0x69, 0x6E, 0x74, 0x00, 0x04, 0x03, 0x02, 0x01, 0x03, // float32: flt = 1234.5678f 0x66, 0x6C, 0x74, 0x00, 0x2B, 0x52, 0x9A, 0x44, 0x04, // color: col = 0x10203040 0x63, 0x6F, 0x6C, 0x00, 0x40, 0x30, 0x20, 0x10, 0x06, // pointer: ptr = 0x11223344 0x70, 0x74, 0x72, 0x00, 0x44, 0x33, 0x22, 0x11, 0x07, // uint64: long = 0x1122334455667788 0x6C, 0x6E, 0x67, 0x00, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0A, // int64, i64 = 0x0102030405070809 0x69, 0x36, 0x34, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x08, // end object 0x08, // end document }; _obj = KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Deserialize(data); }
public void SetUp() { var options = new KvSerializerOptions { HasEscapeSequences = true }; using (var stream = TestDataHelper.OpenResource("Text.escaped_backslash.vdf")) _data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream, options); }
public void SetUp() { var options = new KvSerializerOptions { FileLoader = new StubIncludedFileLoader() }; using (var stream = TestDataHelper.OpenResource("Text.kv_with_include.vdf")) _data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream, options); }
public void InvalidTextSyntaxThrowsKeyValueException(string resourceName) { using (var stream = TestDataHelper.OpenResource("Text." + resourceName + ".vdf")) { Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream), Throws.Exception.TypeOf <KeyValueException>()); } }
public void ThrowsInvalidOperationException() { using (var stream = TestDataHelper.OpenResource("Text.list_of_values_empty_key.vdf")) { Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize<SerializedType>(stream), Throws.Exception.InstanceOf<InvalidOperationException>() .With.Message.EqualTo($"Cannot deserialize a non-array value to type \"{typeof(TEnumerable).Namespace}.{typeof(TEnumerable).Name}\".")); } }
public void ReadsRawValueWhenNotHasEscapeSequences() { KvObject data; using (var stream = TestDataHelper.OpenResource("Text.escaped_garbage.vdf")) { data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream); } Assert.That((string)data["key"], Is.EqualTo(@"\7")); }
public void CanReadTeamFortressCleanupCommands() { KvObject data; using (var stream = TestDataHelper.OpenResource($"Text.steam_440.vdf")) data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream); var cleanupcmds = (string)data["config"]["cleanupcmds"]; const string expected = @"if not exist hl2.exe goto EOF;ren ""..\..\common\Team Fortress 2"" TF2_bak;del /q /s bin\*;cd tf;del /q /s *.cache media\*.mov;mkdir download;cd download;move ..\maps .\;move ..\materials .\;move ..\models .\;move ..\particles .\;move ..\resource .\;move ..\sound .\;move ..\scripts .\;cd resource;del game.ico tf.ttf tf2.ttf tf2build.ttf tf2professor.ttf tf2secondary.ttf tfd.ttf;cd ..\sound\ui;del gamestartup*.mp3 tv_tune*.mp3 holiday\gamestartup_*.mp3;:EOF"; Assert.That(cleanupcmds, Is.EqualTo(expected)); }
public static TObject Deserialize <TObject>(this KvSerializer serializer, string text, KvSerializerOptions options = null) { using (var ms = new MemoryStream()) using (var writer = new StreamWriter(ms)) { writer.Write(text); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); return(serializer.Deserialize <TObject>(ms, options)); } }
private static KvObject ParseResource(string name, string[] conditions) { KvObject data; using (var stream = TestDataHelper.OpenResource(name)) { data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream, new KvSerializerOptions { Conditions = conditions }); } return(data); }
public void ThrowsInvalidDataException(string conditional) { var text = TestDataHelper.ReadTextResource("Text.invalid_conditional.vdf"); text = text.Replace("{CONDITION}", conditional); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text))) { Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream), Throws.Exception.InstanceOf <InvalidDataException>() .With.Message.EqualTo($"Invalid conditional syntax \"{conditional}\"")); } }
public void SerializesToBinaryStructure() { var kvo = new KvObject("TestObject", new[] { new KvObject("key", "value"), new KvObject("key_utf8", "邪恶的战"), new KvObject("int", 0x10203040), new KvObject("flt", 1234.5678f), new KvObject("ptr", new IntPtr(0x12345678)), new KvObject("lng", 0x8877665544332211u), new KvObject("i64", 0x0102030405060708) }); var expectedData = new byte[] { 0x00, // object: TestObject 0x54, 0x65, 0x73, 0x74, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x00, 0x01, // string: key = value 0x6B, 0x65, 0x79, 0x00, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00, 0x01, // string_utf8: key_utf8 = 邪恶的战 0x6B, 0x65, 0x79, 0x5F, 0x75, 0x74, 0x66, 0x38, 0x00, 0xE9, 0x82, 0xAA, 0xE6, 0x81, 0xB6, 0xE7, 0x9A, 0x84, 0xE6, 0x88, 0x98, 0x00, 0x02, // int32: int = 0x10203040 0x69, 0x6E, 0x74, 0x00, 0x40, 0x30, 0x20, 0x10, 0x03, // float32: flt = 1234.5678f 0x66, 0x6C, 0x74, 0x00, 0x2B, 0x52, 0x9A, 0x44, 0x04, // pointer: ptr = 0x12345678 0x70, 0x74, 0x72, 0x00, 0x78, 0x56, 0x34, 0x12, 0x07, // uint64: lng = 0x8877665544332211 0x6C, 0x6E, 0x67, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x0A, // int64, i64 = 0x0102030405070809 0x69, 0x36, 0x34, 0x00, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x08, // end object 0x08, // end document }; using (var ms = new MemoryStream()) { KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Serialize(ms, kvo); Assert.That(ms.ToArray(), Is.EqualTo(expectedData)); } }
public void ThrowsExceptionWhenHasEscapeSequences() { var options = new KvSerializerOptions { HasEscapeSequences = true }; using (var stream = TestDataHelper.OpenResource("Text.escaped_garbage.vdf")) { Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream, options), Throws.Exception.TypeOf <KeyValueException>() .With.InnerException.TypeOf <InvalidDataException>() .With.Message.EqualTo(@"Unknown escaped character '\7'.")); } }
public void ThrowsKeyValueException() { var data = new byte[] { 0x00, 0x54, 0x65, 0x73, 0x74, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x00, 0xA0, // Way out of range 0x6B, 0x65, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08 }; Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Deserialize(data), Throws.Exception.InstanceOf <KeyValueException>().With.InnerException.TypeOf <ArgumentOutOfRangeException>()); }
public void ParseFromBuffer(BinaryReader reader) { if (_headerType != null) { // Version and universe are not present // on pure binary VDF files e.g coplay, so only read them here Version = reader.ReadUInt32(); UniverseType = (EUniverse)reader.ReadUInt32(); // Ensure there is a supported version for the type if (!SupportedVersionMappings.Select(t => t.Key == _headerType && t.Value == Version).Any()) { throw new InvalidOperationException($"Unsupported binary VDF version: {Version}"); } while (reader.BaseStream.Position != reader.BaseStream.Length) { // If we have a terminator with 0 (for appinfo.vdf) // or -1 (for packageinfo.vdf), no more sections to read var terminator = reader.ReadUInt32(); if (_headerType == typeof(BinaryVdfAppHeader) && terminator == BinaryVdfAppHeader.Terminator) { break; } if (_headerType == typeof(BinaryVdfPackageHeader) && terminator == BinaryVdfPackageHeader.Terminator) { break; } // Rewind stream after peek reader.BaseStream.Position = reader.BaseStream.Position - sizeof(uint); // Create the node instance var node = (IBinaryParseable)Activator.CreateInstance(_headerType); node.ParseFromBuffer(reader); Add(node); } } else { // No header type? Skip this entirely and parse raw keyvalues1. Add(KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Deserialize(new NonClosingStream(reader.BaseStream))); } }
public void DeserializationThrowsException() { var data = new byte[] { 0x00, // object: TestObject 0x54, 0x65, 0x73, 0x74, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x00, 0x05, // wstring: key = value. I'm guessing here what the value would look like. Doesn't really matter. 0x6B, 0x65, 0x79, 0x00, 0x76, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x75, 0x00, 0x65, 0x00, 0x00, 0x00, 0x08, // end object 0x08, // end document }; Assert.That( () => KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Deserialize(data), Throws.Exception.InstanceOf <KeyValueException>().With.InnerException.TypeOf <NotSupportedException>()); }
public void CanDeserializeValveResourceFormatSettings() { VkvConfig config; using (var stream = TestDataHelper.OpenResource("Text.vrf_settings_sample.vdf")) { config = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize <VkvConfig>(stream); } Assert.That(config, Is.Not.Null); Assert.Multiple(() => { Assert.That(config.BackgroundColor, Is.EqualTo("#3C3C3C")); Assert.That(config.OpenDirectory, Is.EqualTo(@"D:\SteamLibrary\steamapps\common\The Lab\RobotRepair\vr")); Assert.That(config.SaveDirectory, Is.EqualTo(@"D:\SteamLibrary\steamapps\common\The Lab\RobotRepair\vr")); Assert.That(config.GameSearchPaths, Is.Not.Null & Has.Count.Zero); }); }
public void SerializesToBinaryStructure() { var first = new KvObject("FirstObject", new[] { new KvObject("firstkey", "firstvalue") }); var second = new KvObject("SecondObject", new[] { new KvObject("secondkey", "secondvalue") }); var expectedData = new byte[] { 0x00, // object: FirstObject 0x46, 0x69, 0x72, 0x73, 0x74, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x01, // string: firstkey = firstvalue 0x66, 0x69, 0x72, 0x73, 0x74, 0x6b, 0x65, 0x79, 0x00, 0x66, 0x69, 0x72, 0x73, 0x74, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x00, 0x08, // end object 0x08, // end document 0x00, // object: SecondObject 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x00, 0x01, // string: secondkey = secondvalue 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x6b, 0x65, 0x79, 0x00, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x00, 0x08, // end object 0x08, // end document }; using (var stream = new MemoryStream()) { KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Serialize(stream, first); KvSerializer.Create(KvSerializationFormat.KeyValues1Binary).Serialize(stream, second); Assert.That(stream.ToArray(), Is.EqualTo(expectedData)); } }
public void DuplicatePrimitiveValuesAreNotCircularObjectReference() { var dataObject = new DataObjectWithList { Strings = { "test", "test" }, Ints = { 1, 2, 1 } }; string text; using (var ms = new MemoryStream()) using (var reader = new StreamReader(ms, Encoding.UTF8)) { KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Serialize(ms, dataObject, "test"); ms.Seek(0, SeekOrigin.Begin); text = reader.ReadToEnd(); } var expected = TestDataHelper.ReadTextResource("Text.non_circular_list.vdf"); Assert.That(text, Is.EqualTo(expected)); }
public void SingleLineComment(string resourceName) { using (var stream = TestDataHelper.OpenResource("Text." + resourceName + ".vdf")) Assert.That(() => KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream), Throws.Nothing); }
public void SetUp() { using (var stream = TestDataHelper.OpenResource("Text.duplicate_keys.vdf")) _data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize <Dictionary <string, string> >(stream); }
public void SetUp() { using (var stream = TestDataHelper.OpenResource("Text.escaped_backslash_not_special.vdf")) _data = KvSerializer.Create(KvSerializationFormat.KeyValues1Text).Deserialize(stream); }