View difference between Paste ID: BzYzmg8K and bRzTDWiQ
SHOW: | | - or go back to the newest paste.
1
using System;
2
using RimWorld;
3
using Verse;
4
using UnityEngine;
5
using System.Collections.Generic;
6
using System.Linq;
7
using System.Reflection;
8
using System.Text;
9
10
namespace MyRimworldMod
11
{
12
	[StaticConstructorOnStartup]
13
	internal static class DetourInjector
14
	{
15
		private static Assembly Assembly => Assembly.GetAssembly(typeof(DetourInjector));
16
17
		private static string AssemblyName => Assembly.FullName.Split(',').First();
18
19
		static DetourInjector()
20
		{
21
			LongEventHandler.QueueLongEvent(Inject, "Initializing", true, null);
22
		}
23
24
		private static void Inject()
25
		{
26
			if (DoInject())
27
				Log.Message(AssemblyName + " injected.");
28
			else
29
				Log.Error(AssemblyName + " failed to get injected properly.");
30
		}
31
32
		private const BindingFlags UniversalBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
33
34
		private static bool DoInject()
35
		{
36
			// Detour the DropBloodFilth method
37
			// First MethodInfo is source method to detour
38
			// Second MethodInfo is our method taking its place
39
			MethodInfo Verse_PawnHealthTracker_DropBloodFilth = typeof(Verse.Pawn_HealthTracker).GetMethod("DropBloodFilth");
40
			MethodInfo MyRimworldMod_DropBloodOverride_DropBloodFilth = typeof(MyDetours).GetMethod("_DropBloodFilth");
41
			if (!Detours.TryDetourFromTo(Verse_PawnHealthTracker_DropBloodFilth, MyRimworldMod_MyDetours_DropBloodFilth)){
42
				ErrorDetouring("Pawn_HealthTracker.DropBloodFilth method");
43
				return false;
44
			}
45
46
			// You can do as many detours as you like.
47
48
			// All our detours must have succeeded. Hooray!
49
			return true;
50
		}
51
52
		// Just saves some writing for throwing errors on failed detours
53
		internal static void ErrorDetouring(string classmethod){
54
			Log.Error("Failed to inject " + classmethod + " detour!");
55
		}
56
	}
57
}