public void Analysis(System.Action <string, float> rProgressAction = null)
        {
            mClassSerializers = new List <HotfixCodeGenerator_ClassSerializer>();

            mCommonSerializer = new HotfixCodeGenerator_CommonSerializer(mCommonSerializerPath);
            mCommonSerializer.WriteHead();

            var rSBTypes = HotfixSerializerBinaryTypes.Types;

            for (int i = 0; i < rSBTypes.Count; i++)
            {
                var rGroupName  = string.Empty;
                var rAttributes = new List <HotfixSBGroupAttribute>();
                rAttributes.AddRange(rSBTypes[i].GetCustomAttributes <HotfixSBGroupAttribute>(true));
                if (rAttributes.Count > 0)
                {
                    rGroupName = rAttributes[0].GroupName;
                }
                var rClassSerializer = new HotfixCodeGenerator_ClassSerializer(UtilTool.PathCombine(mGeneratePath, rGroupName, rSBTypes[i].FullName + ".Binary.cs"));
                rClassSerializer.WriteHead();
                rClassSerializer.WriteClass(rSBTypes[i]);
                rClassSerializer.WriteEnd();

                mClassSerializers.Add(rClassSerializer);

                var rSerializeMemberInfo = HotfixSerializerAssists.FindSerializeMembers(rSBTypes[i]);
                foreach (var rMemberInfo in rSerializeMemberInfo)
                {
                    var bDynamic = rMemberInfo.IsDefined(typeof(HotfixSBDynamicAttribute), false);
                    if (rMemberInfo.MemberType == MemberTypes.Field)
                    {
                        mCommonSerializer.AnalyzeGenerateCommon((rMemberInfo as FieldInfo).FieldType, bDynamic);
                    }
                    else if (rMemberInfo.MemberType == MemberTypes.Property)
                    {
                        mCommonSerializer.AnalyzeGenerateCommon((rMemberInfo as PropertyInfo).PropertyType, bDynamic);
                    }
                }
                UtilTool.SafeExecute(rProgressAction, $"Generate File: {rSBTypes[i].FullName}", (float)i / (float)rSBTypes.Count);
                rClassSerializer.Save();
            }
            mCommonSerializer.WriteEnd();
            mCommonSerializer.Save();

            this.GenCsproj();
        }
        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,
                       @"}
    }
}
");
        }
Exemplo n.º 3
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();
        }