Beispiel #1
0
 /// <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);
 }
Beispiel #2
0
        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: {
                    MainLog.Logger.Warn("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);
        }
Beispiel #3
0
 public static void SaveToFile(KeyValue kv, IAbsoluteFilePath fp, bool asBinary = false)
 {
     MainLog.Logger.Debug($"Saving KV to file {fp}, {asBinary}");
     kv.SaveToFile(kv.ToString(), asBinary);
     MainLog.Logger.Debug($"Saved KV to file {fp}, {asBinary}");
 }
Beispiel #4
0
        internal void RecursiveLoadFromBuffer(KVTextReader kvr)
        {
            bool wasQuoted;
            bool wasConditional;

            while (true)
            {
                // bool bAccepted = true;

                // get the key name
                string name = kvr.ReadToken(out wasQuoted, out wasConditional);

                if (string.IsNullOrEmpty(name))
                {
                    throw new Exception("RecursiveLoadFromBuffer: got EOF or empty keyname");
                }

                if (name.StartsWith("}") && !wasQuoted) // top level closed, stop reading
                {
                    break;
                }

                KeyValue dat = new KeyValue(name);
                dat.Children = new List <KeyValue>();
                this.Children.Add(dat);

                // get the value
                string value = kvr.ReadToken(out wasQuoted, out wasConditional);

                if (wasConditional && value != null)
                {
                    // bAccepted = ( value == "[$WIN32]" );
                    value = kvr.ReadToken(out wasQuoted, out wasConditional);
                }

                if (value == null)
                {
                    throw new Exception("RecursiveLoadFromBuffer:  got NULL key");
                }

                if (value.StartsWith("}") && !wasQuoted)
                {
                    throw new Exception("RecursiveLoadFromBuffer:  got } in key");
                }

                if (value.StartsWith("{") && !wasQuoted)
                {
                    dat.RecursiveLoadFromBuffer(kvr);
                }
                else
                {
                    if (wasConditional)
                    {
                        throw new Exception("RecursiveLoadFromBuffer:  got conditional between key and value");
                    }

                    dat.Value = value;
                    // blahconditionalsdontcare
                }
            }
        }