Skip to content

Commit 3f4b076

Browse files
committed
make ModTek compatible, suss apart some bits into their own classes, add logging, add setting the color from mod.json istead of hardcoding
1 parent 6c31a87 commit 3f4b076

File tree

8 files changed

+401
-271
lines changed

8 files changed

+401
-271
lines changed

BTMLColorLOSMod/BTMLColorLOSMod.cs

Lines changed: 13 additions & 268 deletions
Original file line numberDiff line numberDiff line change
@@ -1,284 +1,29 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Reflection;
43
using Harmony;
5-
using BattleTech;
6-
using BattleTech.UI;
7-
using UnityEngine;
4+
using Newtonsoft.Json;
85

96
namespace BTMLColorLOSMod
107
{
11-
[HarmonyPatch(typeof(BattleTech.UI.WeaponRangeIndicators), "DrawLine")]
12-
public static class WeaponRangeIndicators_DrawLine_Patch
8+
public static class BTMLColorLOSMod
139
{
14-
static bool Prefix(
15-
ref Vector3 position,
16-
ref Quaternion rotation,
17-
ref bool isPositionLocked,
18-
ref AbstractActor selectedActor,
19-
ref ICombatant target,
20-
ref bool usingMultifire,
21-
ref bool isLocked,
22-
ref bool isMelee,
23-
WeaponRangeIndicators __instance)
10+
internal static Settings ModSettings = new Settings();
11+
internal static string ModDirectory;
12+
public static void Init(string directory, string settingsJSON)
2413
{
25-
CombatHUD HUD = (CombatHUD) ReflectionHelper.GetPrivateProperty(__instance, "HUD");
26-
if (__instance.DEBUG_showLOSLines)
27-
{
28-
DEBUG_LOSLineDrawer debugDrawer =
29-
(DEBUG_LOSLineDrawer) ReflectionHelper.InvokePrivateMethode(__instance, "GetDebugDrawer",
30-
new object[] { });
31-
debugDrawer.DrawLines(selectedActor, HUD.SelectionHandler.ActiveState, target);
32-
}
14+
var harmony = HarmonyInstance.Create("com.joelmeador.BTMLColorLOSMod");
15+
harmony.PatchAll(Assembly.GetExecutingAssembly());
3316

34-
LineRenderer line =
35-
(LineRenderer) ReflectionHelper.InvokePrivateMethode(__instance, "getLine", new object[] { });
36-
Vector3 vector = Vector3.Lerp(position, position + selectedActor.HighestLOSPosition,
37-
__instance.sourceLaserDestRatio);
38-
Vector3 vector2 = Vector3.Lerp(target.CurrentPosition, target.TargetPosition,
39-
__instance.targetLaserDestRatio);
40-
AbstractActor abstractActor = target as AbstractActor;
41-
// melee
42-
if (isMelee)
17+
ModDirectory = directory;
18+
try
4319
{
44-
line.startWidth = __instance.LOSWidthBegin;
45-
line.endWidth = __instance.LOSWidthEnd;
46-
line.material = __instance.MaterialInRange;
47-
line.startColor = __instance.LOSLockedTarget;
48-
line.endColor = __instance.LOSLockedTarget;
49-
line.positionCount = 2;
50-
line.SetPosition(0, vector);
51-
Vector3 vector3 = vector - vector2;
52-
vector3.Normalize();
53-
vector3 *= __instance.LineEndOffset;
54-
vector2 += vector3;
55-
line.SetPosition(1, vector2);
56-
ReflectionHelper.InvokePrivateMethode(__instance, "SetEnemyTargetable", new object[] {target, true});
57-
List<AbstractActor> allActors = selectedActor.Combat.AllActors;
58-
allActors.Remove(selectedActor);
59-
allActors.Remove(abstractActor);
60-
PathNode pathNode = default(PathNode);
61-
Vector3 attackPosition = default(Vector3);
62-
float num = default(float);
63-
selectedActor.Pathing.GetMeleeDestination(abstractActor, allActors, out pathNode, out attackPosition,
64-
out num);
65-
HUD.InWorldMgr.ShowAttackDirection(HUD.SelectedActor, abstractActor,
66-
HUD.Combat.HitLocation.GetAttackDirection(attackPosition, target), vector2.y,
67-
MeleeAttackType.Punch, 0);
20+
ModSettings = JsonConvert.DeserializeObject<Settings>(settingsJSON);
6821
}
69-
// not melee
70-
else
22+
catch (Exception ex)
7123
{
72-
FiringPreviewManager.PreviewInfo previewInfo =
73-
HUD.SelectionHandler.ActiveState.FiringPreview.GetPreviewInfo(target);
74-
if (previewInfo.availability == FiringPreviewManager.TargetAvailability.NotSet)
75-
{
76-
Debug.LogError("Error - trying to draw line with no FiringPreviewManager availability!");
77-
}
78-
// why is the bad case first. just dump out of the fucking method, doug
79-
else
80-
{
81-
bool flag = HUD.SelectionHandler.ActiveState.SelectionType != SelectionType.Sprint ||
82-
HUD.SelectedActor.CanShootAfterSprinting;
83-
bool flag2 = !isPositionLocked &&
84-
previewInfo.availability != FiringPreviewManager.TargetAvailability.BeyondMaxRange &&
85-
previewInfo.availability != FiringPreviewManager.TargetAvailability.BeyondRotation;
86-
if (flag && (previewInfo.IsCurrentlyAvailable || flag2))
87-
{
88-
// multiple targets, even if only one selected
89-
if (usingMultifire)
90-
{
91-
if (target == HUD.SelectedTarget)
92-
{
93-
LineRenderer lineRenderer = line;
94-
Color color2 = lineRenderer.startColor =
95-
(line.endColor = __instance.LOSMultiTargetKBSelection);
96-
}
97-
else if (isLocked)
98-
{
99-
LineRenderer lineRenderer2 = line;
100-
Color color2 =
101-
lineRenderer2.startColor = (line.endColor = __instance.LOSLockedTarget);
102-
}
103-
else
104-
{
105-
LineRenderer lineRenderer3 = line;
106-
Color color2 =
107-
lineRenderer3.startColor = (line.endColor = __instance.LOSUnlockedTarget);
108-
}
109-
}
110-
// normal shot
111-
else
112-
{
113-
float shotQuality = (float) ReflectionHelper.InvokePrivateMethode(__instance,
114-
"GetShotQuality", new object[] {selectedActor, position, rotation, target});
115-
Color color5 = Color.Lerp(Color.clear, __instance.LOSInRange, shotQuality);
116-
LineRenderer lineRenderer4 = line;
117-
Color color2 = lineRenderer4.startColor = (line.endColor = color5);
118-
}
119-
120-
line.material = __instance.MaterialInRange;
121-
// straight line shot
122-
if (previewInfo.HasLOF)
123-
{
124-
line.positionCount = 2;
125-
line.SetPosition(0, vector);
126-
Vector3 vector4 = vector - vector2;
127-
vector4.Normalize();
128-
vector4 *= __instance.LineEndOffset;
129-
vector2 += vector4;
130-
if (previewInfo.LOFLevel == LineOfFireLevel.LOFClear)
131-
{
132-
if (target == HUD.SelectionHandler.ActiveState.FacingEnemy)
133-
{
134-
line.startWidth =
135-
__instance.LOSWidthBegin * __instance.LOSWidthFacingTargetMultiplier;
136-
line.endWidth = __instance.LOSWidthEnd * __instance.LOSWidthFacingTargetMultiplier;
137-
}
138-
else
139-
{
140-
line.startWidth = __instance.LOSWidthBegin;
141-
line.endWidth = __instance.LOSWidthEnd;
142-
}
143-
144-
line.SetPosition(1, vector2);
145-
}
146-
else
147-
{
148-
if (target == HUD.SelectionHandler.ActiveState.FacingEnemy)
149-
{
150-
line.startWidth =
151-
__instance.LOSWidthBegin * __instance.LOSWidthFacingTargetMultiplier;
152-
line.endWidth =
153-
__instance.LOSWidthBegin * __instance.LOSWidthFacingTargetMultiplier;
154-
}
155-
else
156-
{
157-
line.startWidth = __instance.LOSWidthBegin;
158-
line.endWidth = __instance.LOSWidthBegin;
159-
}
160-
161-
Vector3 collisionPoint = previewInfo.collisionPoint;
162-
collisionPoint = Vector3.Project(collisionPoint - vector, vector2 - vector) + vector;
163-
line.SetPosition(1, collisionPoint);
164-
LineRenderer line2 =
165-
(LineRenderer) ReflectionHelper.InvokePrivateMethode(__instance, "getLine",
166-
new object[] { });
167-
line2.positionCount = 2;
168-
line2.startWidth = __instance.LOSWidthBlocked;
169-
line2.endWidth = __instance.LOSWidthBlocked;
170-
line2.material = __instance.MaterialInRange;
171-
LineRenderer lineRenderer5 = line2;
172-
Color color2 = lineRenderer5.startColor = (line2.endColor = __instance.LOSBlocked);
173-
line2.SetPosition(0, collisionPoint);
174-
line2.SetPosition(1, vector2);
175-
GameObject coverIcon =
176-
(GameObject) ReflectionHelper.InvokePrivateMethode(__instance, "getCoverIcon",
177-
new object[] { });
178-
if (!coverIcon.activeSelf)
179-
{
180-
coverIcon.SetActive(true);
181-
}
182-
183-
coverIcon.transform.position = collisionPoint;
184-
}
185-
}
186-
// arc shot
187-
else
188-
{
189-
// other than formatting this is the only thing that changed from the decompiled code
190-
Vector3[] pointsForArc = WeaponRangeIndicators.GetPointsForArc(18, 30f, vector, vector2);
191-
float shotQuality = (float) ReflectionHelper.InvokePrivateMethode(__instance,
192-
"GetShotQuality", new object[] {selectedActor, position, rotation, target});
193-
// currently this doesn't actually show up cyan, because there is a shader on
194-
// these lines that seems to strip out all color data except the red, so this just makes
195-
// a black line
196-
Color color6 = Color.Lerp(Color.clear, Color.cyan, shotQuality);
197-
line.endColor = line.startColor = color6;
198-
line.positionCount = 18;
199-
line.SetPositions(pointsForArc);
200-
}
201-
202-
ReflectionHelper.InvokePrivateMethode(__instance, "SetEnemyTargetable",
203-
new object[] {target, true});
204-
if (abstractActor != null)
205-
{
206-
HUD.InWorldMgr.ShowAttackDirection(HUD.SelectedActor, abstractActor,
207-
HUD.Combat.HitLocation.GetAttackDirection(position, target), vector2.y,
208-
MeleeAttackType.NotSet, HUD.InWorldMgr.NumWeaponsTargeting(target));
209-
}
210-
}
211-
// sprinted and can't shoot or out of rotation/weapon range
212-
else
213-
{
214-
line.positionCount = 2;
215-
line.SetPosition(0, vector);
216-
line.SetPosition(1, vector2);
217-
LineRenderer lineRenderer6 = line;
218-
Color color2 = lineRenderer6.startColor = (line.endColor = __instance.LOSOutOfRange);
219-
line.material = __instance.MaterialOutOfRange;
220-
ReflectionHelper.InvokePrivateMethode(__instance, "SetEnemyTargetable",
221-
new object[] {target, false});
222-
}
223-
}
24+
Logger.LogError(ex);
25+
ModSettings = new Settings();
22426
}
225-
226-
return false;
227-
}
228-
}
229-
230-
public static class ColorLOSMod
231-
{
232-
public static void Init()
233-
{
234-
var harmony = HarmonyInstance.Create("com.joelmeador.ColorLOS");
235-
harmony.PatchAll(Assembly.GetExecutingAssembly());
236-
}
237-
}
238-
239-
public static class ReflectionHelper
240-
{
241-
public static object InvokePrivateMethode(object instance, string methodname, object[] parameters)
242-
{
243-
Type type = instance.GetType();
244-
MethodInfo methodInfo = type.GetMethod(methodname, BindingFlags.NonPublic | BindingFlags.Instance);
245-
return methodInfo.Invoke(instance, parameters);
246-
}
247-
248-
public static object InvokePrivateMethode(object instance, string methodname, object[] parameters, Type[] types)
249-
{
250-
Type type = instance.GetType();
251-
MethodInfo methodInfo = type.GetMethod(methodname, BindingFlags.NonPublic | BindingFlags.Instance, null,
252-
types, null);
253-
return methodInfo.Invoke(instance, parameters);
254-
}
255-
256-
public static void SetPrivateProperty(object instance, string propertyname, object value)
257-
{
258-
Type type = instance.GetType();
259-
PropertyInfo property = type.GetProperty(propertyname, BindingFlags.NonPublic | BindingFlags.Instance);
260-
property.SetValue(instance, value, null);
261-
}
262-
263-
public static object GetPrivateProperty(object instance, string propertyname)
264-
{
265-
Type type = instance.GetType();
266-
PropertyInfo property = type.GetProperty(propertyname, BindingFlags.NonPublic | BindingFlags.Instance);
267-
return property.GetValue(instance, new object[] { });
268-
}
269-
270-
public static void SetPrivateField(object instance, string fieldname, object value)
271-
{
272-
Type type = instance.GetType();
273-
FieldInfo field = type.GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Instance);
274-
field.SetValue(instance, value);
275-
}
276-
277-
public static object GetPrivateField(object instance, string fieldname)
278-
{
279-
Type type = instance.GetType();
280-
FieldInfo field = type.GetField(fieldname, BindingFlags.NonPublic | BindingFlags.Instance);
281-
return field.GetValue(instance);
28227
}
28328
}
28429
}

