private void ProcessFields(List <MoonfishTagField> fields, ClassInfo @class) { foreach (var field in fields) { FieldInfo fieldInfo; switch (field.Type) { case MoonfishFieldType.FieldTagReference: { fieldInfo = new FieldInfo { Attributes = { new AttributeInfo(typeof(TagReference), null, "\"" + field.Definition.Class + "\"") }, AccessModifiers = AccessModifiers.Internal, Value = field.Strings, FieldTypeName = _valueTypeDictionary[field.Type].FullName }; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldBlock: { fieldInfo = new FieldInfo { Value = IsValidFieldName(field.Strings.Name.ToUpper()) ? field.Strings : field.Definition.DisplayName, AccessModifiers = AccessModifiers.Internal }; if (!_definitionsDictionary.ContainsKey(field.Definition.Name)) { _definitionsDictionary[field.Definition.Name] = BeginProcessTagBlockDefinition(field.Definition); } fieldInfo.FieldTypeName = _definitionsDictionary[field.Definition.Name].Name; fieldInfo.IsArray = true; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldStruct: { fieldInfo = new FieldInfo { Value = IsValidFieldName(field.Strings.Name.ToUpper()) ? field.Strings : field.Definition.Definition.DisplayName, AccessModifiers = AccessModifiers.Internal }; if (!_definitionsDictionary.ContainsKey(field.Definition.Name)) { _definitionsDictionary[field.Definition.Name] = BeginProcessTagBlockDefinition(field.Definition.Definition); } fieldInfo.FieldTypeName = _definitionsDictionary[field.Definition.Name].Name; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldData: { fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = ((MoonfishTagDataDefinition)field.Definition).DataElementSize == 1 ? typeof(Byte).FullName : typeof(short).FullName, IsArray = true }; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldExplanation: { //// Check if there is sub-text for this explaination. //string subtext = ""; //if (field.definition != 0) // subtext = ReadString(reader, field.definition); //// Write the field info to the output file. //writer.WriteLine("//FIELD_EXPLAINATION(\"{0}\", \"{1}\"),", field.Name, subtext.Replace("\n", "<lb>")); break; } case MoonfishFieldType.FieldByteFlags: case MoonfishFieldType.FieldLongFlags: case MoonfishFieldType.FieldWordFlags: case MoonfishFieldType.FieldCharEnum: case MoonfishFieldType.FieldEnum: case MoonfishFieldType.FieldLongEnum: { var enumInfo = new EnumInfo { Value = ToTypeName(field.Strings), AccessModifiers = AccessModifiers.Internal }; var enumDefinition = (MoonfishTagEnumDefinition)field.Definition; enumInfo.ValueNames.AddRange(enumDefinition.Names.Select(x => (GuerillaName)x)); switch (field.Type) { case MoonfishFieldType.FieldByteFlags: enumInfo.BaseType = EnumInfo.Type.Byte; enumInfo.Attributes.Add(new AttributeInfo(typeof(FlagsAttribute))); break; case MoonfishFieldType.FieldWordFlags: enumInfo.BaseType = EnumInfo.Type.Short; enumInfo.Attributes.Add(new AttributeInfo(typeof(FlagsAttribute))); break; case MoonfishFieldType.FieldLongFlags: enumInfo.BaseType = EnumInfo.Type.Int; enumInfo.Attributes.Add(new AttributeInfo(typeof(FlagsAttribute))); break; case MoonfishFieldType.FieldCharEnum: enumInfo.BaseType = EnumInfo.Type.Byte; break; case MoonfishFieldType.FieldEnum: enumInfo.BaseType = EnumInfo.Type.Short; break; case MoonfishFieldType.FieldLongEnum: enumInfo.BaseType = EnumInfo.Type.Int; break; } @class.EnumDefinitions.Add(enumInfo); fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = ToTypeName(enumInfo.Value) }; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldByteBlockFlags: case MoonfishFieldType.FieldWordBlockFlags: case MoonfishFieldType.FieldLongBlockFlags: case MoonfishFieldType.FieldCharBlockIndex1: case MoonfishFieldType.FieldShortBlockIndex1: case MoonfishFieldType.FieldLongBlockIndex1: case MoonfishFieldType.FieldCharBlockIndex2: case MoonfishFieldType.FieldShortBlockIndex2: case MoonfishFieldType.FieldLongBlockIndex2: { fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = _valueTypeDictionary[field.Type].FullName }; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldArrayStart: { var startIndex = fields.IndexOf(field); int endIndex = startIndex; int depth = 0; for (int i = startIndex + 1; i < fields.Count; i++) { if (fields[i].Type == MoonfishFieldType.FieldArrayStart) { depth++; } if (fields[i].Type == MoonfishFieldType.FieldArrayEnd) { if (depth == 0) { endIndex = i + 1; break; } depth--; } } var count = endIndex - startIndex; if (count < 1) { throw new Exception(); } var arrayFieldSubSet = fields.GetRange(startIndex, count); @class.ClassDefinitions.Add(ProcessArrayFields(arrayFieldSubSet)); fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = @class.ClassDefinitions.Last().Name, ArraySize = field.Count, IsArray = true }; @class.Fields.Add(fieldInfo); var remainingFields = fields.GetRange(endIndex, fields.Count - endIndex); ProcessFields(remainingFields, @class); return; } case MoonfishFieldType.FieldArrayEnd: { return; } case MoonfishFieldType.FieldPad: { fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = typeof(Byte).FullName, ArraySize = field.Count, IsArray = true }; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldSkip: { fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = typeof(Byte).FullName, ArraySize = field.Count, IsArray = true }; @class.Fields.Add(fieldInfo); break; } case MoonfishFieldType.FieldUselessPad: case MoonfishFieldType.FieldTerminator: case MoonfishFieldType.FieldCustom: { break; } default: { fieldInfo = new FieldInfo { Value = field.Strings, AccessModifiers = AccessModifiers.Internal, FieldTypeName = _valueTypeDictionary[field.Type].AssemblyQualifiedName }; @class.Fields.Add(fieldInfo); break; } } } }
private void GenerateOutputForSubclass(ClassInfo item, StreamWriter streamWriter, int tabCount) { GenerateOutputForClass(item, streamWriter, true, tabCount); }
void ProcessFields(List <tag_field> fields, ClassInfo @class) { foreach (var field in fields) { var fieldInfo = new FieldInfo(); switch (field.type) { case field_type._field_tag_reference: { fieldInfo = new FieldInfo() { Attributes = { new AttributeInfo(typeof(TagReference), null, "\"" + field.Definition.Class.ToString() + "\"") }, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, Value = field.Name, FieldTypeName = valueTypeDictionary[field.type].FullName, }; @class.Fields.Add(fieldInfo); break; } case field_type._field_block: { fieldInfo = new FieldInfo() { Value = IsValidFieldName(field.Name.ToUpper()) ? field.Name : field.Definition.DisplayName, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, }; if (!DefinitionsDictionary.ContainsKey(field.Definition.Name)) { DefinitionsDictionary[field.Definition.Name] = BeginProcessTagBlockDefinition(field.Definition, field.definition); } fieldInfo.FieldTypeName = DefinitionsDictionary[field.Definition.Name].Value.Name; fieldInfo.IsArray = true; @class.Fields.Add(fieldInfo); break; } case field_type._field_struct: { fieldInfo = new FieldInfo() { Value = IsValidFieldName(field.Name.ToUpper()) ? field.Name : field.Definition.DisplayName, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, }; if (!DefinitionsDictionary.ContainsKey(field.Definition.name)) { DefinitionsDictionary[field.Definition.name] = BeginProcessTagBlockDefinition(field.Definition.Definition, field.Definition.block_definition_address); } fieldInfo.FieldTypeName = DefinitionsDictionary[field.Definition.Name].Value.Name; @class.Fields.Add(fieldInfo); break; } case field_type._field_data: { fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = typeof(Byte).FullName, IsArray = true }; @class.Fields.Add(fieldInfo); break; } case field_type._field_explanation: { //// Check if there is sub-text for this explaination. //string subtext = ""; //if (field.definition != 0) // subtext = ReadString(reader, field.definition); //// Write the field info to the output file. //writer.WriteLine("//FIELD_EXPLAINATION(\"{0}\", \"{1}\"),", field.Name, subtext.Replace("\n", "<lb>")); break; } case field_type._field_byte_flags: case field_type._field_long_flags: case field_type._field_word_flags: case field_type._field_char_enum: case field_type._field_enum: case field_type._field_long_enum: { var enumInfo = new EnumInfo() { Value = GuerillaCs.ToTypeName(field.Name), AccessModifiers = AccessModifiers.Internal }; var enumDefinition = (enum_definition)field.Definition; enumInfo.ValueNames.AddRange(enumDefinition.Options.Select(x => (GuerillaName)x)); switch (field.type) { case field_type._field_byte_flags: enumInfo.BaseType = EnumInfo.Type.Byte; enumInfo.Attributes.Add(new AttributeInfo(typeof(FlagsAttribute))); break; case field_type._field_word_flags: enumInfo.BaseType = EnumInfo.Type.Short; enumInfo.Attributes.Add(new AttributeInfo(typeof(FlagsAttribute))); break; case field_type._field_long_flags: enumInfo.BaseType = EnumInfo.Type.Int; enumInfo.Attributes.Add(new AttributeInfo(typeof(FlagsAttribute))); break; case field_type._field_char_enum: enumInfo.BaseType = EnumInfo.Type.Byte; break; case field_type._field_enum: enumInfo.BaseType = EnumInfo.Type.Short; break; case field_type._field_long_enum: enumInfo.BaseType = EnumInfo.Type.Int; break; } @class.EnumDefinitions.Add(enumInfo); fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = GuerillaCs.ToTypeName(enumInfo.Value) }; enumInfo.ToString(); @class.Fields.Add(fieldInfo); break; } case field_type._field_byte_block_flags: case field_type._field_word_block_flags: case field_type._field_long_block_flags: case field_type._field_char_block_index1: case field_type._field_short_block_index1: case field_type._field_long_block_index1: case field_type._field_char_block_index2: case field_type._field_short_block_index2: case field_type._field_long_block_index2: { fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = valueTypeDictionary[field.type].FullName, }; @class.Fields.Add(fieldInfo); break; } case field_type._field_array_start: { var startIndex = fields.IndexOf(field); var endIndex = fields.FindIndex( startIndex, x => x.type == field_type._field_array_end) + 1; var arrayFieldSubSet = fields.GetRange(startIndex, endIndex - startIndex); @class.ClassDefinitions.Add(ProcessArrayFields(arrayFieldSubSet)); fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = @class.ClassDefinitions.Last().Value, ArraySize = field.Definition, IsArray = true }; @class.Fields.Add(fieldInfo); var remainingFields = fields.GetRange(endIndex, fields.Count - endIndex); ProcessFields(remainingFields, @class); return; } case field_type._field_array_end: { return; } case field_type._field_pad: { fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = typeof(Byte).FullName, ArraySize = field.definition, IsArray = true, }; @class.Fields.Add(fieldInfo); break; } case field_type._field_skip: { fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = typeof(Byte).FullName, ArraySize = field.definition, IsArray = true, }; @class.Fields.Add(fieldInfo); break; } case field_type._field_useless_pad: case field_type._field_terminator: case field_type._field_custom: { break; } default: { fieldInfo = new FieldInfo() { Value = field.Name, AccessModifiers = Moonfish.Guerilla.AccessModifiers.Internal, FieldTypeName = valueTypeDictionary[field.type].AssemblyQualifiedName }; @class.Fields.Add(fieldInfo); break; } } } }
private void GenerateOutputForClass(ClassInfo classInfo, StreamWriter streamWriter, bool subClass = false, int tabCount = 0) { if (!subClass) { var wrapperClassInfo = classInfo.GenerateWrapper(classInfo.Name, classInfo.Name + "Base"); classInfo.Name += "Base"; classInfo.Generate(); foreach (var item in classInfo.Usings) { streamWriter.WriteLine(item); } if (wrapperClassInfo.Attributes.Any(x => x.AttributeType == typeof(TagClassAttribute))) { streamWriter.WriteLine(); streamWriter.WriteLine("namespace Moonfish.Tags".Tab(ref tabCount)); streamWriter.WriteLine("{".Tab(ref tabCount)); var @class = wrapperClassInfo.Attributes.First(x => x.AttributeType == typeof(TagClassAttribute)) .Parameters[0 ].Trim('"'); streamWriter.WriteLine( string.Format("public partial struct {0}", typeof(TagClass).Name).Tab(ref tabCount)); streamWriter.WriteLine("{".Tab(ref tabCount)); var tagClass = (TagClass)@class; var titleCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(tagClass.ToTokenString()); streamWriter.WriteLine( string.Format(@"public static readonly {0} {1} = ({0})""{2}"";", typeof(TagClass).Name, titleCase, @class).Tab(ref tabCount)); streamWriter.WriteLine("};".Tab(ref tabCount)); streamWriter.WriteLine("};".Tab(ref tabCount)); } streamWriter.WriteLine(); streamWriter.WriteLine(classInfo.Namespace.ToString().Tab(ref tabCount)); streamWriter.WriteLine("{".Tab(ref tabCount)); GenerateOutputForSubclass(wrapperClassInfo, streamWriter, tabCount); } classInfo.Attributes.ForEach(x => streamWriter.WriteLine(x.ToString().Tab(ref tabCount))); streamWriter.WriteLine(classInfo.ClassDeclaration.Tab(ref tabCount)); streamWriter.WriteLine("{".Tab(ref tabCount)); if (!subClass) { classInfo.Name = classInfo.Name.Remove(classInfo.Name.LastIndexOf("Base", StringComparison.Ordinal), 4); } foreach (var item in classInfo.Fields) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.Properties) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.Constructors) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.Methods) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.EnumDefinitions) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.ClassDefinitions) { item.Generate(); GenerateOutputForSubclass(item, streamWriter, tabCount); } streamWriter.WriteLine("};".Tab(ref tabCount)); if (!subClass) { streamWriter.WriteLine("}".Tab(ref tabCount)); } streamWriter.Flush(); }
private void GenerateOutputForClass(ClassInfo classInfo, StreamWriter streamWriter, bool subClass = false, int tabCount = 0) { if (!subClass) { var wrapperClassInfo = classInfo.GenerateWrapper(classInfo.Value.Name, classInfo.Value.Name + "Base"); classInfo.Value.Name += "Base"; classInfo.Generate(); foreach (var item in classInfo.Usings) { streamWriter.WriteLine(item); } streamWriter.WriteLine(); streamWriter.WriteLine(classInfo.NamespaceDeclaration.Tab(ref tabCount)); streamWriter.WriteLine("{".Tab(ref tabCount)); GenerateOutputForSubclass(wrapperClassInfo, streamWriter, tabCount); } classInfo.Attributes.ForEach(x => streamWriter.WriteLine(x.ToString().Tab(ref tabCount))); streamWriter.WriteLine(classInfo.ClassDeclaration.Tab(ref tabCount)); streamWriter.WriteLine("{".Tab(ref tabCount)); if (!subClass) { classInfo.Value.Name = classInfo.Value.Name.Remove(classInfo.Value.Name.LastIndexOf("Base"), 4); } foreach (var item in classInfo.Fields) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.Constructors) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.Methods) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.EnumDefinitions) { tabCount = ProcessLines(streamWriter, tabCount, item); } foreach (var item in classInfo.ClassDefinitions) { item.Generate(); GenerateOutputForSubclass(item, streamWriter, tabCount); } streamWriter.WriteLine("};".Tab(ref tabCount)); if (!subClass) { streamWriter.WriteLine("}".Tab(ref tabCount)); } streamWriter.Flush(); }