forked from SubnauticaNitrox/Nitrox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c48110
commit aae2e47
Showing
11 changed files
with
210 additions
and
17 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Nitrox.Test/Patcher/Patches/Dynamic/Flare_Update_PatchTest.cs
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using HarmonyLib; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NitroxTest.Patcher; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
[TestClass] | ||
public class Flare_Update_PatchTest | ||
{ | ||
[TestMethod] | ||
public void Sanity() | ||
{ | ||
IEnumerable<CodeInstruction> originalIl = PatchTestHelper.GetInstructionsFromMethod(Flare_Update_Patch.TARGET_METHOD); | ||
IEnumerable<CodeInstruction> transformedIl = Flare_Update_Patch.Transpiler(originalIl); | ||
transformedIl.Count().Should().Be(originalIl.Count()); | ||
} | ||
} |
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
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
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
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
23 changes: 23 additions & 0 deletions
23
NitroxClient/GameLogic/Spawning/Metadata/Extractor/FlareMetadataExtractor.cs
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
|
||
namespace NitroxClient.GameLogic.Spawning.Metadata.Extractor; | ||
|
||
public class FlareMetadataExtractor : GenericEntityMetadataExtractor<Flare, FlareMetadata> | ||
{ | ||
private readonly Items items; | ||
|
||
public FlareMetadataExtractor(Items items) | ||
{ | ||
this.items = items; | ||
} | ||
|
||
public override FlareMetadata Extract(Flare flare) | ||
{ | ||
// If the flare is being picked up, its metadata must be set accordingly | ||
if (flare.flareActiveState && items.PickingUpObject != flare.gameObject) | ||
{ | ||
return new(flare.energyLeft, flare.hasBeenThrown, flare.flareActivateTime); | ||
} | ||
return new(flare.energyLeft, flare.hasBeenThrown, null); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
NitroxClient/GameLogic/Spawning/Metadata/FlareMetadataProcessor.cs
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
using UnityEngine; | ||
|
||
namespace NitroxClient.GameLogic.Spawning.Metadata; | ||
|
||
public class FlareMetadataProcessor : GenericEntityMetadataProcessor<FlareMetadata> | ||
{ | ||
public override void ProcessMetadata(GameObject gameObject, FlareMetadata metadata) | ||
{ | ||
if (!gameObject.TryGetComponent(out Flare flare)) | ||
{ | ||
Log.Error($"[{nameof(FlareMetadataProcessor)}] Can't apply metadata to {gameObject} because it doesn't have a {nameof(Flare)} component"); | ||
return; | ||
} | ||
flare.energyLeft = metadata.EnergyLeft; | ||
flare.hasBeenThrown = metadata.HasBeenThrown; | ||
|
||
if (metadata.FlareActivateTime.HasValue) | ||
{ | ||
flare.flareActivateTime = metadata.FlareActivateTime.Value; | ||
flare.flareActiveState = true; | ||
// From Flare.OnDrop | ||
flare.useRigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous; | ||
// Subtract the passed time to get to the current real amount of energy | ||
flare.energyLeft -= DayNightCycle.main.timePassedAsFloat - metadata.FlareActivateTime.Value; | ||
flare.GetComponent<WorldForces>().enabled = true; | ||
|
||
// From Flare.Awake but without the part disabling the light | ||
flare.capRenderer.enabled = true; | ||
if (flare.fxControl && !flare.fxIsPlaying) | ||
{ | ||
flare.fxControl.Play(1); | ||
flare.fxIsPlaying = true; | ||
flare.light.enabled = true; | ||
} | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
NitroxModel/DataStructures/GameLogic/Entities/Metadata/FlareMetadata.cs
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using BinaryPack.Attributes; | ||
|
||
namespace NitroxModel.DataStructures.GameLogic.Entities.Metadata; | ||
|
||
[Serializable, DataContract] | ||
public class FlareMetadata : EntityMetadata | ||
{ | ||
[DataMember(Order = 1)] | ||
public float EnergyLeft { get; } | ||
|
||
[DataMember(Order = 2)] | ||
public bool HasBeenThrown { get; } | ||
|
||
[DataMember(Order = 3)] | ||
public float? FlareActivateTime { get; } | ||
|
||
[IgnoreConstructor] | ||
protected FlareMetadata() | ||
{ | ||
// Constructor for serialization. Has to be "protected" for json serialization. | ||
} | ||
|
||
public FlareMetadata(float energyLeft, bool hasBeenThrown, float? flareActivateTime) | ||
{ | ||
EnergyLeft = energyLeft; | ||
HasBeenThrown = hasBeenThrown; | ||
FlareActivateTime = flareActivateTime; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"[FlareMetadata EnergyLeft: {EnergyLeft}, HasBeenThrown: {HasBeenThrown}, FlareActivateTime: {FlareActivateTime}]"; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Reflection; | ||
using NitroxClient.Communication.Abstract; | ||
using NitroxClient.GameLogic; | ||
using NitroxModel.DataStructures; | ||
using NitroxModel.Helper; | ||
using NitroxModel.Packets; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Broadcasts the destruction of a Flare once it's out of energy if the local player simulates it. | ||
/// </summary> | ||
public sealed partial class Flare_OnDestroy_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Flare t) => t.OnDestroy()); | ||
|
||
public static void Prefix(Flare __instance) | ||
{ | ||
if (__instance.TryGetNitroxId(out NitroxId flareId) && | ||
Resolve<SimulationOwnership>().HasAnyLockType(flareId)) | ||
{ | ||
Resolve<IPacketSender>().Send(new EntityDestroyed(flareId)); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NitroxClient.GameLogic; | ||
using NitroxModel.Helper; | ||
using UnityEngine; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Replaces local use of <see cref="Time.deltaTime"/> by <see cref="TimeManager.DeltaTime"/> | ||
/// </summary> | ||
public sealed partial class Flare_Update_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((Flare t) => t.Update()); | ||
private static readonly MethodInfo INSERTED_METHOD = Reflect.Method(() => GetDeltaTime()); | ||
private static readonly MethodInfo MATCHING_FIELD = Reflect.Property(() => Time.deltaTime).GetGetMethod(); | ||
|
||
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return new CodeMatcher(instructions).MatchStartForward(new CodeMatch(OpCodes.Call, MATCHING_FIELD)) | ||
.SetOperandAndAdvance(INSERTED_METHOD) | ||
.InstructionEnumeration(); | ||
} | ||
|
||
/// <summary> | ||
/// Wrapper for dependency resolving and variable querying | ||
/// </summary> | ||
public static float GetDeltaTime() | ||
{ | ||
return Resolve<TimeManager>().DeltaTime; | ||
} | ||
} |