2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-06 16:57:25 +00:00

Import code from previous AssetBuilder version

This commit is contained in:
Jan
2019-09-24 10:45:09 +02:00
parent 5609557516
commit 0d8432d4f7
919 changed files with 154412 additions and 26 deletions

View File

@@ -0,0 +1,9 @@
namespace ZoneCodeGenerator.Domain
{
class DataException : LoadingException
{
public DataException(string message) : base(message)
{
}
}
}

View File

@@ -0,0 +1,20 @@
namespace ZoneCodeGenerator.Domain
{
abstract class DataType
{
public string Namespace { get; }
public string Name { get; }
public DataTypeType Type { get; }
public string FullName => string.IsNullOrEmpty(Namespace) ? Name : $"{Namespace}::{Name}";
public abstract int Alignment { get; }
public abstract int Size { get; }
protected DataType(string _namespace, string name, DataTypeType type)
{
Namespace = _namespace;
Name = name;
Type = type;
}
}
}

View File

@@ -0,0 +1,47 @@
namespace ZoneCodeGenerator.Domain
{
class DataTypeBaseType : DataType
{
public static readonly DataTypeBaseType FLOAT = new DataTypeBaseType("float", 4);
public static readonly DataTypeBaseType DOUBLE = new DataTypeBaseType("double", 8);
public static readonly DataTypeBaseType BOOL = new DataTypeBaseType("bool", 1);
public static readonly DataTypeBaseType CHAR = new DataTypeBaseType("char", 1);
public static readonly DataTypeBaseType CONST_CHAR = new DataTypeBaseType("const char", 1);
public static readonly DataTypeBaseType UNSIGNED_CHAR = new DataTypeBaseType("unsigned char", 1);
public static readonly DataTypeBaseType SHORT = new DataTypeBaseType("short", 2);
public static readonly DataTypeBaseType UNSIGNED_SHORT = new DataTypeBaseType("unsigned short", 2);
public static readonly DataTypeBaseType INT = new DataTypeBaseType("int", 4);
public static readonly DataTypeBaseType UNSIGNED_INT = new DataTypeBaseType("unsigned int", 4);
public static readonly DataTypeBaseType LONG = new DataTypeBaseType("long", 4);
public static readonly DataTypeBaseType UNSIGNED_LONG = new DataTypeBaseType("unsigned long", 4);
public static readonly DataTypeBaseType LONG_LONG = new DataTypeBaseType("long long", 8);
public static readonly DataTypeBaseType UNSIGNED_LONG_LONG = new DataTypeBaseType("unsigned long long", 8);
public static readonly DataTypeBaseType VOID = new DataTypeBaseType("void", 0);
public static readonly DataTypeBaseType[] BASE_TYPES = {
FLOAT,
DOUBLE,
BOOL,
CHAR,
CONST_CHAR,
UNSIGNED_CHAR,
SHORT,
UNSIGNED_SHORT,
INT,
UNSIGNED_INT,
LONG,
UNSIGNED_LONG,
LONG_LONG,
UNSIGNED_LONG_LONG,
VOID
};
public override int Size { get; }
public override int Alignment => Size;
private DataTypeBaseType(string name, int size) : base("", name, DataTypeType.BaseType)
{
Size = size;
}
}
}

View File

@@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace ZoneCodeGenerator.Domain
{
class DataTypeEnum : DataType
{
public DataTypeBaseType ParentType { get; }
public List<EnumMember> Members { get; }
public override int Size => ParentType.Size;
public override int Alignment => ParentType.Alignment;
public DataTypeEnum(string _namespace, string name, DataTypeBaseType parentType) : base(_namespace, name, DataTypeType.Enum)
{
Members = new List<EnumMember>();
ParentType = parentType;
}
}
}

View File

@@ -0,0 +1,46 @@
using System;
using ZoneCodeGenerator.Utils;
namespace ZoneCodeGenerator.Domain
{
class DataTypeStruct : DataTypeWithMembers
{
public DataTypeStruct(string _namespace, string name, int pack) : base(_namespace, name, pack, DataTypeType.Struct)
{
}
protected override int CalculateSize()
{
var currentSize = 0;
var currentBitOffset = 0;
foreach (var member in Members)
{
if (member.VariableType.HasCustomBitSize)
{
currentBitOffset += member.VariableType.CustomBitSize.GetValueOrDefault(0);
}
else
{
if (currentBitOffset > 0)
{
currentBitOffset = currentBitOffset.Align(8);
currentSize += currentBitOffset / 8;
currentBitOffset = 0;
}
currentSize = currentSize.Align(Math.Min(member.Alignment, Pack));
currentSize += member.VariableType.Size;
}
}
if (currentBitOffset > 0)
{
currentBitOffset = currentBitOffset.Align(8);
currentSize += currentBitOffset / 8;
}
return currentSize.Align(Alignment);
}
}
}

View File

@@ -0,0 +1,11 @@
namespace ZoneCodeGenerator.Domain
{
enum DataTypeType
{
Struct,
Union,
Enum,
Typedef,
BaseType
}
}

View File

@@ -0,0 +1,15 @@
namespace ZoneCodeGenerator.Domain
{
class DataTypeTypedef : DataType
{
public TypeDeclaration TypeDefinition { get; }
public DataTypeTypedef(string _namespace, string name, TypeDeclaration typeDefinitionDeclaration) : base(_namespace, name, DataTypeType.Typedef)
{
TypeDefinition = typeDefinitionDeclaration;
}
public override int Alignment => TypeDefinition.Alignment;
public override int Size => TypeDefinition.Size;
}
}

View File

@@ -0,0 +1,20 @@
using System.Linq;
using ZoneCodeGenerator.Utils;
namespace ZoneCodeGenerator.Domain
{
class DataTypeUnion : DataTypeWithMembers
{
public DataTypeUnion(string _namespace, string name, int pack) : base(_namespace, name, pack, DataTypeType.Union)
{
}
protected override int CalculateSize()
{
return Members
.Select(variable => variable.VariableType.Size)
.Max()
.Align(Alignment);
}
}
}

View File

@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Linq;
namespace ZoneCodeGenerator.Domain
{
abstract class DataTypeWithMembers : DataType
{
public int? AlignmentOverride { get; set; }
private int alignment;
public override int Alignment => AlignmentOverride ?? alignment;
private int size;
public override int Size => size;
public int Pack { get; }
public List<Variable> Members { get; }
private bool finalized;
public void FinalizeDataType()
{
if (finalized) return;
finalized = true;
CalculateProperties();
}
protected DataTypeWithMembers(string _namespace, string name, int pack, DataTypeType type) : base(_namespace, name, type)
{
Members = new List<Variable>();
Pack = pack;
finalized = false;
}
private void CalculateProperties()
{
foreach (var member in Members
.Select(variable => variable.VariableType.Type)
.OfType<DataTypeWithMembers>())
{
member.FinalizeDataType();
}
alignment = Members.Select(variable => variable.Alignment).Max();
size = CalculateSize();
}
protected abstract int CalculateSize();
}
}

View File

@@ -0,0 +1,14 @@
namespace ZoneCodeGenerator.Domain
{
class EnumMember
{
public string Name { get; }
public long Value { get; }
public EnumMember(string name, long value)
{
Name = name;
Value = value;
}
}
}

View File

