static void GenerateConfig(ConfigClassInfo classInfo) { var path = StringUtil.Contact("Assets/Script/Game/Config/", classInfo.className, ".cs"); string fullPath = Path.GetFullPath(path); StreamReader sr = new StreamReader(FileTemplatePath); var text = sr.ReadToEnd(); sr.Close(); text = text.Replace("#ClassName#", classInfo.className); text = text.Replace("#FileName#", classInfo.fileName); text = text.Replace("#Fileds#", classInfo.fieldLabel); text = text.Replace("#Parses#", classInfo.parseLabel); text = text.Replace("#KeyType#", classInfo.keyType); text = text.Replace("#KeyField#", classInfo.keyField); bool encoderShouldEmitUTF8Identifier = true; bool throwOnInvalidBytes = false; UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes); bool append = false; StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding); streamWriter.Write(text); streamWriter.Close(); AssetDatabase.ImportAsset(path); ProjectWindowUtil.ShowCreatedAsset(AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object))); }
// Some example code: https://docs.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization ConfigMessageSerializerClass CreateClassInfo() { ConfigMessageSerializerClass messageSerializerClassInfo = new ConfigMessageSerializerClass(); ConfigClassInfo classInfo = new ConfigClassInfo(); classInfo.AssemblyName = Assembly.GetExecutingAssembly().FullName; classInfo.ClassFullName = typeof(SimpleXmlTestClass).FullName; ConfigPropertyInfo propertyInfo1 = new ConfigPropertyInfo(); propertyInfo1.Name = "Property1"; propertyInfo1.Attributes.Add(new MessagePropertyAttribute() { PrepadCharacter = 'T' }); propertyInfo1.Attributes.Add(new CalculatedLengthResultAttribute()); //propertyInfo1._messagePropertyAttribute.MessageLengthType = MessageLengthTypes.RestOfMessage; //propertyInfo1._messagePropertyAttribute.PrepadCharacter = 'T'; //propertyInfo1.AuthenticationClass = typeof(int); classInfo.Properties.Add(propertyInfo1); ConfigPropertyInfo propertyInfo2 = new ConfigPropertyInfo(); propertyInfo2.Name = "Property2"; classInfo.Properties.Add(propertyInfo2); ConfigPropertyInfo propertyInfo3 = new ConfigPropertyInfo(); propertyInfo3.Name = "Property3"; var property3MessagePropertyAttribute = new MessagePropertyAttribute(); property3MessagePropertyAttribute.IsBcd = true; //property3MessagePropertyAttribute.IsAuthenticationField = false; //property3MessagePropertyAttribute.ExcludeFromAuthentication = true; property3MessagePropertyAttribute.Length = 20; property3MessagePropertyAttribute.Format = "YYYYMMDDHHMMSS"; property3MessagePropertyAttribute.BlobType = BlobTypes.None; property3MessagePropertyAttribute.VariableLength = true; property3MessagePropertyAttribute.MinLength = 1; property3MessagePropertyAttribute.MaxLength = 18; property3MessagePropertyAttribute.MinimizeVariableLength = true; //property3MessagePropertyAttribute.ExcludeFromLength = false; property3MessagePropertyAttribute.PrepadCharacter = ' '; //property3MessagePropertyAttribute.MessageLengthType = MessageLengthTypes.RestOfMessage; //property3MessagePropertyAttribute.AuthenticationClass = typeof(AuthenticationCrc32); propertyInfo3.Attributes.Add(property3MessagePropertyAttribute); propertyInfo3.Attributes.Add(new CalculatedLengthResultAttribute() { Exclude = false }); propertyInfo3.Attributes.Add(new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationCrc32))); classInfo.Properties.Add(propertyInfo3); messageSerializerClassInfo.ClassInfo = classInfo; return(messageSerializerClassInfo); }