-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i feel like my iq is going down
- Loading branch information
Showing
2 changed files
with
131 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,14 @@ | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using Immersion.Trackers; | ||
|
||
namespace Immersion.Patches; | ||
|
||
[HarmonyPatch] | ||
public static class LowOxygenAlertPatches | ||
{ | ||
public static bool PatchSucceeded { get; private set; } | ||
|
||
[HarmonyPatch(typeof(LowOxygenAlert), nameof(LowOxygenAlert.Update))] | ||
[HarmonyTranspiler] | ||
private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
CodeMatcher matcher = new(instructions); | ||
|
||
FieldInfo notifField = typeof(LowOxygenAlert.Alert).GetField(nameof(LowOxygenAlert.Alert.notification)); | ||
MethodInfo playMethod = typeof(PDANotification).GetMethod(nameof(PDANotification.Play), []); | ||
matcher.MatchForward(false, | ||
new CodeMatch(OpCodes.Ldloc_2), | ||
new CodeMatch(OpCodes.Ldfld, notifField), | ||
new CodeMatch(OpCodes.Callvirt, playMethod) | ||
); | ||
if (!matcher.IsValid) | ||
{ | ||
LOGGER.LogWarning($"Could not patch {nameof(LowOxygenAlert)}.{nameof(LowOxygenAlert.Update)}, {nameof(OxygenAlerts)} will not function"); | ||
return instructions; | ||
} | ||
matcher.Advance(1); | ||
matcher.RemoveInstructions(2); | ||
matcher.Insert( | ||
CodeInstruction.Call((LowOxygenAlert.Alert alert) => PlayAlert(alert)) | ||
); | ||
|
||
PatchSucceeded = true; | ||
LOGGER.LogDebug($"Oxygen alerts patched for {nameof(OxygenAlerts)}"); | ||
return matcher.InstructionEnumeration(); | ||
} | ||
|
||
[HarmonyPatch(typeof(HintSwimToSurface), nameof(HintSwimToSurface.ShouldShowWarning))] | ||
[HarmonyPostfix] | ||
private static void DisableHint(ref bool __result) | ||
{ | ||
__result &= !(OxygenAlerts.Instance && OxygenAlerts.Instance.enabled); | ||
} | ||
|
||
private static void PlayAlert(LowOxygenAlert.Alert alert) | ||
{ | ||
if (alert is OxygenAlerts.Alert ourAlert) | ||
ourAlert.Play(); | ||
else | ||
alert.notification.Play(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters