static void InjectSpriteBatchDrawString() { var injectees = GameAssembly.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod)) .SelectMany(t => t.Methods) .Where(method => null != method.Body).ToList(); var FullCallbackMethod = typeof(LocalizationBridge).GetMethod("SpriteBatchDrawStringCallback", new Type[] { typeof(SpriteBatch), typeof(SpriteFont), typeof(string), typeof(Vector2), typeof(Color), typeof(float), typeof(Vector2), typeof(float), typeof(SpriteEffects), typeof(float) }); var FullCallback = GameAssembly.MainModule.Import(FullCallbackMethod); var ShortCallbackMethod = typeof(LocalizationBridge).GetMethod("SpriteBatchDrawStringCallback", new Type[] { typeof(SpriteBatch), typeof(SpriteFont), typeof(string), typeof(Vector2), typeof(Color) }); var ShortCallback = GameAssembly.MainModule.Import(ShortCallbackMethod); int count = 0; foreach (var body in injectees.Select(m => m.Body)) { var processor = body.GetILProcessor(); var instructions = body.Instructions.Where(instr => instr.OpCode == OpCodes.Callvirt && instr.ToString().Contains("SpriteBatch::DrawString")).ToList(); foreach (var instr in instructions) { var meth = instr.Operand as MethodReference; if (meth.Parameters.Count == 9) { var writeInstruction = processor.Create(OpCodes.Call, FullCallback); processor.Replace(instr, writeInstruction); } else if (meth.Parameters.Count == 4) { var writeInstruction = processor.Create(OpCodes.Call, ShortCallback); processor.Replace(instr, writeInstruction); } count++; } } }
public void Init() { injectees = def.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod)) .SelectMany(t => t.Methods) .Where(method => null != method.Body).ToList(); var CallbackMethod = typeof(StaticGameContext).GetMethod("SpriteFontMeasureStringCallback", new Type[] { typeof(SpriteFont), typeof(string) }); Callback = def.MainModule.Import(CallbackMethod); }
public MethodReference FindBindingsStaticMethod(string findNamespace, string findClass, string findMethod) { var types = from module in BindingsAssembly.Modules from type in ModuleDefinitionRocks.GetAllTypes(module) where (type.IsClass && type.Namespace == findNamespace && type.Name == findClass) select type; var methods = (from type in types from method in type.Methods where (method.IsStatic && method.Name == findMethod) select method).ToArray(); return(TargetAssembly.MainModule.ImportReference(methods[0])); }
public void Init() { injectees = def.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod)) .SelectMany(t => t.Methods) .Where(method => null != method.Body).ToList(); var FullCallbackMethod = typeof(StaticGameContext).GetMethod("SpriteBatchDrawStringCallback", new Type[] { typeof(SpriteBatch), typeof(SpriteFont), typeof(string), typeof(Vector2), typeof(Color), typeof(float), typeof(Vector2), typeof(float), typeof(SpriteEffects), typeof(float) }); FullCallback = def.MainModule.Import(FullCallbackMethod); var ShortCallbackMethod = typeof(StaticGameContext).GetMethod("SpriteBatchDrawStringCallback", new Type[] { typeof(SpriteBatch), typeof(SpriteFont), typeof(string), typeof(Vector2), typeof(Color) }); ShortCallback = def.MainModule.Import(ShortCallbackMethod); }
static void InjectSpriteFontMeasureString() { var injectees = GameAssembly.Modules.SelectMany(mod => ModuleDefinitionRocks.GetAllTypes(mod)) .SelectMany(t => t.Methods) .Where(method => null != method.Body).ToList(); var CallbackMethod = typeof(LocalizationBridge).GetMethod("SpriteFontMeasureStringCallback", new Type[] { typeof(SpriteFont), typeof(string) }); var Callback = GameAssembly.MainModule.Import(CallbackMethod); foreach (var body in injectees.Select(m => m.Body)) { var processor = body.GetILProcessor(); var instructions = body.Instructions.Where(instr => instr.OpCode == OpCodes.Callvirt && instr.ToString().Contains("SpriteFont::MeasureString")).ToList(); foreach (var instr in instructions) { var meth = instr.Operand as MethodReference; var writeInstruction = processor.Create(OpCodes.Call, Callback); processor.Replace(instr, writeInstruction); } } }