Skip to content

Commit

Permalink
Merge pull request #155 from hubastard/improve-save
Browse files Browse the repository at this point in the history
Improve save
  • Loading branch information
hubastard authored Apr 16, 2021
2 parents 4856b03 + da76cad commit 893f5e8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 14 deletions.
1 change: 0 additions & 1 deletion NebulaHost/MultiplayerHostSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void StartServer(int port, bool loadSaveFile = false)
{
SaveManager.LoadServerData();
}
SaveManager.SaveOnExit = true;
PacketProcessor = new NetPacketProcessor();
StatisticsManager = new StatisticsManager();

Expand Down
1 change: 1 addition & 0 deletions NebulaPatcher/NebulaPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Patches\Dynamic\TrashSystem_Patch.cs" />
<Compile Include="Patches\Dynamic\StorageComponent_Patch.cs" />
<Compile Include="Patches\Dynamic\UIAssemblerWindow_Patch.cs" />
<Compile Include="Patches\Dynamic\UIAutoSave_Patch.cs" />
<Compile Include="Patches\Dynamic\UIEjectorWindow_Patch.cs" />
<Compile Include="Patches\Dynamic\UIInserterWindow_Patch.cs" />
<Compile Include="Patches\Dynamic\UILabWindow_Patch.cs" />
Expand Down
18 changes: 7 additions & 11 deletions NebulaPatcher/Patches/Dynamic/GameSave_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ class GameSave_Patch
[HarmonyPatch("SaveCurrentGame")]
public static bool SaveCurrentGame_Prefix(string saveName)
{
if (SaveManager.SaveOnExit || SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
if (SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
{
SaveManager.SaveServerData(saveName);
SaveManager.SaveOnExit = false;
}

if (SimulatedWorld.Initialized && !LocalPlayer.IsMasterClient)
{
return false;
}

return true;
// Only save if in single player or if you are the host
return (!SimulatedWorld.Initialized && !SimulatedWorld.ExitingMultiplayerSession) || LocalPlayer.IsMasterClient;
}

[HarmonyPrefix]
Expand All @@ -32,19 +27,20 @@ public static void LoadCurrentGame_Prefix(string saveName)
SaveManager.SetLastSave(saveName);
}

[HarmonyPrefix]
[HarmonyPatch("AutoSave")]
public static bool AutoSave_Prefix()
{
//Do not trigger autosave for the clients in multiplayer
// Only save if in single player or if you are the host
return !SimulatedWorld.Initialized || LocalPlayer.IsMasterClient;
}

[HarmonyPrefix]
[HarmonyPatch("SaveAsLastExit")]
public static bool SaveAsLastExit_Prefix()
{
//Do not trigger autosave for the clients in multiplayer
return !SimulatedWorld.Initialized || LocalPlayer.IsMasterClient;
// Only save if in single player or if you are the host
return (!SimulatedWorld.Initialized && !SimulatedWorld.ExitingMultiplayerSession) || LocalPlayer.IsMasterClient;
}
}
}
28 changes: 28 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIAutoSave_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HarmonyLib;
using NebulaModel.Logger;
using NebulaWorld;
using UnityEngine;

namespace NebulaPatcher.Patches.Dynamic
{
[HarmonyPatch(typeof(UIAutoSave))]
class UIAutoSave_Patch
{
[HarmonyPostfix]
[HarmonyPatch("_OnOpen")]
public static void _OnOpen_Postfix(UIAutoSave __instance)
{
// Hide AutoSave failed message on clients, since client cannot save in multiplayer
CanvasGroup contentCanvas = AccessTools.Field(__instance.GetType(), "contentCanvas").GetValue(__instance) as CanvasGroup;
contentCanvas?.gameObject.SetActive(!SimulatedWorld.Initialized || LocalPlayer.IsMasterClient);
Log.Warn($"UIAutoSave active: {contentCanvas?.gameObject.activeSelf}");
}

[HarmonyPrefix]
[HarmonyPatch("_OnLateUpdate")]
public static bool _OnLateUpdate_Prefix()
{
return !SimulatedWorld.Initialized || LocalPlayer.IsMasterClient;
}
}
}
20 changes: 18 additions & 2 deletions NebulaPatcher/Patches/Dynamic/UIEscMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ class UIEscMenu_Patch

[HarmonyPrefix]
[HarmonyPatch("_OnOpen")]
public static void _OnOpen_Prefix()
public static void _OnOpen_Prefix(UIEscMenu __instance)
{
// Disable save game button if you are a client in a multiplayer session
Button saveGameWindowButton = AccessTools.Field(typeof(UIEscMenu), "button2").GetValue(__instance) as Button;
SetButtonEnableState(saveGameWindowButton, !SimulatedWorld.Initialized || LocalPlayer.IsMasterClient);

// Disable load game button if in a multiplayer session
Button loadGameWindowButton = AccessTools.Field(typeof(UIEscMenu), "button3").GetValue(__instance) as Button;
SetButtonEnableState(loadGameWindowButton, !SimulatedWorld.Initialized);

// If we are in a multiplayer game already make sure to hide the host game button
if (SimulatedWorld.Initialized)
{
Expand Down Expand Up @@ -64,10 +72,18 @@ private static void OnHostCurrentGameClick()
var session = NebulaBootstrapper.Instance.CreateMultiplayerHostSession();
session.StartServer(port, true);

// Manually call the OnGameLoadCompleted here since we are already in a game.
// Manually call the OnGameLoadCompleted manually since we are already in a game.
SimulatedWorld.OnGameLoadCompleted();

GameMain.Resume();
}

private static void SetButtonEnableState(Button button, bool enable)
{
ColorBlock buttonColors = button.colors;
buttonColors.disabledColor = new Color(1f, 1f, 1f, 0.15f);
button.interactable = enable;
button.colors = buttonColors;
}
}
}
3 changes: 3 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NebulaModel;
using NebulaModel.Logger;
using NebulaPatcher.MonoBehaviours;
using NebulaWorld;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
Expand All @@ -19,6 +20,8 @@ class UIMainMenu_Patch
[HarmonyPatch("_OnOpen")]
public static void _OnOpen_Postfix()
{
SimulatedWorld.ExitingMultiplayerSession = false;

GameObject overlayCanvas = GameObject.Find("Overlay Canvas");
if (overlayCanvas == null)
{
Expand Down
1 change: 1 addition & 0 deletions NebulaWorld/LocalPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static void LeaveGame()
PendingFactories.Clear();
IsMasterClient = false;
SimulatedWorld.Clear();
SimulatedWorld.ExitingMultiplayerSession = true;

if (!UIRoot.instance.backToMainMenu)
{
Expand Down
2 changes: 2 additions & 0 deletions NebulaWorld/SimulatedWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public static class SimulatedWorld
public static bool Initialized { get; private set; }
public static bool IsGameLoaded { get; private set; }
public static bool IsPlayerJoining { get; set; }
public static bool ExitingMultiplayerSession { get; set; }

public static void Initialize()
{
FactoryManager.Initialize();
remotePlayersModels = new Dictionary<ushort, RemotePlayerModel>();
Initialized = true;
ExitingMultiplayerSession = false;
}

/// <summary>
Expand Down

0 comments on commit 893f5e8

Please sign in to comment.