Skip to content

Commit

Permalink
Added waiting for players skip and auto skip intro on DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannify committed Jun 23, 2023
1 parent 1369502 commit e0fa4ba
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 40 deletions.
28 changes: 20 additions & 8 deletions NitroxClient/GameLogic/InitialSync/PlayerInitialSyncProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,42 @@ public PlayerInitialSyncProcessor(Items item, ItemContainers itemContainers)

public override IEnumerator Process(InitialPlayerSync packet, WaitScreen.ManualWaitItem waitScreenItem)
{
SetPlayerPermissions(packet.Permissions);
waitScreenItem.SetProgress(0.16f);
LocalPlayer localPlayer = NitroxServiceLocator.LocateService<LocalPlayer>();

SetPlayerPermissions(localPlayer, packet.Permissions);
waitScreenItem.SetProgress(0.14f);
yield return null;

SetPlayerIntroCinematicMode(localPlayer, packet.IntroCinematicMode);
waitScreenItem.SetProgress(0.28f);
yield return null;

SetPlayerGameObjectId(packet.PlayerGameObjectId);
waitScreenItem.SetProgress(0.33f);
waitScreenItem.SetProgress(0.42f);
yield return null;

yield return AddStartingItemsToPlayer(packet.FirstTimeConnecting);
waitScreenItem.SetProgress(0.50f);
waitScreenItem.SetProgress(0.56f);
yield return null;

SetPlayerStats(packet.PlayerStatsData);
waitScreenItem.SetProgress(0.66f);
waitScreenItem.SetProgress(0.7f);
yield return null;

SetPlayerGameMode(packet.GameMode);
waitScreenItem.SetProgress(0.83f);
waitScreenItem.SetProgress(0.84f);
yield return null;
}

private void SetPlayerPermissions(Perms permissions)
private void SetPlayerPermissions(LocalPlayer localPlayer, Perms permissions)
{
localPlayer.Permissions = permissions;
}

private void SetPlayerIntroCinematicMode(LocalPlayer localPlayer, IntroCinematicMode introCinematicMode)
{
NitroxServiceLocator.LocateService<LocalPlayer>().Permissions = permissions;
localPlayer.IntroCinematicMode = introCinematicMode;
Log.Info($"Received initial sync player IntroCinematicMode: {introCinematicMode}");
}

private void SetPlayerGameObjectId(NitroxId id)
Expand Down
4 changes: 3 additions & 1 deletion NitroxClient/GameLogic/LocalPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class LocalPlayer : ILocalNitroxPlayer
public PlayerSettings PlayerSettings => multiplayerSession.PlayerSettings;

public Perms Permissions;

public IntroCinematicMode IntroCinematicMode;

