Skip to content

Commit 35728f2

Browse files
committed
improved error handling
1 parent ee372a9 commit 35728f2

12 files changed

Lines changed: 95 additions & 119 deletions

File tree

FezMultiplayerDedicatedServer/MultiplayerServer/FezDedicatedServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ static void Main(string[] prog_args)
567567
catch (Exception e)
568568
{
569569
Console.Error.WriteLine(e);
570-
System.Diagnostics.Debugger.Launch();
570+
SharedTools.HandleUnexpectedException(e);
571571
}
572572
server.Dispose();
573573
}

FezMultiplayerDedicatedServer/MultiplayerServer/MultiplayerServerNetcode.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,6 @@ Dictionary<string, string> parseHeaders(IEnumerable<string> headerLines)
398398
}
399399
else if (ping)
400400
{
401-
System.Diagnostics.Debugger.Launch();
402-
System.Diagnostics.Debugger.Break();
403401
SendWebSocketMessage(writer, message, opcode: 0xA);
404402
}
405403
else if (message.Length > 0)
@@ -1204,8 +1202,7 @@ protected override void ProcessActiveLevelState(ActiveLevelState activeLevelStat
12041202
//TODO not yet implemented
12051203
if (SharedConstants.TODO_Debug_EnableLevelStateSync)
12061204
{
1207-
System.Diagnostics.Debugger.Launch();
1208-
System.Diagnostics.Debugger.Break();
1205+
SharedTools.HandleUnexpectedException(new NotImplementedException());
12091206
}
12101207
}
12111208
/// <summary>

FezMultiplayerMod/MultiplayerMod/FezMultiplayerMod.cs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public FezMultiplayerMod(Game game)
131131
{
132132
settingsFileReadFailed = true;
133133
mp.FatalException = e;
134-
SharedTools.LogWarning("FezMultiplayerMod", $"Failed to read settings file (path: \"{System.IO.Path.GetFullPath(SettingsFilePath)}\"", (int)Common.LogSeverity.Error);
134+
SharedTools.LogWarning("FezMultiplayerMod", $"Failed to read settings file (path: \"{System.IO.Path.GetFullPath(SettingsFilePath)}\"", LogSeverity.Error);
135135
}
136136
mp = new MultiplayerClient(settings);
137137
void TryWriteSettingsFile()
@@ -143,7 +143,7 @@ void TryWriteSettingsFile()
143143
catch (Exception e)
144144
{
145145
mp.FatalException = e;
146-
SharedTools.LogWarning("FezMultiplayerMod", $"Failed to {(settingsFileReadFailed ? "read & " : "")}write to settings file (path: \"{System.IO.Path.GetFullPath(SettingsFilePath)}\"", (int)Common.LogSeverity.Error);
146+
SharedTools.LogWarning("FezMultiplayerMod", $"Failed to {(settingsFileReadFailed ? "read & " : "")}write to settings file (path: \"{System.IO.Path.GetFullPath(SettingsFilePath)}\"", LogSeverity.Error);
147147
}
148148
}
149149

