Skip to content

Commit 5ae3d30

Browse files
authored
Merge pull request #543 from sp00ktober/bugfix
more error messages and reconnect command
2 parents af25612 + fbc3f51 commit 5ae3d30

File tree

9 files changed

+136
-6
lines changed

9 files changed

+136
-6
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using HarmonyLib;
2+
using NebulaWorld;
3+
using NebulaWorld.GameStates;
4+
5+
namespace NebulaPatcher.Patches.Dynamic
6+
{
7+
[HarmonyPatch(typeof(GameCamera))]
8+
public class GameCamera_Patch
9+
{
10+
[HarmonyPrefix]
11+
[HarmonyPatch(nameof(GameCamera.Logic))]
12+
public static bool Logic_Prefix()
13+
{
14+
// prevent NRE while doing a reconnect as a client issued through the chat command
15+
return !(GameStatesManager.DuringReconnect && GameMain.mainPlayer == null);
16+
}
17+
}
18+
}

NebulaPatcher/Patches/Dynamic/GameData_Patch.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NebulaModel.Packets.Players;
77
using NebulaPatcher.Patches.Transpilers;
88
using NebulaWorld;
9+
using NebulaWorld.Warning;
910
using System;
1011
using UnityEngine;
1112

@@ -56,13 +57,29 @@ public static bool GetOrCreateFactory_Prefix(GameData __instance, ref PlanetFact
5657
factoryIndex = GameMain.data.factoryCount++;
5758
planet.factoryIndex = factoryIndex;
5859
__instance.factories[factoryIndex] = new PlanetFactory();
59-
__instance.factories[factoryIndex].Import(factoryIndex, __instance, reader.BinaryReader);
60+
try
61+
{
62+
__instance.factories[factoryIndex].Import(factoryIndex, __instance, reader.BinaryReader);
63+
}
64+
catch(System.InvalidOperationException e)
65+
{
66+
HandleRemoteDataImportError(e);
67+
return false;
68+
}
6069
planet.factory = __instance.factories[factoryIndex];
6170
}
6271
else
6372
{
6473
factoryIndex = planet.factoryIndex;
65-
__instance.factories[factoryIndex].Import(factoryIndex, __instance, reader.BinaryReader);
74+
try
75+
{
76+
__instance.factories[factoryIndex].Import(factoryIndex, __instance, reader.BinaryReader);
77+
}
78+
catch (System.InvalidOperationException e)
79+
{
80+
HandleRemoteDataImportError(e);
81+
return false;
82+
}
6683
planet.factory = __instance.factories[factoryIndex];
6784
}
6885
// Initial FactoryProductionStat for other in-game stats checking functions
@@ -81,6 +98,15 @@ public static bool GetOrCreateFactory_Prefix(GameData __instance, ref PlanetFact
8198
return false;
8299
}
83100

101+
// a user reported to receive an error regarding decompressing the lz4 data which breaks the game as the planet factory cant be loaded.
102+
// it is not known what exactly caused the error but that client seemed to have an instable internet connection.
103+
// so we give advice in this rare situation to issue a reconnect.
104+
private static void HandleRemoteDataImportError(System.InvalidOperationException e)
105+
{
106+
WarningManager.DisplayCriticalWarning($"Failed to properly decompress and import factory data.\nPlease do a reconnect.\nSee the logfile for more information.");
107+
Log.Error($"There was an error while decompressing and importing factory data, probably due to an instable internet connection. See full error below.\n{e.StackTrace}");
108+
}
109+
84110
[HarmonyPrefix]
85111
[HarmonyPatch(nameof(GameData.OnActivePlanetLoaded))]
86112
public static bool OnActivePlanetLoaded_Prefix(GameData __instance, PlanetData planet)

NebulaPatcher/Patches/Dynamic/MonitorComponent_Patch.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ public static void SetTargetBelt_Prefix(MonitorComponent __instance, int __0, in
131131
new MonitorSettingUpdatePacket(planetId, __instance.id, MonitorSettingEvent.SetTargetBelt, __0, __1));
132132
}
133133
}
134+
135+
[HarmonyPrefix]
136+
[HarmonyPatch(nameof(MonitorComponent.InternalUpdate))]
137+
public static bool InternalUpdate_Prefix(MonitorComponent __instance, CargoTraffic _traffic, EntityData[] _entityPool, SpeakerComponent[] _speakerPool, AnimData[] _animPool)
138+
{
139+
if (Multiplayer.IsActive && __instance.targetBeltId > _traffic.beltPool.Length)
140+
{
141+
if (Multiplayer.Session.LocalPlayer.IsHost)
142+
{
143+
_traffic.factory.RemoveEntityWithComponents(__instance.entityId);
144+
WarningManager.DisplayTemporaryWarning($"Broken Traffic Monitor detected on {_traffic.factory.planet.displayName}\nIt was removed, clients should reconnect!", 15000);
145+
}
146+
return false;
147+
}
148+
return true;
149+
}
134150
}
135151
#pragma warning restore Harmony003 // Harmony non-ref patch parameters modified
136152
}

NebulaPatcher/Patches/Dynamic/UIEscMenu_Patch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void OnButton6Click_Prefix()
3737
QuitGame();
3838
}
3939

40-
private static void QuitGame()
40+
public static void QuitGame()
4141
{
4242
if (Multiplayer.IsActive)
4343
{

NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,14 @@ private static void AddMultiplayerJoinMenu()
194194

195195
private static void OnJoinGameButtonClick()
196196
{
197-
// Remove whitespaces from connection string
198197
string s = new string(hostIPAdressInput.text.ToCharArray().Where(c => !char.IsWhiteSpace(c)).ToArray());
198+
JoinGame(s);
199+
}
200+
201+
public static void JoinGame(string ip)
202+
{
203+
// Remove whitespaces from connection string
204+
string s = ip;
199205

200206
// Taken from .net IPEndPoint
201207
IPEndPoint result = null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using HarmonyLib;
2+
using NebulaModel;
3+
using NebulaWorld.GameStates;
4+
5+
namespace NebulaPatcher.Patches.Dynamic
6+
{
7+
[HarmonyPatch(typeof(VFPreload))]
8+
internal class VFPreload_Patch
9+
{
10+
[HarmonyPostfix]
11+
[HarmonyPatch(nameof(VFPreload.InvokeOnLoadWorkEnded))]
12+
public static void InvokeOnLoadWorkEnded_Postfix()
13+
{
14+
if (GameStatesManager.DuringReconnect)
15+
{
16+
string ip = "127.0.0.1:8469";
17+
if (Config.Options.RememberLastIP && !string.IsNullOrWhiteSpace(Config.Options.LastIP))
18+
{
19+
ip = Config.Options.LastIP;
20+
}
21+
22+
UIMainMenu_Patch.JoinGame(ip);
23+
GameStatesManager.DuringReconnect = false;
24+
}
25+
}
26+
}
27+
}

NebulaWorld/Chat/ChatCommandRegistry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static ChatCommandRegistry()
6666
RegisterCommand("xconsole", new XConsoleCommandHandler(), "x");
6767
RegisterCommand("navigate", new NavigateCommandHandler(), "n");
6868
RegisterCommand("system", new SystemCommandHandler(), "s");
69+
RegisterCommand("reconnect", new ReconnectCommandHandler(), "r");
6970
}
7071
}
7172

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NebulaModel.DataStructures;
2+
using NebulaWorld.GameStates;
3+
using NebulaWorld.MonoBehaviours.Local;
4+
5+
namespace NebulaWorld.Chat.Commands
6+
{
7+
public class ReconnectCommandHandler : IChatCommandHandler
8+
{
9+
public void Execute(ChatWindow window, string[] parameters)
10+
{
11+
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
12+
{
13+
window.SendLocalChatMessage("This command can only be used in multiplayer and as client!", ChatMessageType.CommandErrorMessage);
14+
return;
15+
}
16+
GameStatesManager.DoFastReconnect();
17+
}
18+
19+
public string[] GetUsage()
20+
{
21+
return new string[] { $"" };
22+
}
23+
24+
public string GetDescription()
25+
{
26+
return "performs a reconnect.";
27+
}
28+
}
29+
}

NebulaWorld/GameStates/GameStatesManager.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using NebulaModel.Logger;
2-
using System;
1+
using System;
32

43
namespace NebulaWorld.GameStates
54
{
@@ -16,9 +15,17 @@ public void Dispose()
1615

1716
public static long RealGameTick => GameMain.gameTick;
1817
public static float RealUPS => (float)FPSController.currentUPS;
18+
public static bool DuringReconnect = false;
1919

2020
public static void NotifyTickDifference(float delta)
2121
{
2222
}
23+
24+
public static void DoFastReconnect()
25+
{
26+
// trigger game exit to main menu
27+
DuringReconnect = true;
28+
UIRoot.instance.uiGame.escMenu.OnButton5Click();
29+
}
2330
}
2431
}

0 commit comments

Comments
 (0)