Skip to content

Commit

Permalink
Add pings for all players in the small cyclops sonar HUD
Browse files Browse the repository at this point in the history
  • Loading branch information
tornac1234 committed Aug 12, 2024
1 parent 990dd36 commit c57adb1
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using HarmonyLib;
using NitroxPatcher.PatternMatching;
using NitroxTest.Patcher;

namespace NitroxPatcher.Patches.Dynamic;

[TestClass]
public class CyclopsSonarDisplay_NewEntityOnSonar_PatchTest
{
[TestMethod]
public void Sanity()
{
IEnumerable<CodeInstruction> originalIl = PatchTestHelper.GetInstructionsFromMethod(CyclopsSonarDisplay_NewEntityOnSonar_Patch.TARGET_METHOD);
IEnumerable<CodeInstruction> transformedIl = CyclopsSonarDisplay_NewEntityOnSonar_Patch.Transpiler(originalIl);
transformedIl.Count().Should().Be(originalIl.Count() + 3);
Console.WriteLine(transformedIl.ToPrettyString());
}
}
8 changes: 8 additions & 0 deletions NitroxClient/GameLogic/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NitroxModel.GameLogic.FMOD;
using NitroxModel.Helper;
using NitroxModel.MultiplayerSession;
using UnityEngine;

namespace NitroxClient.GameLogic;

Expand Down Expand Up @@ -47,6 +48,13 @@ internal IEnumerable<RemotePlayer> GetAll()
return playersById.Values;
}

public HashSet<GameObject> GetAllPlayerObjects()
{
HashSet<GameObject> remotePlayerObjects = GetAll().Select(player => player.Body).ToSet();
remotePlayerObjects.Add(Player.mainObject);
return remotePlayerObjects;
}

public RemotePlayer Create(PlayerContext playerContext)
{
Validate.NotNull(playerContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;
using NitroxClient.GameLogic;
using NitroxModel.Helper;

namespace NitroxPatcher.Patches.Persistent;

public sealed partial class CyclopsSonarCreatureDetector_CheckForCreaturesInRange_Patch : NitroxPatch, IPersistentPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((CyclopsSonarCreatureDetector t) => t.CheckForCreaturesInRange());

public const CyclopsSonarDisplay.EntityType PLAYER_TYPE = (CyclopsSonarDisplay.EntityType)2;

public static void Postfix(CyclopsSonarCreatureDetector __instance)
{
__instance.ChekItemsOnHashSet(Resolve<PlayerManager>().GetAllPlayerObjects(), PLAYER_TYPE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using NitroxClient.GameLogic;
using NitroxClient.GameLogic.PlayerLogic;
using NitroxModel.Helper;
using NitroxModel_Subnautica.DataStructures;
using NitroxPatcher.Patches.Persistent;
using UnityEngine;

namespace NitroxPatcher.Patches.Dynamic;

public sealed partial class CyclopsSonarDisplay_NewEntityOnSonar_Patch : NitroxPatch, IPersistentPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((CyclopsSonarDisplay t) => t.NewEntityOnSonar(default));

/*
* }
* this.entitysOnSonar.Add(entityPing2);
* SetupPing(component, entityData); <----- INSERTED LINE
*/
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
return new CodeMatcher(instructions).End()
.InsertAndAdvance(TARGET_METHOD.Ldloc<CyclopsHUDSonarPing>())
.InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_1))
.InsertAndAdvance(new CodeInstruction(OpCodes.Call, Reflect.Method(() => SetupPing(default, default))))
.InstructionEnumeration();
}

public static void SetupPing(CyclopsHUDSonarPing ping, CyclopsSonarCreatureDetector.EntityData entityData)
{
if (entityData.entityType != CyclopsSonarCreatureDetector_CheckForCreaturesInRange_Patch.PLAYER_TYPE)
{
return;
}

Color color;
if (entityData.gameObject == Player.mainObject)
{
color = Resolve<LocalPlayer>().PlayerSettings.PlayerColor.ToUnity();
}
else if (entityData.gameObject.TryGetComponent(out RemotePlayerIdentifier remotePlayerIdentifier))
{
color = remotePlayerIdentifier.RemotePlayer.PlayerSettings.PlayerColor.ToUnity();
}
else
{
return;
}

CyclopsHUDSonarPing sonarPing = ping.GetComponent<CyclopsHUDSonarPing>();
// Set isCreaturePing to true so that CyclopsHUDSonarPing.Start runs the SetColor code
sonarPing.isCreaturePing = true;
sonarPing.passiveColor = color;
sonarPing.Start();
sonarPing.isCreaturePing = false;

// We remove the pulse to be able to differentiate those signals from the creatures and decoy ones
GameObject.Destroy(sonarPing.transform.Find("Ping/PingPulse").gameObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Reflection;
using NitroxClient.MonoBehaviours;
using NitroxModel.Helper;
using UnityEngine;

namespace NitroxPatcher.Patches.Persistent;

public sealed partial class Application_targetFrameRate_Patch : NitroxPatch, IPersistentPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Property(() => Application.targetFrameRate).GetSetMethod();

public static void Postfix()
{
MovementReplicator.UpdateInterpolationTime();
}
}

0 comments on commit c57adb1

Please sign in to comment.