Skip to content

Instantly share code, notes, and snippets.

@tobiasschulz
Created August 4, 2015 23:43
Show Gist options
  • Save tobiasschulz/ae4365d2b1304c4f93ff to your computer and use it in GitHub Desktop.
Save tobiasschulz/ae4365d2b1304c4f93ff to your computer and use it in GitHub Desktop.
using System;
using Mono.Cecil;
using System.Linq;
using Mono.Cecil.Cil;
using System.IO;
namespace XamarinPatch
{
class MainClass
{
public static void Main (string[] args)
{
//const string path = "../../../../../PoSDB-Xamarin//packages/Xamarin.Forms.1.4.4.6392/lib/MonoAndroid10/Xamarin.Forms.Core.dll";
string[] files = Directory.GetFiles (@"../../../../../", "Xamarin.Forms.Core.dll", SearchOption.AllDirectories);
foreach (string file in files) {
patchXamarinFormsCoreDll (file);
}
}
static void patchXamarinFormsCoreDll (string path)
{
var assembly = AssemblyDefinition.ReadAssembly (path);
ModuleDefinition module = assembly.MainModule;
TypeDefinition mainClass = module.GetType ("Xamarin.Forms.AnimationExtensions");
MethodDefinition method = mainClass.Methods.Single (m => m.Name == "HandleTweenerUpdated");
Console.WriteLine ("ExceptionHandlers.Count: " + method.Body.ExceptionHandlers.Count);
if (method.Body.ExceptionHandlers.Count == 0) {
var il = method.Body.GetILProcessor ();
var write = il.Create (OpCodes.Call, module.Import (typeof(Console).GetMethod ("WriteLine", new [] { typeof(object) })));
var ret = il.Create (OpCodes.Ret);
var leave = il.Create (OpCodes.Leave, ret);
il.InsertAfter (method.Body.Instructions.Last (), write);
il.InsertAfter (write, leave);
il.InsertAfter (leave, ret);
var handler = new ExceptionHandler (ExceptionHandlerType.Catch) {
TryStart = method.Body.Instructions.First (),
TryEnd = write,
HandlerStart = write,
HandlerEnd = ret,
CatchType = module.Import (typeof(Exception)),
};
method.Body.ExceptionHandlers.Add (handler);
string pathPatched = path + ".patched.dll";
assembly.Write (pathPatched);
System.IO.File.Copy (pathPatched, path, true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment