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