Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple fixes #2089

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Persist gamemode and add a minecraft-like command for it
  • Loading branch information
tornac1234 committed Oct 29, 2023
commit bd4ec4a2eaf55e79c75cedc25a8c9e4bf64a9103
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.GameLogic;
using NitroxModel.Packets;

namespace NitroxClient.Communication.Packets.Processors
namespace NitroxClient.Communication.Packets.Processors;

public class GameModeChangedProcessor : ClientPacketProcessor<GameModeChanged>
{
public class GameModeChangedProcessor : ClientPacketProcessor<GameModeChanged>
private readonly LocalPlayer localPlayer;
private readonly PlayerManager playerManager;

public GameModeChangedProcessor(LocalPlayer localPlayer, PlayerManager playerManager)
{
this.localPlayer = localPlayer;
this.playerManager = playerManager;
}

public override void Process(GameModeChanged packet)
{
public override void Process(GameModeChanged packet)
if (packet.AllPlayers || packet.PlayerId == localPlayer.PlayerId)
{
GameModeUtils.SetGameMode((GameModeOption)(int)packet.GameMode, GameModeOption.None);
}
if (packet.AllPlayers)
{
foreach (RemotePlayer remotePlayer in playerManager.GetAll())
{
remotePlayer.SetGameMode(packet.GameMode);
}
}
else if (playerManager.TryFind(packet.PlayerId, out RemotePlayer remotePlayer))
{
remotePlayer.SetGameMode(packet.GameMode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void SetPlayerStats(PlayerStatsData statsData)
Player.main.GetPDA().Close();
}

private void SetPlayerGameMode(ServerGameMode gameMode)
private void SetPlayerGameMode(NitroxGameMode gameMode)
{
Log.Info($"Received initial sync packet with gamemode {gameMode}");
GameModeUtils.SetGameMode((GameModeOption)(int)gameMode, GameModeOption.None);
Expand Down
16 changes: 16 additions & 0 deletions NitroxClient/GameLogic/RemotePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NitroxClient.MonoBehaviours.Gui.HUD;
using NitroxClient.Unity.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel.Server;
using UnityEngine;
using UWE;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -88,6 +89,7 @@ public void InitializeGameObject(GameObject playerBody)
SetupSkyAppliers();

vitals = playerVitalsManager.CreateOrFindForPlayer(this);
RefreshVitalsVisibility();
}

public void Attach(Transform transform, bool keepWorldTransform = false)
Expand Down Expand Up @@ -338,5 +340,19 @@ private void SetupSkyAppliers()
skyApplier.dynamic = true;
skyApplier.renderers = Body.GetComponentsInChildren<SkinnedMeshRenderer>(true);
}

public void SetGameMode(NitroxGameMode gameMode)
{
PlayerContext.GameMode = gameMode;
RefreshVitalsVisibility();
}

private void RefreshVitalsVisibility()
{
if (vitals)
{
vitals.gameObject.SetActive(PlayerContext.GameMode != NitroxGameMode.CREATIVE);
}
}
}
}
25 changes: 14 additions & 11 deletions NitroxClient/MonoBehaviours/PlayerStatsBroadcaster.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NitroxClient.GameLogic;
using NitroxClient.Unity.Helper;
using UnityEngine;

namespace NitroxClient.MonoBehaviours;
Expand All @@ -13,7 +14,12 @@ public class PlayerStatsBroadcaster : MonoBehaviour
public void Awake()
{
localPlayer = this.Resolve<LocalPlayer>();
survival = Player.main.GetComponent<Survival>();
survival = Player.main.AliveOrNull()?.GetComponent<Survival>();
if (!survival)
{
Log.Error($"Couldn't find the {nameof(Survival)} instance on the main {nameof(Player)} instance. Destroying {nameof(PlayerStatsBroadcaster)}");
Destroy(this);
}
}