@@ -234,7 +234,7 @@ void AddCommand(string name, string helpText, Func<string[], List<string>> autoc
234234
FezugConsolePrint($"Disconnected from {mp.RemoteEndpoint}", Color.Yellow);
235235
}
236236
};
237-
SharedTools.OnLogWarning += (message, severity) => FezugConsolePrintWithSeverity(message, severity);
237+
SharedTools.OnLogWarning += (message, severity) => FezugConsolePrintWithSeverity(message, (int)severity);
238238
var default_ips = new List<string>() { "127.0.0.1", "[::1]" }.Select(a => a + ":" + SharedConstants.DefaultPort);
239239
AddCommand("mp_connect",
240240
$"mp_connect <ip> - [{nameof(FezMultiplayerMod)}] connect to the given IP address",
@@ -321,13 +321,20 @@ public override void Initialize()
321321
}
322322
private int ShownFontTest = 0;
323323
private float ShownFontTestScale = 0.95f;
324+
private const long ExtraMessageShowTimeMilliseconds = 3000;
325+
private const long MainMessageShowTimeMilliseconds = 7000;
326+
324327
public override void Update(GameTime gameTime)
325328
{
326329
base.Update(gameTime);
327-
if (mp.LastExtraMessageUpdate.ElapsedMilliseconds > 3000)
330+
if (mp.LastExtraMessageUpdate.ElapsedMilliseconds > ExtraMessageShowTimeMilliseconds)
328331
{
329332
mp.ExtraMessage = null;
330333
}
334+
if (mp.LastMainMessageUpdate.ElapsedMilliseconds > MainMessageShowTimeMilliseconds)
335+
{
336+
mp.ErrorMessage = null;
337+
}
331338

332339
#if DEBUG
333340
if (KeyboardState.GetKeyState(TestHostServerConnectionKey) == FezButtonState.Pressed)
@@ -389,24 +396,12 @@ public override void Update(GameTime gameTime)
389396
{
390397
//Connection refused
391398
//TODO
392-
#if DEBUG
393-
if (!System.Diagnostics.Debugger.IsAttached)
394-
{
395-
System.Diagnostics.Debugger.Launch();
396-
}
397-
System.Diagnostics.Debugger.Break();
398-
#endif
399+
SharedTools.HandleUnexpectedException(e);
399400
throw e;
400401
}
401402
catch (Exception e)
402403
{
403-
#if DEBUG
404-
if (!System.Diagnostics.Debugger.IsAttached)
405-
{
406-
System.Diagnostics.Debugger.Launch();
407-
}
408-
System.Diagnostics.Debugger.Break();
409-
#endif
404+
SharedTools.HandleUnexpectedException(e);
410405
throw e;//This should never happen
411406
}
412407
}
@@ -520,10 +515,7 @@ public override void Draw(GameTime gameTime)
520515
}
521516
catch (Exception e)
522517
{
523-
FezSharedTools.SharedTools.LogWarning(typeof(FezMultiplayerMod).Name, e.ToString());
524-
#if DEBUG
525-
System.Diagnostics.Debugger.Launch();
526-
#endif
518+
SharedTools.HandleUnexpectedException(e, LogSeverity.Warning);
527519
}
528520
}
529521
}
@@ -544,20 +536,20 @@ public override void Draw(GameTime gameTime)
544536
}
545537
if (mp.FatalException != null)
546538
{
539+
// Note: this code is duplicated in GuiPlayerList.Draw
547540
if (!ShowingFatalException)
548541
{
549542
ShowingFatalException = true;
550543
ShowingFatalExceptionStartTimestamp = gameTime.TotalGameTime;
551-
#if DEBUG
552-
System.Diagnostics.Debugger.Launch();
544+
SharedTools.HandleUnexpectedException(mp.FatalException);
553545
//TODO relay connection problems to the user more effectively
554-
#endif
555546
}
556547
s += "\nWarning: " + mp.FatalException.Message;
557548

558549

559550
if ((gameTime.TotalGameTime - ShowingFatalExceptionStartTimestamp).TotalSeconds > 5.0f)
560551
{
552+
// clear the error
561553
mp.FatalException = null;
562554
ShowingFatalException = false;
563555
}
@@ -617,7 +609,7 @@ private ActionType GetActionTypeForRotationDifference(ActionType action, Viewpoi
617609
int newIndex = baseActionIndex + (int)viewpointDifference;
618610
if (newIndex >= actionTypeRotationMap.Length || newIndex < 0)
619611
{
620-
System.Diagnostics.Debugger.Launch();
612+
SharedTools.HandleUnexpectedException(new IndexOutOfRangeException($"Index {newIndex} is outside the range {0} and {actionTypeRotationMap.Length}"));
621613
return action;
622614
}
623615
ActionType newAction = actionTypeRotationMap[newIndex];

FezMultiplayerMod/MultiplayerMod/GuiPlayerList.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public override void Initialize()
9898
public override void Update(GameTime gameTime)
9999
{
100100
base.Update(gameTime);
101-
if (!ServiceHelper.FirstLoadDone)
101+
if (!ServiceHelper.FirstLoadDone || InputManager == null)
102102
{
103103
return;
104104
}
@@ -123,31 +123,29 @@ public override void Update(GameTime gameTime)
123123
public int borderWidth = 1;
124124
public override void Draw(GameTime gameTime)
125125
{
126-
if (this.disposing || !Visible || !ServiceHelper.FirstLoadDone)
126+
if (this.disposing || !Visible || !ServiceHelper.FirstLoadDone || FontManager == null)
127127
{
128128
return;
129129
}
130130

131131
List<string> lines = new List<string>();
132132
var me = mp.MyPlayerMetadata;
133-
if (me != null)
133+
if (me != null && mp.ActiveConnectionState != ConnectionState.Connected)
134134
{
135135
lines.Add($"(you): {mp.MyAppearance.PlayerName}, "//{mp.MyUuid}, "
136136
+ $"{((me.CurrentLevelName == null || me.CurrentLevelName.Length == 0) ? "???" : me.CurrentLevelName)}, "
137137
+ $"{me.Action}, {me.CameraViewpoint}, "
138138
+ $"{me.Position.Round(3)}, "
139139
+ $"ping: {(mp.ConnectionLatencyUpDown) / TimeSpan.TicksPerMillisecond}ms");
140140
}
141-
string connectionStatusText = "";
142141
if (mp.ExtraMessage != null)
143142
{
144-
connectionStatusText += $"{mp.ExtraMessage}\n";
143+
lines.Add(mp.ExtraMessage);
145144
}
146145
if (mp.ErrorMessage != null)
147146
{
148-
connectionStatusText += $"{mp.ErrorMessage}\n";
147+
lines.Add(mp.ErrorMessage);
149148
}
150-
lines.Add(connectionStatusText);
151149

152150
bool doDrawPlayerTable = false;
153151
switch (mp.ActiveConnectionState)
@@ -166,23 +164,9 @@ public override void Draw(GameTime gameTime)
166164
}
167165
if (mp.FatalException != null)
168166
{
169-
if (!ShowingFatalException)
170-
{
171-
ShowingFatalException = true;
172-
ShowingFatalExceptionStartTimestamp = gameTime.TotalGameTime;
173-
#if DEBUG
174-
System.Diagnostics.Debugger.Launch();
175-
//TODO relay connection problems to the user more effectively
176-
#endif
177-
}
167+
// fatal exception handling is in FezMultiplayerMod.Draw
168+
//TODO relay connection problems to the user more effectively
178169
lines.Add("Warning: " + mp.FatalException.Message);
179-
180-
181-
if ((gameTime.TotalGameTime - ShowingFatalExceptionStartTimestamp).TotalSeconds > 5.0f)
182-
{
183-
mp.FatalException = null;
184-
ShowingFatalException = false;
185-
}
186170
}
187171
float scale = 1f;
188172
drawer.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullNone);
@@ -191,6 +175,7 @@ public override void Draw(GameTime gameTime)
191175
{
192176
Vector2 lineSize = RichTextRenderer.MeasureString(FontManager, line) * scale;
193177
origin.X = (GraphicsDevice.Viewport.Width - lineSize.X) / 2f;
178+
drawer.DrawRect(new Rectangle((int)origin.X, (int)origin.Y, (int)lineSize.X, (int)lineSize.Y).Inset(lineSize.Y * -0.5f, 0), BackgroundColor);
194179
drawer.DrawTextRichShadow(FontManager, line, origin, scale);
195180
origin.Y += lineSize.Y;
196181
}
@@ -353,7 +338,5 @@ public override void Draw(GameTime gameTime)
353338
}
354339
}
355340
}
356-
private bool ShowingFatalException = false;
357-
private TimeSpan ShowingFatalExceptionStartTimestamp = TimeSpan.Zero;
358341
}
359342
}

