Exemplo n.º 1
0
        private void _WriteGetMetadataTypeMethod(Type type)
        {
            _writer.WriteLine("[Export(ExportFlags.AutoCode)]");

            if (_rootTypes.Contains(type))
            {
                _writer.WriteLine("public virtual ushort GetMetadataType ()");
                using (CodeScope.CreateCSharpScope(_writer))
                {
                    _writer.WriteLine("throw new NotImplementedException(\"This method should be override~\");");
                }
            }
            else
            {
                var overrideText = _rootTypes.Contains(EditorMetaCommon.GetRootMetadata(type)) ? "override " : string.Empty;
                _writer.WriteLine("public {0}ushort GetMetadataType ()", overrideText);

                using (CodeScope.CreateCSharpScope(_writer))
                {
                    //var typeName = EditorMetaCommon.GetMetaTypeName(type);
                    //_writer.WriteLine("return (ushort) MetadataType.{0};", typeName);
                    _writer.WriteLine("throw new NotImplementedException(\"This method should be override~\");");
                }
            }
        }
Exemplo n.º 2
0
        private void _WriteSaveMethod(Type type, List <MemberBase> members)
        {
            if (_rootTypes.Contains(type))
            {
                _writer.WriteLine("[Export(ExportFlags.AutoCode)]");
                _writer.WriteLine("public virtual void Save (IOctetsWriter writer)");
                using (CodeScope.CreateCSharpScope(_writer))
                {
                    _writer.WriteLine("throw new NotImplementedException(\"This method should be override~\");");
                }
            }
            else
            {
                using (MacroScope.CreateEditorScope(_writer.BaseWriter))
                {
                    _writer.WriteLine("[Export(ExportFlags.AutoCode)]");

                    var overrideText = _rootTypes.Contains(EditorMetaCommon.GetRootMetadata(type)) ? "override " : string.Empty;
                    _writer.WriteLine("public {0}void Save (IOctetsWriter writer)", overrideText);
                    using (CodeScope.Create(_writer, "{\n", "}\n"))
                    {
                        for (int index = 0; index < members.Count; ++index)
                        {
                            var member = members[index];
                            member.WriteSave(_writer);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void _WriteOneClassOrStruct(Type type, ExportFlags flags)
        {
            var classOrStruct = type.IsClass ? "class" : "struct";
            var sealedClass   = type.IsClass && EditorMetaCommon.IsFinalType(type) ? "sealed " : string.Empty;

            _writer.WriteLine("[Serializable]");
            _writer.WriteLine("{0}partial {1} {2} : {3}", sealedClass, classOrStruct, type.Name, typeof(ILoadable).Name);

            using (MacroScope.CreateEditorScope(_writer.BaseWriter))
            {
                _writer.WriteLine(", {0}", typeof(ISavable).Name);
            }

            using (CodeScope.CreateCSharpScope(_writer))
            {
                var nestedTypes = type.GetNestedTypes(BindingFlags.Instance | BindingFlags.Public);
                _WriteNestedTypes(nestedTypes, flags);

                var members = new List <MemberBase>();
                _CollectSerializableMembers(type, members);

                _WriteSaveMethod(type, members);
                _WriteLoadMethod(type, members);
                _WriteToStringMethod(type, members);
                _WriteGetMetadataTypeMethod(type);
            }
        }
Exemplo n.º 4
0
 public override void WriteNotEqualsReturn(CodeWriter writer)
 {
     writer.WriteLine("if ((!string.IsNullOrEmpty({0}) || !string.IsNullOrEmpty(that.{0})) && ({0} != that.{0}))", _name);
     using (CodeScope.CreateCSharpScope(writer))
     {
         writer.WriteLine("return false;");
     }
 }
Exemplo n.º 5
0
 public override void WriteNotEqualsReturn(CodeWriter writer)
 {
     writer.WriteLine("if ({0} != that.{0})", _name);
     using (CodeScope.CreateCSharpScope(writer))
     {
         writer.WriteLine("return false;");
     }
 }
Exemplo n.º 6
0
 public override void WriteNotEqualsReturn(CodeWriter writer)
 {
     writer.WriteLine("if (!Unique.ColorTools.IsEqual({0}, that.{0}))", _name);
     using (CodeScope.CreateCSharpScope(writer))
     {
         writer.WriteLine("return false;");
     }
 }
Exemplo n.º 7
0
        public override void WriteSave(CodeWriter writer)
        {
            writer.WriteLine("int {0}Count = null != {0} ? {0}.Count : 0 ;", _name);
            writer.WriteLine("writer.Write ({0}Count);", _name);
            writer.WriteLine("for (int index= 0; index < {0}Count; ++index)", _name);

            using (CodeScope.CreateCSharpScope(writer))
            {
                writer.WriteLine("var item = {0}[index];", _name);
                var elementType = _type.GetGenericArguments()[0];
                _WriteSaveType(writer, elementType, "item");
            }
        }
Exemplo n.º 8
0
        public override void WriteSave(CodeWriter writer)
        {
            writer.WriteLine("int {0}Length = null != {0} ? {0}.Length : 0 ;", _name);
            writer.WriteLine("writer.Write ({0}Length);", _name);
            writer.WriteLine("for (int i{1}= 0; i{1} < {0}Length; ++i{1})", _name, writer.Indent);

            using (CodeScope.CreateCSharpScope(writer))
            {
                writer.WriteLine("var item{1} = {0}[i{1}];", _name, writer.Indent - 1);
                var elementType = _type.GetElementType();
                _WriteSaveType(writer, elementType, string.Format("item{0}", writer.Indent - 1));
            }
        }
Exemplo n.º 9
0
        public override void WriteLoad(CodeWriter writer)
        {
            Type   elementType     = _type.GetGenericArguments() [0];
            string elementTypeName = EditorMetaCommon.GetNestedClassName(elementType);

            writer.WriteLine("int {0}Count = reader.ReadInt32();", _name);
            writer.WriteLine("if (null == {0}) {{ {0} = new List<{1}>({0}Count); }} else {{ {0}.Clear(); }}", _name, elementTypeName);
            writer.WriteLine("for (int index= 0; index < {0}Count; ++index)", _name);

            using (CodeScope.CreateCSharpScope(writer))
            {
                writer.WriteLine("{0}.Add (default({1}));", _name, elementTypeName);
                _WriteLoadType(writer, elementType, _name + "[index]");
            }
        }
Exemplo n.º 10
0
 public override void WriteLoad(CodeWriter writer)
 {
     if (_type.IsClass)
     {
         writer.WriteLine();
         writer.WriteLine("if (reader.ReadBoolean())");
         using (CodeScope.CreateCSharpScope(writer))
         {
             _WriteLoadMetadata(writer);
         }
     }
     else if (_type.IsValueType)
     {
         writer.WriteLine("{0}.Load(reader);", _name);
     }
 }
Exemplo n.º 11
0
 public override void WriteSave(CodeWriter writer)
 {
     if (_type.IsClass)
     {
         writer.WriteLine();
         writer.WriteLine("writer.Write(null != {0});", _name);
         writer.WriteLine("if (null != {0})", _name);
         using (CodeScope.CreateCSharpScope(writer))
         {
             _WriteSaveMetadata(writer);
         }
     }
     else if (_type.IsValueType)
     {
         writer.WriteLine("{0}.Save(writer);", _name);
     }
 }
Exemplo n.º 12
0
        public override void WriteNotEqualsReturn(CodeWriter writer)
        {
            writer.WriteLine("int {0}Length1 = null != {0} ? {0}.Length : 0 ;", _name);
            writer.WriteLine("int {0}Length2 = null != that.{0} ? that.{0}.Length : 0 ;", _name);
            writer.WriteLine("if ({0}Length1 != {0}Length2)", _name);
            using (CodeScope.CreateCSharpScope(writer))
            {
                writer.WriteLine("return false;");
            }

            writer.WriteLine("for (int i{1}= 0; i{1} < {0}Length1; ++i{1})", _name, writer.Indent);

            using (CodeScope.CreateCSharpScope(writer))
            {
                var elementType = _type.GetElementType();
                var itemName    = string.Format("{0}[i{1}]", _name, writer.Indent - 1);
                _WriteNotEqualsReturn(writer, elementType, itemName);
            }
        }
Exemplo n.º 13
0
 public override void WriteNotEqualsReturn(CodeWriter writer)
 {
     if (_type.IsValueType)
     {
         writer.WriteLine("if (!{0}.EqualsTo (that.{0}))", _name);
         using (CodeScope.CreateCSharpScope(writer))
         {
             writer.WriteLine("return false;");
         }
     }
     else
     {
         writer.WriteLine("if (null == {0} && null != that.{0} || null != {0} && !{0}.EqualsTo (that.{0}))", _name);
         using (CodeScope.CreateCSharpScope(writer))
         {
             writer.WriteLine("return false;");
         }
     }
 }
Exemplo n.º 14
0
        private void _WriteEqualsToMethod(Type type, List <MemberBase> members)
        {
            _writer.WriteLine("[Export(ExportFlags.AutoCode)]");
            if (_rootTypes.Contains(type))
            {
                _writer.WriteLine("public virtual bool EqualsTo (IMetadata other)");
                using (CodeScope.CreateCSharpScope(_writer))
                {
                    _writer.WriteLine("throw new NotImplementedException(\"This method should be override~\");");
                }
            }
            else
            {
                var overrideText = _rootTypes.Contains(EditorMetaCommon.GetRootMetadata(type)) ? "override " : string.Empty;
                _writer.WriteLine("public {0}bool EqualsTo (IMetadata other)", overrideText);

                using (CodeScope.CreateCSharpScope(_writer))
                {
                    var typeName = EditorMetaCommon.GetNestedClassName(type);
                    _writer.WriteLine("var that = ({0}) other;", typeName);

                    if (type.IsClass)
                    {
                        _writer.WriteLine("if (null == that)");
                        using (CodeScope.CreateCSharpScope(_writer))
                        {
                            _writer.WriteLine("return false;");
                        }
                    }

                    var memberCount = members.Count;

                    for (int index = 0; index < memberCount; ++index)
                    {
                        var member = members[index];
                        member.WriteNotEqualsReturn(_writer);
                    }

                    _writer.WriteLine("return true;");
                }
            }
        }
Exemplo n.º 15
0
        public override void WriteLoad(CodeWriter writer)
        {
            string childtype = EditorMetaCommon.GetNestedClassName(_type.GetElementType()).Replace("+", ".");
            string prefix    = childtype;
            string postfix   = string.Empty;
            int    first     = childtype.IndexOf("[", StringComparison.Ordinal);

            if (first >= 0)
            {
                prefix  = childtype.Substring(0, first);
                postfix = childtype.Substring(first);
            }

            writer.WriteLine("{0} = new {1}[reader.ReadInt32()]{2};", _name, prefix, postfix);
            writer.WriteLine("for (int i{1} = 0; i{1} < {0}.Length; i{1}++)", _name, writer.Indent);
            using (CodeScope.CreateCSharpScope(writer))
            {
                _WriteLoadType(writer, _type.GetElementType(), _name + string.Format("[i{0}]", writer.Indent - 1));
            }
        }
Exemplo n.º 16
0
 private void _WriteMetadataManagerTemplates(List <string> templates)
 {
     using (_writer = new CodeWriter(_GetTemplateManagerPath()))
     {
         _WriteFileHead();
         using (CodeScope.Create(_writer, "{\n", "}"))
         {
             _writer.WriteLine("public partial class TemplateManager");
             using (CodeScope.CreateCSharpScope(_writer))
             {
                 _writer.WriteLine("partial void _LoadTemplates()");
                 using (CodeScope.CreateCSharpScope(_writer))
                 {
                     foreach (var template in templates)
                     {
                         _writer.WriteLine("_AddLoaderTemplate<{0}>();", template);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 17
0
 private void _WriterMetadataManagerConfigs(List <string> configs)
 {
     using (_writer = new CodeWriter(_GetConfigManagerPath()))
     {
         _WriteFileHead();
         using (CodeScope.Create(_writer, "{\n", "}"))
         {
             _writer.WriteLine("public partial class ConfigManager");
             using (CodeScope.CreateCSharpScope(_writer))
             {
                 _writer.WriteLine("partial void _LoadConfigs()");
                 using (CodeScope.CreateCSharpScope(_writer))
                 {
                     foreach (var config in configs)
                     {
                         _writer.WriteLine("_AddLoaderConfig<{0}>();", config);
                     }
                 }
             }
         }
     }
 }