Skip to content

Commit

Permalink
fix: Fix BadImageFormatException
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed May 14, 2024
1 parent 6acceb1 commit 64a2ceb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/CppParser/CppType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

namespace Hosihikari.Generation.CppParser;

public class CppType
Expand Down Expand Up @@ -99,6 +101,7 @@ public CppType(
/// <summary>
/// Gets a value indicating whether the type is a template type.
/// </summary>
[MemberNotNullWhen(true, nameof(TemplateTypes))]
public bool IsTemplate => TemplateTypes is not null;

private string GetTypeString()
Expand Down Expand Up @@ -157,7 +160,7 @@ public IEnumerable<CppType> ToEnumerableReversed()
return ToEnumerable().Reverse();
}

public override int GetHashCode() => ToString().GetHashCode();
public override int GetHashCode() => GetTypeString().GetHashCode();

public override bool Equals(object? obj)
{
Expand Down
12 changes: 6 additions & 6 deletions src/ILGenerate/AssemblyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public TypeSystem(AssemblyDefinition assemblyDefinition)
TargetFrameworkAttribute = types.FirstOrDefault(t => t.Name == "TargetFrameworkAttribute") ?? throw new Exception("TargetFrameworkAttribute type is not found");
}

public TypeDefinition Object { get; private set; }
public TypeDefinition String { get; private set; }
public TypeDefinition IDisposable { get; private set; }
public TypeDefinition GC { get; private set; }
public TypeDefinition ValueType { get; private set; }
public TypeDefinition Object { get; }
public TypeDefinition String { get; }
public TypeDefinition IDisposable { get; }
public TypeDefinition GC { get; }
public TypeDefinition ValueType { get; }

public TypeDefinition TargetFrameworkAttribute { get; private set; }
public TypeDefinition TargetFrameworkAttribute { get; }
}

public class AssemblyGenerator
Expand Down
8 changes: 3 additions & 5 deletions src/ILGenerate/CecilExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ namespace Hosihikari.Generation.ILGenerate;

public static class CecilExtension
{
public static TypeDefinition DefineType(this ModuleDefinition declaringModule, string @namespace, string name, TypeAttributes attributes)
public static TypeDefinition DefineType(this ModuleDefinition declaringModule, string @namespace, string name, TypeAttributes attributes, TypeReference? baseType = null)
{
var type = new TypeDefinition(@namespace, name, attributes);
var type = new TypeDefinition(@namespace, name, attributes, baseType);
declaringModule.Types.Add(type);
return type;
}

public static TypeDefinition DefineType(this TypeDefinition declaringType, string @namespace, string name, TypeAttributes attributes, TypeReference? baseType = null)
{
var type = baseType is null ?
new TypeDefinition(@namespace, name, attributes) :
new TypeDefinition(@namespace, name, attributes, baseType);
var type = new TypeDefinition(@namespace, name, attributes, baseType);
declaringType.NestedTypes.Add(type);
return type;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ILGenerate/MethodGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private bool TestPropertyMethod([NotNullWhen(true)] out bool? isGetter, [NotNull
if (name.StartsWith(prefix) && name.Length > prefix.Length)
{
isGetter = true;
propertyName = name[prefix.Length..];
propertyName = prefix is "get" ? name[prefix.Length..] : name;
return true;
}
}
Expand All @@ -224,7 +224,7 @@ private bool TestPropertyMethod([NotNullWhen(true)] out bool? isGetter, [NotNull
if (name.StartsWith(prefix) && name.Length > prefix.Length)
{
isGetter = false;
propertyName = name[prefix.Length..];
propertyName = prefix is "set" ? name[prefix.Length..] : name;
return true;
}
}
Expand Down
29 changes: 25 additions & 4 deletions src/ILGenerate/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ private TypeGenerator(AssemblyGenerator assemblyGenerator, OriginalClass @class,

Type = Assembly.MainModule.DefineType(
GenerateNamespace(cppType.RootType.Namespaces),
cppType.RootType.TypeIdentifier,
GenerateTypeName(cppType),
TypeAttributes.Class |
TypeAttributes.Public);
TypeAttributes.Public,
Assembly.ImportRef(typeof(object)));
Type.Interfaces.Add(
new(
new GenericInstanceType(Assembly.ImportRef(typeof(ICppInstance<>)))
Expand Down Expand Up @@ -93,9 +94,10 @@ private TypeGenerator(AssemblyGenerator assemblyGenerator, CppType cppType)

Type = Assembly.Module.DefineType(
GenerateNamespace(cppType.RootType.Namespaces),
cppType.RootType.TypeIdentifier,
GenerateTypeName(cppType),
TypeAttributes.Class |
TypeAttributes.Public);
TypeAttributes.Public,
Assembly.ImportRef(typeof(object)));
Type.Interfaces.Add(
new(
new GenericInstanceType(Assembly.ImportRef(typeof(ICppInstance<>)))
Expand Down Expand Up @@ -184,6 +186,21 @@ private static string GenerateNamespace(string[]? namespaces)
return builder.ToString();
}

private static string GenerateTypeName(CppType cppType)
{
cppType = cppType.RootType;
if (cppType.IsTemplate is false)
return cppType.TypeIdentifier;

StringBuilder builder = new(cppType.TypeIdentifier);
foreach (var type in cppType.TemplateTypes)
{
builder.Append('_').Append(GenerateTypeName(type));
}

return builder.ToString();
}

public async ValueTask<bool> GenerateAsync()
{
if (Generated)
Expand Down Expand Up @@ -532,6 +549,10 @@ await Task.Run(() =>
MethodAttributes.Static,
Assembly.ImportRef(typeof(void)),
parameterTypes: [new("ptr", ParameterAttributes.None, Assembly.ImportRef(typeof(nint)))]);
destructInstanceMethod.Overrides.Add(Assembly.ImportRef(
typeof(ICppInstanceNonGeneric)
.GetMethods()
.First(f => f.Name is nameof(ICppInstanceNonGeneric.DestructInstance))));
var destructMethod = Type.DefineMethod(
nameof(ICppInstanceNonGeneric.Destruct),
Expand Down
6 changes: 4 additions & 2 deletions src/ILGenerate/TypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ where type.Item1.Type is CppTypeEnum.Enum && type.type.IsEnum

public async ValueTask<TypeGenerator?> GetOrRegisterTypeAsync(CppType type, OriginalClass? @class)
{
type = type.RootType;

if (Types.TryGetValue(type, out var typeGenerator))
return typeGenerator;

Expand Down Expand Up @@ -125,8 +127,8 @@ await Task.Run(() =>
}
else
{
if (autoRegister)
ret = (await RegisterTypeAsync(type, null))?.Type;
if (autoRegister && type.IsTemplate is false)
ret = (await GetOrRegisterTypeAsync(type, null))?.Type;
else
ret = null;
}
Expand Down

0 comments on commit 64a2ceb

Please sign in to comment.