From 3a3a94b61024b6cad8d507273a253d0dd40db7ee Mon Sep 17 00:00:00 2001 From: Wojciech Figat Date: Tue, 18 Oct 2022 15:02:49 +0200 Subject: [PATCH] Add Mono.Cecil to Flax.Build --- Source/Platforms/DotNet/Mono.Cecil.dll | 3 + Source/Platforms/DotNet/Mono.Cecil.pdb | 3 + Source/Tools/Flax.Build/Flax.Build.csproj | 4 + .../Tools/Flax.Build/Utilities/MonoCecil.cs | 106 ++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 Source/Platforms/DotNet/Mono.Cecil.dll create mode 100644 Source/Platforms/DotNet/Mono.Cecil.pdb create mode 100644 Source/Tools/Flax.Build/Utilities/MonoCecil.cs diff --git a/Source/Platforms/DotNet/Mono.Cecil.dll b/Source/Platforms/DotNet/Mono.Cecil.dll new file mode 100644 index 000000000..e14dd37ce --- /dev/null +++ b/Source/Platforms/DotNet/Mono.Cecil.dll @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bf98db6182162915571b114eedb4dcc2c3130564b389719cfb0eb76ce46fc4f9 +size 358400 diff --git a/Source/Platforms/DotNet/Mono.Cecil.pdb b/Source/Platforms/DotNet/Mono.Cecil.pdb new file mode 100644 index 000000000..458e14b7a --- /dev/null +++ b/Source/Platforms/DotNet/Mono.Cecil.pdb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3ac01c6e32ddb22a52b60fa29f75d2d2e8ff07ab60e0dcacb3400226a24b0a1 +size 184440 diff --git a/Source/Tools/Flax.Build/Flax.Build.csproj b/Source/Tools/Flax.Build/Flax.Build.csproj index 0d592b0ac..e91be5b0c 100644 --- a/Source/Tools/Flax.Build/Flax.Build.csproj +++ b/Source/Tools/Flax.Build/Flax.Build.csproj @@ -49,6 +49,9 @@ ..\..\..\Source\Platforms\DotNet\Ionic.Zip.Reduced.dll + + ..\..\..\Source\Platforms\DotNet\Mono.Cecil.dll + ..\..\..\Source\Platforms\DotNet\Microsoft.VisualStudio.Setup.Configuration.Interop.dll True @@ -165,6 +168,7 @@ + diff --git a/Source/Tools/Flax.Build/Utilities/MonoCecil.cs b/Source/Tools/Flax.Build/Utilities/MonoCecil.cs new file mode 100644 index 000000000..305b4e34f --- /dev/null +++ b/Source/Tools/Flax.Build/Utilities/MonoCecil.cs @@ -0,0 +1,106 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System; +using System.Linq; +using Mono.Cecil; + +namespace Flax.Build +{ + /// + /// The utilities for Mono.Cecil library usage. + /// + internal static class MonoCecil + { + public static bool HasAttribute(this TypeDefinition type, string fullName) + { + return type.CustomAttributes.Any(x => x.AttributeType.FullName == fullName); + } + + public static bool HasAttribute(this FieldDefinition type, string fullName) + { + return type.CustomAttributes.Any(x => x.AttributeType.FullName == fullName); + } + + public static bool HasInterface(this TypeDefinition type, string fullName) + { + return type.Interfaces.Any(x => x.InterfaceType.FullName == fullName); + } + + public static bool HasMethod(this TypeDefinition type, string name) + { + return type.Methods.Any(x => x.Name == name); + } + + public static MethodDefinition GetMethod(this TypeDefinition type, string name) + { + return type.Methods.First(x => x.Name == name); + } + + public static MethodDefinition GetMethod(this TypeDefinition type, string name, int argCount) + { + return type.Methods.First(x => x.Name == name && x.Parameters.Count == argCount); + } + + public static FieldDefinition GetField(this TypeDefinition type, string name) + { + return type.Fields.First(x => x.Name == name); + } + + public static bool IsScriptingObject(this TypeDefinition type) + { + if (type.FullName == "FlaxEngine.Object") + return true; + return type.BaseType.IsScriptingObject(); + } + + public static bool IsScriptingObject(this TypeReference type) + { + if (type == null) + return false; + if (type.FullName == "FlaxEngine.Object") + return true; + try + { + return type.Resolve().IsScriptingObject(); + } + catch + { + return false; + } + } + + public static bool CanBeResolved(this TypeReference type) + { + while (type != null) + { + if (type.Scope.Name == "Windows") + return false; + if (type.Scope.Name == "mscorlib") + return type.Resolve() != null; + + try + { + type = type.Resolve().BaseType; + } + catch + { + return false; + } + } + return true; + } + + public static MethodReference InflateGeneric(this MethodReference generic, TypeReference T) + { + var instance = new GenericInstanceMethod(generic); + instance.GenericArguments.Add(T); + return instance; + } + + public static void GetTypeReference(this ModuleDefinition module, string fullName, out TypeReference type) + { + if (!module.TryGetTypeReference(fullName, out type)) + throw new Exception($"Failed to find type {fullName} from module {module.FileName}."); + } + } +}