BTMLColorLOSMod/BTMLColorLOSMod.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<Reference Include="0Harmony, Version=1.0.9.1, Culture=neutral, PublicKeyToken=null">
3434
<HintPath>bin\Debug\0Harmony.dll</HintPath>
3535
</Reference>
36+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
37+
<HintPath>..\..\..\..\Library\Application Support\Steam\steamapps\common\BATTLETECH\BattleTech.app\Contents\Resources\Data\Managed\Newtonsoft.Json.dll</HintPath>
38+
</Reference>
3639
<Reference Include="System" />
3740
<Reference Include="System.Core" />
3841
<Reference Include="System.Xml.Linq" />
@@ -43,7 +46,7 @@
4346
<HintPath>..\..\..\..\Downloads\BattleTechModLoader-v0.2.0\0Harmony.dll</HintPath>
4447
</Reference>
4548
<Reference Include="Assembly-CSharp">
46-
<HintPath>..\..\..\..\Library\Application Support\Steam\steamapps\common\BATTLETECH\BattleTech.app\Contents\Resources\Data\Managed\Assembly-CSharp.dll</HintPath>
49+
<HintPath>../../../../Library/Application Support/Steam/steamapps/common/BATTLETECH/BattleTech.app/Contents/Resources/Data/Managed/Assembly-CSharp.dll</HintPath>
4750
</Reference>
4851
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
4952
<HintPath>..\..\..\..\Library\Application Support\Steam\steamapps\common\BATTLETECH\BattleTech.app\Contents\Resources\Data\Managed\UnityEngine.dll</HintPath>
@@ -54,7 +57,14 @@
5457
</ItemGroup>
5558
<ItemGroup>
5659
<Compile Include="BTMLColorLOSMod.cs" />
60+
<Compile Include="Logger.cs" />
5761
<Compile Include="Properties\AssemblyInfo.cs" />
62+
<Compile Include="ReflectionHelper.cs" />
63+
<Compile Include="Settings.cs" />
64+
<Compile Include="WeaponRangeIndicators_DrawLine_Patch.cs" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<Content Include="mod.json" />
5868
</ItemGroup>
5969
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6070
</Project>

BTMLColorLOSMod/Logger.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace BTMLColorLOSMod
5+
{
6+
public class Logger
7+
{
8+
public static void LogError(Exception ex)
9+
{
10+
var filePath = $"{BTMLColorLOSMod.ModDirectory}/BTMLColorLOSMod.log";
11+
using (var writer = new StreamWriter(filePath, true))
12+
{
13+
writer.WriteLine($"Message: {ex.Message}");
14+
writer.WriteLine($"StackTrace: {ex.StackTrace}");
15+
WriteLogFooter(writer);
16+
}
17+
}
18+
19+
public static void LogLine(String line)
20+
{
21+
var filePath = $"{BTMLColorLOSMod.ModDirectory}/BTMLColorLOSMod.log";
22+
using (var writer = new StreamWriter(filePath, true))
23+
{
24+
writer.WriteLine(line);
25+
WriteLogFooter(writer);
26+
}
27+
}
28+
29+
private static void WriteLogFooter(StreamWriter writer)
30+
{
31+
writer.WriteLine($"Date: {DateTime.Now}");
32+
writer.WriteLine();
33+
writer.WriteLine(new string(c: '-', count: 50));
34+
writer.WriteLine();
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)