Add try {} catch to handle exceptions thrown from IO operations

This commit is contained in:
baldurk
2015-07-07 19:06:31 +02:00
parent 5232ee7626
commit f6527107dc
7 changed files with 285 additions and 128 deletions
+52 -36
View File
@@ -2176,45 +2176,53 @@ namespace renderdocui.Windows
if (res == DialogResult.OK)
{
StreamWriter writer = File.CreateText(csvSaveDialog.FileName);
if (MeshView)
try
{
writer.Write("Vertex,");
writer.Write("Index,");
}
else
{
writer.Write("Row,");
}
StreamWriter writer = File.CreateText(csvSaveDialog.FileName);
UIState ui = m_ContextUIState;
for (int i = 0; i < ui.m_Input.BufferFormats.Length; i++)
{
for (int j = 0; j < ui.m_Input.BufferFormats[i].format.compCount - 1; j++)
writer.Write(ui.m_Input.BufferFormats[i].name + " " + j + ",");
writer.Write(ui.m_Input.BufferFormats[i].name + " " + (ui.m_Input.BufferFormats[i].format.compCount - 1));
if (i < ui.m_Input.BufferFormats.Length - 1)
writer.Write(",");
}
writer.Write(Environment.NewLine);
foreach (DataGridViewRow row in ui.m_GridView.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
if (MeshView)
{
writer.Write(row.Cells[i].Value.ToString());
if (i < row.Cells.Count - 1)
writer.Write("Vertex,");
writer.Write("Index,");
}
else
{
writer.Write("Row,");
}
UIState ui = m_ContextUIState;
for (int i = 0; i < ui.m_Input.BufferFormats.Length; i++)
{
for (int j = 0; j < ui.m_Input.BufferFormats[i].format.compCount - 1; j++)
writer.Write(ui.m_Input.BufferFormats[i].name + " " + j + ",");
writer.Write(ui.m_Input.BufferFormats[i].name + " " + (ui.m_Input.BufferFormats[i].format.compCount - 1));
if (i < ui.m_Input.BufferFormats.Length - 1)
writer.Write(",");
}
writer.Write(Environment.NewLine);
}
writer.Flush();
writer.Close();
foreach (DataGridViewRow row in ui.m_GridView.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
writer.Write(row.Cells[i].Value.ToString());
if (i < row.Cells.Count - 1)
writer.Write(",");
}
writer.Write(Environment.NewLine);
}
writer.Flush();
writer.Close();
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save to " + csvSaveDialog.FileName + Environment.NewLine + ex.ToString(), "Cannot save",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
@@ -2227,13 +2235,21 @@ namespace renderdocui.Windows
if (res == DialogResult.OK)
{
FileStream writer = File.Create(rawSaveDialog.FileName);
try
{
FileStream writer = File.Create(rawSaveDialog.FileName);
UIState ui = m_ContextUIState;
writer.Write(ui.m_RawData, 0, ui.m_RawData.Length);
UIState ui = m_ContextUIState;
writer.Write(ui.m_RawData, 0, ui.m_RawData.Length);
writer.Flush();
writer.Close();
writer.Flush();
writer.Close();
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save to " + csvSaveDialog.FileName + Environment.NewLine + ex.ToString(), "Cannot save",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
+68 -47
View File
@@ -204,30 +204,37 @@ namespace renderdocui.Windows.Dialogs
private void SaveSettings(string filename)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CaptureSettings));
StreamWriter writer = File.CreateText(filename);
xs.Serialize(writer, GetSettings());
writer.Flush();
writer.Close();
try
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CaptureSettings));
StreamWriter writer = File.CreateText(filename);
xs.Serialize(writer, GetSettings());
writer.Flush();
writer.Close();
}
catch (System.IO.IOException ex)
{
// Can't recover, but let user know that we couldn't save their settings.
MessageBox.Show(String.Format("Error saving config file: {1}\n{0}", filename, ex.Message));
}
}
public void LoadSettings(string filename)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CaptureSettings));
StreamReader reader = File.OpenText(filename);
try
{
StreamReader reader = File.OpenText(filename);
SetSettings((CaptureSettings)xs.Deserialize(reader));
reader.Close();
}
catch (System.Xml.XmlException)
{
}
catch (System.InvalidOperationException)
catch (System.Exception)
{
MessageBox.Show(String.Format("Failed to load capture settings from file {0}", filename), "Failed to load settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
reader.Close();
}
private void FillProcessList()
@@ -751,52 +758,66 @@ namespace renderdocui.Windows.Dialogs
var regfile = Path.Combine(Path.GetTempPath(), "RenderDoc_RestoreGlobalHook.reg");
if (Environment.Is64BitProcess)
try
{
EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("Wow6432Node"),
path, "x86\\renderdocshim32.dll",
out prevAppInitWoW64Enabled, out prevAppInitWoW64);
EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"),
path, "renderdocshim64.dll",
out prevAppInitEnabled, out prevAppInit);
using (FileStream s = File.OpenWrite(regfile))
if (Environment.Is64BitProcess)
{
using(StreamWriter sw = new StreamWriter(s))
EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("Wow6432Node"),
path, "x86\\renderdocshim32.dll",
out prevAppInitWoW64Enabled, out prevAppInitWoW64);
EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"),
path, "renderdocshim64.dll",
out prevAppInitEnabled, out prevAppInit);
using (FileStream s = File.OpenWrite(regfile))
{
sw.WriteLine("Windows Registry Editor Version 5.00");
sw.WriteLine("");
sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion\\Windows]");
sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitWoW64Enabled));
sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInitWoW64));
sw.WriteLine("");
sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows]");
sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitEnabled));
sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInit));
sw.Flush();
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine("Windows Registry Editor Version 5.00");
sw.WriteLine("");
sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion\\Windows]");
sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitWoW64Enabled));
sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInitWoW64));
sw.WriteLine("");
sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows]");
sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitEnabled));
sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInit));
sw.Flush();
}
}
}
else
{
// if this is a 64-bit OS, it will re-direct our request to Wow6432Node anyway, so we
// don't need to handle that manually
EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), path, "renderdocshim32.dll",
out prevAppInitEnabled, out prevAppInit);
using (FileStream s = File.OpenWrite(regfile))
{
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine("Windows Registry Editor Version 5.00");
sw.WriteLine("");
sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows]");
sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitEnabled));
sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInit));
sw.Flush();
}
}
}
}
else
catch (System.Exception ex)
{
// if this is a 64-bit OS, it will re-direct our request to Wow6432Node anyway, so we
// don't need to handle that manually
EnableAppInit(Registry.LocalMachine.CreateSubKey("SOFTWARE"), path, "renderdocshim32.dll",
out prevAppInitEnabled, out prevAppInit);
MessageBox.Show("Aborting. Couldn't save backup .reg file to " + regfile + Environment.NewLine + ex.ToString(), "Cannot save registry backup",
MessageBoxButtons.OK, MessageBoxIcon.Error);
using (FileStream s = File.OpenWrite(regfile))
{
using (StreamWriter sw = new StreamWriter(s))
{
sw.WriteLine("Windows Registry Editor Version 5.00");
sw.WriteLine("");
sw.WriteLine("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows]");
sw.WriteLine(String.Format("\"LoadAppInit_DLLs\"=dword:{0:X8}", prevAppInitEnabled));
sw.WriteLine(String.Format("\"AppInit_DLLs\"=\"{0}\"", prevAppInit));
sw.Flush();
}
}
// won't recurse because it's not enabled yet
toggleGlobalHook.Checked = false;
toggleGlobalHook.Enabled = true;
return;
}
ExitPipeThread();
+38 -7
View File
@@ -390,7 +390,15 @@ namespace renderdocui.Windows
// This ensures that if the user deletes the saved path we can still open or re-save it.
if (path.Length > 0)
{
File.Copy(log.localpath, path, true);
try
{
File.Copy(log.localpath, path, true);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save to " + path + Environment.NewLine + ex.ToString(), "Cannot save",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return true;
}
@@ -445,10 +453,17 @@ namespace renderdocui.Windows
if (!log.saved)
{
if (log.localpath == m_Core.LogFileName)
m_Main.OwnTemporaryLog = true;
else
File.Delete(log.localpath);
try
{
if (log.localpath == m_Core.LogFileName)
m_Main.OwnTemporaryLog = true;
else
File.Delete(log.localpath);
}
catch (System.Exception)
{
// couldn't delete log - deleted from under us?
}
}
}
captures.Items.Clear();
@@ -525,7 +540,14 @@ namespace renderdocui.Windows
m_Main.CloseLogfile();
}
File.Delete(log.localpath);
try
{
File.Delete(log.localpath);
}
catch (System.Exception)
{
// couldn't delete log - deleted from under us?
}
}
captures.Items.Remove(item);
@@ -545,7 +567,16 @@ namespace renderdocui.Windows
var temppath = m_Core.TempLogFilename(log.exe);
File.Copy(log.localpath, temppath);
try
{
File.Copy(log.localpath, temppath);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save log to temporary location" + Environment.NewLine + ex.ToString(), "Cannot save temporary log",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo(Application.ExecutablePath, String.Format("--tempfile \"{0}\"", temppath));
+27 -3
View File
@@ -409,7 +409,15 @@ namespace renderdocui.Windows.Dialogs
string fn = ValidData(e.Data);
if (fn.Length > 0)
{
scriptEditor.Text = File.ReadAllText(fn);
try
{
scriptEditor.Text = File.ReadAllText(fn);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't open file " + saveDialog.FileName + Environment.NewLine + ex.ToString(), "Cannot open script",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
mode_Changed(scriptMode, null);
}
@@ -421,7 +429,15 @@ namespace renderdocui.Windows.Dialogs
if (res == DialogResult.OK)
{
scriptEditor.Text = File.ReadAllText(openDialog.FileName);
try
{
scriptEditor.Text = File.ReadAllText(openDialog.FileName);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't load from " + openDialog.FileName + Environment.NewLine + ex.ToString(), "Cannot open script",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
@@ -431,7 +447,15 @@ namespace renderdocui.Windows.Dialogs
if (res == DialogResult.OK)
{
File.WriteAllText(saveDialog.FileName, scriptEditor.Text);
try
{
File.WriteAllText(saveDialog.FileName, scriptEditor.Text);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save to " + saveDialog.FileName + Environment.NewLine + ex.ToString(), "Cannot save script",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
+20 -5
View File
@@ -1071,8 +1071,15 @@ namespace renderdocui.Windows
CloseLogfile();
if (deletepath.Length > 0)
File.Delete(deletepath);
try
{
if (deletepath.Length > 0)
File.Delete(deletepath);
}
catch (System.Exception)
{
// can't delete it! maybe already deleted?
}
return true;
}
@@ -1090,9 +1097,17 @@ namespace renderdocui.Windows
return false;
}
// we copy the (possibly) temp log to the desired path, but the log item remains referring to the original path.
// This ensures that if the user deletes the saved path we can still open or re-save it.
File.Copy(m_Core.LogFileName, saveDialog.FileName, true);
try
{
// we copy the (possibly) temp log to the desired path, but the log item remains referring to the original path.
// This ensures that if the user deletes the saved path we can still open or re-save it.
File.Copy(m_Core.LogFileName, saveDialog.FileName, true);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save to " + saveDialog.FileName + Environment.NewLine + ex.ToString(), "Cannot save",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// we don't prompt to save on closing - if the user deleted the log that we just saved, then
// that is up to them.
+8 -1
View File
@@ -690,7 +690,14 @@ namespace renderdocui.Windows
{
using (StreamReader reader = new StreamReader(stream))
{
File.WriteAllText(syntaxpath, reader.ReadToEnd());
try
{
File.WriteAllText(syntaxpath, reader.ReadToEnd());
}
catch (System.Exception)
{
// silently fail if we can't write to the path - syntax highlighting will just be broken
}
}
}
}
+72 -29
View File
@@ -468,8 +468,16 @@ namespace renderdocui.Windows
var enc = new UnicodeEncoding();
var path = Path.GetTempFileName();
dockPanel.SaveAsXml(path, "", enc);
data.panelLayout = File.ReadAllText(path, enc);
File.Delete(path);
try
{
data.panelLayout = File.ReadAllText(path, enc);
File.Delete(path);
}
catch (System.Exception ex)
{
// can't recover
return writer.ToString();
}
data.darkBack = darkBack;
data.lightBack = lightBack;
@@ -618,36 +626,43 @@ namespace renderdocui.Windows
if (!m_CustomShaders.ContainsKey(key))
{
string source = File.ReadAllText(f);
m_CustomShaders.Add(key, ResourceId.Null);
m_CustomShadersBusy.Add(key);
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
try
{
string errors = "";
string source = File.ReadAllText(f);
ResourceId id = r.BuildCustomShader("main", source, 0, ShaderStageType.Pixel, out errors);
if (m_CustomShaderEditor.ContainsKey(key))
m_CustomShaders.Add(key, ResourceId.Null);
m_CustomShadersBusy.Add(key);
m_Core.Renderer.BeginInvoke((ReplayRenderer r) =>
{
string errors = "";
ResourceId id = r.BuildCustomShader("main", source, 0, ShaderStageType.Pixel, out errors);
if (m_CustomShaderEditor.ContainsKey(key))
{
BeginInvoke((MethodInvoker)delegate
{
m_CustomShaderEditor[key].ShowErrors(errors);
});
}
BeginInvoke((MethodInvoker)delegate
{
m_CustomShaderEditor[key].ShowErrors(errors);
customShader.Items.Add(fn);
m_CustomShaders[key] = id;
m_CustomShadersBusy.Remove(key);
customShader.AutoCompleteSource = AutoCompleteSource.None;
customShader.AutoCompleteSource = AutoCompleteSource.ListItems;
UI_UpdateChannels();
});
}
BeginInvoke((MethodInvoker)delegate
{
customShader.Items.Add(fn);
m_CustomShaders[key] = id;
m_CustomShadersBusy.Remove(key);
customShader.AutoCompleteSource = AutoCompleteSource.None;
customShader.AutoCompleteSource = AutoCompleteSource.ListItems;
UI_UpdateChannels();
});
});
}
catch (System.Exception ex)
{
// just continue, skip this file
}
}
}
}
@@ -691,8 +706,14 @@ namespace renderdocui.Windows
, Environment.NewLine);
}
File.WriteAllText(path, src);
try
{
File.WriteAllText(path, src);
}
catch (System.Exception ex)
{
// ignore this file
}
// auto-open edit window
customEdit_Click(sender, e);
@@ -703,8 +724,21 @@ namespace renderdocui.Windows
var filename = customShader.Text;
var key = filename.ToUpperInvariant();
string src = "";
try
{
src = File.ReadAllText(Path.Combine(Core.ConfigDirectory, filename + m_Core.APIProps.ShaderExtension));
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't open file for shader " + filename + Environment.NewLine + ex.ToString(), "Cannot open shader",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var files = new Dictionary<string, string>();
files.Add(filename, File.ReadAllText(Path.Combine(Core.ConfigDirectory, filename + m_Core.APIProps.ShaderExtension)));
files.Add(filename, src);
ShaderViewer s = new ShaderViewer(m_Core, true, "Custom Shader", files,
// Save Callback
@@ -713,7 +747,16 @@ namespace renderdocui.Windows
foreach (var f in updatedfiles)
{
var path = Path.Combine(Core.ConfigDirectory, f.Key + m_Core.APIProps.ShaderExtension);
File.WriteAllText(path, f.Value);
try
{
File.WriteAllText(path, f.Value);
}
catch (System.Exception ex)
{
MessageBox.Show("Couldn't save file for shader " + filename + Environment.NewLine + ex.ToString(), "Cannot save shader",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
},