public static string GetSerializerFieldLine_Serialize(Type fieldType, string fieldAccessor, string fullFieldAccessor = null) { if (fullFieldAccessor == null) { fullFieldAccessor = "obj" + fieldAccessor; } if (fieldType.IsEnum) { return($"{NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.Serialize(({fieldType.GetEnumUnderlyingType()}){fullFieldAccessor}, writer);"); } if (NetSerializationCodeGenUtility.ConsideredAsValueType(fieldType)) { return($"{NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.Serialize(ref {fullFieldAccessor}, writer);"); } else { return($"{NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.Serialize_Class({fullFieldAccessor}, writer);"); } }
public static string GetSerializerFieldLine_Deserialize(Type fieldType, string fieldAccessor, string fullFieldAccessor = null) { if (fullFieldAccessor == null) { fullFieldAccessor = "obj" + fieldAccessor; } if (fieldType.IsEnum) { return($"{fullFieldAccessor} = ({fieldType.GetPrettyFullName()}){NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.Deserialize(reader);"); } else if (NetSerializationCodeGenUtility.ConsideredAsValueType(fieldType)) { return($"{NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.Deserialize(ref {fullFieldAccessor}, reader);"); } else { return($"{fullFieldAccessor} = {NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.Deserialize_Class(reader);"); } }
public static string GetSerializerFieldLine_GetNetBitSize(Type fieldType, string fieldAccessor, string fullFieldAccessor = null) { if (fullFieldAccessor == null) { fullFieldAccessor = "obj" + fieldAccessor; } if (fieldType.IsEnum) { return($"result += {NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.GetSerializedBitSize();"); } if (NetSerializationCodeGenUtility.ConsideredAsValueType(fieldType)) { return($"result += {NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.GetSerializedBitSize(ref {fullFieldAccessor});"); } else { return($"result += {NetSerializationCodeGenUtility.GetSerializerNameFromType(fieldType)}.GetSerializedBitSize_Class({fullFieldAccessor});"); } }
public static void Generate(Type type, string completePath, bool clear, bool appendInFile) { NetSerializableAttribute serializableAttribute = type.GetCustomAttribute <NetSerializableAttribute>(); List <FieldInfo> fields = new List <FieldInfo>(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); fields.RemoveAll((f) => NetSerializationCodeGenUtility.ShouldIgnoreCodeGeneration(f, type)); string typeFullName = type.GetPrettyFullName(); Type baseClass = type.BaseType == typeof(object) || type.BaseType == typeof(ValueType) ? null : type.BaseType; bool couldBeDynamic = !NetSerializationCodeGenUtility.ConsideredAsValueType(type); bool isDynamicType = serializableAttribute != null ? serializableAttribute.IsBaseClass : false; string methodObjParameter_Rcv = couldBeDynamic ? (typeFullName + " obj") : ("ref " + typeFullName + " obj"); StreamWriter writer = GetFileStream(completePath); if (!appendInFile) { writer.Write( @"// THIS CODE IS GENERATED // DO NOT MODIFY IT using System; using System.Collections.Generic; "); } writer.WriteLine("public static class " + NetSerializationCodeGenUtility.GetSerializerNameFromType(type)); writer.WriteLine("{"); if (couldBeDynamic) { writer.WriteLine(" public static int GetSerializedBitSize_Class(" + methodObjParameter_Rcv + ")"); writer.WriteLine(" {"); if (clear) { writer.WriteLine(" return 0;"); } else { writer.WriteLine(" if (obj == null)"); writer.WriteLine(" return 1;"); if (isDynamicType) { writer.WriteLine(" return 1 + NetSerializer.GetSerializedBitSize(obj);"); } else { writer.WriteLine(" return 1 + GetSerializedBitSize(obj);"); } } writer.WriteLine(" }"); writer.WriteLine(); } writer.WriteLine(" public static int GetSerializedBitSize(" + methodObjParameter_Rcv + ")"); writer.WriteLine(" {"); if (clear) { writer.WriteLine(" return 0;"); } else { writer.WriteLine(" int result = 0;"); foreach (var field in fields) { if (field.FieldType.IsArray) { s_generateArrayCode.Add(field.FieldType); } else if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(List <>)) { s_generateListCode.Add(field.FieldType); } writer.WriteLine(" " + ModelHelpers.GetSerializerFieldLine_GetNetBitSize(field.FieldType, '.' + field.Name)); } if (baseClass != null) { writer.WriteLine(" " + ModelHelpers.GetSerializerFieldLine_GetNetBitSizeBaseClass(baseClass)); } writer.WriteLine(" return result;"); } writer.WriteLine(" }"); writer.WriteLine(); if (couldBeDynamic) { writer.WriteLine(" public static void Serialize_Class(" + methodObjParameter_Rcv + ", BitStreamWriter writer)"); writer.WriteLine(" {"); if (clear) { // nothing } else { writer.WriteLine(" if (obj == null)"); writer.WriteLine(" {"); writer.WriteLine(" writer.WriteBit(false);"); writer.WriteLine(" return;"); writer.WriteLine(" }"); writer.WriteLine(" writer.WriteBit(true);"); if (isDynamicType) { writer.WriteLine(" NetSerializer.Serialize(obj, writer);"); } else { writer.WriteLine(" Serialize(obj, writer);"); } } writer.WriteLine(" }"); } writer.WriteLine(" public static void Serialize(" + methodObjParameter_Rcv + ", BitStreamWriter writer)"); writer.WriteLine(" {"); if (!clear) { foreach (var field in fields) { writer.WriteLine(" " + ModelHelpers.GetSerializerFieldLine_Serialize(field.FieldType, '.' + field.Name)); } if (baseClass != null) { writer.WriteLine(" " + ModelHelpers.GetSerializerFieldLine_SerializeBaseClass(baseClass)); } } writer.WriteLine(" }"); writer.WriteLine(); if (couldBeDynamic) { writer.WriteLine(" public static " + typeFullName + " Deserialize_Class(BitStreamReader reader)"); writer.WriteLine(" {"); if (clear) { writer.WriteLine(" return null;"); } else { writer.WriteLine(" if (reader.ReadBit() == false)"); writer.WriteLine(" {"); writer.WriteLine(" return null;"); writer.WriteLine(" }"); if (isDynamicType) { writer.WriteLine(" return (" + typeFullName + ")NetSerializer.Deserialize(reader);"); } else { writer.WriteLine(" " + typeFullName + " obj = new " + typeFullName + "();"); writer.WriteLine(" Deserialize(obj, reader);"); writer.WriteLine(" return obj;"); } } writer.WriteLine(" }"); } writer.WriteLine(" public static void Deserialize(" + methodObjParameter_Rcv + ", BitStreamReader reader)"); writer.WriteLine(" {"); if (!clear) { foreach (var field in fields) { writer.WriteLine(" " + ModelHelpers.GetSerializerFieldLine_Deserialize(field.FieldType, '.' + field.Name)); } if (baseClass != null) { writer.WriteLine(" " + ModelHelpers.GetSerializerFieldLine_DeserializeBaseClass(baseClass)); } } writer.WriteLine(" }"); writer.WriteLine("}"); }
static void GenerateCode(Type[] netMessageTypes) { ulong crc = GetHashFromNetMessageTypes(netMessageTypes); if (!Directory.Exists(NetSerializationCodeGenSettings.REGISTRY_FILEPATH)) { Directory.CreateDirectory(NetSerializationCodeGenSettings.REGISTRY_FILEPATH); } if (!File.Exists(CompletePath)) { File.Create(CompletePath).Close(); } using (FileStream fileStream = File.Open(CompletePath, FileMode.Truncate)) { using (StreamWriter writer = new StreamWriter(fileStream)) { writer.Flush(); writer.WriteLine("// THIS CODE IS GENERATED"); writer.WriteLine("// DO NOT MODIFY IT"); writer.WriteLine(); writer.WriteLine("using System;"); writer.WriteLine("using System.Collections.Generic;"); writer.WriteLine(); writer.WriteLine("public static class " + NetSerializationCodeGenSettings.REGISTRY_CLASSNAME); writer.WriteLine("{"); writer.WriteLine(" public static readonly ulong crc = " + crc + ";"); writer.WriteLine(); writer.WriteLine(" public static readonly Type[] types = new Type[]"); writer.WriteLine(" {"); bool addComma = false; foreach (Type type in netMessageTypes) { if (addComma) { writer.WriteLine(" ,"); } writer.WriteLine(" typeof(" + type.GetPrettyFullName() + ")"); addComma = true; } writer.WriteLine(" };"); writer.WriteLine(); writer.WriteLine(" public static readonly Dictionary<Type, Func<object, int>> map_GetBitSize = new Dictionary<Type, Func<object, int>>()"); writer.WriteLine(" {"); addComma = false; for (int i = 0; i < netMessageTypes.Length; i++) { Type t = netMessageTypes[i]; if (addComma) { writer.WriteLine(" ,"); } writer.WriteLine(" [typeof(" + t.GetPrettyFullName() + ")] = (obj) =>"); writer.WriteLine(" {"); writer.WriteLine(" " + t.GetPrettyFullName() + " castedObj = (" + t.GetPrettyFullName() + ")obj;"); if (NetSerializationCodeGenUtility.ConsideredAsValueType(t)) { writer.WriteLine(" return " + NetSerializationCodeGenUtility.GetSerializerNameFromType(t) + ".GetSerializedBitSize(ref castedObj);"); } else { writer.WriteLine(" return " + NetSerializationCodeGenUtility.GetSerializerNameFromType(t) + ".GetSerializedBitSize(castedObj);"); } writer.WriteLine(" }"); addComma = true; } writer.WriteLine(" };"); writer.WriteLine(); writer.WriteLine(" public static readonly Dictionary<Type, Action<object, BitStreamWriter>> map_Serialize = new Dictionary<Type, Action<object, BitStreamWriter>>()"); writer.WriteLine(" {"); addComma = false; for (int i = 0; i < netMessageTypes.Length; i++) { Type t = netMessageTypes[i]; if (addComma) { writer.WriteLine(" ,"); } writer.WriteLine(" [typeof(" + t.GetPrettyFullName() + ")] = (obj, writer) =>"); writer.WriteLine(" {"); writer.WriteLine(" " + t.GetPrettyFullName() + " castedObj = (" + t.GetPrettyFullName() + ")obj;"); if (NetSerializationCodeGenUtility.ConsideredAsValueType(t)) { writer.WriteLine(" " + NetSerializationCodeGenUtility.GetSerializerNameFromType(t) + ".Serialize(ref castedObj, writer);"); } else { writer.WriteLine(" " + NetSerializationCodeGenUtility.GetSerializerNameFromType(t) + ".Serialize(castedObj, writer);"); } writer.WriteLine(" }"); addComma = true; } writer.WriteLine(" };"); writer.WriteLine(); writer.WriteLine(" public static readonly Dictionary<UInt16, Func<BitStreamReader, object>> map_Deserialize = new Dictionary<UInt16, Func<BitStreamReader, object>>()"); writer.WriteLine(" {"); addComma = false; for (int i = 0; i < netMessageTypes.Length; i++) { Type t = netMessageTypes[i]; if (addComma) { writer.WriteLine(" ,"); } writer.WriteLine(" [" + i + "] = (reader) =>"); writer.WriteLine(" {"); writer.WriteLine(" " + t.GetPrettyFullName() + " obj = new " + t.GetPrettyFullName() + "();"); if (NetSerializationCodeGenUtility.ConsideredAsValueType(t)) { writer.WriteLine(" " + NetSerializationCodeGenUtility.GetSerializerNameFromType(t) + ".Deserialize(ref obj, reader);"); } else { writer.WriteLine(" " + NetSerializationCodeGenUtility.GetSerializerNameFromType(t) + ".Deserialize(obj, reader);"); } writer.WriteLine(" return obj;"); writer.WriteLine(" }"); addComma = true; } writer.WriteLine(" };"); writer.WriteLine("}"); } } }