public override string ToString( ) { var stringBuilder = new StringBuilder ( ); foreach (var attribute in this.Attributes) { stringBuilder.AppendLine(attribute.ToString()); } stringBuilder.AppendLine(string.Format("{0} enum {1} : {2}", AccessModifiersExtensions.ToString(AccessModifiers), Value.Name, BaseType.ToString( ).ToLowerInvariant( )).Trim( )); stringBuilder.AppendLine( ); stringBuilder.AppendLine("{"); var isFlags = Attributes.Any(x => x.AttributeType == typeof(FlagsAttribute)); var value = isFlags ? 1 : 0; foreach (var item in ValueNames) { if (item.HasDescription) { stringBuilder.AppendSummary(item.Description); } stringBuilder.AppendLine(string.Format("{0} = {1},", GuerillaCs.ToTypeName(item.Name), value)); value = isFlags ? value << 1 : ++value; } stringBuilder.AppendLine("};"); return(stringBuilder.ToString( ).Trim( )); }
public void Format( ) { TokenDictionary tokenDictionary = new TokenDictionary( ); string name, @namespace; if (GuerillaCs.SplitNamespaceFromFieldName(Value.Name, out name, out @namespace)) { this.Value.Name = tokenDictionary.GenerateValidToken(GuerillaCs.ToTypeName(name)); this.Namespace = GuerillaCs.ToTypeName(@namespace); } else { this.Value.Name = tokenDictionary.GenerateValidToken(GuerillaCs.ToTypeName(this.Value.Name)); } FormatFieldNames(tokenDictionary); foreach (var item in EnumDefinitions) { item.Format( ); } foreach (var item in ClassDefinitions) { item.Format( ); } }
public void Format( ) { var tokenDictionary = new ClassInfo.TokenDictionary( ); for (int i = 0; i < ValueNames.Count; i++) { ValueNames[i] = tokenDictionary.GenerateValidToken(GuerillaCs.ToTypeName(ValueNames[i])); } }
ClassInfo ProcessArrayFields(List <tag_field> fields) { ClassInfo arrayClass = new ClassInfo() { Value = GuerillaCs.ToTypeName(fields[0].Name), AccessModifiers = Moonfish.Guerilla.AccessModifiers.Public, }; fields.RemoveAt(0); ProcessFields(fields, arrayClass); return(arrayClass); }
public ClassInfo BeginProcessTagBlockDefinition(TagBlockDefinition block, int address, string group_tag = "", string className = "") { var size = CalculateSizeOfFieldSet(block.LatestFieldSet.Fields); ClassInfo @class = new ClassInfo() { AccessModifiers = AccessModifiers.Public, Value = className == string.Empty ? GuerillaCs.ToTypeName(block.Name) : GuerillaCs.ToTypeName(className), Attributes = { new AttributeInfo(typeof(LayoutAttribute), "Size", size) } }; ProcessFields(block.LatestFieldSet.Fields, @class); @class.Format(); return(@class); }
public void DumpTagLayout(string folder, string tagClassName, string outputClassName) { var readTag = h2Tags.Where(x => x.Class.ToString() == tagClassName).First(); var info = (ClassInfo)BeginProcessTagBlockDefinition(readTag.Definition, readTag.definition_address, readTag.Class.ToString(), ""); using (var stream = new FileStream(Path.Combine(folder, info.Value.Name + ".cs"), FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { var parentTag = h2Tags.Where(x => x.Class == readTag.ParentClass); if (parentTag.Any()) { info.BaseClass = new ClassInfo.TokenDictionary().GenerateValidToken( GuerillaCs.ToTypeName(parentTag.Single().Definition.Name)); } info.Attributes.Add(new AttributeInfo(typeof(TagClassAttribute)) { Parameters = { "\"" + readTag.Class.ToString() + "\"" } }); var streamWriter = new StreamWriter(stream); info.Generate(); GenerateOutputForClass(info, streamWriter); } var localDefinitions = DefinitionsDictionary.Select(x => x.Value); foreach (var item in localDefinitions) { using (var stream = new FileStream(Path.Combine(folder, item.Value.Name + ".cs"), FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { item.Generate(); GenerateOutputForClass(item, new StreamWriter(stream)); } } }
void FormatFieldNames(TokenDictionary tokenDictionary) { using (var code = new Microsoft.CSharp.CSharpCodeProvider( )) { foreach (var item in Fields) { var token = tokenDictionary.GenerateValidToken(GuerillaCs.ToMemberName(item.Value.Name)); item.Value.Name = token; } foreach (var item in Methods) { var token = tokenDictionary.GenerateValidToken(GuerillaCs.ToMemberName(item.ClassName)); item.ClassName = token; } foreach (var item in EnumDefinitions) { var token = tokenDictionary.GenerateValidToken(GuerillaCs.ToTypeName(item.Value.Name)); item.Value.Name = token; } } }
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; } } } }