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

Crops persistence #2137

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxModel.Packets;

namespace NitroxClient.Communication.Packets.Processors;

public class FastCheatChangedProcessor : ClientPacketProcessor<FastCheatChanged>
{
public override void Process(FastCheatChanged packet)
{
switch (packet.Cheat)
{
case FastCheatChanged.FastCheat.FAST_HATCH:
NoCostConsoleCommand.main.fastHatchCheat = packet.Value;
break;

case FastCheatChanged.FastCheat.FAST_GROW:
NoCostConsoleCommand.main.fastGrowCheat = packet.Value;
break;
}
}
}
22 changes: 20 additions & 2 deletions NitroxClient/GameLogic/InitialSync/PlayerInitialSyncProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Text;
using NitroxClient.GameLogic.InitialSync.Abstract;
using NitroxClient.MonoBehaviours;
using NitroxModel.DataStructures;
Expand Down Expand Up @@ -34,7 +35,7 @@ public PlayerInitialSyncProcessor(Items item, ItemContainers itemContainers, Loc
AddStep(sync => AddStartingItemsToPlayer(sync.FirstTimeConnecting));
AddStep(sync => SetPlayerStats(sync.PlayerStatsData));
AddStep(sync => SetPlayerGameMode(sync.GameMode));
AddStep(sync => SetPlayerKeepInventoryOnDeath(sync.KeepInventoryOnDeath));
AddStep(sync => ApplySettings(sync.KeepInventoryOnDeath, sync.SessionSettings.FastHatch, sync.SessionSettings.FastGrow));
}

private void SetPlayerPermissions(Perms permissions)
Expand Down Expand Up @@ -137,8 +138,25 @@ private static void SetPlayerGameMode(NitroxGameMode gameMode)
GameModeUtils.SetGameMode((GameModeOption)(int)gameMode, GameModeOption.None);
}

private void SetPlayerKeepInventoryOnDeath(bool keepInventoryOnDeath)
private void ApplySettings(bool keepInventoryOnDeath, bool fastHatch, bool fastGrow)
{
localPlayer.KeepInventoryOnDeath = keepInventoryOnDeath;
NoCostConsoleCommand.main.fastHatchCheat = fastHatch;
NoCostConsoleCommand.main.fastGrowCheat = fastGrow;
if (!fastHatch && !fastGrow)
{
return;
}

StringBuilder cheatsEnabled = new("Cheats enabled:");
if (fastHatch)
{
cheatsEnabled.Append(" fastHatch");
}
if (fastGrow)
{
cheatsEnabled.Append(" fastGrow");
}
ErrorMessage.AddDebug(cheatsEnabled.ToString());
}
}
10 changes: 10 additions & 0 deletions NitroxModel/DataStructures/GameLogic/SessionSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NitroxModel.DataStructures.GameLogic;

/// <summary>
/// States that are temporary for one session so we don't need to persist them
/// </summary>
public class SessionSettings
{
public bool FastHatch;
public bool FastGrow;
}
22 changes: 22 additions & 0 deletions NitroxModel/Packets/FastCheatChanged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace NitroxModel.Packets;

[Serializable]
public class FastCheatChanged : Packet
{
public FastCheat Cheat{ get; }
public bool Value { get; }

public FastCheatChanged(FastCheat cheat, bool value)
{
Cheat = cheat;
Value = value;
}

public enum FastCheat : byte
{
FAST_HATCH,
FAST_GROW
}
}
10 changes: 8 additions & 2 deletions NitroxModel/Packets/InitialPlayerSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class InitialPlayerSync : Packet
public TimeData TimeData { get; }
public bool IsFirstPlayer { get; }
public Dictionary<NitroxId, int> BuildOperationIds { get; }

public bool KeepInventoryOnDeath { get; }
public SessionSettings SessionSettings { get; }

