Fixed naming of variables in the code generator class

This commit is contained in:
Samuele Lorefice
2025-02-26 16:01:16 +01:00
parent 5aeaf064ce
commit 4166482022

View File

@@ -16,14 +16,14 @@ namespace CodeGenerator {
using CFieldRefExp = CodeFieldReferenceExpression; using CFieldRefExp = CodeFieldReferenceExpression;
public static class Program { public static class Program {
private static BlendFile blendfile; private static BlendFile _blendfile;
private static readonly StringBuilder sb = new(); private static readonly StringBuilder Sb = new();
private const string OutPath = @"GeneratedOutput"; private const string OutPath = @"GeneratedOutput";
private const string Namespace = "BlendFile"; private const string Namespace = "BlendFile";
private static HashSet<string> customTypes; private static HashSet<string> _customTypes;
private static void Log(string message) { private static void Log(string message) {
sb.AppendLine(message); Sb.AppendLine(message);
Console.WriteLine(message); Console.WriteLine(message);
} }
@@ -41,38 +41,38 @@ namespace CodeGenerator {
OutputCodeFiles(rootNs, false); OutputCodeFiles(rootNs, false);
Log("Finished generating C# code!"); Log("Finished generating C# code!");
File.AppendAllText("Log.txt", sb.ToString()); File.AppendAllText("Log.txt", Sb.ToString());
} }
private static void ReadBlendFile() { private static void ReadBlendFile() {
Log("Reading empty.blend file"); Log("Reading empty.blend file");
blendfile = BlendFile.FromFile("empty.blend"); _blendfile = BlendFile.FromFile("empty.blend");
Log($"Header: Blender v{blendfile.Hdr.Version} {blendfile.Hdr.Endian}\n" + Log($"Header: Blender v{_blendfile.Hdr.Version} {_blendfile.Hdr.Endian}\n" +
$"DataBlocks: {blendfile.Blocks.Count}\n" + $"DataBlocks: {_blendfile.Blocks.Count}\n" +
$"DNA1: {blendfile.SdnaStructs.Count} structures\n"); $"DNA1: {_blendfile.SdnaStructs.Count} structures\n");
} }
private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) { private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) {
CodeNamespace rootNs = new CodeNamespace(Namespace); CodeNamespace rootNs = new CodeNamespace(Namespace);
rootNs.Types.Add(GenerateDNAFieldAttributeType()); rootNs.Types.Add(GenerateDnaFieldAttributeType());
rootNs.Types.Add(GenerateDNAClassAttributeType()); rootNs.Types.Add(GenerateDnaClassAttributeType());
CodeNamespace ns = new CodeNamespace(Namespace+".DNA"); CodeNamespace ns = new CodeNamespace(Namespace+".DNA");
ns.Imports.Add(new(rootNs.Name)); ns.Imports.Add(new(rootNs.Name));
customTypes = new(); _customTypes = new();
foreach (var type in blendfile.SdnaStructs) { foreach (var type in _blendfile.SdnaStructs) {
Log($"Generating struct {type.Type}"); Log($"Generating struct {type.Type}");
bool referenceSelf = false; bool referenceSelf = false;
bool referencePointer = false; bool referencePointer = false;
//Add the type to the custom types list //Add the type to the custom types list
customTypes.Add(type.Type); _customTypes.Add(type.Type);
//Create a new type declaration //Create a new type declaration
var ctd = new CodeTypeDeclaration(type.Type); var ctd = new CodeTypeDeclaration(type.Type);
ctd.CustomAttributes.Add(new CodeAttributeDeclaration("DNAClassAttribute", ctd.CustomAttributes.Add(new ("DNAClassAttribute",
new CodeAttributeArgument(new CodePrimitiveExpression(type.IdxType)), new CodeAttributeArgument(new CodePrimitiveExpression(type.IdxType)),
new CodeAttributeArgument(new CodePrimitiveExpression(type.Type)) new CodeAttributeArgument(new CodePrimitiveExpression(type.Type))
)); ));
@@ -115,8 +115,7 @@ namespace CodeGenerator {
cmf = CreateMemberField(field); cmf = CreateMemberField(field);
} }
var size = 0; var attributes = GenerateDnaFieldAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size);
var attributes = GenerateDNAFieldAttribute(index, field, field.M_Parent.M_Parent, totalSize, out size);
totalSize += size; totalSize += size;
cmf.CustomAttributes.Add(attributes); cmf.CustomAttributes.Add(attributes);
ctd.Members.Add(cmf); ctd.Members.Add(cmf);
@@ -137,7 +136,7 @@ namespace CodeGenerator {
return ns; return ns;
} }
private static CodeTypeDeclaration GenerateDNAFieldAttributeType() { private static CodeTypeDeclaration GenerateDnaFieldAttributeType() {
var attributeBuilder = AttributeBuilder.Instance; var attributeBuilder = AttributeBuilder.Instance;
return attributeBuilder.New("DNAFieldAttribute") return attributeBuilder.New("DNAFieldAttribute")
@@ -153,7 +152,7 @@ namespace CodeGenerator {
.Build(); .Build();
} }
private static CodeTypeDeclaration GenerateDNAClassAttributeType() { private static CodeTypeDeclaration GenerateDnaClassAttributeType() {
var attributeBuilder = AttributeBuilder.Instance; var attributeBuilder = AttributeBuilder.Instance;
return attributeBuilder.New("DNAClassAttribute") return attributeBuilder.New("DNAClassAttribute")
@@ -165,7 +164,7 @@ namespace CodeGenerator {
.Build(); .Build();
} }
private static CodeAttributeDeclaration GenerateDNAFieldAttribute(int index, BlendFile.DnaField field, private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, BlendFile.DnaField field,
BlendFile.Dna1Body body, int offset, out int size) { BlendFile.Dna1Body body, int offset, out int size) {
string t; string t;
size = body.Lengths[field.IdxType]; size = body.Lengths[field.IdxType];
@@ -219,7 +218,7 @@ namespace CodeGenerator {
if (t != null) cmf = new(t, field.Name.ParseFName()); //Built-in type if (t != null) cmf = new(t, field.Name.ParseFName()); //Built-in type
else { else {
cmf = new(new CodeTypeReference(field.Type), field.Name.ParseFName()); //Custom type cmf = new(new CodeTypeReference(field.Type), field.Name.ParseFName()); //Custom type
customTypes.Add(field.Type); _customTypes.Add(field.Type);
} }
cmf.Attributes = MemberAttributes.Public; cmf.Attributes = MemberAttributes.Public;
return cmf; return cmf;
@@ -240,7 +239,7 @@ namespace CodeGenerator {
if (t != null) cmf = new(t, name); //Built-in type if (t != null) cmf = new(t, name); //Built-in type
else { else {
cmf = new(field.Type, name); //Custom type cmf = new(field.Type, name); //Custom type
customTypes.Add(field.Type); _customTypes.Add(field.Type);
} }
//Set the field attributes //Set the field attributes
@@ -348,7 +347,7 @@ namespace CodeGenerator {
return cc; return cc;
} }
private static void SetupCCU(out CodeGeneratorOptions genOpts, out CSharpCodeProvider provider, private static void SetupCcu(out CodeGeneratorOptions genOpts, out CSharpCodeProvider provider,
out CodeCompileUnit ccu) { out CodeCompileUnit ccu) {
genOpts = new() { genOpts = new() {
BlankLinesBetweenMembers = false, BlankLinesBetweenMembers = false,
@@ -373,7 +372,7 @@ namespace CodeGenerator {
private static void OutputCodeFiles(CodeNamespace ns, bool outputExtraTypes = true) { private static void OutputCodeFiles(CodeNamespace ns, bool outputExtraTypes = true) {
string rootPath = Path.GetDirectoryName(GetOutputPath(ns, "")); string rootPath = Path.GetDirectoryName(GetOutputPath(ns, ""));
if (!Path.Exists(rootPath)) Directory.CreateDirectory(rootPath!); if (!Path.Exists(rootPath)) Directory.CreateDirectory(rootPath!);
SetupCCU(out var codeGeneratorOptions, out var provider, out var ccu); SetupCcu(out var codeGeneratorOptions, out var provider, out var ccu);
CodeNamespace tempNs = new CodeNamespace(ns.Name); CodeNamespace tempNs = new CodeNamespace(ns.Name);
tempNs.Imports.AddRange(ns.Imports.Cast<CodeNamespaceImport>().ToArray()); tempNs.Imports.AddRange(ns.Imports.Cast<CodeNamespaceImport>().ToArray());
ccu.Namespaces.Add(tempNs); ccu.Namespaces.Add(tempNs);
@@ -386,8 +385,8 @@ namespace CodeGenerator {
} }
if (!outputExtraTypes) return; if (!outputExtraTypes) return;
customTypes.ExceptWith(ns.Types.OfType<CodeTypeDeclaration>().Select(t => t.Name)); _customTypes.ExceptWith(ns.Types.OfType<CodeTypeDeclaration>().Select(t => t.Name));
foreach (var type in customTypes) { foreach (var type in _customTypes) {
Log($"Creating empty struct for missing {type}"); Log($"Creating empty struct for missing {type}");
var ctd = new CodeTypeDeclaration(type) { var ctd = new CodeTypeDeclaration(type) {
IsStruct = true, IsStruct = true,