public void Update()
Expand All @@ -25,16 +31,13 @@ public void Update()
{
time = 0;

if (survival)
{
float oxygen = Player.main.oxygenMgr.GetOxygenAvailable();
float maxOxygen = Player.main.oxygenMgr.GetOxygenCapacity();
float health = Player.main.liveMixin.health;
float food = survival.food;
float water = survival.water;
float infectionAmount = Player.main.infectedMixin.GetInfectedAmount();
localPlayer.BroadcastStats(oxygen, maxOxygen, health, food, water, infectionAmount);
}
float oxygen = Player.main.oxygenMgr.GetOxygenAvailable();
float maxOxygen = Player.main.oxygenMgr.GetOxygenCapacity();
float health = Player.main.liveMixin.health;
float food = survival.food;
float water = survival.water;
float infectionAmount = Player.main.infectedMixin.GetInfectedAmount();
localPlayer.BroadcastStats(oxygen, maxOxygen, health, food, water, infectionAmount);
}
}
}
16 changes: 8 additions & 8 deletions NitroxLauncher/Pages/ServerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public void SaveConfigSettings()
{
Config.SaveName = TBWorldName.Text;
if (IsNewWorld) { Config.Seed = TBWorldSeed.Text; }
if (RBFreedom.IsChecked == true) { Config.GameMode = ServerGameMode.FREEDOM; }
else if (RBSurvival.IsChecked == true) { Config.GameMode = ServerGameMode.SURVIVAL; }
else if (RBCreative.IsChecked == true) { Config.GameMode = ServerGameMode.CREATIVE; }
else if (RBHardcore.IsChecked == true) { Config.GameMode = ServerGameMode.HARDCORE; }
if (RBFreedom.IsChecked == true) { Config.GameMode = NitroxGameMode.FREEDOM; }
else if (RBSurvival.IsChecked == true) { Config.GameMode = NitroxGameMode.SURVIVAL; }
else if (RBCreative.IsChecked == true) { Config.GameMode = NitroxGameMode.CREATIVE; }
else if (RBHardcore.IsChecked == true) { Config.GameMode = NitroxGameMode.HARDCORE; }

Config.DisableConsole = !CBCheats.IsChecked ?? Config.DisableConsole;
Config.MaxConnections = Convert.ToInt32(TBMaxPlayerCap.Text);
Expand Down Expand Up @@ -109,16 +109,16 @@ public void UpdateVisualWorldSettings()
TBWorldSeed.Text = Config.Seed;
switch (Config.GameMode)
{
case ServerGameMode.FREEDOM:
case NitroxGameMode.FREEDOM:
RBFreedom.IsChecked = true;
break;
case ServerGameMode.SURVIVAL:
case NitroxGameMode.SURVIVAL:
RBSurvival.IsChecked = true;
break;
case ServerGameMode.CREATIVE:
case NitroxGameMode.CREATIVE:
RBCreative.IsChecked = true;
break;
case ServerGameMode.HARDCORE:
case NitroxGameMode.HARDCORE:
RBHardcore.IsChecked = true;
break;
}
Expand Down
50 changes: 26 additions & 24 deletions NitroxModel/MultiplayerSession/PlayerContext.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
using System;
using System;
using NitroxModel.DataStructures;
using NitroxModel.Server;

namespace NitroxModel.MultiplayerSession
namespace NitroxModel.MultiplayerSession;

[Serializable]
public class PlayerContext
{
[Serializable]
public class PlayerContext
{
public string PlayerName { get; }
public ushort PlayerId { get; }
public NitroxId PlayerNitroxId { get; }
public bool WasBrandNewPlayer { get; }
public PlayerSettings PlayerSettings { get; }
public bool IsMuted { get; set; }
public string PlayerName { get; }
public ushort PlayerId { get; }
public NitroxId PlayerNitroxId { get; }
public bool WasBrandNewPlayer { get; }
public PlayerSettings PlayerSettings { get; }
public bool IsMuted { get; set; }
public NitroxGameMode GameMode { get; set; }

public PlayerContext(string playerName, ushort playerId, NitroxId playerNitroxId, bool wasBrandNewPlayer, PlayerSettings playerSettings, bool isMuted)
{
PlayerName = playerName;
PlayerId = playerId;
PlayerNitroxId = playerNitroxId;
WasBrandNewPlayer = wasBrandNewPlayer;
PlayerSettings = playerSettings;
IsMuted = isMuted;
}
public PlayerContext(string playerName, ushort playerId, NitroxId playerNitroxId, bool wasBrandNewPlayer, PlayerSettings playerSettings, bool isMuted, NitroxGameMode gameMode)
{
PlayerName = playerName;
PlayerId = playerId;
PlayerNitroxId = playerNitroxId;
WasBrandNewPlayer = wasBrandNewPlayer;
PlayerSettings = playerSettings;
IsMuted = isMuted;
GameMode = gameMode;
}

public override string ToString()
{
return $"[PlayerContext - PlayerName: {PlayerName}, PlayerId: {PlayerId}, PlayerNitroxId: {PlayerNitroxId}, WasBrandNewPlayer: {WasBrandNewPlayer}, PlayerSettings: {PlayerSettings}]";
}
public override string ToString()
{
return $"[PlayerContext - PlayerName: {PlayerName}, PlayerId: {PlayerId}, PlayerNitroxId: {PlayerNitroxId}, WasBrandNewPlayer: {WasBrandNewPlayer}, PlayerSettings: {PlayerSettings}, GameMode: {GameMode}]";
}
}
31 changes: 22 additions & 9 deletions NitroxModel/Packets/GameModeChanged.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
using System;
using System;
using NitroxModel.Server;

namespace NitroxModel.Packets
namespace NitroxModel.Packets;

[Serializable]
public class GameModeChanged : Packet
{
[Serializable]
public class GameModeChanged : Packet
public ushort PlayerId { get; }
public bool AllPlayers { get; }
public NitroxGameMode GameMode { get; }

public GameModeChanged(ushort playerId, bool allPlayers, NitroxGameMode gameMode)
{
public ServerGameMode GameMode { get; }
PlayerId = playerId;
AllPlayers = allPlayers;
GameMode = gameMode;
}

public GameModeChanged(ServerGameMode gameMode)
{
GameMode = gameMode;
}
public static GameModeChanged ForPlayer(ushort playerId, NitroxGameMode gameMode)
{
return new(playerId, false, gameMode);
}

public static GameModeChanged ForAllPlayers(NitroxGameMode gameMode)
{
return new(0, true, gameMode);
}
}
6 changes: 3 additions & 3 deletions NitroxModel/Packets/InitialPlayerSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class InitialPlayerSync : Packet
public List<PlayerContext> OtherPlayers { get; }
public List<Entity> GlobalRootEntities { get; }
public List<NitroxId> InitialSimulationOwnerships { get; }
public ServerGameMode GameMode { get; }
public NitroxGameMode GameMode { get; }
public Perms Permissions { get; }
public SubnauticaPlayerPreferences Preferences { get; }
public TimeData TimeData { get; }
Expand All @@ -49,7 +49,7 @@ public class InitialPlayerSync : Packet
IEnumerable<PlayerContext> otherPlayers,
IEnumerable<Entity> globalRootEntities,
IEnumerable<NitroxId> initialSimulationOwnerships,
ServerGameMode gameMode,
NitroxGameMode gameMode,
Perms perms,
SubnauticaPlayerPreferences preferences,
TimeData timeData,
Expand Down Expand Up @@ -94,7 +94,7 @@ public class InitialPlayerSync : Packet
List<PlayerContext> otherPlayers,
List<Entity> globalRootEntities,
List<NitroxId> initialSimulationOwnerships,
ServerGameMode gameMode,
NitroxGameMode gameMode,
Perms permissions,
SubnauticaPlayerPreferences preferences,
TimeData timeData,
Expand Down
12 changes: 12 additions & 0 deletions NitroxModel/Server/NitroxGameMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NitroxModel.Server;

/// <summary>
/// GameModes according to Subnautica's enum GameModeOption
/// </summary>
public enum NitroxGameMode
{
SURVIVAL = 0,
FREEDOM = 2,
HARDCORE = 257,
CREATIVE = 1790,
}
10 changes: 0 additions & 10 deletions NitroxModel/Server/ServerGameMode.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Reflection;
using HarmonyLib;
using NitroxClient.Communication.Abstract;
using NitroxModel.Helper;
using NitroxModel.Packets;
using NitroxModel.Server;

namespace NitroxPatcher.Patches.Dynamic;

/// <inheritdoc cref="CrashedShipExploder_OnConsoleCommand_Patch"/>
public sealed class GameModeConsoleCommands_OnConsoleCommand_Patch : NitroxPatch, IDynamicPatch
{
public static readonly MethodInfo TARGET_METHOD_SURVIVAL = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_survival());
public static readonly MethodInfo TARGET_METHOD_CREATIVE = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_creative());
public static readonly MethodInfo TARGET_METHOD_FREEDOM = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_freedom());
public static readonly MethodInfo TARGET_METHOD_HARDCORE = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_hardcore());