FezMultiplayerMod/MultiplayerMod/MultiplayerClient.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ protected override ActiveLevelState GetCurrentLevelState()
7171
//TODO not yet implemented
7272
if (SharedConstants.TODO_Debug_EnableLevelStateSync)
7373
{
74-
System.Diagnostics.Debugger.Launch();
75-
System.Diagnostics.Debugger.Break();
74+
SharedTools.HandleUnexpectedException(new NotImplementedException());
7675
}
7776
return new ActiveLevelState();
7877
}
@@ -84,8 +83,7 @@ protected override void ProcessActiveLevelState(ActiveLevelState activeLevelStat
8483
//TODO not yet implemented
8584
if (SharedConstants.TODO_Debug_EnableLevelStateSync)
8685
{
87-
System.Diagnostics.Debugger.Launch();
88-
System.Diagnostics.Debugger.Break();
86+
SharedTools.HandleUnexpectedException(new NotImplementedException());
8987
}
9088
}
9189
}

FezMultiplayerMod/MultiplayerMod/MultiplayerClientNetcode.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Common;
21
using FezGame.Services;
32
using FezSharedTools;
43
using MonoMod.RuntimeDetour;
@@ -21,10 +20,11 @@ public enum ConnectionState
2120
public abstract class MultiplayerClientNetcode : SharedNetcode<PlayerMetadata>, IDisposable
2221
{
2322
internal volatile Stopwatch LastExtraMessageUpdate = Stopwatch.StartNew();
23+
internal volatile Stopwatch LastMainMessageUpdate = Stopwatch.StartNew();
2424
internal volatile string ExtraMessage;
2525
protected static void LogStatus(LogSeverity severity, string message, bool extra = false)
2626
{
27-
FezSharedTools.SharedTools.LogWarning(typeof(MultiplayerClientNetcode).Name, message, (int)severity);
27+
FezSharedTools.SharedTools.LogWarning(typeof(MultiplayerClientNetcode).Name, message, severity);
2828
if (Instance != null)
2929
{
3030
if (extra)
@@ -35,6 +35,7 @@ protected static void LogStatus(LogSeverity severity, string message, bool extra
3535
else
3636
{
3737
Instance.ErrorMessage = message;
38+
Instance.LastMainMessageUpdate.Restart();
3839
}
3940
}
4041
}

FezMultiplayerMod/MultiplayerMod/RichTextRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FezEngine.Components;
22
using FezEngine.Services;
33
using FezEngine.Tools;
4+
using FezSharedTools;
45
using Microsoft.Xna.Framework;
56
using Microsoft.Xna.Framework.Graphics;
67
using System;
@@ -894,8 +895,7 @@ private static Vector2 ProcessECMA48EscapeCodes(in SpriteFont defaultFont, in fl
894895
catch (Exception e)
895896
{
896897
tokensize = fontData.Font.MeasureString("" + (fontData.Font.DefaultCharacter ?? ' ')) * fontData.Scale * scale;
897-
FezSharedTools.SharedTools.LogWarning(typeof(RichTextRenderer).Name, e.ToString());
898-
System.Diagnostics.Debugger.Launch();
898+
SharedTools.HandleUnexpectedException(e, LogSeverity.Warning);
899899
}
900900
onToken(token, currentPositionOffset, tokens, currentTokenIndex, tokensize);
901901
linesize.Y = Math.Max(linesize.Y, tokensize.Y);

FezMultiplayerMod/MultiplayerMod/ServerDiscoverer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using FezSharedTools;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Net;
@@ -55,13 +56,13 @@ internal ServerDiscoverer(IPEndPoint MulticastEndpoint)
5556
return;
5657
}
5758
byte[] receivedBytes = client.EndReceive(result, ref remoteEndPoint);
58-
string receivedMessage = FezSharedTools.SharedConstants.UTF8.GetString(receivedBytes);
59+
string receivedMessage = SharedConstants.UTF8.GetString(receivedBytes);
5960

6061
try
6162
{
6263
OnReceiveData(remoteEndPoint, receivedMessage
6364
.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries)
64-
.Select(a => a.Split(new[] { FezSharedTools.SharedConstants.ServerDiscoveryEntrySeparator }, 2, StringSplitOptions.None))
65+
.Select(a => a.Split(new[] { SharedConstants.ServerDiscoveryEntrySeparator }, 2, StringSplitOptions.None))
6566
.GroupBy(p => p[0])
6667
.ToDictionary(g => g.Key, g =>
6768
{
@@ -72,8 +73,7 @@ internal ServerDiscoverer(IPEndPoint MulticastEndpoint)
7273
}
7374
catch (Exception e)
7475
{
75-
FezSharedTools.SharedTools.LogWarning(typeof(ServerDiscoverer).Name, e.ToString());
76-
System.Diagnostics.Debugger.Launch();
76+
SharedTools.HandleUnexpectedException(e, LogSeverity.Warning);
7777
}
7878
}, null);
7979
}
@@ -85,7 +85,7 @@ internal ServerDiscoverer(IPEndPoint MulticastEndpoint)
8585
}
8686
catch (Exception e)
8787
{
88-
System.Diagnostics.Debugger.Break();
88+
SharedTools.HandleUnexpectedException(e);
8989
throw e;
9090
}
9191
})

FezMultiplayerMod/MultiplayerMod/ServerListMenu.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,7 @@ private MenuLevel CurrentMenuLevel
661661
{
662662
if (value == null)
663663
{
664-
System.Diagnostics.Debugger.Launch();
665-
System.Diagnostics.Debugger.Break();
664+
SharedTools.HandleUnexpectedException(new ArgumentNullException());
666665
return;
667666
}
668667
__currentMenu = value;

0 commit comments

Comments
 (0)