Skip to content

Commit

Permalink
Update Mono.Cecil to 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Chicken-Bones committed Nov 17, 2018
1 parent 3e724d9 commit 425c6b5
Show file tree
Hide file tree
Showing 23 changed files with 188 additions and 79 deletions.
13 changes: 8 additions & 5 deletions RoslynWrapper/RoslynPdbFixer.cs
Expand Up @@ -10,11 +10,14 @@ public class RoslynPdbFixer
/// Mono.Cecil can parse the roslyn debug info, and then output a compatible pdb file and binary using the windows API
/// </summary>
public static void Fix(string dll) {
var asm = AssemblyDefinition.ReadAssembly(dll);
asm.MainModule.ReadSymbols();
var fstream = File.Create(dll);
asm.Write(fstream, new WriterParameters { WriteSymbols = true });
fstream.Close();
AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(dll, new ReaderParameters {
InMemory = true,
ReadSymbols = true
});

using (var stream = File.Create(dll)) {
asm.Write(stream, new WriterParameters { WriteSymbols = true });
}
}
}
}
Binary file modified RoslynWrapper/bin/Release/RoslynWrapper.dll
Binary file not shown.
21 changes: 5 additions & 16 deletions patches/tModLoader/Terraria.ModLoader/AssemblyManager.cs
Expand Up @@ -344,26 +344,15 @@ private class HeaderCopyWriter : ISymbolWriter
{
private ModuleDefinition module;

public HeaderCopyWriter(ModuleDefinition module)
{
public HeaderCopyWriter(ModuleDefinition module) {
this.module = module;
}

public bool GetDebugHeader(out ImageDebugDirectory directory, out byte[] header)
{
if (!module.HasDebugHeader)
{
directory = new ImageDebugDirectory();
header = null;
return false;
}

directory = module.GetDebugHeader(out header);
return true;
}
public ImageDebugHeader GetDebugHeader() => module.GetDebugHeader();

public ISymbolReaderProvider GetReaderProvider() => throw new NotImplementedException();
public void Write(MethodDebugInformation info) => throw new NotImplementedException();

public void Write(Mono.Cecil.Cil.MethodBody body) { }
public void Write(MethodSymbols symbols) { }
public void Dispose() { }
}

Expand Down
Binary file modified references/Mono.Cecil.Pdb.dll
Binary file not shown.
Binary file modified references/Mono.Cecil.dll
Binary file not shown.
Binary file modified references/MonoMod.RuntimeDetour.dll
Binary file not shown.
Binary file modified references/MonoMod.Utils.dll
Binary file not shown.
Binary file modified references/TerrariaHooks.Mono.dll
Binary file not shown.
Binary file modified references/TerrariaHooks.Windows.dll
Binary file not shown.
52 changes: 52 additions & 0 deletions setup/HookGenWrapper/HookGenWrapper.cs
@@ -0,0 +1,52 @@
using Mono.Cecil;
using MonoMod;
using MonoMod.RuntimeDetour.HookGen;
using System.IO;
using XnaToFna;

namespace HookGenWrapper
{
/// <summary>
/// HookGen/MonoMod uses Cecil 0.10 while ILSpy uses Cecil 0.9, so separate projects are required
/// </summary>
public static class HookGenWrapper
{
public static void HookGen(string inputPath, string outputPath, string refsDir)
{
using (MonoModder mm = new MonoModder {
InputPath = inputPath,
OutputPath = outputPath,
ReadingMode = ReadingMode.Deferred,

MissingDependencyThrow = false,
}) {
mm.Read();
mm.MapDependencies();
mm.DependencyCache["MonoMod.RuntimeDetour"] = ModuleDefinition.ReadModule(Path.Combine(refsDir, "MonoMod.RuntimeDetour.dll"));

HookGenerator gen = new HookGenerator(mm, "TerrariaHooks") {
HookPrivate = true,
};
gen.Generate();
gen.OutputModule.Write(outputPath);
}
}

public static void XnaToFna(string inputPath, string refsDir)
{
using (var xnaToFnaUtil = new XnaToFnaUtil {
HookCompat = false,
HookHacks = false,
HookEntryPoint = false,
HookBinaryFormatter = false,
HookReflection = false,
AddAssemblyReference = false
})
{
xnaToFnaUtil.ScanPath(Path.Combine(refsDir, "FNA.dll"));
xnaToFnaUtil.ScanPath(inputPath);
xnaToFnaUtil.RelinkAll();
}
}
}
}
61 changes: 61 additions & 0 deletions setup/HookGenWrapper/HookGenWrapper.csproj
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A386559E-0850-47AD-8F5B-D1492FA27980}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HookGenWrapper</RootNamespace>
<AssemblyName>HookGenWrapper</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil">
<HintPath>..\..\references\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="MonoMod">
<HintPath>..\lib\MonoMod.exe</HintPath>
</Reference>
<Reference Include="MonoMod.RuntimeDetour.HookGen">
<HintPath>..\lib\MonoMod.RuntimeDetour.HookGen.exe</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="XnaToFna">
<HintPath>..\lib\XnaToFna.exe</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="HookGenWrapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
36 changes: 36 additions & 0 deletions setup/HookGenWrapper/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HookGenWrapper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HookGenWrapper")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a386559e-0850-47ad-8f5b-d1492fa27980")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
44 changes: 4 additions & 40 deletions setup/Setup/HookGenTask.cs
@@ -1,8 +1,4 @@
using Mono.Cecil;
using MonoMod;
using MonoMod.RuntimeDetour.HookGen;
using System.IO;
using XnaToFna;
using System.IO;

