- Home /
Altering "Assembly-CSharp.dll" with Mono Cecil produces a zero bytes dll
We want to bring a few of Cecil's post-processing features into our Unity game, but have run into an unknown issue I haven't seen anyone get. Basically, in our post-processor file (Assets/Editor/SixMinutePostprocess.cs),
using UnityEditor;
using System;
using Mono.Cecil;
[InitializeOnLoadAttribute]
static class SixMinutePostprocess
{
static SixMinutePostprocess()
{
Console.WriteLine("Init AssemblyPostProcessor");
string path = "Library/ScriptAssemblies/Assembly-CSharp.dll";
ModuleDefinition.ReadModule(path).Write(path);
}
}
But when that's done, Unity's main application gives out about all sorts of things missing, and when you check the newly altered dll, it's an empty file, zero bytes. Running similar code outside of Unity gives expected results, but I can't figure out how to get it to run properly with Unity's builders.
I'm using the Cecil build from Unity's own Github, https://github.com/Unity-Technologies/cecil/releases (which, as I said, works as expected in another project).
Answer by Joebobaggins · Oct 24, 2016 at 10:03 PM
You need to add module search paths to your ReadModule call.
// this will add paths for all your currently loaded assemblies
DefaultAssemblyResolver resolver = new DefaultAssemblyResolver();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
resolver.AddSearchDirectory(Path.GetDirectoryName(assembly.Location));
ReaderParameters parameters = new ReaderParameters { AssemblyResolver = resolver };
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path, parameters);
Your answer
Follow this Question
Related Questions
Locking assembly reloading in editor 0 Answers
Can Editor scripts be in external DLL's or assemblies? 1 Answer
Editor class for Animation window 1 Answer
Unity Entering Play Mode Taking 24s In **Empty Project** 0 Answers
Re-compile C# assemblies 1 Answer