public InitialPlayerSync(NitroxId playerGameObjectId,
bool firstTimeConnecting,
Expand All @@ -59,7 +61,8 @@ public InitialPlayerSync(NitroxId playerGameObjectId,
TimeData timeData,
bool isFirstPlayer,
Dictionary<NitroxId, int> buildOperationIds,
bool keepInventoryOnDeath)
bool keepInventoryOnDeath,
SessionSettings sessionSettings)
{
AssignedEscapePodId = assignedEscapePodId;
PlayerGameObjectId = playerGameObjectId;
Expand All @@ -84,6 +87,7 @@ public InitialPlayerSync(NitroxId playerGameObjectId,
IsFirstPlayer = isFirstPlayer;
BuildOperationIds = buildOperationIds;
KeepInventoryOnDeath = keepInventoryOnDeath;
SessionSettings = sessionSettings;
}

/// <remarks>Used for deserialization</remarks>
Expand All @@ -110,7 +114,8 @@ public InitialPlayerSync(
TimeData timeData,
bool isFirstPlayer,
Dictionary<NitroxId, int> buildOperationIds,
bool keepInventoryOnDeath)
bool keepInventoryOnDeath,
SessionSettings sessionSettings)
{
AssignedEscapePodId = assignedEscapePodId;
PlayerGameObjectId = playerGameObjectId;
Expand All @@ -135,6 +140,7 @@ public InitialPlayerSync(
IsFirstPlayer = isFirstPlayer;
BuildOperationIds = buildOperationIds;
KeepInventoryOnDeath = keepInventoryOnDeath;
SessionSettings = sessionSettings;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Reflection;
using NitroxModel.Helper;

namespace NitroxPatcher.Patches.Dynamic;

/// <summary>
/// Prevents "bobthebuilder" command from enabling "fasthatch" and "fastgrow", since both commands are managed by the server
/// </summary>
public sealed partial class NoCostConsoleCommand_OnConsoleCommand_bobthebuilder_Patch : NitroxPatch, IDynamicPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((NoCostConsoleCommand t) => t.OnConsoleCommand_bobthebuilder(default));

public static void Prefix(out (bool, bool) __state)
{
__state = (NoCostConsoleCommand.main.fastHatchCheat, NoCostConsoleCommand.main.fastGrowCheat);
ErrorMessage.AddWarning("fastHatch and fastGrow cheats weren't enabled because they're shared cheat commands. Please enable them manually with their respective commands \"fastgrow\" and \"fasthatch\"");
}

public static void Postfix((bool, bool) __state)
{
NoCostConsoleCommand.main.fastHatchCheat = __state.Item1;
NoCostConsoleCommand.main.fastGrowCheat = __state.Item2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Reflection;
using NitroxClient.Communication.Abstract;
using NitroxModel.Helper;
using NitroxModel.Packets;

namespace NitroxPatcher.Patches.Dynamic;

/// <summary>
/// Replaces "fastgrow" command by the Nitrox server command implementation
/// </summary>
public sealed partial class NoCostConsoleCommand_OnConsoleCommand_fastgrow_Patch : NitroxPatch, IDynamicPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((NoCostConsoleCommand t) => t.OnConsoleCommand_fastgrow(default));

public static bool Prefix()
{
bool newValue = !NoCostConsoleCommand.main.fastGrowCheat; // toggle is basic functionning
Resolve<IPacketSender>().Send(new ServerCommand($"fast grow {newValue}"));
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Reflection;
using NitroxClient.Communication.Abstract;
using NitroxModel.Helper;
using NitroxModel.Packets;

namespace NitroxPatcher.Patches.Dynamic;

/// <summary>
/// Replaces "fasthatch" command by the Nitrox server command implementation
/// </summary>
public sealed partial class NoCostConsoleCommand_OnConsoleCommand_fasthatch_Patch : NitroxPatch, IDynamicPatch
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((NoCostConsoleCommand t) => t.OnConsoleCommand_fasthatch(default));

public static bool Prefix()
{
bool newValue = !NoCostConsoleCommand.main.fastHatchCheat; // toggle is basic functionning
Resolve<IPacketSender>().Send(new ServerCommand($"fast hatch {newValue}"));
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.Unity;
using NitroxModel.DataStructures.Util;
using NitroxModel.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel.Networking;
using NitroxModel.Packets;
Expand All @@ -14,7 +13,6 @@
using NitroxServer.GameLogic;
using NitroxServer.GameLogic.Bases;
using NitroxServer.GameLogic.Entities;
using NitroxServer.Serialization;
using NitroxServer.Serialization.World;

namespace NitroxServer.Communication.Packets.Processors
Expand All @@ -28,8 +26,9 @@ public class PlayerJoiningMultiplayerSessionProcessor : UnauthenticatedPacketPro
private readonly EntityRegistry entityRegistry;
private readonly SubnauticaServerConfig serverConfig;
private readonly NtpSyncer ntpSyncer;
private readonly SessionSettings sessionSettings;

public PlayerJoiningMultiplayerSessionProcessor(ScheduleKeeper scheduleKeeper, StoryManager storyManager, PlayerManager playerManager, World world, EntityRegistry entityRegistry, SubnauticaServerConfig serverConfig, NtpSyncer ntpSyncer)
public PlayerJoiningMultiplayerSessionProcessor(ScheduleKeeper scheduleKeeper, StoryManager storyManager, PlayerManager playerManager, World world, EntityRegistry entityRegistry, SubnauticaServerConfig serverConfig, NtpSyncer ntpSyncer, SessionSettings sessionSettings)
{
this.scheduleKeeper = scheduleKeeper;
this.storyManager = storyManager;
Expand All @@ -38,6 +37,7 @@ public PlayerJoiningMultiplayerSessionProcessor(ScheduleKeeper scheduleKeeper, S
this.entityRegistry = entityRegistry;
this.serverConfig = serverConfig;
this.ntpSyncer = ntpSyncer;
this.sessionSettings = sessionSettings;
}

public override void Process(PlayerJoiningMultiplayerSession packet, INitroxConnection connection)
Expand Down Expand Up @@ -87,7 +87,8 @@ public override void Process(PlayerJoiningMultiplayerSession packet, INitroxConn
storyManager.GetTimeData(),
isFirstPlayer,
BuildingManager.GetEntitiesOperations(globalRootEntities),
serverConfig.KeepInventoryOnDeath
serverConfig.KeepInventoryOnDeath,
sessionSettings
);

player.SendPacket(initialPlayerSync);
Expand Down
74 changes: 74 additions & 0 deletions NitroxServer/ConsoleCommands/FastCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Packets;
using NitroxServer.ConsoleCommands.Abstract;
using NitroxServer.ConsoleCommands.Abstract.Type;
using NitroxServer.GameLogic;

namespace NitroxServer.ConsoleCommands;

public class FastCommand : Command
{
private readonly PlayerManager playerManager;
private readonly SessionSettings sessionSettings;

public FastCommand(PlayerManager playerManager, SessionSettings sessionSettings) : base("fast", Perms.MODERATOR, "Enables/disables a fast cheat command, whether it be \"hatch\" or \"grow\"")
{
AddParameter(new TypeString("cheat", true, "The name of the fast cheat to change: \"hatch\" or \"grow\""));
AddParameter(new TypeBoolean("value", false, "Whether the cheat will be enabled or disabled. Default count as a toggle"));
this.playerManager = playerManager;
this.sessionSettings = sessionSettings;
}

protected override void Execute(CallArgs args)
{
string cheatName = args.Get<string>(0).ToLowerInvariant();

FastCheatChanged.FastCheat cheat;
bool value;
switch (cheatName)
{
case "hatch":
cheat = FastCheatChanged.FastCheat.FAST_HATCH;
value = sessionSettings.FastHatch;
break;

case "grow":
cheat = FastCheatChanged.FastCheat.FAST_GROW;
value = sessionSettings.FastGrow;
break;

default:
SendMessage(args.Sender, "Must provide a valid cheat name: \"hatch\" or \"grow\"");
return;
}

if (args.IsValid(1))
{
bool newValue = args.Get<bool>(1);
if (newValue == value)
{
SendMessage(args.Sender, $"Fast {cheatName} already set to {newValue}");
return;
}
value = newValue;
}
else
{
// If the value wasn't provided then we toggle it
value = !value;
}

switch (cheat)
{
case FastCheatChanged.FastCheat.FAST_HATCH:
sessionSettings.FastHatch = value;
break;
case FastCheatChanged.FastCheat.FAST_GROW:
sessionSettings.FastGrow = value;
break;
}

playerManager.SendPacketToAllPlayers(new FastCheatChanged(cheat, value));
SendMessageToAllPlayers($"Fast {cheatName} changed to {value} by {args.SenderName}");
}
}
2 changes: 2 additions & 0 deletions NitroxServer/Serialization/World/World.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Server;
using NitroxServer.GameLogic;
using NitroxServer.GameLogic.Bases;
Expand All @@ -20,6 +21,7 @@ public class World
public BuildingManager BuildingManager { get; set; }
public StoryManager StoryManager { get; set; }
public GameData GameData { get; set; }
public SessionSettings SessionSettings { get; set; }
public NitroxGameMode GameMode { get; set; }
public string Seed { get; set; }
}
Expand Down
3 changes: 2 additions & 1 deletion NitroxServer/Serialization/World/WorldPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public World CreateWorld(PersistedWorldData pWorldData, NitroxGameMode gameMode)
EntityRegistry = entityRegistry,
GameData = pWorldData.WorldData.GameData,
GameMode = gameMode,
Seed = seed
Seed = seed,
SessionSettings = new()
};

world.TimeKeeper = new(world.PlayerManager, ntpSyncer, pWorldData.WorldData.GameData.StoryTiming.ElapsedSeconds, pWorldData.WorldData.GameData.StoryTiming.RealTimeElapsed);
Expand Down
1 change: 1 addition & 0 deletions NitroxServer/ServerAutoFacRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private void RegisterWorld(ContainerBuilder containerBuilder)
containerBuilder.Register(c => c.Resolve<World>().GameData.PDAState).SingleInstance();
containerBuilder.Register(c => c.Resolve<World>().GameData.StoryGoals).SingleInstance();
containerBuilder.Register(c => c.Resolve<World>().GameData.StoryTiming).SingleInstance();
containerBuilder.Register(c => c.Resolve<World>().SessionSettings).SingleInstance();
}

private void RegisterGameSpecificServices(ContainerBuilder containerBuilder, Assembly assembly)
Expand Down