Skip to content

Commit

Permalink
Harden game loading, try to prevent crashes & storing invalid game file
Browse files Browse the repository at this point in the history
names in the settings file.
  • Loading branch information
Datyedyeguy committed Mar 3, 2018
1 parent 6ecdbff commit 1a36b50
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions RandoTracker/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)));
}
}
Expand Down

0 comments on commit 1a36b50

Please sign in to comment.