コード例 #1
0
        public void Encode(Datamodel dm, int encoding_version, Stream stream)
        {
            Writer          = new KV2Writer(stream);
            EncodingVersion = encoding_version;

            Writer.Write(String.Format(CodecUtilities.HeaderPattern, "keyvalues2", encoding_version, dm.Format, dm.FormatVersion));
            Writer.WriteLine();

            Users = new Dictionary <Element, int>();

            if (EncodingVersion >= 9 && dm.PrefixAttributes.Count > 0)
            {
                Writer.WriteTokens("$prefix_element$");
                Writer.WriteLine("{");
                Writer.Indent++;
                foreach (var attr in dm.PrefixAttributes)
                {
                    WriteAttribute(attr.Key, attr.Value.GetType(), attr.Value, false);
                }
                Writer.Indent--;
                Writer.WriteLine("}");
            }

            CountUsers(dm.Root);
            WriteElement(dm.Root);
            Writer.WriteLine();

            foreach (var pair in Users.Where(pair => pair.Value > 1))
            {
                if (pair.Key == dm.Root)
                {
                    continue;
                }
                Writer.WriteLine();
                WriteElement(pair.Key);
                Writer.WriteLine();
            }

            Writer.Flush();
        }
コード例 #2
0
ファイル: KeyValues2.cs プロジェクト: Artfunkel/Datamodel.NET
        public void Encode(Datamodel dm, int encoding_version, Stream stream)
        {
            Writer = new KV2Writer(stream);
            EncodingVersion = encoding_version;

            Writer.Write(String.Format(CodecUtilities.HeaderPattern, "keyvalues2", encoding_version, dm.Format, dm.FormatVersion));
            Writer.WriteLine();

            Users = new Dictionary<Element, int>();

            if (EncodingVersion >= 9 && dm.PrefixAttributes.Count > 0)
            {
                Writer.WriteTokens("$prefix_element$");
                Writer.WriteLine("{");
                Writer.Indent++;
                foreach (var attr in dm.PrefixAttributes)
                    WriteAttribute(attr.Key, attr.Value.GetType(), attr.Value, false);
                Writer.Indent--;
                Writer.WriteLine("}");
            }

            CountUsers(dm.Root);
            WriteElement(dm.Root);
            Writer.WriteLine();

            foreach (var pair in Users.Where(pair => pair.Value > 1))
            {
                if (pair.Key == dm.Root)
                    continue;
                Writer.WriteLine();
                WriteElement(pair.Key);
                Writer.WriteLine();
            }

            Writer.Flush();
        }
コード例 #3
0
        void WriteAttribute(string name, Type type, object value, bool in_array)
        {
            bool is_element = type == typeof(Element);

            Type inner_type = null;

            if (!in_array)
            {
                inner_type = Datamodel.GetArrayInnerType(type);
                if (inner_type == typeof(byte) && !ValidAttributes[EncodingVersion].Contains(typeof(byte)))
                {
                    inner_type = null; // fall back on the "binary" type in older KV2 versions
                }
            }
            if (!ValidAttributes[EncodingVersion].Contains(inner_type ?? type))
            {
                throw new CodecException(type.Name + " is not valid in KeyValues2 " + EncodingVersion);
            }

            if (inner_type != null)
            {
                is_element = inner_type == typeof(Element);

                Writer.WriteTokenLine(name, TypeNames[inner_type] + "_array");

                if (((System.Collections.IList)value).Count == 0)
                {
                    Writer.Write(" [ ]");
                    return;
                }

                if (is_element)
                {
                    Writer.WriteLine("[");
                }
                else
                {
                    Writer.Write(" [");
                }

                Writer.Indent++;
                foreach (var array_value in (System.Collections.IList)value)
                {
                    WriteAttribute(null, inner_type, array_value, true);
                }
                Writer.Indent--;
                Writer.TrimEnd(1); // remove trailing comma

                if (inner_type == typeof(Element))
                {
                    Writer.WriteLine("]");
                }
                else
                {
                    Writer.Write(" ]");
                }
                return;
            }

            if (is_element)
            {
                var elem = value as Element;
                var id   = elem == null ? "" : elem.ID.ToString();

                if (in_array)
                {
                    if (elem != null && Users[elem] == 1)
                    {
                        Writer.WriteLine();
                        WriteElement(elem);
                    }
                    else
                    {
                        Writer.WriteTokenLine("element", id);
                    }
                    Writer.Write(",");
                }
                else
                {
                    if (elem != null && Users.ContainsKey(elem) && Users[elem] == 1)
                    {
                        Writer.WriteLine(String.Format("\"{0}\" ", name));
                        WriteElement(elem);
                    }
                    else
                    {
                        Writer.WriteTokenLine(name, "element", id);
                    }
                }
            }
            else
            {
                if (type == typeof(bool))
                {
                    value = (bool)value ? 1 : 0;
                }
                else if (type == typeof(float))
                {
                    value = (float)value;
                }
                else if (type == typeof(byte[]))
                {
                    value = BitConverter.ToString((byte[])value).Replace("-", String.Empty);
                }
                else if (type == typeof(TimeSpan))
                {
                    value = ((TimeSpan)value).TotalSeconds;
                }
                else if (type == typeof(System.Drawing.Color))
                {
                    var c = (System.Drawing.Color)value;
                    value = String.Join(" ", new int[] { c.R, c.G, c.B, c.A });
                }
                else if (type == typeof(UInt64))
                {
                    value = ((UInt64)value).ToString("X");
                }
                else if (type == typeof(Vector2))
                {
                    var arr = new float[2];
                    ((Vector2)value).CopyTo(arr);
                    value = string.Join(" ", arr);
                }
                else if (type == typeof(Vector3))
                {
                    var arr = new float[3];
                    ((Vector3)value).CopyTo(arr);
                    value = string.Join(" ", arr);
                }
                else if (type == typeof(Vector4))
                {
                    var arr = new float[4];
                    ((Vector4)value).CopyTo(arr);
                    value = string.Join(" ", arr);
                }
                else if (type == typeof(Quaternion))
                {
                    var arr = new float[4];
                    var q   = (Quaternion)value;
                    value = string.Join(" ", q.X, q.Y, q.Z, q.W);
                }
                else if (type == typeof(Matrix4x4))
                {
                    var arr = new float[4 * 4];
                    var m   = (Matrix4x4)value;
                    value = string.Join(" ", m.M11, m.M12, m.M13, m.M14, m.M21, m.M22, m.M23, m.M24, m.M31, m.M32, m.M33, m.M34, m.M41, m.M42, m.M43, m.M44);
                }

                if (in_array)
                {
                    Writer.Write(String.Format(" \"{0}\",", value.ToString()));
                }
                else
                {
                    Writer.WriteTokenLine(name, TypeNames[type], value.ToString());
                }
            }
        }