11using System ;
2- using System . Collections . Generic ;
32using System . Reflection ;
43using Harmony ;
5- using BattleTech ;
6- using BattleTech . UI ;
7- using UnityEngine ;
4+ using Newtonsoft . Json ;
85
96namespace 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}
0 commit comments