-
Notifications
You must be signed in to change notification settings - Fork 1
/
Battery_Patch.cs
189 lines (170 loc) · 6.47 KB
/
Battery_Patch.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using BepInEx;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using static ErrorMessage;
namespace Tweaks_Fixes
{
class Battery_Patch
{
public static HashSet<PowerRelay> subPowerRelays = new HashSet<PowerRelay>();
static EnergyMixin playerToolEM;
static EnergyInterface propCannonEI;
static Dictionary<string, float> defaultBatteryCharge = new Dictionary<string, float>();
public static HashSet<TechType> notRechargableBatteries = new HashSet<TechType>();
[HarmonyPatch(typeof(EnergyMixin), "ConsumeEnergy")]
class EnergyMixin_OnAfterDeserialize_Patch
{
static void Prefix(EnergyMixin __instance, ref float amount)
{
if (playerToolEM == __instance)
{
//AddDebug("tool Consume Energy");
amount *= ConfigMenu.toolEnergyConsMult.Value;
}
}
}
[HarmonyPatch(typeof(PlayerTool), "OnDraw")]
class PlayerTool_OnDraw_Patch
{
static void Postfix(PlayerTool __instance)
{
//AddDebug("PlayerTool OnDraw ");
playerToolEM = __instance.energyMixin;
}
}
[HarmonyPatch(typeof(PropulsionCannonWeapon), "OnDraw")]
class PropulsionCannonWeapon_OnDraw_Patch
{
static void Postfix(PropulsionCannonWeapon __instance)
{
propCannonEI = __instance.propulsionCannon.energyInterface;
}
}
[HarmonyPatch(typeof(EnergyInterface), "ConsumeEnergy")]
class EnergyInterface_ConsumeEnergy_Patch
{
static void Prefix(EnergyInterface __instance, ref float amount)
{
if (propCannonEI == __instance)
{
//AddDebug(" propCannon ConsumeEnergy");
amount *= ConfigMenu.toolEnergyConsMult.Value;
}
}
}
[HarmonyPatch(typeof(Vehicle), "ConsumeEngineEnergy")]
class Vehicle_ConsumeEngineEnergy_Patch
{
static void Prefix(Vehicle __instance, ref float energyCost)
{
//AddDebug("Vehicle Consume Energy");
energyCost *= ConfigMenu.vehicleEnergyConsMult.Value;
}
}
[HarmonyPatch(typeof(SubControl), "Start")]
class PowerRelay_Start_Patch
{
static void Postfix(SubControl __instance)
{
if (__instance.powerRelay)
{
subPowerRelays.Add(__instance.powerRelay);
}
}
}
[HarmonyPatch(typeof(PowerSystem), "ConsumeEnergy")]
class PowerSystem_ConsumeEnergy_Patch
{
static void Prefix(ref float amount, IPowerInterface powerInterface)
{
if (ConfigMenu.vehicleEnergyConsMult.Value == 1f)
return;
PowerRelay pr = powerInterface as PowerRelay;
if (pr && subPowerRelays.Contains(pr))
{
//AddDebug("Sub Consume Energy ");
amount *= ConfigMenu.vehicleEnergyConsMult.Value;
}
else
{
//AddDebug("base Consume Energy ");
amount *= ConfigMenu.baseEnergyConsMult.Value;
}
}
}
[HarmonyPatch(typeof(Battery))]
class Battery_Patch_
{
[HarmonyPostfix] // runs for new batteries too
[HarmonyPatch("OnAfterDeserialize")]
static void OnAfterDeserializePostfix(Battery __instance)
{
if (ConfigMenu.batteryChargeMult.Value == 1f || __instance.name.IsNullOrWhiteSpace())
return;
//AddDebug(__instance.name + " Battery OnAfterDeserialize " + __instance._capacity);
if (!defaultBatteryCharge.ContainsKey(__instance.name))
{
defaultBatteryCharge[__instance.name] = __instance._capacity;
}
if (defaultBatteryCharge.ContainsKey(__instance.name))
{
__instance._capacity = defaultBatteryCharge[__instance.name] * ConfigMenu.batteryChargeMult.Value;
if (__instance.charge > __instance._capacity)
__instance.charge = __instance._capacity;
}
}
}
[HarmonyPatch(typeof(Charger), "Start")]
class Charger_Start_Patch
{
static void Postfix(Charger __instance)
{
//AddDebug(__instance.name + " Charger Start");
foreach (TechType tt in notRechargableBatteries)
{
if (__instance.allowedTech.Contains(tt))
{
__instance.allowedTech.Remove(tt);
//AddDebug("remove " + tt + " from " + __instance.name);
}
}
//Main.logger.LogMessage(__instance.name + " Charger Start");
//foreach (var tt in __instance.allowedTech)
// Main.logger.LogMessage(__instance.name + " allowedTech " + tt);
}
}
//[HarmonyPatch(typeof(Charger), "IsAllowedToAdd")]
class Charger_IsAllowedToAdd_Patch
{
static bool Prefix(Charger __instance, Pickupable pickupable, ref bool __result)
{
if (pickupable == null)
{
__result = false;
return false;
}
TechType tt = pickupable.GetTechType();
//string name = t.AsString();
//TechTypeExtensions.FromString(name, out TechType tt, true);
//TechType techType = pickupable.GetTechType();
if (tt == TechType.None)
{
__result = false;
return false;
}
if (notRechargableBatteries.Contains(tt))
{
AddDebug("nonRechargeable " + tt);
__result = false;
return false;
}
if (__instance.allowedTech != null && __instance.allowedTech.Contains(tt))
__result = true;
return false;
}
}
}
}