namespace Terraria.ModLoader.Setup
{
Expand All @@ -22,24 +18,7 @@ public override void Run()
File.Delete(outputPath);

taskInterface.SetStatus($"Hooking: Terraria.exe -> TerrariaHooks.dll");

using (MonoModder mm = new MonoModder {
InputPath = Program.TerrariaPath,
OutputPath = outputPath,
ReadingMode = ReadingMode.Deferred,

MissingDependencyThrow = false,
}) {
mm.Read();
mm.MapDependencies();
mm.DependencyCache["MonoMod.RuntimeDetour"] = ModuleDefinition.ReadModule(Path.Combine(Program.ReferencesDir, "MonoMod.RuntimeDetour.dll"));

HookGenerator gen = new HookGenerator(mm, "TerrariaHooks") {
HookPrivate = true,
};
gen.Generate();
gen.OutputModule.Write(outputPath);
}
HookGenWrapper.HookGenWrapper.HookGen(Program.TerrariaPath, outputPath, Program.ReferencesDir);

taskInterface.SetStatus($"XnaToFna: TerrariaHooks.Windows.dll -> TerrariaHooks.Mono.dll");

Expand All @@ -48,23 +27,8 @@ public override void Run()
File.Delete(monoPath);

File.Copy(outputPath, monoPath);

using (var xnaToFnaUtil = new XnaToFnaUtil {
HookCompatHelpers = false,
HookEntryPoint = false,
DestroyLocks = false,
StubMixedDeps = false,
DestroyMixedDeps = false,
HookBinaryFormatter = false,
HookReflection = false,
AddAssemblyReference = false
})
{
xnaToFnaUtil.ScanPath(Path.Combine(Program.ReferencesDir, "FNA.dll"));
xnaToFnaUtil.ScanPath(monoPath);
xnaToFnaUtil.RelinkAll();
}

HookGenWrapper.HookGenWrapper.XnaToFna(monoPath, Program.ReferencesDir);

File.Delete(Path.ChangeExtension(monoPath, "pdb"));
}
}
Expand Down
6 changes: 5 additions & 1 deletion setup/Setup/Program.cs
Expand Up @@ -32,7 +32,11 @@ static class Program
Application.SetCompatibleTextRenderingDefault(false);

AppDomain.CurrentDomain.AssemblyResolve += (sender, resArgs) => {
var name = new AssemblyName(resArgs.Name).Name;
var assemblyName = new AssemblyName(resArgs.Name);
var name = assemblyName.Name;
if (name == "Mono.Cecil" && assemblyName.Version == new Version(0, 9, 6, 0)) //multiple cecil versions need to be loaded separately
name += "_" + assemblyName.Version;
return ResolveAssemblyFrom(libDir, name) ?? ResolveAssemblyFrom(ReferencesDir, name);
};

Expand Down
Binary file modified setup/bin/setup.exe
Binary file not shown.
Binary file modified setup/bin/setup.pdb
Binary file not shown.
Binary file modified setup/lib/Mono.Cecil.Mdb.dll
Binary file not shown.
Binary file added setup/lib/Mono.Cecil_0.9.6.0.dll
Binary file not shown.
Binary file modified setup/lib/MonoMod.RuntimeDetour.HookGen.exe
Binary file not shown.
Binary file modified setup/lib/MonoMod.exe
Binary file not shown.
Binary file modified setup/lib/XnaToFna.exe
Binary file not shown.
23 changes: 8 additions & 15 deletions setup/setup.csproj
Expand Up @@ -65,21 +65,8 @@
<HintPath>lib\ILSpy.exe</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Cecil">
<HintPath>..\references\Mono.Cecil.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="MonoMod">
<HintPath>lib\MonoMod.exe</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="MonoMod.RuntimeDetour.HookGen">
<HintPath>lib\MonoMod.RuntimeDetour.HookGen.exe</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XnaToFna">
<HintPath>lib\XnaToFna.exe</HintPath>
<Private>False</Private>
<Reference Include="Mono.Cecil_0.9.6.0">
<HintPath>lib\Mono.Cecil_0.9.6.0.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down Expand Up @@ -175,6 +162,12 @@
<ItemGroup>
<None Include="icon\file.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="HookGenWrapper\HookGenWrapper.csproj">
<Project>{a386559e-0850-47ad-8f5b-d1492fa27980}</Project>
<Name>HookGenWrapper</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
11 changes: 9 additions & 2 deletions setup/setup.sln
@@ -1,19 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2036
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "setup", "setup.csproj", "{ECAD06B8-1A3B-4288-A02C-198A198EF98E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HookGenWrapper", "HookGenWrapper\HookGenWrapper.csproj", "{A386559E-0850-47AD-8F5B-D1492FA27980}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ECAD06B8-1A3B-4288-A02C-198A198EF98E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ECAD06B8-1A3B-4288-A02C-198A198EF98E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A386559E-0850-47AD-8F5B-D1492FA27980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A386559E-0850-47AD-8F5B-D1492FA27980}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FDF598EF-0C8A-4148-807A-13F6C1A5A033}
EndGlobalSection
EndGlobal

0 comments on commit 425c6b5

Please sign in to comment.