From 1a36b500aa380dc4696b01dcacb201393d375527 Mon Sep 17 00:00:00 2001 From: Hans Brigman Date: Fri, 2 Mar 2018 23:35:04 -0500 Subject: [PATCH] Harden game loading, try to prevent crashes & storing invalid game file names in the settings file. --- RandoTracker/Form1.cs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/RandoTracker/Form1.cs b/RandoTracker/Form1.cs index 336be3c..2c3ad99 100644 --- a/RandoTracker/Form1.cs +++ b/RandoTracker/Form1.cs @@ -107,6 +107,12 @@ private void Form1_Load(object sender, EventArgs e) // ignore error } + if (string.IsNullOrWhiteSpace(gameFile)) + { + // Load something at least + gameFile = Path.Combine(GetExecutingDirectory(), "sml2.xml"); + } + this.Left = 200; this.Top = 200; @@ -191,6 +197,7 @@ private void Form1_Load(object sender, EventArgs e) catch (Exception ex) { MessageBox.Show($"Error loading {gameFile}. {ex.Message}", "Error Loading Game"); + gameFile = string.Empty; return; } @@ -345,7 +352,16 @@ private FontFamily loadFont(string fontName) private void loadGame() { - if (initialLoad) return; + if (initialLoad) + { + return; + } + + if (string.IsNullOrWhiteSpace(gameFile)) + { + return; + } + for (int i = 0; i < 4; i++) { if (lblPlayers[i] != null) @@ -423,7 +439,7 @@ private void loadGame() if (game == null) { - MessageBox.Show("Unable to find root game tag", "Required Tag"); + MessageBox.Show($"Unable to find root game tag for {gameFile}", "Required Tag"); return; } @@ -1678,7 +1694,17 @@ private void reloadLayout() } else { - loadGame(); + try + { + loadGame(); + } + catch (Exception ex) + { + MessageBox.Show($"Failed to reload {gameFile}. {ex.Message}", "Layout Load Error"); + gameFile = string.Empty; + return; + } + serverSendBytes(0xf4, Encoding.UTF8.GetBytes(Path.GetFileNameWithoutExtension(gameFile))); } }