예제 #1
0
        public void WriteDictionary(Type rType, bool bDynamic)
        {
            if (this.ReceiveGeneratedDictionaryType(rType, bDynamic))
            {
                return;
            }

            var rTypeName  = HotfixSerializerAssists.GetTypeName(rType);
            var rKeyType   = rType.GetGenericArguments()[0];
            var rValueType = rType.GetGenericArguments()[1];

            var rTDText  = bDynamic ? "Dynamic" : string.Empty;
            var rTDKText = bDynamic && !HotfixSerializerAssists.IsBaseType(rKeyType, false) ? "Dynamic" : string.Empty;
            var rTDVText = bDynamic && !HotfixSerializerAssists.IsBaseType(rValueType, false) ? "Dynamic" : string.Empty;

            this.StringBuilder?
            .T(2).F("public static void Serialize{0}(this BinaryWriter rWriter, {1} value)", rTDText, rTypeName).N()
            .T(2).A("{").N()
            .T(3).A("var bValid = (null != value);").N()
            .T(3).A("rWriter.Serialize(bValid);").N()
            .T(3).A("if (!bValid) return;").N()
            .L(1)
            .T(3).A("rWriter.Serialize(value.Count);").N()
            .T(3).A("foreach(var rPair in value)").N()
            .T(3).A("{").N()
            .T(4).F("rWriter.Serialize{0}({1});", rTDKText, (rKeyType.IsEnum ? "(int)rPair.Key" : "rPair.Key")).N()
            .T(4).F("rWriter.Serialize{0}({1});", rTDVText, (rValueType.IsEnum ? "(int)rPair.Value" : "rPair.Value")).N()
            .T(3).A("}").N()
            .T(2).A("}").N()
            .L(1);

            this.StringBuilder?
            .T(2).F("public static {0} Deserialize{1}(this BinaryReader rReader, {2} value)", rTypeName, rTDText, rTypeName).N()
            .T(2).A("{").N()
            .T(3).A("var bValid = rReader.Deserialize(default(bool));").N()
            .T(3).A("if (!bValid) return null;").N()
            .L(1)
            .T(3).A("var nCount  = rReader.Deserialize(default(int));").N()
            .T(3).F("var rResult = new {0}();", rTypeName).N()
            .T(3).A("for (int nIndex = 0; nIndex < nCount; ++ nIndex)").N()
            .T(3).A("{").N()
            .T(4).F("var rKey   = {0}rReader.Deserialize{1}({2});",
                    (rKeyType.IsEnum ? string.Format("({0})", rKeyType.FullName) : string.Empty),
                    rTDKText,
                    HotfixSerializerAssists.GetDeserializeUnwrap(rKeyType)).N()
            .T(4).F("var rValue = {0}rReader.Deserialize{1}({2});",
                    (rValueType.IsEnum ? string.Format("({0})", rValueType.FullName) : string.Empty),
                    rTDVText,
                    HotfixSerializerAssists.GetDeserializeUnwrap(rValueType)).N()
            .T(4).A("rResult.Add(rKey, rValue);").N()
            .T(3).A("}").N()
            .T(3).A("return rResult;").N()
            .T(2).A("}").N()
            .L(1);
        }
        public void WriteDictionary(Type rType, bool bDynamic)
        {
            if (this.ReceiveGeneratedDictionaryType(rType, bDynamic))
            {
                return;
            }

            var rTypeName  = HotfixSerializerAssists.GetTypeName(rType);
            var rKeyType   = rType.GetGenericArguments()[0];
            var rValueType = rType.GetGenericArguments()[1];

            var rTDText  = bDynamic ? "Dynamic" : string.Empty;
            var rTDKText = bDynamic && !HotfixSerializerAssists.IsBaseType(rKeyType, false) ? "Dynamic" : string.Empty;
            var rTDVText = bDynamic && !HotfixSerializerAssists.IsBaseType(rValueType, false) ? "Dynamic" : string.Empty;

            this.WriteBraceCode(2,
                                $"public static void Serialize{rTDText}(this BinaryWriter rWriter, {rTypeName} value)",
                                @"{",
                                $@"var bValid = (null != value);
			rWriter.Serialize(bValid);
			if (!bValid) return;

			rWriter.Serialize(value.Count);
            {this.GenerateBraceCode(0, 
            "foreach(var rPair in value)",
"            {",
$@"            rWriter.Serialize{rTDKText}({(rKeyType.IsEnum ? "(int)rPair.Key" : "rPair.Key")});
				rWriter.Serialize{rTDVText}({(rValueType.IsEnum ? "(int)rPair.Value" : "rPair.Value")});",
"            }")}",
                                "}");

            this.WriteBraceCode(2,
                                $"public static {rTypeName} Deserialize{rTDText}(this BinaryReader rReader, {rTypeName} value)",
                                @"{",
                                $@"var bValid = rReader.Deserialize(default(bool));
			if (!bValid) return null;

			var nCount  = rReader.Deserialize(default(int));
            var rResult = new {rTypeName}();
            {this.GenerateBraceCode(0,
            "for (int nIndex = 0; nIndex < nCount; ++ nIndex)",
"            {",
$@"            var rKey   = {(rKeyType.IsEnum ? string.Format("({0})", rKeyType.FullName) : string.Empty)}rReader.Deserialize{rTDKText}({HotfixSerializerAssists.GetDeserializeUnwrap(rKeyType)});
                var rValue = {(rValueType.IsEnum ? string.Format("({0})", rValueType.FullName) : string.Empty)}rReader.Deserialize{rTDVText}({HotfixSerializerAssists.GetDeserializeUnwrap(rValueType)});
                rResult.Add(rKey, rValue);",
"            }")}
            return rResult;",
                                "}");
        }
