Skip to content

Commit

Permalink
Handle some nullable warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed May 13, 2024
1 parent 4379408 commit 87a0b14
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
7 changes: 4 additions & 3 deletions Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public IMethodDefOrRef RewriteMethodRef(IMethodDefOrRef methodRef)
{
var newType = GlobalContext.GetNewTypeForOriginal(methodRef.DeclaringType.Resolve());
var newMethod = newType.GetMethodByOldMethod(methodRef.Resolve()).NewMethod;
return this.NewAssembly.ManifestModule.DefaultImporter.ImportMethod(newMethod);
return NewAssembly.ManifestModule!.DefaultImporter.ImportMethod(newMethod);
}

public ITypeDefOrRef RewriteTypeRef(ITypeDescriptor typeRef)
Expand All @@ -71,9 +71,10 @@ public ITypeDefOrRef RewriteTypeRef(ITypeDescriptor typeRef)

public TypeSignature RewriteTypeRef(TypeSignature? typeRef)
{
if (typeRef == null) return Imports.Il2CppObjectBase;
if (typeRef == null)
return Imports.Il2CppObjectBase;

var sourceModule = NewAssembly.ManifestModule;
var sourceModule = NewAssembly.ManifestModule!;

if (typeRef is ArrayBaseTypeSignature arrayType)
{
Expand Down
9 changes: 7 additions & 2 deletions Il2CppInterop.Generator/Contexts/FieldRewriteContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
Expand Down Expand Up @@ -31,12 +32,14 @@ public FieldRewriteContext(TypeRewriteContext declaringType, FieldDefinition ori

declaringType.NewType.Fields.Add(pointerField);

Debug.Assert(pointerField.Signature is not null);
PointerField = new MemberReference(DeclaringType.SelfSubstitutedRef, pointerField.Name, new FieldSignature(pointerField.Signature.FieldType));
}

private string UnmangleFieldNameBase(FieldDefinition field, GeneratorOptions options)
{
if (options.PassthroughNames) return field.Name;
if (options.PassthroughNames)
return field.Name;

if (!field.Name.IsObfuscated(options))
{
Expand All @@ -45,6 +48,7 @@ private string UnmangleFieldNameBase(FieldDefinition field, GeneratorOptions opt
return field.Name.FilterInvalidInSourceChars();
}

Debug.Assert(field.Signature is not null);
var accessModString = MethodAccessTypeLabels[(int)(field.Attributes & FieldAttributes.FieldAccessMask)];
var staticString = field.IsStatic ? "_Static" : "";
return "field_" + accessModString + staticString + "_" +
Expand All @@ -54,7 +58,8 @@ private string UnmangleFieldNameBase(FieldDefinition field, GeneratorOptions opt
private string UnmangleFieldName(FieldDefinition field, GeneratorOptions options,
Dictionary<string, int>? renamedFieldCounts)
{
if (options.PassthroughNames) return field.Name;
if (options.PassthroughNames)
return field.Name;

if (!field.Name.IsObfuscated(options))
{
Expand Down
2 changes: 1 addition & 1 deletion Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public MethodRewriteContext(TypeRewriteContext declaringType, MethodDefinition o
var passthroughNames = declaringType.AssemblyContext.GlobalContext.Options.PassthroughNames;

OriginalNameObfuscated = !passthroughNames &&
(OriginalMethod?.Name?.IsObfuscated(declaringType.AssemblyContext.GlobalContext
(OriginalMethod.Name?.IsObfuscated(declaringType.AssemblyContext.GlobalContext
.Options) ?? false);

var newAttributes = AdjustAttributes(originalMethod.Attributes, originalMethod.Name == "Finalize");
Expand Down
4 changes: 1 addition & 3 deletions Il2CppInterop.Generator/Extensions/CustomAttributeEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public static int ExtractFieldOffset(this IHasCustomAttribute originalField)
var attribute = originalMethod.CustomAttributes.SingleOrDefault(it => it.Constructor?.DeclaringType?.Name == attributeName);
var field = attribute?.Signature?.NamedArguments.SingleOrDefault(it => it.MemberName == parameterName);

if (field?.MemberName is null) return null;

return (Utf8String?)field.Argument.Element;
return (Utf8String?)field?.Argument.Element;
}

private static long ExtractLong(this IHasCustomAttribute originalMethod, string attributeName, string parameterName)
Expand Down
13 changes: 9 additions & 4 deletions Il2CppInterop.Generator/Extensions/ILGeneratorEx.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Collections;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static void EmitObjectStore(this ILProcessor body, TypeSignature original
var classPointerTypeRef = new GenericInstanceTypeSignature(imports.Il2CppClassPointerStore.ToTypeDefOrRef(), imports.Il2CppClassPointerStore.IsValueType, newType);
var classPointerFieldRef =
CecilAdapter.CreateFieldReference("NativeClassPtr", imports.Module.IntPtr(), classPointerTypeRef.ToTypeDefOrRef());
body.Add(OpCodes.Ldsfld, enclosingType.NewType.Module.DefaultImporter.ImportField(classPointerFieldRef));
body.Add(OpCodes.Ldsfld, enclosingType.NewType.Module!.DefaultImporter.ImportField(classPointerFieldRef));
body.Add(OpCodes.Ldc_I4_0);
body.Add(OpCodes.Conv_U);
body.Add(OpCodes.Call, imports.IL2CPP_il2cpp_class_value_size.Value);
Expand All @@ -96,6 +97,7 @@ private static void EmitObjectStoreGeneric(ILProcessor body, TypeSignature origi

var imports = enclosingType.AssemblyContext.Imports;

Debug.Assert(enclosingType.NewType.Module is not null);
body.Add(OpCodes.Ldtoken, newType.ToTypeDefOrRef());
body.Add(OpCodes.Call, enclosingType.NewType.Module.TypeGetTypeFromHandle());
body.Add(OpCodes.Dup);
Expand Down Expand Up @@ -241,8 +243,9 @@ private static void EmitObjectToPointerGeneric(ILProcessor body, TypeSignature o
TypeSignature newType, TypeRewriteContext enclosingType, int argumentIndex,
bool valueTypeArgument0IsAPointer, bool allowNullable, bool unboxNonBlittableType)
{
var imports = enclosingType.AssemblyContext.Imports;

var imports = enclosingType.AssemblyContext.Imports;

Debug.Assert(enclosingType.NewType.Module is not null);
body.Add(OpCodes.Ldtoken, newType.ToTypeDefOrRef());
body.Add(OpCodes.Call, enclosingType.NewType.Module.TypeGetTypeFromHandle());
body.Add(OpCodes.Callvirt, enclosingType.NewType.Module.TypeGetIsValueType());
Expand Down Expand Up @@ -322,7 +325,8 @@ public static void EmitPointerToObject(this ILProcessor body, TypeSignature orig
body.Add(OpCodes.Ldloc, pointerVariable);
}
else
{
{
Debug.Assert(enclosingType.NewType.Module is not null);
var classPointerTypeRef = new GenericInstanceTypeSignature(imports.Il2CppClassPointerStore.ToTypeDefOrRef(), imports.Il2CppClassPointerStore.IsValueType, convertedReturnType);
var classPointerFieldRef =
CecilAdapter.CreateFieldReference("NativeClassPtr", imports.Module.IntPtr(),
Expand Down Expand Up @@ -390,6 +394,7 @@ private static void EmitPointerToObjectGeneric(ILProcessor body, TypeSignature o
public static void GenerateBoxMethod(RuntimeAssemblyReferences imports, TypeDefinition targetType,
IFieldDescriptor classHandle, TypeSignature il2CppObjectTypeDef)
{
Debug.Assert(targetType.Module is not null);
var method = new MethodDefinition("BoxIl2CppObject", MethodAttributes.Public | MethodAttributes.HideBySig,
MethodSignature.CreateInstance(targetType.Module.DefaultImporter.ImportTypeSignature(il2CppObjectTypeDef)));
targetType.Methods.Add(method);
Expand Down
4 changes: 2 additions & 2 deletions Il2CppInterop.Generator/Extensions/StringEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ public static string GetUnmangledName(this TypeSignature typeRef)
}
else
{
if (typeRef.Namespace == "Il2CppInterop.Runtime" && typeRef.Name.StartsWith("Il2Cpp") &&
if (typeRef.Namespace == "Il2CppInterop.Runtime" && (typeRef.Name?.StartsWith("Il2Cpp") ?? false) &&
typeRef.Name.Contains("Array"))
builder.Append("ArrayOf");
else
builder.Append(typeRef.Name.Replace('`', '_'));
builder.Append(typeRef.Name?.Replace('`', '_'));
}

return builder.ToString();
Expand Down

0 comments on commit 87a0b14

Please sign in to comment.