-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.cs
265 lines (239 loc) · 9.65 KB
/
Core.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BattleTech;
using BattleTech.Rendering;
using Harmony;
using Newtonsoft.Json;
using UnityEngine;
using static BetterHeadlights.Core;
// ReSharper disable UnusedMember.Global
// ReSharper disable InconsistentNaming
namespace BetterHeadlights
{
public static class Core
{
internal static Settings settings;
internal static bool headlightsOn = true;
internal static Dictionary<string, List<Transform>> lightTracker =
new Dictionary<string, List<Transform>>();
internal static readonly Dictionary<string, float> IntensityMap = new Dictionary<string, float>
{
{"LOW", 200_000f},
{"MID", 400_000f},
{"HIGH", 800_000f},
{"SUPER", 2_000_000f},
};
public static void Init(string modSettings)
{
// read settings
try
{
settings = JsonConvert.DeserializeObject<Settings>(modSettings);
}
catch (Exception)
{
settings = new Settings();
}
Log($"Starting up {DateTime.Now.ToShortTimeString()}");
var harmony = HarmonyInstance.Create("ca.gnivler.BattleTech.BetterHeadlights");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
internal static void Log(object input)
{
//FileLog.Log($"[BetterHeadlights] {input ?? "null"}");
}
}
// adjust headlight settings
[HarmonyPatch(typeof(LightSpawner), "SpawnLight")]
public class LightSpawner_SpawnLight_Patch
{
private const float extraRadius = 10_000f;
// target spot lights only and configure them per the settings
public static void Postfix(LightSpawner __instance, BTLight ___spawnedLight)
{
if (__instance.type == LightSpawner.LightTypes.point)
{
return;
}
Log($"adjusting: {__instance.name} ({__instance.transform.parent.name})");
if (settings.ExtraRange)
{
___spawnedLight.radius = extraRadius;
}
if (settings.Intensity != "VANILLA" &&
IntensityMap.ContainsKey(settings.Intensity))
{
if (__instance.name.StartsWith("light"))
{
// magic numbers.. sorry. adjusted to visuals
___spawnedLight.intensity = IntensityMap[settings.Intensity] / 5f;
___spawnedLight.spotlightAngleOuter = settings.Angle * 1.20f;
}
else
{
___spawnedLight.intensity = IntensityMap[settings.Intensity];
___spawnedLight.spotlightAngleOuter = settings.Angle;
}
}
}
}
// toggle headlights - hotkey hook
[HarmonyPatch(typeof(CombatGameState), "Update")]
public static class CombatGameState_Update_Patch
{
public static void Postfix()
{
var hotkeyH = (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) &&
(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) &&
Input.GetKeyDown(KeyCode.H);
if (hotkeyH)
{
Log("Toggling headlights");
headlightsOn = !headlightsOn;
}
}
}
[HarmonyPatch(typeof(VehicleRepresentation), "Update")]
public static class VehicleRepresentation_Update_Patch
{
public static void Postfix(VehicleRepresentation __instance)
{
try
{
if (!settings.BlipLights)
{
return;
}
// only want to patch blips
var localPlayerTeam = UnityGameInstance.BattleTechGame.Combat.LocalPlayerTeam;
var visibilityLevel = localPlayerTeam.VisibilityToTarget(__instance.parentActor);
if (visibilityLevel == VisibilityLevel.None)
{
return;
}
Transform transform;
// there is one parent transform which is not active
// only try to find the transform if not memoized
var vehicleGUID = __instance.parentVehicle.GUID;
if (!lightTracker.ContainsKey(vehicleGUID))
{
Log($"Adding vehicle {__instance.parentVehicle.Nickname} with {__instance.VisibleLights.Length} lights");
foreach (var light in __instance.VisibleLights)
{
transform = light.GetComponentsInParent<Transform>(true)
.FirstOrDefault(x => !x.gameObject.activeSelf);
if (transform != null)
{
// add single inactive transform
lightTracker.Add(__instance.parentVehicle.GUID, new List<Transform> {transform});
break;
}
}
// it doesn't have any inactive transforms so we don't care about it really
// missiles seem to run this method. add it so it doesn't check every frame
if (!lightTracker.ContainsKey(vehicleGUID))
{
lightTracker.Add(__instance.parentVehicle.GUID, new List<Transform> {__instance.transform.parent});
}
}
// grab the inactive transform, activate it but disable the mesh
transform = lightTracker[__instance.parentVehicle.GUID].FirstOrDefault();
if (transform != null)
{
transform.gameObject.SetActive(true);
var mesh = transform.GetComponentInChildren<SkinnedMeshRenderer>();
foreach (var child in transform.GetComponentsInChildren<Component>())
{
if (child is SkinnedMeshRenderer)
{
mesh.enabled = visibilityLevel == VisibilityLevel.LOSFull;
}
if (child is BTLight btLight)
{
btLight.enabled = visibilityLevel >= VisibilityLevel.Blip0Minimum;
}
}
}
}
catch (NullReferenceException)
{
// do nothing (harmless NREs at game load)
}
catch (Exception ex)
{
Log(ex);
}
}
}
// toggle headlights - effect
[HarmonyPatch(typeof(MechRepresentation), "Update")]
public static class MechRepresentation_Update_Patch
{
public static void Postfix(MechRepresentation __instance)
{
try
{
if (!settings.BlipLights)
{
return;
}
// memoize all mech lights (should capture new spawns too)
if (!lightTracker.ContainsKey(__instance.parentMech.GUID))
{
var transforms = __instance.GetComponentsInChildren<Transform>(true)
.Where(x => x.name.Contains("headlight")).ToList();
Log($"Adding mech {__instance.parentMech.MechDef.Name} with {transforms.Count} lights");
lightTracker.Add(__instance.parentMech.GUID, transforms);
}
// Update() runs over by several frames after loading/restarting a mission
// so hooking separately those is problematic because it repopulates with bad data
// have to deal with it inline
var lights = lightTracker[__instance.parentMech.GUID].Where(x => x != null).ToList();
if (lights.Count == 0)
{
Log(new string('>', 100) + " Invalid mechs in dictionary, clearing");
lightTracker.Clear();
headlightsOn = true;
return;
}
// player controlled lights
if (__instance.pilotRep.pilot.Team.LocalPlayerControlsTeam)
{
// Where clause should return Count 0 if the lights are already set, skipping SetActive()
foreach (var light in lights.Where(x => x.gameObject.activeSelf != headlightsOn))
{
light.gameObject.SetActive(headlightsOn);
}
}
try
{
var localPlayerTeam = UnityGameInstance.BattleTechGame.Combat.LocalPlayerTeam;
var visibilityLevel = localPlayerTeam.VisibilityToTarget(__instance.parentActor);
if (visibilityLevel == VisibilityLevel.None || visibilityLevel == VisibilityLevel.LOSFull)
{
return;
}
// enemy mech is a blip, lights on
lights.Do(light => light.gameObject.SetActive(true));
}
catch (NullReferenceException)
{
// do nothing (harmless NREs at load)
}
}
catch (Exception ex)
{
Log(ex);
}
}
}
}
public class Settings
{
public bool ExtraRange = true;
public string Intensity = "MID";
public float Angle = 45;
public bool BlipLights = true;
}