Add Mono.Cecil to Flax.Build

This commit is contained in:
Wojciech Figat
2022-10-18 15:02:49 +02:00
parent c12ea8428e
commit 3a3a94b610
4 changed files with 116 additions and 0 deletions

BIN
Source/Platforms/DotNet/Mono.Cecil.dll (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Source/Platforms/DotNet/Mono.Cecil.pdb (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -49,6 +49,9 @@
<Reference Include="Ionic.Zip.Reduced"> <Reference Include="Ionic.Zip.Reduced">
<HintPath>..\..\..\Source\Platforms\DotNet\Ionic.Zip.Reduced.dll</HintPath> <HintPath>..\..\..\Source\Platforms\DotNet\Ionic.Zip.Reduced.dll</HintPath>
</Reference> </Reference>
<Reference Include="Mono.Cecil">
<HintPath>..\..\..\Source\Platforms\DotNet\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Setup.Configuration.Interop"> <Reference Include="Microsoft.VisualStudio.Setup.Configuration.Interop">
<HintPath>..\..\..\Source\Platforms\DotNet\Microsoft.VisualStudio.Setup.Configuration.Interop.dll</HintPath> <HintPath>..\..\..\Source\Platforms\DotNet\Microsoft.VisualStudio.Setup.Configuration.Interop.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes> <EmbedInteropTypes>True</EmbedInteropTypes>
@@ -165,6 +168,7 @@
<Compile Include="Projects\VisualStudio\VisualStudioProjectGenerator.cs" /> <Compile Include="Projects\VisualStudio\VisualStudioProjectGenerator.cs" />
<Compile Include="Projects\VisualStudio\VisualStudioVersion.cs" /> <Compile Include="Projects\VisualStudio\VisualStudioVersion.cs" />
<Compile Include="Projects\XCodeProjectGenerator.cs" /> <Compile Include="Projects\XCodeProjectGenerator.cs" />
<Compile Include="Utilities\MonoCecil.cs" />
<Compile Include="Utilities\StringWriterWithEncoding.cs" /> <Compile Include="Utilities\StringWriterWithEncoding.cs" />
<Compile Include="Utilities\Tokenizer.cs" /> <Compile Include="Utilities\Tokenizer.cs" />
<Compile Include="Utilities\TwoWayEnumerator.cs" /> <Compile Include="Utilities\TwoWayEnumerator.cs" />

View File

@@ -0,0 +1,106 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using System;
using System.Linq;
using Mono.Cecil;
namespace Flax.Build
{
/// <summary>
/// The utilities for Mono.Cecil library usage.
/// </summary>
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}.");
}
}
}