public LocalPlayer(IMultiplayerSession multiplayerSession, IPacketSender packetSender, ThrottledPacketSender throttledPacketSender)
{
this.multiplayerSession = multiplayerSession;
Expand All @@ -47,6 +48,7 @@ public LocalPlayer(IMultiplayerSession multiplayerSession, IPacketSender packetS
playerModel = new Lazy<GameObject>(() => Body.RequireGameObject("player_view"));
bodyPrototype = new Lazy<GameObject>(CreateBodyPrototype);
Permissions = Perms.PLAYER;
IntroCinematicMode = IntroCinematicMode.NONE;
}

public void BroadcastLocation(Vector3 location, Vector3 velocity, Quaternion bodyRotation, Quaternion aimingRotation, Optional<VehicleMovementData> vehicle)
Expand Down
11 changes: 10 additions & 1 deletion NitroxClient/GameLogic/PlayerLogic/PlayerCinematics.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using NitroxClient.Communication.Abstract;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Packets;

namespace NitroxClient.GameLogic.PlayerLogic;

public class PlayerCinematics
{
private readonly IPacketSender packetSender;
private readonly LocalPlayer localPlayer;

public PlayerCinematics(IPacketSender packetSender)
public PlayerCinematics(IPacketSender packetSender, LocalPlayer localPlayer)
{
this.packetSender = packetSender;
this.localPlayer = localPlayer;
}

public void StartCinematicMode(ushort playerId, NitroxId controllerID, int controllerNameHash, string key)
Expand All @@ -22,4 +25,10 @@ public void EndCinematicMode(ushort playerId, NitroxId controllerID, int control
{
packetSender.Send(new PlayerCinematicControllerCall(playerId, controllerID, controllerNameHash, key, false));
}

public void SetLocalIntroCinematicMode(IntroCinematicMode introCinematicMode)
{
localPlayer.IntroCinematicMode = introCinematicMode;
packetSender.Send(new SetIntroCinematicMode(localPlayer.PlayerId, introCinematicMode));
}
}
10 changes: 10 additions & 0 deletions NitroxModel/DataStructures/GameLogic/IntroCinematicMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NitroxModel.DataStructures.GameLogic;

public enum IntroCinematicMode : byte
{
NONE,
LOADING,
WAITING,
START,
COMPLETED
}
6 changes: 3 additions & 3 deletions NitroxModel/MultiplayerSession/PlayerContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using NitroxModel.DataStructures;
using NitroxModel.Packets;
using NitroxModel.DataStructures.GameLogic;

namespace NitroxModel.MultiplayerSession;

Expand All @@ -11,11 +11,11 @@ public class PlayerContext
public ushort PlayerId { get; }
public NitroxId PlayerNitroxId { get; }
public bool WasBrandNewPlayer { get; }
public SetIntroCinematicMode.IntroCinematicMode IntroCinematicMode { get; set; }
public IntroCinematicMode IntroCinematicMode { get; set; }
public PlayerSettings PlayerSettings { get; }
public bool IsMuted { get; set; }

public PlayerContext(string playerName, ushort playerId, NitroxId playerNitroxId, bool wasBrandNewPlayer, SetIntroCinematicMode.IntroCinematicMode introCinematicMode, PlayerSettings playerSettings, bool isMuted)
public PlayerContext(string playerName, ushort playerId, NitroxId playerNitroxId, bool wasBrandNewPlayer, IntroCinematicMode introCinematicMode, PlayerSettings playerSettings, bool isMuted)
{
PlayerName = playerName;
PlayerId = playerId;
Expand Down
7 changes: 6 additions & 1 deletion NitroxModel/Packets/InitialPlayerSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NitroxModel.Packets
{
[Serializable]
public class InitialPlayerSync : Packet
{
{
public NitroxId AssignedEscapePodId { get; }
public List<EquippedItemData> EquippedItems { get; }
public List<BasePiece> BasePieces { get; }
Expand All @@ -31,6 +31,7 @@ public class InitialPlayerSync : Packet
public List<NitroxId> InitialSimulationOwnerships { get; }
public ServerGameMode GameMode { get; }
public Perms Permissions { get; }
public IntroCinematicMode IntroCinematicMode { get; }
public SubnauticaPlayerPreferences Preferences { get; }
public TimeData TimeData { get; }

Expand All @@ -52,6 +53,7 @@ public InitialPlayerSync(NitroxId playerGameObjectId,
IEnumerable<NitroxId> initialSimulationOwnerships,
ServerGameMode gameMode,
Perms perms,
IntroCinematicMode introCinematicMode,
SubnauticaPlayerPreferences preferences,
TimeData timeData)
{
Expand All @@ -73,6 +75,7 @@ public InitialPlayerSync(NitroxId playerGameObjectId,
InitialSimulationOwnerships = initialSimulationOwnerships.ToList();
GameMode = gameMode;
Permissions = perms;
IntroCinematicMode = introCinematicMode;
Preferences = preferences;
TimeData = timeData;
}
Expand All @@ -97,6 +100,7 @@ public InitialPlayerSync(
List<NitroxId> initialSimulationOwnerships,
ServerGameMode gameMode,
Perms permissions,
IntroCinematicMode introCinematicMode,
SubnauticaPlayerPreferences preferences,
TimeData timeData)
{
Expand All @@ -118,6 +122,7 @@ public InitialPlayerSync(
InitialSimulationOwnerships = initialSimulationOwnerships;
GameMode = gameMode;
Permissions = permissions;
IntroCinematicMode = introCinematicMode;
Preferences = preferences;
TimeData = timeData;
}
Expand Down
8 changes: 1 addition & 7 deletions NitroxModel/Packets/SetIntroCinematicMode.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using System;
using NitroxModel.DataStructures.GameLogic;

namespace NitroxModel.Packets;

[Serializable]
public class SetIntroCinematicMode : Packet
{
public enum IntroCinematicMode
{
LOADING,
WAITING,
START,
COMPLETED
}
public ushort PlayerId { get; }
public IntroCinematicMode Mode { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Reflection;
using HarmonyLib;
using NitroxClient.GameLogic.PlayerLogic;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Helper;

namespace NitroxPatcher.Patches.Dynamic;

public class EscapePod_StopIntroCinematic_Patch : NitroxPatch, IDynamicPatch
{
private static readonly MethodInfo targetMethod = Reflect.Method((EscapePod e) => e.StopIntroCinematic(default(bool)));

public static void Postfix(bool isInterrupted)
{
if (isInterrupted)
{
//TODO: Currently this is sent as an unauthenticated packet when skipping the intro up front (Configuration=Debug) and is therefor not processed by the server.
Resolve<PlayerCinematics>().SetLocalIntroCinematicMode(IntroCinematicMode.COMPLETED);
}
}

public override void Patch(Harmony harmony)
{
PatchPostfix(harmony, targetMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using NitroxClient.Communication.Abstract;
using NitroxClient.GameLogic;
using NitroxClient.GameLogic.PlayerLogic;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Helper;
using NitroxModel.Packets;
using UnityEngine;

namespace NitroxPatcher.Patches.Dynamic;
Expand All @@ -19,6 +19,7 @@ public class uGUI_SceneIntro_IntroSequence_Patch : NitroxPatch, IDynamicPatch
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
return new CodeMatcher(instructions)
// Insert custom check if cinematic should be started => waiting for other player & enable skip functionality
.MatchEndForward(
new CodeMatch(OpCodes.Ldfld, Reflect.Field((uGUI_SceneIntro si) => si.moveNext)),
new CodeMatch(OpCodes.Brfalse))
Expand All @@ -27,6 +28,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
new CodeInstruction(OpCodes.Call, Reflect.Method(() => IsRemoteCinematicReady(default))),
new CodeInstruction(OpCodes.And)
)
// Run our prepare code when cinematic starts
.MatchEndForward(
new CodeMatch(OpCodes.Ldsfld, Reflect.Field(() => EscapePod.main)),
new CodeMatch(OpCodes.Callvirt, Reflect.Method(() => EscapePod.main.TriggerIntroCinematic()))
Expand All @@ -35,10 +37,26 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
.Insert(
new CodeInstruction(OpCodes.Call, Reflect.Method(() => StartRemoteCinematic()))
)
// Disable cinematic skip when cinematic already started
.MatchStartForward(
new CodeMatch(OpCodes.Call, Reflect.Property(() => Time.time).GetMethod)
)
.SetInstructionAndAdvance(
new CodeInstruction(OpCodes.Ldc_R4, -1f)
)
.Advance(1)
.Insert(
new CodeInstruction(OpCodes.Ldloc_1),
new CodeInstruction(OpCodes.Ldfld, Reflect.Field((uGUI_SceneIntro si) => si.skipText)),
new CodeInstruction(OpCodes.Ldc_R4, 0f),
new CodeInstruction(OpCodes.Call, Reflect.Method(() => TMProExtensions.SetAlpha(default, default(float))))
)
// Replace intro text
.MatchEndForward(
new CodeMatch(OpCodes.Ldstr, "IntroUWEPresents")
)
.SetOperandAndAdvance("Nitrox_IntroUWEPresents")
// Run our cleanup code when cinematic ends
.MatchEndForward(
new CodeMatch(OpCodes.Ldloc_1),
new CodeMatch(OpCodes.Ldc_I4_0),
Expand All @@ -59,22 +77,30 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
private static bool IsRemoteCinematicReady(uGUI_SceneIntro uGuiSceneIntro)
{
if (callbackRun) return true;

if (Resolve<LocalPlayer>().IntroCinematicMode == IntroCinematicMode.COMPLETED)
{
uGuiSceneIntro.Stop(true);
EndRemoteCinematic();
}

if (!uGuiSceneIntro.moveNext) return false;

if (!packetSend)
{
uGuiSceneIntro.skipHintStartTime = Time.time;
uGuiSceneIntro.mainText.SetText("Waiting for partner to join");
uGuiSceneIntro.mainText.SetState(true);

ushort playerId = Resolve<LocalPlayer>().PlayerId;
Resolve<IPacketSender>().Send(new SetIntroCinematicMode(playerId, SetIntroCinematicMode.IntroCinematicMode.WAITING));
Resolve<PlayerCinematics>().SetLocalIntroCinematicMode(IntroCinematicMode.WAITING);
packetSend = true;
return false;
}

if (Resolve<PlayerManager>().GetAllRemotePlayers().Any(r => r.PlayerContext.IntroCinematicMode == SetIntroCinematicMode.IntroCinematicMode.START))
RemotePlayer[] allReadyRemotePlayers = Resolve<PlayerManager>().GetAllRemotePlayers().Where(r => r.PlayerContext.IntroCinematicMode == IntroCinematicMode.START).ToArray();
if (allReadyRemotePlayers.Length > 0)
{
partner = Resolve<PlayerManager>().GetAllRemotePlayers().First(r => r.PlayerContext.IntroCinematicMode == SetIntroCinematicMode.IntroCinematicMode.START);
partner = allReadyRemotePlayers.First();

uGuiSceneIntro.moveNext = false;
uGuiSceneIntro.mainText.FadeOut(0.2f, uGuiSceneIntro.Callback);
Expand Down Expand Up @@ -133,7 +159,7 @@ public void Awake()
seatRight.Find("life_pod_seat_01_right_damaged_jnt3")
};

seatArmRestLeft = modelRoot.Find("life_pod_seat_01_left_damaged_jnt4/life_pod_seat_01_left_damaged_jnt5");
seatArmRestLeft = modelRoot.Find("life_pod_seat_01_left_damaged_jnt4/life_pod_seat_01_left_damaged_jnt5");
seatArmRestRight = modelRoot.Find("life_pod_seat_01_right_damaged_jnt4/life_pod_seat_01_right_damaged_jnt5");

seatBarRendererRight = modelRoot.parent.Find("lifepod_damaged_03_geo/life_pod_seat_01_R").GetComponentsInChildren<SkinnedMeshRenderer>();
Expand Down Expand Up @@ -223,6 +249,7 @@ Quaternion InverseRotateAroundEscapePod(Quaternion from)
{
seatPartsRight[i].localRotation = seatPartsLeft[i].localRotation;
}

seatArmRestRight.localPosition = -seatArmRestLeft.localPosition;
}

Expand Down Expand Up @@ -255,8 +282,7 @@ private static void EndRemoteCinematic()
partner.ArmsController.enabled = true;
partner.AnimationController.UpdatePlayerAnimations = true;

ushort playerId = Resolve<LocalPlayer>().PlayerId;
Resolve<IPacketSender>().Send(new SetIntroCinematicMode(playerId, SetIntroCinematicMode.IntroCinematicMode.COMPLETED));
Resolve<PlayerCinematics>().SetLocalIntroCinematicMode(IntroCinematicMode.COMPLETED);
}

public override void Patch(Harmony harmony)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if DEBUG
using System.Reflection;
using HarmonyLib;
using NitroxModel.Helper;
Expand All @@ -10,11 +11,12 @@ public class MainGameController_ShouldPlayIntro_Patch : NitroxPatch, IPersistent

public static void Postfix(ref bool __result)
{
//__result = true;
__result = false;
}

public override void Patch(Harmony harmony)
{
PatchPostfix(harmony, TARGET_METHOD);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public override void Process(PlayerJoiningMultiplayerSession packet, NitroxConne
simulations,
world.GameMode,
player.Permissions,
wasBrandNewPlayer ? IntroCinematicMode.LOADING : IntroCinematicMode.COMPLETED,
new(new(player.PingInstancePreferences), player.PinnedRecipePreferences.ToList()),
storyManager.GetTimeData()
);
Expand Down
Loading

0 comments on commit e0fa4ba

Please sign in to comment.