public static bool PrefixSurvival() => BroadcastGameModeChange(NitroxGameMode.SURVIVAL);
public static bool PrefixCreative() => BroadcastGameModeChange(NitroxGameMode.CREATIVE);
public static bool PrefixFreedom() => BroadcastGameModeChange(NitroxGameMode.FREEDOM);
public static bool PrefixHardcore() => BroadcastGameModeChange(NitroxGameMode.HARDCORE);

private static bool BroadcastGameModeChange(NitroxGameMode gameMode)
{
Resolve<IPacketSender>().Send(new ServerCommand($"gamemode {gameMode}"));
return false;
}

public override void Patch(Harmony harmony)
{
PatchPrefix(harmony, TARGET_METHOD_SURVIVAL, ((Func<bool>)PrefixSurvival).Method);
PatchPrefix(harmony, TARGET_METHOD_CREATIVE, ((Func<bool>)PrefixCreative).Method);
PatchPrefix(harmony, TARGET_METHOD_FREEDOM, ((Func<bool>)PrefixFreedom).Method);
PatchPrefix(harmony, TARGET_METHOD_HARDCORE, ((Func<bool>)PrefixHardcore).Method);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override void Process(PlayerJoiningMultiplayerSession packet, NitroxConne
GetOtherPlayers(player),
globalRootEntities,
simulations,
world.GameMode,
player.GameMode,
player.Permissions,
new(new(player.PingInstancePreferences), player.PinnedRecipePreferences.ToList()),
storyManager.GetTimeData(),
Expand Down
Loading