Fix NullReferenceException, guard against DirectoryNotFoundException (#26)

* disable "Test All Servers Without Data Clear" by default

* set various controls Enabled property to be conditional on project presence (paving the way for a "File > Close")

* prevent unexpected case where test command is enabled despite no project being loaded

* guard against DirectoryNotFoundException, log it as a MessageBox

* set enabled to true if, only if, and when project is loaded successfully (avoids performance hit of changing Enabled on paint)

* "true" is shorter than "enabled"...
This commit is contained in:
Andrew DeLisa 2019-01-02 16:19:05 -05:00 committed by GrapeshotGames
parent e7ba48f8f6
commit 37d9e2c930
2 changed files with 29 additions and 15 deletions

View file

@ -388,6 +388,7 @@
// //
// testAllServersWithoutDataClearToolStripMenuItem // testAllServersWithoutDataClearToolStripMenuItem
// //
this.testAllServersWithoutDataClearToolStripMenuItem.Enabled = false;
this.testAllServersWithoutDataClearToolStripMenuItem.Name = "testAllServersWithoutDataClearToolStripMenuItem"; this.testAllServersWithoutDataClearToolStripMenuItem.Name = "testAllServersWithoutDataClearToolStripMenuItem";
this.testAllServersWithoutDataClearToolStripMenuItem.Size = new System.Drawing.Size(261, 22); this.testAllServersWithoutDataClearToolStripMenuItem.Size = new System.Drawing.Size(261, 22);
this.testAllServersWithoutDataClearToolStripMenuItem.Text = "Test All Servers (Without Data clear)"; this.testAllServersWithoutDataClearToolStripMenuItem.Text = "Test All Servers (Without Data clear)";

View file

@ -178,6 +178,18 @@ namespace ServerGridEditor
createProjBtn.Visible = !isVisible; createProjBtn.Visible = !isVisible;
} }
public void EnableProjectMenuItems()
{
editToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = true;
mapImageToolStripMenuItem.Enabled = true;
slippyMapToolStripMenuItem.Enabled = true;
cellImagesToolStripMenuItem.Enabled = true;
localExportToolStripMenuItem.Enabled = true;
editServerTemplatesToolStripMenuItem.Enabled = true;
testAllServersWithoutDataClearToolStripMenuItem.Enabled = true;
}
public void SetScaleTxt(float unrealUnits) public void SetScaleTxt(float unrealUnits)
{ {
scaleLbl.Text = "1 pixel = " + unrealUnits + " unreal units"; scaleLbl.Text = "1 pixel = " + unrealUnits + " unreal units";
@ -263,18 +275,9 @@ namespace ServerGridEditor
public void DrawMapToGraphics(ref Graphics g, bool cull = false, bool ignoreTranslation = false, bool forExport = false) public void DrawMapToGraphics(ref Graphics g, bool cull = false, bool ignoreTranslation = false, bool forExport = false)
{ {
if (currentProject == null) if (currentProject == null)
return; return;
editToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = true;
mapImageToolStripMenuItem.Enabled = true;
editServerTemplatesToolStripMenuItem.Enabled = true;
cellImagesToolStripMenuItem.Enabled = true;
slippyMapToolStripMenuItem.Enabled = true;
localExportToolStripMenuItem.Enabled = true;
UpdateScrollBars(); UpdateScrollBars();
RectangleF? culling = null; RectangleF? culling = null;
@ -1635,6 +1638,7 @@ namespace ServerGridEditor
if (loadedProj.successfullyLoaded) if (loadedProj.successfullyLoaded)
{ {
EnableProjectMenuItems();
actualJsonFile = openFileDialog.SafeFileName; actualJsonFile = openFileDialog.SafeFileName;
currentProject = loadedProj; currentProject = loadedProj;
SetScaleTxt(1 / currentProject.coordsScaling); SetScaleTxt(1 / currentProject.coordsScaling);
@ -1999,16 +2003,25 @@ namespace ServerGridEditor
void TestAllServers(bool clearSaveData = false) void TestAllServers(bool clearSaveData = false)
{ {
if (currentProject == null)
return;
string jsonFileName = MainForm.gameDir + "/" + MainForm.actualJsonFile; string jsonFileName = MainForm.gameDir + "/" + MainForm.actualJsonFile;
var enclosingDirectory = Path.GetDirectoryName(jsonFileName);
if (!Directory.Exists(enclosingDirectory))
{
MessageBox.Show($"Asked to create {MainForm.actualJsonFile} in non-existent directory:\n{enclosingDirectory}", "Test Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
File.WriteAllText(jsonFileName, currentProject.Serialize(this)); File.WriteAllText(jsonFileName, currentProject.Serialize(this));
int i = 0; int i = 0;
if (currentProject != null) foreach (Server server in currentProject.servers)
foreach (Server server in currentProject.servers) {
{ ProcessStartInfo serverStartInfo, clientStartInfo;
ProcessStartInfo serverStartInfo, clientStartInfo; server.LaunchPreview(out serverStartInfo, out clientStartInfo, false, clearSaveData, false, ++i);
server.LaunchPreview(out serverStartInfo, out clientStartInfo, false, clearSaveData, false, ++i); }
}
} }
private void editSpawnerTemplatesToolStripMenuItem_Click(object sender, EventArgs e) private void editSpawnerTemplatesToolStripMenuItem_Click(object sender, EventArgs e)