void RecursiveSaveBinaryToStreamCore(Stream f)
 {
     // Only supported types ATM:
     // 1. KeyValue with children (no value itself)
     // 2. String KeyValue
     if (Children.Any())
     {
         f.WriteByte(( byte )Type.None);
         StreamScratches.WriteNullTermString(f, Name, Encoding.UTF8);
         foreach (var child in Children)
         {
             child.RecursiveSaveBinaryToStreamCore(f);
         }
         f.WriteByte(( byte )Type.End);
     }
     else
     {
         f.WriteByte(( byte )Type.String);
         StreamScratches.WriteNullTermString(f, Name, Encoding.UTF8);
         StreamScratches.WriteNullTermString(f, Value ?? string.Empty, Encoding.UTF8);
     }
 }
        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 = StreamScratches.ReadNullTermString(input, 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 = StreamScratches.ReadNullTermString(input, Encoding.UTF8);
                    break;
                }

                case Type.WideString:
                {
                    // DebugLog.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(StreamScratches.ReadInt32(input));
                    break;
                }

                case Type.UInt64:
                {
                    current.Value = Convert.ToString(StreamScratches.ReadUInt64(input));
                    break;
                }

                case Type.Float32:
                {
                    current.Value = Convert.ToString(StreamScratches.ReadFloat(input));
                    break;
                }

                case Type.Int64:
                {
                    current.Value = Convert.ToString(StreamScratches.ReadInt64(input));
                    break;
                }

                default:
                {
                    return(false);
                }
                }

                if (parent != null)
                {
                    parent.Children.Add(current);
                }
                current = new KeyValue();
            }

            return(true);
        }