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,64 +1,105 @@
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; } }
public EngineInfo() // 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()
{ {
testprogramname = DEFAULT_ENGINE_NAME; testprogramname = DEFAULT_ENGINE_NAME;
} }
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)
EngineInfo newInfo = new EngineInfo(); {
newInfo.TestSkill = (int)Math.Ceiling(gameconfig.Skills.Count / 2f); //set Medium skill level try { testprogramdialog.InitialDirectory = Path.GetDirectoryName(testapplication.Text); }
newInfo.TestSkin = "Sonic"; catch(Exception) { }
newInfo.TestGametype = -1; //Single player }
configinfo.TestEngines.Add(newInfo);
configinfo.Changed = true;
//store current engine name
if(!String.IsNullOrEmpty(cbEngineSelector.Text))
configinfo.TestProgramName = cbEngineSelector.Text;
//refresh engines list // Browse for test program
cbEngineSelector.Items.Clear(); if(testprogramdialog.ShowDialog() == DialogResult.OK)
foreach(EngineInfo info in configinfo.TestEngines) {
cbEngineSelector.Items.Add(info.TestProgramName); preventchanges = true;
cbEngineSelector.SelectedIndex = configinfo.TestEngines.Count - 1; // Add new EngineInfo
btnRemoveEngine.Enabled = true; EngineInfo newInfo = new EngineInfo();
newInfo.TestSkill = (int)Math.Ceiling(gameconfig.Skills.Count / 2f); // Set Medium skill level
configinfo.TestEngines.Add(newInfo);
configinfo.Changed = true;
preventchanges = false; // Store current engine name
if(!String.IsNullOrEmpty(cbEngineSelector.Text))
configinfo.TestProgramName = cbEngineSelector.Text;
// Open engine browser // Refresh engines list
browsetestprogram_Click(this, EventArgs.Empty); cbEngineSelector.Items.Clear();
foreach(EngineInfo info in configinfo.TestEngines)
cbEngineSelector.Items.Add(info.TestProgramName);
cbEngineSelector.SelectedIndex = configinfo.TestEngines.Count - 1;
btnRemoveEngine.Enabled = true;
preventchanges = false;
// Set engine path
testapplication.Text = testprogramdialog.FileName;
}
} }
//mxd //mxd