-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeacefulEnemies.cs
100 lines (85 loc) · 2.74 KB
/
PeacefulEnemies.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AIs;
using Harmony;
using JetBrains.Annotations;
using UnityEngine;
public class PeacefulEnemies : Mod
{
private HarmonyInstance m_harmony;
private const string ModName = "PeacefulEnemies";
private const string HarmonyId = "com.janniksam.greenhell.peacefulenemies";
public void Start()
{
Debug.Log(string.Format("Mod {0} has been loaded!", ModName));
m_harmony = HarmonyInstance.Create(HarmonyId);
PatchAttackGoals();
}
private void PatchAttackGoals()
{
var aiGoals = ReflectiveEnumerator.GetDerivedTypes<AIGoal>();
foreach (var aiGoalType in aiGoals)
{
var goal = InstantiateGoal(aiGoalType);
if (goal == null)
{
continue;
}
if (!goal.IsAttackGoal())
{
continue;
}
Debug.Log(string.Format("{0}: Patching {1}... ", ModName, goal.GetType().Name));
var original = aiGoalType.GetMethod("ShouldPerform");
var prefix = typeof(GoalAttackPatch).GetMethod("Prefix");
m_harmony.Patch(original, new HarmonyMethod(prefix));
}
Debug.Log(string.Format("{0}: Patching done... ", ModName));
}
private static AIGoal InstantiateGoal(Type aiGoal)
{
var goal = (AIGoal) Activator.CreateInstance(aiGoal);
var enumVal = aiGoal.ToString().Remove(0, 8);
AIGoalType type;
if (!Enum.TryParse(enumVal, out type))
{
return null;
}
goal.m_Type = type;
return goal;
}
[UsedImplicitly]
public class GoalAttackPatch
{
[UsedImplicitly]
public static bool Prefix(
// ReSharper disable once InconsistentNaming
// ReSharper disable once RedundantAssignment
ref bool __result)
{
// never attack
__result = false;
// skip the old logic
return false;
}
}
public void OnModUnload()
{
Debug.Log(string.Format("Mod {0} has been unloaded!", ModName));
m_harmony.UnpatchAll(HarmonyId);
}
}
public static class ReflectiveEnumerator
{
public static IEnumerable<Type> GetDerivedTypes<T>() where T : class
{
var assembly = Assembly.GetAssembly(typeof(T));
foreach (var type in assembly.GetTypes().Where(myType => myType.IsClass &&
myType.IsSubclassOf(typeof(T))))
{
yield return type;
}
}
}