Fixed, Game configurations window: new test engine entry was still added when pressing "New Test Engine" button, then canceling the browse dialog.

Fixed, general interface: in some cases opening "Test Map" drop-down resulted in an exception when trying to access a disposed/non-existing test engine icon.
This commit is contained in:
MaxED 2016-04-26 22:28:03 +00:00 committed by spherallic
parent b7fbb443e2
commit 6982133004
2 changed files with 92 additions and 52 deletions

View file

@ -1,27 +1,48 @@
using System; #region ================== Namespaces
using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
#endregion
namespace CodeImp.DoomBuilder.GZBuilder.Data namespace CodeImp.DoomBuilder.GZBuilder.Data
{ {
public class EngineInfo : IDisposable public class EngineInfo : IDisposable
{ {
// Disposing #region ================== Constants
private bool isdisposed;
public const string DEFAULT_ENGINE_NAME = "Engine with no name"; public const string DEFAULT_ENGINE_NAME = "Engine with no name";
#endregion
#region ================== Variables
// Settings
private string testprogramname; private string testprogramname;
public string TestProgramName { get { return testprogramname; } set { testprogramname = value; CheckProgramName(); } } private string testprogram;
public string TestProgram; private Bitmap icon;
public string TestParameters; public string TestParameters;
public bool CustomParameters; public bool CustomParameters;
public int TestSkill; public int TestSkill;
public string TestSkin; public string TestSkin;
public int TestGametype; public int TestGametype;
public bool TestShortPaths; public bool TestShortPaths;
private Bitmap icon;
public Bitmap TestProgramIcon { get { return icon; } } // Disposing
private bool isdisposed;
#endregion
#region ================== Properties
public string TestProgramName { get { return testprogramname; } set { testprogramname = value; CheckProgramName(); } }
public string TestProgram { get { return testprogram; } set { testprogram = value; CheckProgramName(); } }
public Bitmap TestProgramIcon { get { if(icon == null) UpdateIcon(); return icon; } }
#endregion
#region ================== Constructors / Disposer
public EngineInfo() public EngineInfo()
{ {
@ -31,34 +52,54 @@ namespace CodeImp.DoomBuilder.GZBuilder.Data
public EngineInfo(EngineInfo other) public EngineInfo(EngineInfo other)
{ {
testprogramname = other.TestProgramName; testprogramname = other.TestProgramName;
TestProgram = other.TestProgram; testprogram = other.testprogram;
TestParameters = other.TestParameters; TestParameters = other.TestParameters;
CustomParameters = other.CustomParameters; CustomParameters = other.CustomParameters;
TestSkill = other.TestSkill; TestSkill = other.TestSkill;
TestSkin = other.TestSkin; TestSkin = other.TestSkin;
TestGametype = other.TestGametype; TestGametype = other.TestGametype;
TestShortPaths = other.TestShortPaths; TestShortPaths = other.TestShortPaths;
icon = other.icon;
UpdateIcon();
} }
public void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
icon.Dispose();
// Done
isdisposed = true;
}
}
#endregion
#region ================== Methods
private void CheckProgramName() private void CheckProgramName()
{ {
if(testprogramname == DEFAULT_ENGINE_NAME && !String.IsNullOrEmpty(TestProgram)) if(testprogramname == DEFAULT_ENGINE_NAME && !String.IsNullOrEmpty(testprogram))
{ {
// Get engine name from path // Get engine name from path
testprogramname = Path.GetFileNameWithoutExtension(TestProgram); testprogramname = Path.GetFileNameWithoutExtension(testprogram);
} }
}
// Update icon private void UpdateIcon()
{
if(icon != null) if(icon != null)
{ {
icon.Dispose(); icon.Dispose();
icon = null; icon = null;
} }
if(File.Exists(TestProgram)) if(File.Exists(testprogram))
{ {
Icon i = Icon.ExtractAssociatedIcon(TestProgram); Icon i = Icon.ExtractAssociatedIcon(testprogram);
icon = (i != null ? i.ToBitmap() : new Bitmap(Properties.Resources.Question)); icon = (i != null ? i.ToBitmap() : new Bitmap(Properties.Resources.Question));
} }
else else
@ -67,17 +108,6 @@ namespace CodeImp.DoomBuilder.GZBuilder.Data
} }
} }
public void Dispose() #endregion
{ }
// Not already disposed?
if (!isdisposed)
{
// Clean up
icon.Dispose();
// Done
isdisposed = true;
}
}
}
} }

View file

@ -746,31 +746,41 @@ namespace CodeImp.DoomBuilder.Windows
//mxd //mxd
private void btnNewEngine_Click(object sender, EventArgs e) private void btnNewEngine_Click(object sender, EventArgs e)
{ {
preventchanges = true; // Set initial directory?
if(testapplication.Text.Length > 0)
{
try { testprogramdialog.InitialDirectory = Path.GetDirectoryName(testapplication.Text); }
catch(Exception) { }
}
EngineInfo newInfo = new EngineInfo(); // Browse for test program
newInfo.TestSkill = (int)Math.Ceiling(gameconfig.Skills.Count / 2f); //set Medium skill level if(testprogramdialog.ShowDialog() == DialogResult.OK)
newInfo.TestSkin = "Sonic"; {
newInfo.TestGametype = -1; //Single player preventchanges = true;
configinfo.TestEngines.Add(newInfo);
configinfo.Changed = true;
//store current engine name // Add new EngineInfo
if(!String.IsNullOrEmpty(cbEngineSelector.Text)) EngineInfo newInfo = new EngineInfo();
configinfo.TestProgramName = cbEngineSelector.Text; newInfo.TestSkill = (int)Math.Ceiling(gameconfig.Skills.Count / 2f); // Set Medium skill level
configinfo.TestEngines.Add(newInfo);
configinfo.Changed = true;
//refresh engines list // Store current engine name
cbEngineSelector.Items.Clear(); if(!String.IsNullOrEmpty(cbEngineSelector.Text))
foreach(EngineInfo info in configinfo.TestEngines) configinfo.TestProgramName = cbEngineSelector.Text;
cbEngineSelector.Items.Add(info.TestProgramName);
cbEngineSelector.SelectedIndex = configinfo.TestEngines.Count - 1; // Refresh engines list
btnRemoveEngine.Enabled = true; cbEngineSelector.Items.Clear();
foreach(EngineInfo info in configinfo.TestEngines)
cbEngineSelector.Items.Add(info.TestProgramName);
preventchanges = false; cbEngineSelector.SelectedIndex = configinfo.TestEngines.Count - 1;
btnRemoveEngine.Enabled = true;
// Open engine browser preventchanges = false;
browsetestprogram_Click(this, EventArgs.Empty);
// Set engine path
testapplication.Text = testprogramdialog.FileName;
}
} }
//mxd //mxd