コード例 #1
0
        public static KeyValue LoadAsBinary(string path)
        {
            if (File.Exists(path) == false)
            {
                return(null);
            }

            try
            {
                var input = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var kv    = new KeyValue();

                if (kv.ReadAsBinary(input) == false)
                {
                    return(null);
                }

                input.Close();
                return(kv);
            }
            catch (Exception)
            {
                return(null);
            }
        }
コード例 #2
0
 public static KeyValue LoadAsBinary(string path)
 {
     if (!File.Exists(path))
     {
         return((KeyValue)null);
     }
     try
     {
         FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         KeyValue   keyValue   = new KeyValue();
         if (!keyValue.ReadAsBinary((Stream)fileStream))
         {
             return((KeyValue)null);
         }
         fileStream.Close();
         return(keyValue);
     }
     catch (Exception ex)
     {
         return((KeyValue)null);
     }
 }
コード例 #3
0
        public bool ReadAsBinary(Stream input)
        {
            this.Children = new List <KeyValue>();
            try
            {
                while (true)
                {
                    KeyValueType keyValueType = (KeyValueType)input.ReadValueU8();
                    if (keyValueType != KeyValueType.End)
                    {
                        KeyValue keyValue = new KeyValue();
                        keyValue.Type = keyValueType;
                        keyValue.Name = input.ReadStringUTF8Z();
                        switch (keyValueType)
                        {
                        case KeyValueType.None:
                            keyValue.ReadAsBinary(input);
                            break;

                        case KeyValueType.String:
                            keyValue.Valid = true;
                            keyValue.Value = (object)input.ReadStringUTF8Z();
                            break;

                        case KeyValueType.Int32:
                            keyValue.Valid = true;
                            keyValue.Value = (object)input.ReadValueS32();
                            break;

                        case KeyValueType.Float32:
                            keyValue.Valid = true;
                            keyValue.Value = (object)input.ReadValueF32();
                            break;

                        case KeyValueType.Pointer:
                            keyValue.Valid = true;
                            keyValue.Value = (object)input.ReadValueU32();
                            break;

                        case KeyValueType.WideString:
                            goto label_5;

                        case KeyValueType.Color:
                            keyValue.Valid = true;
                            keyValue.Value = (object)input.ReadValueU32();
                            break;

                        case KeyValueType.UInt64:
                            keyValue.Valid = true;
                            keyValue.Value = (object)input.ReadValueU64();
                            break;

                        default:
                            goto label_11;
                        }
                        if (input.Position < input.Length)
                        {
                            this.Children.Add(keyValue);
                        }
                        else
                        {
                            goto label_13;
                        }
                    }
                    else
                    {
                        goto label_15;
                    }
                }
label_5:
                throw new FormatException("wstring is unsupported");
label_11:
                throw new FormatException();
label_13:
                throw new FormatException();
label_15:
                this.Valid = true;
                return(input.Position == input.Length);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
コード例 #4
0
        public bool ReadAsBinary(Stream input)
        {
            this.Children = new List <KeyValue>();

            try
            {
                while (true)
                {
                    var type = (KeyValueType)input.ReadValueU8();

                    if (type == KeyValueType.End)
                    {
                        break;
                    }

                    var current = new KeyValue
                    {
                        Type = type,
                        Name = input.ReadStringUnicode(),
                    };

                    switch (type)
                    {
                    case KeyValueType.None:
                    {
                        current.ReadAsBinary(input);
                        break;
                    }

                    case KeyValueType.String:
                    {
                        current.Valid = true;
                        current.Value = input.ReadStringUnicode();
                        break;
                    }

                    case KeyValueType.WideString:
                    {
                        throw new FormatException("wstring is unsupported");
                    }

                    case KeyValueType.Int32:
                    {
                        current.Valid = true;
                        current.Value = input.ReadValueS32();
                        break;
                    }

                    case KeyValueType.UInt64:
                    {
                        current.Valid = true;
                        current.Value = input.ReadValueU64();
                        break;
                    }

                    case KeyValueType.Float32:
                    {
                        current.Valid = true;
                        current.Value = input.ReadValueF32();
                        break;
                    }

                    case KeyValueType.Color:
                    {
                        current.Valid = true;
                        current.Value = input.ReadValueU32();
                        break;
                    }

                    case KeyValueType.Pointer:
                    {
                        current.Valid = true;
                        current.Value = input.ReadValueU32();
                        break;
                    }

                    default:
                    {
                        throw new FormatException();
                    }
                    }

                    if (input.Position >= input.Length)
                    {
                        throw new FormatException();
                    }

                    this.Children.Add(current);
                }

                this.Valid = true;
                return(input.Position == input.Length);
            }
            catch (Exception)
            {
                return(false);
            }
        }