From 2ed21988668eec0f38560b636245f346cf676f80 Mon Sep 17 00:00:00 2001 From: atenfyr Date: Thu, 23 Jan 2025 04:30:06 -0600 Subject: [PATCH] fail nicely when exception thrown in Form1 constructor --- UAssetGUI/Form1.cs | 174 ++++++++++++++++++++++++--------------------- 1 file changed, 91 insertions(+), 83 deletions(-) diff --git a/UAssetGUI/Form1.cs b/UAssetGUI/Form1.cs index 9d63a77..c4e9d52 100644 --- a/UAssetGUI/Form1.cs +++ b/UAssetGUI/Form1.cs @@ -76,119 +76,127 @@ public Form1() UAGUtils.InitializeInvoke(this); - UAGConfig.Load(); + try + { + UAGConfig.Load(); - Assembly assembly = Assembly.GetExecutingAssembly(); - UAGUtils._displayVersion = assembly.GetCustomAttribute().InformationalVersion; + Assembly assembly = Assembly.GetExecutingAssembly(); + UAGUtils._displayVersion = assembly.GetCustomAttribute().InformationalVersion; - string gitVersionGUI = string.Empty; - using (Stream stream = assembly.GetManifestResourceStream("UAssetGUI.git_commit.txt")) - { - if (stream != null) + string gitVersionGUI = string.Empty; + using (Stream stream = assembly.GetManifestResourceStream("UAssetGUI.git_commit.txt")) { - using (StreamReader reader = new StreamReader(stream)) + if (stream != null) { - if (reader != null) gitVersionGUI = reader.ReadToEnd().Trim(); + using (StreamReader reader = new StreamReader(stream)) + { + if (reader != null) gitVersionGUI = reader.ReadToEnd().Trim(); + } } } - } - if (!gitVersionGUI.All("0123456789abcdef".Contains)) gitVersionGUI = string.Empty; + if (!gitVersionGUI.All("0123456789abcdef".Contains)) gitVersionGUI = string.Empty; - string gitVersionAPI = string.Empty; - using (Stream stream = typeof(PropertyData).Assembly.GetManifestResourceStream("UAssetAPI.git_commit.txt")) - { - if (stream != null) + string gitVersionAPI = string.Empty; + using (Stream stream = typeof(PropertyData).Assembly.GetManifestResourceStream("UAssetAPI.git_commit.txt")) { - using (StreamReader reader = new StreamReader(stream)) + if (stream != null) { - if (reader != null) gitVersionAPI = reader.ReadToEnd().Trim(); + using (StreamReader reader = new StreamReader(stream)) + { + if (reader != null) gitVersionAPI = reader.ReadToEnd().Trim(); + } } } - } - if (!gitVersionAPI.All("0123456789abcdef".Contains)) gitVersionAPI = string.Empty; + if (!gitVersionAPI.All("0123456789abcdef".Contains)) gitVersionAPI = string.Empty; - if (!string.IsNullOrEmpty(gitVersionGUI)) - { - UAGUtils._displayVersion += " (" + gitVersionGUI; - if (!string.IsNullOrEmpty(gitVersionAPI)) + if (!string.IsNullOrEmpty(gitVersionGUI)) { - UAGUtils._displayVersion += " - " + gitVersionAPI; + UAGUtils._displayVersion += " (" + gitVersionGUI; + if (!string.IsNullOrEmpty(gitVersionAPI)) + { + UAGUtils._displayVersion += " - " + gitVersionAPI; + } + UAGUtils._displayVersion += ")"; } - UAGUtils._displayVersion += ")"; - } - this.Text = DisplayVersion; - this.AllowDrop = true; - dataGridView1.Visible = true; + this.Text = DisplayVersion; + this.AllowDrop = true; + dataGridView1.Visible = true; - // Extra data viewer - byteView1 = new ByteViewer - { - Dock = DockStyle.Fill, - AutoScroll = true, - AutoSize = true, - Visible = false - }; - splitContainer1.Panel2.Controls.Add(byteView1); + // Extra data viewer + byteView1 = new ByteViewer + { + Dock = DockStyle.Fill, + AutoScroll = true, + AutoSize = true, + Visible = false + }; + splitContainer1.Panel2.Controls.Add(byteView1); - jsonView = new TextBox - { - Dock = DockStyle.Fill, - Visible = false, - AutoSize = true, - Multiline = true, - ReadOnly = true, - MaxLength = int.MaxValue, - ScrollBars = ScrollBars.Both, - }; - splitContainer1.Panel2.Controls.Add(jsonView); + jsonView = new TextBox + { + Dock = DockStyle.Fill, + Visible = false, + AutoSize = true, + Multiline = true, + ReadOnly = true, + MaxLength = int.MaxValue, + ScrollBars = ScrollBars.Both, + }; + splitContainer1.Panel2.Controls.Add(jsonView); - jsonView.TextChanged += (object sender, EventArgs e) => { if (tableEditor == null) return; tableEditor.dirtySinceLastLoad = true; SetUnsavedChanges(true); }; + jsonView.TextChanged += (object sender, EventArgs e) => { if (tableEditor == null) return; tableEditor.dirtySinceLastLoad = true; SetUnsavedChanges(true); }; - importBinaryData.Visible = false; - exportBinaryData.Visible = false; - setBinaryData.Visible = false; + importBinaryData.Visible = false; + exportBinaryData.Visible = false; + setBinaryData.Visible = false; - // Enable double buffering to look nicer - if (!SystemInformation.TerminalServerSession) - { - Type ourGridType = dataGridView1.GetType(); - PropertyInfo pi = ourGridType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); - pi.SetValue(dataGridView1, true, null); - } + // Enable double buffering to look nicer + if (!SystemInformation.TerminalServerSession) + { + Type ourGridType = dataGridView1.GetType(); + PropertyInfo pi = ourGridType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); + pi.SetValue(dataGridView1, true, null); + } - // Auto resizing - SizeChanged += frm_sizeChanged; - FormClosing += frm_closing; + // Auto resizing + SizeChanged += frm_sizeChanged; + FormClosing += frm_closing; - // position of ByteViewer buttons depends on splitter location so resize if splitter moves - splitContainer1.SplitterMoved += (sender, e) => { ForceResize(); }; + // position of ByteViewer buttons depends on splitter location so resize if splitter moves + splitContainer1.SplitterMoved += (sender, e) => { ForceResize(); }; - // Drag-and-drop support - DragEnter += new DragEventHandler(frm_DragEnter); - DragDrop += new DragEventHandler(frm_DragDrop); + // Drag-and-drop support + DragEnter += new DragEventHandler(frm_DragEnter); + DragDrop += new DragEventHandler(frm_DragDrop); - dataGridView1.MouseWheel += dataGridView1_MouseWheel; - //dataGridView1.EditMode = UAGConfig.Data.DoubleClickToEdit ? DataGridViewEditMode.EditProgrammatically : DataGridViewEditMode.EditOnEnter; + dataGridView1.MouseWheel += dataGridView1_MouseWheel; + //dataGridView1.EditMode = UAGConfig.Data.DoubleClickToEdit ? DataGridViewEditMode.EditProgrammatically : DataGridViewEditMode.EditOnEnter; - menuStrip1.Renderer = new UAGMenuStripRenderer(); - foreach (ToolStripMenuItem entry in menuStrip1.Items) - { - entry.DropDownOpened += (sender, args) => - { - isDropDownOpened[entry] = true; - }; - entry.DropDownClosed += (sender, args) => + menuStrip1.Renderer = new UAGMenuStripRenderer(); + foreach (ToolStripMenuItem entry in menuStrip1.Items) { - isDropDownOpened[entry] = false; - }; - } + entry.DropDownOpened += (sender, args) => + { + isDropDownOpened[entry] = true; + }; + entry.DropDownClosed += (sender, args) => + { + isDropDownOpened[entry] = false; + }; + } - ac7decrypt = new AC7Decrypt(); + ac7decrypt = new AC7Decrypt(); - UpdateRPC(); + UpdateRPC(); + } + catch (Exception ex) + { + MessageBox.Show("An error occured while initializing!\n" + ex.GetType() + ": " + ex.Message + "\n\nUAssetGUI will now close.", "UAssetGUI"); + Environment.Exit(1); // kill the process + } } private static Dictionary isDropDownOpened = new Dictionary();