@@ -0,0 +1,30 @@
namespace ZoneCodeGenerator.Domain.FastFileStructure
{
class FastFileBlock
{
public enum Type
{
Temp,
Runtime,
Delay,
Normal
}
public string Name { get; }
public int Index { get; }
public Type BlockType { get; }
public bool IsDefault { get; }
public bool IsTemp => BlockType == Type.Temp;
public bool IsRuntime => BlockType == Type.Runtime;
public bool IsDelay => BlockType == Type.Delay;
public bool IsNormal => BlockType == Type.Normal;
public FastFileBlock(string name, int index, Type blockType, bool isDefault)
{
Name = name;
Index = index;
BlockType = blockType;
IsDefault = isDefault;
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
namespace ZoneCodeGenerator.Domain
{
class ForwardDeclaration : DataType
{
private DataType forwardedType;
public DataType ForwardedType
{
get => forwardedType;
set
{
if (forwardedType == null)
{
if(value.Type != Type)
throw new DataException($"Type of forwarded type '{Name}' does not match previous declaration");
forwardedType = value;
}
else if(forwardedType != value)
{
throw new DataException($"Tried to set forwarded type '{Name}' multiple times with different types");
}
}
}
public ForwardDeclaration(string _namespace, string name, DataTypeType dataTypeType) : base(_namespace, name, dataTypeType)
{
switch (dataTypeType)
{
case DataTypeType.Enum:
case DataTypeType.Struct:
case DataTypeType.Union:
break;
default:
throw new ArgumentException($"Cannot create forward declaration for type '{dataTypeType}'");
}
forwardedType = null;
}
public override int Alignment => 0;
public override int Size => 0;
}
}

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace ZoneCodeGenerator.Domain
{
class Namespace
{
private static readonly Regex nameRegex = new Regex(@"^[a-zA-Z_$][a-zA-Z0-9_$]*$");
private readonly Stack<string> namespaceStack;
public Namespace()
{
namespaceStack = new Stack<string>();
}
public void Push(string _namespace)
{
if(!nameRegex.IsMatch(_namespace))
throw new ArgumentException("Namespace name invalid");
namespaceStack.Push(_namespace);
}
public string Pop()
{
return namespaceStack.Pop();
}
public string GetName()
{
if (namespaceStack.Count == 0)
return "";
var result = "";
var stackSnapshot = namespaceStack.ToArray();
// The stack is read from top to bottom. Therefore we need to access it in reverse order here.
for(var i = stackSnapshot.Length - 1; i >= 0; i--)
{
if (!string.IsNullOrEmpty(result))
result += "::";
result += stackSnapshot[i];
}
return result;
}
public override string ToString()
{
return GetName();
}
public static string Combine(Namespace _namespace, string typename)
{
return $"{_namespace}::{typename}";
}
}
}

View File

@@ -0,0 +1,7 @@
namespace ZoneCodeGenerator.Domain
{
abstract class ReferenceType
{
}
}

View File

@@ -0,0 +1,12 @@
namespace ZoneCodeGenerator.Domain
{
class ReferenceTypeArray : ReferenceType
{
public int ArraySize { get; }
public ReferenceTypeArray(int arraySize)
{
ArraySize = arraySize;
}
}
}

View File

@@ -0,0 +1,6 @@
namespace ZoneCodeGenerator.Domain
{
class ReferenceTypePointer : ReferenceType
{
}
}

View File

@@ -0,0 +1,16 @@
namespace ZoneCodeGenerator.Domain.StructureInformation
{
class MemberInformation
{
public StructureInformation StructureType { get; }
public Variable Member { get; set; }
public bool IsScriptString { get; set; }
public MemberInformation(Variable member, StructureInformation structureType)
{
Member = member;
StructureType = structureType;
IsScriptString = false;
}
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ZoneCodeGenerator.Domain.FastFileStructure;
using ZoneCodeGenerator.Persistence;
namespace ZoneCodeGenerator.Domain.StructureInformation
{
class StructureInformation
{
public DataTypeWithMembers Type { get; }
public bool IsUnion => Type is DataTypeUnion;
public FastFileBlock Block { get; set; }
public EnumMember AssetEnumEntry { get; set; }
public bool IsAsset => AssetEnumEntry != null;
private int? fastFileAlign;
public bool HasNonDefaultAlign => fastFileAlign != null;
public int FastFileAlign
{
get => fastFileAlign ?? Type.Alignment;
set => fastFileAlign = value;
}
public List<StructureInformation> Usages { get; }
public List<MemberInformation> OrderedMembers { get; }
public bool NonEmbeddedReferenceExists { get; set; }
public bool PointerReferenceExists { get; set; }
public bool ArrayReferenceExists { get; set; }
public bool HasNameMember => Type.Members.Any(variable => variable.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase));
public StructureInformation(DataTypeWithMembers type)
{
AssetEnumEntry = null;
fastFileAlign = null;
Type = type;
NonEmbeddedReferenceExists = false;
PointerReferenceExists = false;
ArrayReferenceExists = false;
Usages = new List<StructureInformation>();
OrderedMembers = new List<MemberInformation>();
}
}
}

View File

@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Linq;
namespace ZoneCodeGenerator.Domain
{
class TypeDeclaration
{
private const int PointerSize = 4;
private DataType type;
public DataType Type
{
get
{
if (type is DataTypeTypedef typedef)
{
return typedef.TypeDefinition.Type;
}
return type;
}
set => type = value;
}
public int? CustomBitSize { get; }
public bool HasCustomBitSize => CustomBitSize != null;
private readonly List<ReferenceType> references;
public IReadOnlyList<ReferenceType> References => references.AsReadOnly();
public int Alignment => references.OfType<ReferenceTypePointer>().Any() ? PointerSize : type.Alignment;
public int Size
{
get
{
var currentSize = Type.Size;
foreach (var reference in References.Reverse())
{
switch (reference)
{
case ReferenceTypePointer _:
currentSize = PointerSize;
break;
case ReferenceTypeArray array:
currentSize *= array.ArraySize;
break;
}
}
return currentSize;
}
}
public TypeDeclaration(DataType type, List<ReferenceType> references)
{
this.type = type;
this.references = references ?? new List<ReferenceType>();
CustomBitSize = null;
}
public TypeDeclaration(DataType type, int customBitSize, List<ReferenceType> references) : this(type, references)
{
CustomBitSize = customBitSize;
}
}
}

View File

@@ -0,0 +1,19 @@
namespace ZoneCodeGenerator.Domain
{
class Variable
{
public string Name { get; }
public int? AlignmentOverride { get; set; }
public int Alignment => AlignmentOverride ?? VariableType.Alignment;
public TypeDeclaration VariableType { get; }
public Variable(string name, TypeDeclaration type)
{
Name = name;
VariableType = type;
}
}
}