using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DM2LLGDATMaker
{
	/// <summary>
	/// Form1 の概要の説明です。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox comboBoxConf;
		private System.Windows.Forms.GroupBox groupBoxExp;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBoxGD;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBoxExpDir;
		private System.Windows.Forms.Button buttonBrowseGD;
		private System.Windows.Forms.Button buttonBrowseExpDir;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBoxNewGD;
		private System.Windows.Forms.Button buttonBrowseNewGD;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox textBoxGDINI;
		private System.Windows.Forms.Button buttonBrowseGDINI;
		private System.Windows.Forms.OpenFileDialog openFileDialogInGD;
		private System.Windows.Forms.SaveFileDialog saveFileDialogExpDir;
		private System.Windows.Forms.SaveFileDialog saveFileDialogIntoGD;
		private System.Windows.Forms.OpenFileDialog openFileDialogInGDINI;
		private System.Windows.Forms.Button buttonExpNow;
		private System.Windows.Forms.Button buttonBuildNow;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.comboBoxConf = new System.Windows.Forms.ComboBox();
			this.groupBoxExp = new System.Windows.Forms.GroupBox();
			this.buttonExpNow = new System.Windows.Forms.Button();
			this.buttonBrowseExpDir = new System.Windows.Forms.Button();
			this.textBoxExpDir = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.buttonBrowseGD = new System.Windows.Forms.Button();
			this.textBoxGD = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.buttonBuildNow = new System.Windows.Forms.Button();
			this.buttonBrowseGDINI = new System.Windows.Forms.Button();
			this.textBoxGDINI = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.buttonBrowseNewGD = new System.Windows.Forms.Button();
			this.textBoxNewGD = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.openFileDialogInGD = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialogExpDir = new System.Windows.Forms.SaveFileDialog();
			this.saveFileDialogIntoGD = new System.Windows.Forms.SaveFileDialog();
			this.openFileDialogInGDINI = new System.Windows.Forms.OpenFileDialog();
			this.groupBoxExp.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(161, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "Predefined game configuration";
			// 
			// comboBoxConf
			// 
			this.comboBoxConf.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxConf.Items.AddRange(new object[] {
															  "DM2 Amiga",
															  "DM2 PC",
															  "DM2 PC9801"});
			this.comboBoxConf.Location = new System.Drawing.Point(8, 24);
			this.comboBoxConf.Name = "comboBoxConf";
			this.comboBoxConf.Size = new System.Drawing.Size(272, 20);
			this.comboBoxConf.TabIndex = 1;
			this.comboBoxConf.SelectedIndexChanged += new System.EventHandler(this.comboBoxConf_SelectedIndexChanged);
			// 
			// groupBoxExp
			// 
			this.groupBoxExp.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.groupBoxExp.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.buttonExpNow,
																					  this.buttonBrowseExpDir,
																					  this.textBoxExpDir,
																					  this.label3,
																					  this.buttonBrowseGD,
																					  this.textBoxGD,
																					  this.label2});
			this.groupBoxExp.Location = new System.Drawing.Point(8, 48);
			this.groupBoxExp.Name = "groupBoxExp";
			this.groupBoxExp.Size = new System.Drawing.Size(272, 128);
			this.groupBoxExp.TabIndex = 2;
			this.groupBoxExp.TabStop = false;
			this.groupBoxExp.Text = "Exporter";
			// 
			// buttonExpNow
			// 
			this.buttonExpNow.Location = new System.Drawing.Point(8, 96);
			this.buttonExpNow.Name = "buttonExpNow";
			this.buttonExpNow.TabIndex = 7;
			this.buttonExpNow.Text = "Export";
			this.buttonExpNow.Click += new System.EventHandler(this.buttonExpNow_Click);
			// 
			// buttonBrowseExpDir
			// 
			this.buttonBrowseExpDir.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.buttonBrowseExpDir.Location = new System.Drawing.Point(200, 72);
			this.buttonBrowseExpDir.Name = "buttonBrowseExpDir";
			this.buttonBrowseExpDir.Size = new System.Drawing.Size(64, 19);
			this.buttonBrowseExpDir.TabIndex = 5;
			this.buttonBrowseExpDir.Text = "Browse";
			this.buttonBrowseExpDir.Click += new System.EventHandler(this.buttonBrowseExpDir_Click);
			// 
			// textBoxExpDir
			// 
			this.textBoxExpDir.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBoxExpDir.Location = new System.Drawing.Point(8, 72);
			this.textBoxExpDir.Name = "textBoxExpDir";
			this.textBoxExpDir.Size = new System.Drawing.Size(184, 19);
			this.textBoxExpDir.TabIndex = 4;
			this.textBoxExpDir.Text = "";
			// 
			// label3
			// 
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(8, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(161, 12);
			this.label3.TabIndex = 3;
			this.label3.Text = "An empty folder for extraction";
			// 
			// buttonBrowseGD
			// 
			this.buttonBrowseGD.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.buttonBrowseGD.Location = new System.Drawing.Point(200, 32);
			this.buttonBrowseGD.Name = "buttonBrowseGD";
			this.buttonBrowseGD.Size = new System.Drawing.Size(64, 19);
			this.buttonBrowseGD.TabIndex = 2;
			this.buttonBrowseGD.Text = "Browse";
			this.buttonBrowseGD.Click += new System.EventHandler(this.buttonBrowseGD_Click);
			// 
			// textBoxGD
			// 
			this.textBoxGD.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBoxGD.Location = new System.Drawing.Point(8, 32);
			this.textBoxGD.Name = "textBoxGD";
			this.textBoxGD.Size = new System.Drawing.Size(184, 19);
			this.textBoxGD.TabIndex = 1;
			this.textBoxGD.Text = "";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(8, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 12);
			this.label2.TabIndex = 0;
			this.label2.Text = "GRAPHICS.DAT";
			// 
			// groupBox2
			// 
			this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.buttonBuildNow,
																					this.buttonBrowseGDINI,
																					this.textBoxGDINI,
																					this.label6,
																					this.buttonBrowseNewGD,
																					this.textBoxNewGD,
																					this.label5});
			this.groupBox2.Location = new System.Drawing.Point(8, 184);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(272, 128);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Builder";
			// 
			// buttonBuildNow
			// 
			this.buttonBuildNow.Location = new System.Drawing.Point(8, 96);
			this.buttonBuildNow.Name = "buttonBuildNow";
			this.buttonBuildNow.TabIndex = 7;
			this.buttonBuildNow.Text = "Build";
			this.buttonBuildNow.Click += new System.EventHandler(this.buttonBuildNow_Click);
			// 
			// buttonBrowseGDINI
			// 
			this.buttonBrowseGDINI.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.buttonBrowseGDINI.Location = new System.Drawing.Point(200, 72);
			this.buttonBrowseGDINI.Name = "buttonBrowseGDINI";
			this.buttonBrowseGDINI.Size = new System.Drawing.Size(64, 19);
			this.buttonBrowseGDINI.TabIndex = 5;
			this.buttonBrowseGDINI.Text = "Browse";
			this.buttonBrowseGDINI.Click += new System.EventHandler(this.buttonBrowseGDINI_Click);
			// 
			// textBoxGDINI
			// 
			this.textBoxGDINI.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBoxGDINI.Location = new System.Drawing.Point(8, 72);
			this.textBoxGDINI.Name = "textBoxGDINI";
			this.textBoxGDINI.Size = new System.Drawing.Size(184, 19);
			this.textBoxGDINI.TabIndex = 4;
			this.textBoxGDINI.Text = "";
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(8, 56);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(253, 12);
			this.label6.TabIndex = 3;
			this.label6.Text = "GRAPHICS.DAT.INI for materials to be imported";
			// 
			// buttonBrowseNewGD
			// 
			this.buttonBrowseNewGD.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.buttonBrowseNewGD.Location = new System.Drawing.Point(200, 32);
			this.buttonBrowseNewGD.Name = "buttonBrowseNewGD";
			this.buttonBrowseNewGD.Size = new System.Drawing.Size(64, 19);
			this.buttonBrowseNewGD.TabIndex = 2;
			this.buttonBrowseNewGD.Text = "Browse";
			this.buttonBrowseNewGD.Click += new System.EventHandler(this.buttonBrowseNewGD_Click);
			// 
			// textBoxNewGD
			// 
			this.textBoxNewGD.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textBoxNewGD.Location = new System.Drawing.Point(8, 32);
			this.textBoxNewGD.Name = "textBoxNewGD";
			this.textBoxNewGD.Size = new System.Drawing.Size(184, 19);
			this.textBoxNewGD.TabIndex = 1;
			this.textBoxNewGD.Text = "";
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(8, 16);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(114, 12);
			this.label5.TabIndex = 0;
			this.label5.Text = "New GRAPHICS.DAT";
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 327);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(292, 22);
			this.statusBar1.TabIndex = 4;
			// 
			// openFileDialogInGD
			// 
			this.openFileDialogInGD.Filter = "GRAPHICS.DAT (*.DAT)|*.DAT";
			// 
			// saveFileDialogExpDir
			// 
			this.saveFileDialogExpDir.Filter = "Select workdir for export|*.*";
			this.saveFileDialogExpDir.OverwritePrompt = false;
			// 
			// saveFileDialogIntoGD
			// 
			this.saveFileDialogIntoGD.DefaultExt = "dat";
			this.saveFileDialogIntoGD.Filter = "GRAPHICS.DAT (*.DAT)|*.DAT";
			// 
			// openFileDialogInGDINI
			// 
			this.openFileDialogInGDINI.Filter = "GRAPHICS.DAT.INI (*.INI)|*.INI";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(292, 349);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.statusBar1,
																		  this.groupBox2,
																		  this.groupBoxExp,
																		  this.comboBoxConf,
																		  this.label1});
			this.Name = "Form1";
			this.Text = "DM2LowLevelGDATMaker Rev.0 - Main";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.Closed += new System.EventHandler(this.Form1_Closed);
			this.groupBoxExp.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		static void Main()
		{
			System.Windows.Forms.Application.Run(new Form1());
			return;
		}

		protected void RegSerialize(
			WinIni pReg,
			string sApp,
			string sKey,
			System.Windows.Forms.Control pWnd,
			bool fSave
			)
		{
			string sWndType = pWnd.GetType().Name;
			string sVal;

			if (fSave)
			{
				switch (sWndType)
				{
					case "TextBox":
						pReg.WriteProfileString(
							sApp, 
							sKey, 
							((System.Windows.Forms.TextBox)pWnd).Text
							);
						break;
					case "ComboBox":
						System.Diagnostics.Debug.Assert(
							((System.Windows.Forms.ComboBox)pWnd).DropDownStyle == System.Windows.Forms.ComboBoxStyle.DropDownList,
							"Unexpected DropDownStyle"
							);
						pReg.WriteProfileString(
							sApp, 
							sKey, 
							((System.Windows.Forms.ComboBox)pWnd).SelectedIndex.ToString()
							);
						break;
					case "CheckBox":
						pReg.WriteProfileString(
							sApp, 
							sKey, 
							((System.Windows.Forms.CheckBox)pWnd).Checked ? "1" : "0"
							);
						break;
				}
			}
			else
			{
				switch (sWndType)
				{
					case "TextBox":
						((System.Windows.Forms.TextBox)pWnd).Text = pReg.GetProfileString(
							sApp, 
							sKey, 
							""
							);
						break;
					case "ComboBox":
						System.Diagnostics.Debug.Assert(
							((System.Windows.Forms.ComboBox)pWnd).DropDownStyle == System.Windows.Forms.ComboBoxStyle.DropDownList,
							"Unexpected DropDownStyle"
							);
						sVal = pReg.GetProfileString(
							sApp, 
							sKey, 
							""
							);
						if (sVal.Length > 0)
							((System.Windows.Forms.ComboBox)pWnd).SelectedIndex = int.Parse(sVal);
						break;
					case "CheckBox":
						sVal = pReg.GetProfileString(
							sApp, 
							sKey, 
							""
							);
						if (sVal.Length > 0)
							((System.Windows.Forms.CheckBox)pWnd).Checked = (sVal == "1") ? true : false;
						break;
				}
			}
		}

		protected static string g_sApp = "DM2LLGDATMaker.Main";
		protected static string g_sAppMerge = "DM2LLGDATMaker.Merge";

		protected void RegExchanger(bool fSave)
		{
			try
			{
				WinIni pReg = new WinIni();
				string sReg = DM2LLGDATMakerShared.IniPath;
				pReg.LoadFrom(sReg);

				RegSerialize(pReg, g_sApp, "predefined_game_configuration", comboBoxConf, fSave);
				RegSerialize(pReg, g_sApp, "exporter_graphics_dat", textBoxGD, fSave);
				RegSerialize(pReg, g_sApp, "exporter_work_dir", textBoxExpDir, fSave);
				//RegSerialize(pReg, g_sApp, "exporter_endian", comboBoxEndianExp, fSave);
				RegSerialize(pReg, g_sApp, "builder_new_graphics_dat", textBoxNewGD, fSave);
				RegSerialize(pReg, g_sApp, "builder_graphics_dat_ini", textBoxGDINI, fSave);
				//RegSerialize(pReg, g_sApp, "builder_endian", comboBoxEndianImp, fSave);

				if (fSave)
				{
					pReg.SaveTo(sReg);
				}
			}
			catch (System.ApplicationException)
			{

			}
		}

		protected void ExportGD(
			System.String sFileIn,
			System.String sExpDir
			)
		{
			DM2GDFR pGD = new DM2GDFR(sFileIn);
			pGD.Init();
			int iFile = 0;
			bool fLittleEndian = pGD.IsLittleEndian;
			System.Text.StringBuilder pMergeInfo = new System.Text.StringBuilder(1024 * 300);
			foreach (BinaryEntry pEntry in pGD.Entries)
			{
				string sFile = iFile.ToString("0000") + ".bin";
				string sFilePath = DM2LLGDATMakerShared.JoinPath(sExpDir, sFile);

				System.IO.Stream pFileInto = System.IO.File.Open(sFilePath, System.IO.FileMode.Create);
				pFileInto.Write(pEntry.Binary, 0, pEntry.Binary.Length);
				pFileInto.Close();

				pMergeInfo.AppendFormat("{0}\r\n", sFile);

				iFile++;
			}
			string sMergePath = DM2LLGDATMakerShared.JoinPath(sExpDir, "filelist.txt");
			System.IO.TextWriter pMergeInto = new System.IO.StreamWriter(sMergePath, false, System.Text.Encoding.Default);
			pMergeInto.Write(pMergeInfo.ToString());
			pMergeInto.Close();
			WinIni pIni = new WinIni();

			string sIniPath = DM2LLGDATMakerShared.JoinPath(sExpDir, "graphics.dat.ini");
			pIni.WriteProfileString(g_sAppMerge, "file_list", "filelist.txt");
			pIni.WriteProfileString(g_sAppMerge, "little_endian", fLittleEndian ? "1" : "0");
			pIni.SaveTo(sIniPath);
		}

		protected void BuildGD(
			System.String sFileInto,
			System.String sFileIni
			)
		{
			string sImpDir = DM2LLGDATMakerShared.GetDir(sFileIni);

			WinIni pIni = new WinIni();
			pIni.LoadFrom(sFileIni);
			string sFileListFile = pIni.GetProfileString(g_sAppMerge, "file_list", "");
			string sFileListFilePath = DM2LLGDATMakerShared.JoinPath(sImpDir, sFileListFile);

			bool fLittleEndian = pIni.GetProfileString(g_sAppMerge, "little_endian", "") == "1";

			System.IO.TextReader pTextIn;
			pTextIn = new System.IO.StreamReader(sFileListFilePath, System.Text.Encoding.Default);
			string s;
			int nEntries = 0;
			while ((s = pTextIn.ReadLine()) != null && s.Length > 0)
				nEntries++;
			pTextIn.Close();
			pTextIn = new System.IO.StreamReader(sFileListFilePath, System.Text.Encoding.Default);
			DM2GDFM pGD = new DM2GDFM(sFileInto, fLittleEndian, nEntries);
			pGD.Init();
			while ((s = pTextIn.ReadLine()) != null && s.Length > 0)
			{
				string sPath = DM2LLGDATMakerShared.JoinPath(sImpDir, s);
				System.IO.Stream pFileIn = System.IO.File.OpenRead(sPath);
				Byte[] pBinary = new Byte[pFileIn.Length];
				pFileIn.Read(pBinary, 0, pBinary.Length);
				BinaryEntry pEntry = new BinaryEntry(pBinary);
				pGD.Add(pEntry);
				pFileIn.Close();
			}
			pGD.Burn();
			pGD.Close();
		}

		private void comboBoxConf_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			switch (comboBoxConf.SelectedIndex)
			{
				case 0:
					//comboBoxEndianExp.SelectedIndex = 1;
					//comboBoxEndianImp.SelectedIndex = 1;
					break;
				case 1:
				case 2:
					//comboBoxEndianExp.SelectedIndex = 0;
					//comboBoxEndianImp.SelectedIndex = 0;
					break;
			}
		}

		private void buttonBrowseGD_Click(object sender, System.EventArgs e)
		{
			System.Windows.Forms.OpenFileDialog pDlg = openFileDialogInGD;
			System.Windows.Forms.TextBox pTextBox = textBoxGD;

			pDlg.FileName = pTextBox.Text;
			switch (pDlg.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					pTextBox.Text = pDlg.FileName;
					break;
			}
		}

		private void buttonBrowseGDINI_Click(object sender, System.EventArgs e)
		{
			System.Windows.Forms.OpenFileDialog pDlg = openFileDialogInGDINI;
			System.Windows.Forms.TextBox pTextBox = textBoxGDINI;

			pDlg.FileName = pTextBox.Text;
			switch (pDlg.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					pTextBox.Text = pDlg.FileName;
					break;
			}
		}

		private void buttonBrowseExpDir_Click(object sender, System.EventArgs e)
		{
			System.Windows.Forms.SaveFileDialog pDlg = saveFileDialogExpDir;
			System.Windows.Forms.TextBox pTextBox = textBoxExpDir;

			pDlg.FileName = DM2LLGDATMakerShared.JoinPath(pTextBox.Text, "(dir)");
			switch (pDlg.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					pTextBox.Text = DM2LLGDATMakerShared.GetDir(pDlg.FileName);
					break;
			}
		}

		private void buttonBrowseNewGD_Click(object sender, System.EventArgs e)
		{
			System.Windows.Forms.SaveFileDialog pDlg = saveFileDialogIntoGD;
			System.Windows.Forms.TextBox pTextBox = textBoxNewGD;

			pDlg.FileName = pTextBox.Text;
			switch (pDlg.ShowDialog())
			{
				case System.Windows.Forms.DialogResult.OK:
					pTextBox.Text = pDlg.FileName;
					break;
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			RegExchanger(false);
		}

		private void Form1_Closed(object sender, System.EventArgs e)
		{
			RegExchanger(true);
		}

		private void buttonExpNow_Click(object sender, System.EventArgs e)
		{
			switch (System.Windows.Forms.MessageBox.Show("Would you retry?", Text, System.Windows.Forms.MessageBoxButtons.RetryCancel, System.Windows.Forms.MessageBoxIcon.Exclamation))
			{
				case System.Windows.Forms.DialogResult.Retry:
					break;
				default:
					return;
			}

			statusBar1.Text = "";
			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			try
			{
				ExportGD(textBoxGD.Text, textBoxExpDir.Text);
			}
			catch (System.Exception err)
			{
				statusBar1.Text = err.Message;
			}
			finally
			{
				Cursor = System.Windows.Forms.Cursors.Default;
			}
		}

		private void buttonBuildNow_Click(object sender, System.EventArgs e)
		{
			switch (System.Windows.Forms.MessageBox.Show("Would you retry?", Text, System.Windows.Forms.MessageBoxButtons.RetryCancel, System.Windows.Forms.MessageBoxIcon.Exclamation))
			{
				case System.Windows.Forms.DialogResult.Retry:
					break;
				default:
					return;
			}

			statusBar1.Text = "";
			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			try
			{
				BuildGD(textBoxNewGD.Text, textBoxGDINI.Text);
			}
			catch (System.Exception err)
			{
				statusBar1.Text = err.Message;
			}
			finally
			{
				Cursor = System.Windows.Forms.Cursors.Default;
			}
		}
	}
}
