Ejemplo n.º 1
0
        private void StateSave(string path)
        {
            Dictionary <string, Value> values = new Dictionary <string, Value> ();

            foreach (TreeNode root in this.treeViewValue.Nodes)
            {
                foreach (KeyValuePair <Value, Value> pair in this.ValuesBuild(root.Nodes))
                {
                    values[pair.Key.AsString] = pair.Value;
                }
            }

            try
            {
                using (BinaryWriter writer = new BinaryWriter(new FileStream(path, FileMode.Create), Encoding.UTF8))
                {
                    writer.Write(2);

                    ValueAccessor.Save(writer, values);

                    writer.Write(this.parameters.BlockBegin);
                    writer.Write(this.parameters.BlockContinue);
                    writer.Write(this.parameters.BlockEnd);
                    writer.Write(this.parameters.TrimmerIndex);
                    writer.Write(this.textBoxInput.Text);
                }

                MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "State successfully saved to \"{0}\".", path), "File save successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Cannot open output file \"{0}\"", path), "File save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 2
0
        private static bool Load(BinaryReader reader, out Value value)
        {
            List <KeyValuePair <Value, Value> > array;
            Value        arrayKey;
            Value        arrayValue;
            int          count;
            ValueContent type;

            type = (ValueContent)reader.ReadInt32();

            switch (type)
            {
            case ValueContent.Boolean:
                value = reader.ReadBoolean() ? BooleanValue.True : BooleanValue.False;

                break;

            case ValueContent.Map:
                count = reader.ReadInt32();
                array = new List <KeyValuePair <Value, Value> > (count);

                while (count-- > 0)
                {
                    if (!ValueAccessor.Load(reader, out arrayKey) || !ValueAccessor.Load(reader, out arrayValue))
                    {
                        value = null;

                        return(false);
                    }

                    array.Add(new KeyValuePair <Value, Value> (arrayKey, arrayValue));
                }

                value = array;

                break;

            case ValueContent.Number:
                value = reader.ReadDecimal();

                break;

            case ValueContent.String:
                value = reader.ReadString();

                break;

            case ValueContent.Void:
                value = VoidValue.Instance;

                break;

            default:
                value = null;

                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private static void Save(BinaryWriter writer, Value value)
        {
            writer.Write((int)value.Type);

            switch (value.Type)
            {
            case ValueContent.Boolean:
                writer.Write(value.AsBoolean);

                break;

            case ValueContent.Map:
                writer.Write(value.Fields.Count);

                foreach (KeyValuePair <Value, Value> pair in value.Fields)
                {
                    ValueAccessor.Save(writer, pair.Key);
                    ValueAccessor.Save(writer, pair.Value);
                }

                break;

            case ValueContent.Number:
                writer.Write(value.AsNumber);

                break;

            case ValueContent.String:
                writer.Write(value.AsString);

                break;
            }
        }
Ejemplo n.º 4
0
        private void StateLoad(string path, bool dialog)
        {
            TreeNode root;
            Dictionary <string, Value> values;
            int version;

            if (this.treeViewValue.Nodes.Count < 1)
            {
                return;
            }

            root = this.treeViewValue.Nodes[0];
            root.Nodes.Clear();

            try
            {
                using (BinaryReader reader = new BinaryReader(new FileStream(path, FileMode.Open), Encoding.UTF8))
                {
                    values  = new Dictionary <string, Value> ();
                    version = reader.ReadInt32();

                    if (version < 1 || version > 2)
                    {
                        MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Incompatible file format"));

                        return;
                    }

                    if (ValueAccessor.Load(reader, values))
                    {
                        foreach (KeyValuePair <string, Value> pair in values)
                        {
                            root.Nodes.Add(this.NodeCreate(pair.Key, pair.Value));
                        }
                    }

                    this.parameters = new SettingForm.Parameters
                    {
                        BlockBegin    = reader.ReadString(),
                        BlockContinue = reader.ReadString(),
                        BlockEnd      = reader.ReadString(),
                        TrimmerIndex  = version > 1 ? reader.ReadInt32() : TrimmerCollection.DEFAULT_INDEX
                    };

                    this.textBoxInput.Text = reader.ReadString();
                }

                root.ExpandAll();

                if (dialog)
                {
                    MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "State successfully loaded from \"{0}\".", path), "File save successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch
            {
                MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Cannot open input file \"{0}\"", path), "File load error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Ejemplo n.º 5
0
        public static void Save(BinaryWriter writer, IDictionary <string, Value> values)
        {
            writer.Write(values.Count);

            foreach (KeyValuePair <string, Value> pair in values)
            {
                writer.Write(pair.Key);

                ValueAccessor.Save(writer, pair.Value);
            }
        }
Ejemplo n.º 6
0
        public static bool Load(BinaryReader reader, IDictionary <string, Value> values)
        {
            int    count;
            string key;
            Value  value;

            for (count = reader.ReadInt32(); count-- > 0;)
            {
                key = reader.ReadString();

                if (!ValueAccessor.Load(reader, out value))
                {
                    return(false);
                }

                values[key] = value;
            }

            return(true);
        }