Detect held rifle via CheckFireInput hook instead of scanning, cache lookups
This commit is contained in:
+75
-25
@@ -66,7 +66,8 @@ public class RifleScope : SonsMod
|
|||||||
private static string _configPath;
|
private static string _configPath;
|
||||||
|
|
||||||
private static RifleAnimatorController _rifle;
|
private static RifleAnimatorController _rifle;
|
||||||
private static float _nextSearch;
|
private static RifleAnimatorController _pendingRifle;
|
||||||
|
private static WeaponMod _suppressorMod;
|
||||||
private static float _hipFov;
|
private static float _hipFov;
|
||||||
private static float _vanillaAimFov = 35f;
|
private static float _vanillaAimFov = 35f;
|
||||||
private static float _targetAimFov;
|
private static float _targetAimFov;
|
||||||
@@ -94,6 +95,7 @@ public class RifleScope : SonsMod
|
|||||||
public RifleScope()
|
public RifleScope()
|
||||||
{
|
{
|
||||||
OnUpdateCallback = OnUpdate;
|
OnUpdateCallback = OnUpdate;
|
||||||
|
HarmonyPatchAll = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnSdkInitialized()
|
protected override void OnSdkInitialized()
|
||||||
@@ -121,13 +123,17 @@ public class RifleScope : SonsMod
|
|||||||
{
|
{
|
||||||
if (!_rifle || !_rifle.gameObject.activeInHierarchy)
|
if (!_rifle || !_rifle.gameObject.activeInHierarchy)
|
||||||
{
|
{
|
||||||
_rifle = null;
|
if (_rifle || _hiddenCam)
|
||||||
SetZoomed(false, null);
|
{
|
||||||
if (Time.unscaledTime < _nextSearch)
|
_rifle = null;
|
||||||
return;
|
SetZoomed(false, null);
|
||||||
_nextSearch = Time.unscaledTime + 0.5f;
|
}
|
||||||
if (!FindRifle())
|
|
||||||
|
var pending = _pendingRifle;
|
||||||
|
_pendingRifle = null;
|
||||||
|
if (!pending || !pending.gameObject.activeInHierarchy)
|
||||||
return;
|
return;
|
||||||
|
AdoptRifle(pending);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_enabled)
|
if (!_enabled)
|
||||||
@@ -147,34 +153,41 @@ public class RifleScope : SonsMod
|
|||||||
SetZoomed(zoomed, cam);
|
SetZoomed(zoomed, cam);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool FindRifle()
|
internal static void OnWeaponTick(RangedWeaponController controller)
|
||||||
{
|
{
|
||||||
foreach (var rifle in UnityEngine.Object.FindObjectsOfType<RifleAnimatorController>())
|
if (!controller || (_rifle && _rifle.Pointer == controller.Pointer))
|
||||||
|
return;
|
||||||
|
var rifle = controller.TryCast<RifleAnimatorController>();
|
||||||
|
if (rifle && rifle.IsLocalPlayer())
|
||||||
|
_pendingRifle = rifle;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AdoptRifle(RifleAnimatorController rifle)
|
||||||
|
{
|
||||||
|
_rifle = rifle;
|
||||||
|
_projectile = FindProjectileTransform(rifle);
|
||||||
|
_originalFireEvent ??= rifle._gunShotAudioEvent;
|
||||||
|
if (_enabled)
|
||||||
{
|
{
|
||||||
if (!rifle || !rifle.gameObject.activeInHierarchy || !rifle.IsLocalPlayer())
|
if (!_overlay)
|
||||||
continue;
|
CreateOverlay();
|
||||||
_rifle = rifle;
|
HideRedDot(rifle);
|
||||||
_projectile = FindProjectileTransform(rifle);
|
|
||||||
_originalFireEvent ??= rifle._gunShotAudioEvent;
|
|
||||||
if (_enabled)
|
|
||||||
{
|
|
||||||
if (!_overlay)
|
|
||||||
CreateOverlay();
|
|
||||||
HideRedDot(rifle);
|
|
||||||
}
|
|
||||||
if (_suppressor)
|
|
||||||
SetSuppressor(rifle, true);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
if (_suppressor)
|
||||||
|
SetSuppressor(rifle, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static WeaponMod FindSuppressorMod()
|
private static WeaponMod FindSuppressorMod()
|
||||||
{
|
{
|
||||||
|
if (_suppressorMod)
|
||||||
|
return _suppressorMod;
|
||||||
foreach (var mod in Resources.FindObjectsOfTypeAll<WeaponMod>())
|
foreach (var mod in Resources.FindObjectsOfTypeAll<WeaponMod>())
|
||||||
{
|
{
|
||||||
if (mod && mod.name == SuppressorModName)
|
if (mod && mod.name == SuppressorModName)
|
||||||
|
{
|
||||||
|
_suppressorMod = mod;
|
||||||
return mod;
|
return mod;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -223,8 +236,30 @@ public class RifleScope : SonsMod
|
|||||||
|
|
||||||
private static Type FindGameType(string name)
|
private static Type FindGameType(string name)
|
||||||
{
|
{
|
||||||
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||||
|
foreach (var asm in assemblies)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var t = asm.GetType(name, false) ?? asm.GetType("Il2Cpp." + name, false);
|
||||||
|
if (t != null)
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var asm in assemblies)
|
||||||
|
{
|
||||||
|
var asmName = asm.GetName().Name ?? string.Empty;
|
||||||
|
if (asm.IsDynamic || asmName.StartsWith("System", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
asmName.StartsWith("Unity", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
asmName.StartsWith("Il2Cppmscorlib", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
asmName.StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
asmName.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase))
|
||||||
|
continue;
|
||||||
|
|
||||||
Type[] types;
|
Type[] types;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -1105,4 +1140,19 @@ public class RifleScope : SonsMod
|
|||||||
RLog.Error($"RifleScope could not write config: {e.Message}");
|
RLog.Error($"RifleScope could not write config: {e.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HarmonyPatch(typeof(RangedWeaponController), nameof(RangedWeaponController.CheckFireInput))]
|
||||||
|
internal static class CheckFireInputPatch
|
||||||
|
{
|
||||||
|
private static void Postfix(RangedWeaponController __instance)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
global::RifleScope.RifleScope.OnWeaponTick(__instance);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user