private static bool TryReadAsBinaryCore(Stream input, KeyValue current, KeyValue parent) { current.Children = new List <KeyValue>(); while (true) { var type = (Type)input.ReadByte(); if (type == Type.End) { break; } current.Name = input.ReadNullTermString(Encoding.UTF8); switch (type) { case Type.None: { var child = new KeyValue(); var didReadChild = TryReadAsBinaryCore(input, child, current); if (!didReadChild) { return(false); } break; } case Type.String: { current.Value = input.ReadNullTermString(Encoding.UTF8); break; } case Type.WideString: { System.Diagnostics.Debug.WriteLine("KeyValue", "Encountered WideString type when parsing binary KeyValue, which is unsupported. Returning false."); return(false); } case Type.Int32: case Type.Color: case Type.Pointer: { current.Value = Convert.ToString(input.ReadInt32()); break; } case Type.UInt64: { current.Value = Convert.ToString(input.ReadUInt64()); break; } case Type.Float32: { current.Value = Convert.ToString(input.ReadFloat()); break; } default: { return(false); } } if (parent != null) { parent.Children.Add(current); } current = new KeyValue(); } return(true); }
/// <summary> /// Attempts to load the given filename as a binary <see cref="KeyValue"/>. /// </summary> /// <param name="path">The path to the file to load.</param> /// <param name="keyValue">The resulting <see cref="KeyValue"/> object if the load was successful, or <c>null</c> if unsuccessful.</param> /// <returns><c>true</c> if the load was successful, or <c>false</c> on failure.</returns> public static bool TryLoadAsBinary(string path, out KeyValue keyValue) { keyValue = LoadFromFile(path, true); return(keyValue != null); }