예제 #3
0
        public void WriteArray(Type rType, bool bDynamic)
        {
            if (this.ReceiveGeneratedArrayType(rType, bDynamic))
            {
                return;
            }

            var rTypeName    = HotfixSerializerAssists.GetTypeName(rType);
            var rElementType = rType.GetElementType();

            var rTDText  = bDynamic ? "Dynamic" : string.Empty;
            var rTDEText = bDynamic && !HotfixSerializerAssists.IsBaseType(rElementType, false) ? "Dynamic" : string.Empty;

            this.StringBuilder?
            .T(2).F("public static void Serialize{0}(this BinaryWriter rWriter, {1} value)", rTDText, rTypeName).N()
            .T(2).A("{").N()
            .T(3).A("var bValid = (null != value);").N()
            .T(3).A("rWriter.Serialize(bValid);").N()
            .T(3).A("if (!bValid) return;").N()
            .L(1)
            .T(3).A("rWriter.Serialize(value.Length);").N()
            .T(3).A("for (int nIndex = 0; nIndex < value.Length; nIndex++)").N()
            .T(4).F("rWriter.Serialize{0}({1});", rTDEText, (rElementType.IsEnum ? "(int)value[nIndex]" : "value[nIndex]")).N()
            .T(2).A("}").N()
            .L(1);

            this.StringBuilder?
            .T(2).F("public static {0} Deserialize(this BinaryReader rReader, {1} value)", rTypeName, rTypeName).N()
            .T(2).A("{").N()
            .T(3).A("var bValid = rReader.Deserialize(default(bool));").N()
            .T(3).A("if (!bValid) return null;").N()
            .L(1)
            .T(3).A("var nCount  = rReader.Deserialize(default(int));").N()
            .T(3).F("var rResult = new {0};", rTypeName.Insert(rTypeName.IndexOf('[') + 1, "nCount")).N()
            .T(3).A("for (int nIndex = 0; nIndex < nCount; nIndex++)").N()
            .T(4).F("rResult[nIndex] = {0}rReader.Deserialize{1}({2});",
                    (rElementType.IsEnum ? string.Format("({0})", rElementType.FullName) : string.Empty),
                    rTDEText,
                    HotfixSerializerAssists.GetDeserializeUnwrap(rElementType)).N()
            .T(3).A("return rResult;").N()
            .T(2).A("}").N()
            .L(1);
        }
        public void WriteArray(Type rType, bool bDynamic)
        {
            if (this.ReceiveGeneratedArrayType(rType, bDynamic))
            {
                return;
            }

            var rTypeName    = HotfixSerializerAssists.GetTypeName(rType);
            var rElementType = rType.GetElementType();

            var rTDText  = bDynamic ? "Dynamic" : string.Empty;
            var rTDEText = bDynamic && !HotfixSerializerAssists.IsBaseType(rElementType, false) ? "Dynamic" : string.Empty;

            this.WriteBraceCode(2,
                                $"public static void Serialize{rTDText}(this BinaryWriter rWriter, {rTypeName} value)",
                                "{",
                                $@"var bValid = (null != value);
	        rWriter.Serialize(bValid);
	        if (!bValid) return;

	        rWriter.Serialize(value.Length); 
	        for (int nIndex = 0; nIndex < value.Length; nIndex++)
                rWriter.Serialize{rTDEText}({(rElementType.IsEnum ? "(int)value[nIndex]" : "value[nIndex]")});",
                                "}");

            this.WriteBraceCode(2,
                                $"public static {rTypeName} Deserialize(this BinaryReader rReader, {rTypeName} value)",
                                "{",
                                $@"var bValid = rReader.Deserialize(default(bool));
			if (!bValid) return null;

            var nCount  = rReader.Deserialize(default(int));
            var rResult = new {rTypeName.Insert(rTypeName.IndexOf('[') + 1, "nCount")};
			for (int nIndex = 0; nIndex < nCount; nIndex++)
                rResult[nIndex] = {(rElementType.IsEnum ? string.Format("({0})", rElementType.FullName) : string.Empty)}rReader.Deserialize{rTDEText}({HotfixSerializerAssists.GetDeserializeUnwrap(rElementType)});
            return rResult;",
                                "}");
        }
        public void AnalyzeGenerateCommon(Type rType, bool bDynamic)
        {
            if (HotfixSerializerAssists.IsBaseType(rType))
            {
                return;
            }

            if (rType.IsArray)
            {
                WriteArray(rType, bDynamic);
                AnalyzeGenerateCommon(rType.GetElementType(), bDynamic);
            }
            else if (rType.GetInterface("System.Collections.IList") != null)
            {
                WriteList(rType, bDynamic);
                AnalyzeGenerateCommon(rType.GetGenericArguments()[0], bDynamic);
            }
            else if (rType.GetInterface("System.Collections.IDictionary") != null || rType.GetInterface("Knight.Core.IDict") != null)
            {
                WriteDictionary(rType, bDynamic);
                AnalyzeGenerateCommon(rType.GetGenericArguments()[0], bDynamic);
                AnalyzeGenerateCommon(rType.GetGenericArguments()[1], bDynamic);
            }
        }
        public void WriteClass(Type rType)
        {
            this.Write(0,
                       $"namespace {rType.Namespace}");
            this.Write(0,
                       "{");
            this.Write(1,
                       $"public partial class {rType.Name}");
            this.Write(1,
                       @"{
        public override void Serialize(BinaryWriter rWriter)
	    {
            base.Serialize(rWriter);");

            var rAllSerializeMembers = HotfixSerializerAssists.FindSerializeMembers(rType);

            for (int i = 0; i < rAllSerializeMembers.Count; i++)
            {
                var rMemberInfo = rAllSerializeMembers[i];
                var rParamText  = HotfixSerializerAssists.GetClassMemberDummyText(rAllSerializeMembers[i]);

                if (rMemberInfo.IsDefined(typeof(HotfixSBDynamicAttribute), true) &&
                    !HotfixSerializerAssists.IsBaseType(HotfixSerializerAssists.GetMemberType(rMemberInfo), false))
                {
                    this.Write(3, $"rWriter.SerializeDynamic({rParamText});");
                }
                else
                {
                    this.Write(3, $"rWriter.Serialize({rParamText});");
                }
            }
            this.Write(2,
                       "}");

            this.Write(2,
                       @"public override void Deserialize(BinaryReader rReader)
	    {
		    base.Deserialize(rReader);"        );

            for (int i = 0; i < rAllSerializeMembers.Count; i++)
            {
                var rMemberInfo      = rAllSerializeMembers[i];
                var rMemberText      = HotfixSerializerAssists.GetClassMemberTypeText(rMemberInfo);
                var rMemberDummyText = HotfixSerializerAssists.GetClassMemberDummyText(rMemberInfo);

                if (rMemberInfo.IsDefined(typeof(HotfixSBDynamicAttribute), false) &&
                    !HotfixSerializerAssists.IsBaseType(HotfixSerializerAssists.GetMemberType(rMemberInfo), false))
                {
                    this.Write(3, $"this.{rMemberInfo.Name} = {rMemberText}rReader.DeserializeDynamic({rMemberDummyText});");
                }
                else
                {
                    this.Write(3, $"this.{rMemberInfo.Name} = {rMemberText}rReader.Deserialize({rMemberDummyText});");
                }
            }
            this.Write(2,
                       @"}
    }
}
");
        }
예제 #7
0
        public void WriteClass(Type rType)
        {
            this.StringBuilder?
            .F("namespace {0}", rType.Namespace).N()
            .A("{").N()
            .T(1).F("public partial class {0}", rType.Name).N()
            .T(1).A("{").N()
            .T(2).A("public override void Serialize(BinaryWriter rWriter)").N()
            .T(2).A("{").N()
            .T(3).A("base.Serialize(rWriter);").N();

            var rAllSerializeMembers = HotfixSerializerAssists.FindSerializeMembers(rType);

            for (int i = 0; i < rAllSerializeMembers.Count; i++)
            {
                var rMemberInfo = rAllSerializeMembers[i];
                var rParamText  = HotfixSerializerAssists.GetClassMemberDummyText(rAllSerializeMembers[i]);

                if (rMemberInfo.IsDefined(typeof(HotfixSBDynamicAttribute), true) &&
                    !HotfixSerializerAssists.IsBaseType(HotfixSerializerAssists.GetMemberType(rMemberInfo), false))
                {
                    this.StringBuilder?
                    .T(3).F("rWriter.SerializeDynamic({0});", rParamText).N();
                }
                else
                {
                    this.StringBuilder?
                    .T(3).F("rWriter.Serialize({0});", rParamText).N();
                }
            }
            this.StringBuilder
            .T(2).A("}").N();

            this.StringBuilder
            .T(2).A("public override void Deserialize(BinaryReader rReader)").N()
            .T(2).A("{").N()
            .T(3).A("base.Deserialize(rReader);").N();

            for (int i = 0; i < rAllSerializeMembers.Count; i++)
            {
                var rMemberInfo      = rAllSerializeMembers[i];
                var rMemberText      = HotfixSerializerAssists.GetClassMemberTypeText(rMemberInfo);
                var rMemberDummyText = HotfixSerializerAssists.GetClassMemberDummyText(rMemberInfo);

                if (rMemberInfo.IsDefined(typeof(HotfixSBDynamicAttribute), false) &&
                    !HotfixSerializerAssists.IsBaseType(HotfixSerializerAssists.GetMemberType(rMemberInfo), false))
                {
                    this.StringBuilder
                    .T(3).F("this.{0} = {1}rReader.DeserializeDynamic({2});", rMemberInfo.Name, rMemberText, rMemberDummyText).N();
                }
                else
                {
                    this.StringBuilder
                    .T(3).F("this.{0} = {1}rReader.Deserialize({2});", rMemberInfo.Name, rMemberText, rMemberDummyText).N();
                }
            }

            this.StringBuilder
            .T(2).A("}").N()
            .T(1).A("}").N()
            .A("}").N();
        }