From f98a7c841107f63b0a806fe7b686142d912239df Mon Sep 17 00:00:00 2001 From: dexy Date: Sun, 22 Mar 2020 05:08:13 +1100 Subject: [PATCH] Support for saving files outside of RPFs in MetaForm, ModelForm and RelForm --- CodeWalker/Forms/MetaForm.cs | 61 +++++++++++++++++++++++------------ CodeWalker/Forms/ModelForm.cs | 11 +++++-- CodeWalker/Forms/RelForm.cs | 61 +++++++++++++++++++++++------------ 3 files changed, 91 insertions(+), 42 deletions(-) diff --git a/CodeWalker/Forms/MetaForm.cs b/CodeWalker/Forms/MetaForm.cs index e631660..65312ab 100644 --- a/CodeWalker/Forms/MetaForm.cs +++ b/CodeWalker/Forms/MetaForm.cs @@ -343,7 +343,6 @@ namespace CodeWalker.Forms //(currently just return false and revert to XML file save) if (!(exploreForm?.EditMode ?? false)) return false; - if (rpfFileEntry?.Parent == null) return false; byte[] data = null; @@ -409,34 +408,56 @@ namespace CodeWalker.Forms return false; } - if (!rpfFileEntry.Path.ToLowerInvariant().StartsWith("mods")) + if (rpfFileEntry?.Parent != null) { - if (MessageBox.Show("This file is NOT located in the mods folder - Are you SURE you want to save this file?\r\nWARNING: This could cause permanent damage to your game!!!", "WARNING: Are you sure about this?", MessageBoxButtons.YesNo) != DialogResult.Yes) + if (!rpfFileEntry.Path.ToLowerInvariant().StartsWith("mods")) { - return false;//that was a close one + if (MessageBox.Show("This file is NOT located in the mods folder - Are you SURE you want to save this file?\r\nWARNING: This could cause permanent damage to your game!!!", "WARNING: Are you sure about this?", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return false;//that was a close one + } + } + + try + { + if (!(exploreForm?.EnsureRpfValidEncryption(rpfFileEntry.File) ?? false)) return false; + + var newentry = RpfFile.CreateFile(rpfFileEntry.Parent, rpfFileEntry.Name, data); + if (newentry != rpfFileEntry) + { } + rpfFileEntry = newentry; + + exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... + + modified = false; + + StatusLabel.Text = metaFormat.ToString() + " file saved successfully at " + DateTime.Now.ToString(); + + return true; //victory! + } + catch (Exception ex) + { + MessageBox.Show("Error saving file to RPF! The RPF archive may be corrupted...\r\n" + ex.ToString(), "Really Bad Error"); } } - - try + else if (!string.IsNullOrEmpty(rpfFileEntry?.Path)) { - if (!(exploreForm?.EnsureRpfValidEncryption(rpfFileEntry.File) ?? false)) return false; + try + { + File.WriteAllBytes(rpfFileEntry.Path, data); - var newentry = RpfFile.CreateFile(rpfFileEntry.Parent, rpfFileEntry.Name, data); - if (newentry != rpfFileEntry) - { } - rpfFileEntry = newentry; + exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... - exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... + modified = false; - modified = false; + StatusLabel.Text = metaFormat.ToString() + " file saved successfully at " + DateTime.Now.ToString(); - StatusLabel.Text = metaFormat.ToString() + " file saved successfully at " + DateTime.Now.ToString(); - - return true; //victory! - } - catch (Exception ex) - { - MessageBox.Show("Error saving file to RPF! The RPF archive may be corrupted...\r\n" + ex.ToString(), "Really Bad Error"); + return true; //victory! + } + catch (Exception ex) + { + MessageBox.Show("Error saving file to filesystem!\r\n" + ex.ToString(), "File I/O Error"); + } } return false; diff --git a/CodeWalker/Forms/ModelForm.cs b/CodeWalker/Forms/ModelForm.cs index 006bc0f..fd182ec 100644 --- a/CodeWalker/Forms/ModelForm.cs +++ b/CodeWalker/Forms/ModelForm.cs @@ -1223,7 +1223,7 @@ namespace CodeWalker.Forms if (string.IsNullOrEmpty(FilePath)) { if (!editMode) saveAs = true; - if (rpfFileEntry?.Parent == null) saveAs = true; + if (rpfFileEntry == null) saveAs = true; } else { @@ -1251,6 +1251,7 @@ namespace CodeWalker.Forms if (SaveFileDialog.ShowDialog() != DialogResult.OK) return; fn = SaveFileDialog.FileName; + FilePath = fn; } @@ -1335,12 +1336,18 @@ namespace CodeWalker.Forms } else { + if (string.IsNullOrEmpty(fn)) + { + fn = rpfFileEntry?.Path; + } + try { File.WriteAllBytes(fn, fileBytes); fileName = Path.GetFileName(fn); - FilePath = fn; + + exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... StatusLabel.Text = fileName + " saved successfully at " + DateTime.Now.ToString(); } diff --git a/CodeWalker/Forms/RelForm.cs b/CodeWalker/Forms/RelForm.cs index 3a4b65e..ae5b1b4 100644 --- a/CodeWalker/Forms/RelForm.cs +++ b/CodeWalker/Forms/RelForm.cs @@ -182,7 +182,6 @@ namespace CodeWalker.Forms { if (!(exploreForm?.EditMode ?? false)) return false; - if (rpfFileEntry?.Parent == null) return false; byte[] data = null; @@ -219,34 +218,56 @@ namespace CodeWalker.Forms return false; } - if (!rpfFileEntry.Path.ToLowerInvariant().StartsWith("mods")) + if (rpfFileEntry?.Parent != null) { - if (MessageBox.Show("This file is NOT located in the mods folder - Are you SURE you want to save this file?\r\nWARNING: This could cause permanent damage to your game!!!", "WARNING: Are you sure about this?", MessageBoxButtons.YesNo) != DialogResult.Yes) + if (!rpfFileEntry.Path.ToLowerInvariant().StartsWith("mods")) { - return false;//that was a close one + if (MessageBox.Show("This file is NOT located in the mods folder - Are you SURE you want to save this file?\r\nWARNING: This could cause permanent damage to your game!!!", "WARNING: Are you sure about this?", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return false;//that was a close one + } + } + + try + { + if (!(exploreForm?.EnsureRpfValidEncryption(rpfFileEntry.File) ?? false)) return false; + + var newentry = RpfFile.CreateFile(rpfFileEntry.Parent, rpfFileEntry.Name, data); + if (newentry != rpfFileEntry) + { } + rpfFileEntry = newentry; + + exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... + + modified = false; + + StatusLabel.Text = metaFormat.ToString() + " file saved successfully at " + DateTime.Now.ToString(); + + return true; //victory! + } + catch (Exception ex) + { + MessageBox.Show("Error saving file to RPF! The RPF archive may be corrupted...\r\n" + ex.ToString(), "Really Bad Error"); } } - - try + else if (!string.IsNullOrEmpty(rpfFileEntry?.Path)) { - if (!(exploreForm?.EnsureRpfValidEncryption(rpfFileEntry.File) ?? false)) return false; + try + { + File.WriteAllBytes(rpfFileEntry.Path, data); - var newentry = RpfFile.CreateFile(rpfFileEntry.Parent, rpfFileEntry.Name, data); - if (newentry != rpfFileEntry) - { } - rpfFileEntry = newentry; + exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... - exploreForm?.RefreshMainListViewInvoke(); //update the file details in explorer... + modified = false; - modified = false; + StatusLabel.Text = metaFormat.ToString() + " file saved successfully at " + DateTime.Now.ToString(); - StatusLabel.Text = metaFormat.ToString() + " file saved successfully at " + DateTime.Now.ToString(); - - return true; //victory! - } - catch (Exception ex) - { - MessageBox.Show("Error saving file to RPF! The RPF archive may be corrupted...\r\n" + ex.ToString(), "Really Bad Error"); + return true; //victory! + } + catch (Exception ex) + { + MessageBox.Show("Error saving file to filesystem!\r\n" + ex.ToString(), "File I/O Error"); + } } return false;