Config improvements

- Move loading to MainForm_Load instead of constructor
- Config doesn't exist by default
- User is prompted to create config if it can't be loaded (user has to
  select the working folder)
- Error reporting shows the stack trace
This commit is contained in:
jeffman 2019-01-08 20:50:56 -05:00
parent f4e553be7c
commit 383772f118
4 changed files with 46 additions and 14 deletions

View File

@ -526,6 +526,7 @@
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "MOTHER 1+2 Funland";
this.Load += new System.EventHandler(this.MainForm_Load);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
this.mainMenu.ResumeLayout(false);
this.mainMenu.PerformLayout();

View File

@ -16,6 +16,7 @@ namespace ScriptToolGui
public partial class MainForm : Form
{
// Config
static readonly string configFile = "config.json";
Config config;
// Static/const members
@ -80,10 +81,18 @@ namespace ScriptToolGui
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
try
{
config = Config.Read("config.json");
config = GetConfig();
if (config == null)
{
MessageBox.Show("Could not load a valid configuration. The application will now exit.");
Close();
}
previewer.M12Compiler = m12Compiler;
previewer.CharLookup = ebCharLookup;
@ -97,15 +106,43 @@ namespace ScriptToolGui
collectionSelector.SelectedIndex = 0;
collectionSelector_SelectionChangeCommitted(null, null);
}
catch (Exception e)
{
MessageBox.Show("There was an error starting the tool." + Environment.NewLine +
"Reason: " + e.Message);
badStart = true;
this.Load += (s, ee) => this.Close();
Activate();
}
catch (Exception ex)
{
badStart = true;
MessageBox.Show($"There was an error starting the tool.{Environment.NewLine}{ex}");
Close();
}
}
private Config GetConfig()
{
config = Config.Read(configFile);
if (config == null)
{
if (MessageBox.Show($"There was a problem reading {configFile}. Create a new one now?",
"Config error",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (var dialog = new FolderBrowserDialog())
{
dialog.Description = "Select the working folder for the repository";
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
config = new Config { WorkingFolder = dialog.SelectedPath };
config.Write(configFile);
}
}
}
}
return config;
}
private void PopulateCollectionSelector()

View File

@ -17,9 +17,6 @@
</ItemGroup>
<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="eb-char-lookup.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

View File

@ -1,3 +0,0 @@
{
"WorkingFolder": "..\\..\\..\\..\\..\\working"
}