diff --git a/pk3DS/GARCTool.cs b/pk3DS/GARCTool.cs index 53dc38a..895417a 100644 --- a/pk3DS/GARCTool.cs +++ b/pk3DS/GARCTool.cs @@ -8,7 +8,8 @@ namespace pk3DS { public class GARCTool { - public static bool garcPack(string folderPath, string garcPath, ProgressBar pBar1 = null, Label label = null, bool supress = false) + // Note: This has been customized from the original GARCTool source to not use temporary files (instead uses MemoryStream). + public static bool garcPackMS(string folderPath, string garcPath, ProgressBar pBar1 = null, Label label = null, bool supress = false) { // Check to see if our input folder exists. if (!new DirectoryInfo(folderPath).Exists) { Util.Error("Folder does not exist."); return false; } @@ -32,13 +33,11 @@ public static bool garcPack(string folderPath, string garcPath, ProgressBar pBar // Scan through to see if we have to compress anything. - try - { - File.Delete(garcPath); // Delete the old garc if it exists, then write our new one - } - catch { } + // Delete the old garc if it exists, then write our new one + try { File.Delete(garcPath); } catch { } - using (var newGARC = File.Create(garcPath)) + // Set up the Header Info + using (var newGARC = new MemoryStream()) using (BinaryWriter gw = new BinaryWriter(newGARC)) { @@ -86,9 +85,9 @@ public static bool garcPack(string folderPath, string garcPath, ProgressBar pBar } catch (Exception e) { Util.Error("Invalid packing filenames", e.ToString()); return false; } - if (!Directory.Exists("temp")) Directory.CreateDirectory("temp"); - string garcName = "temp" + Path.DirectorySeparatorChar + Util.getRandomFileName(); - using (var GARCdata = File.Create(garcName)) + // Assemble the GARCData. + using (var GARCdata = new MemoryStream()) + { for (int i = 0; i < filepaths.Length; i++) { string name = filepaths[fp[i]]; @@ -137,26 +136,25 @@ public static bool garcPack(string folderPath, string garcPath, ProgressBar pBar else { pBar1.PerformStep(); } } - gw.Write((uint)0x46494D42); - gw.Write((uint)0x0000000C); - gw.Write((uint)offset); + gw.Write((uint)0x46494D42); + gw.Write((uint)0x0000000C); + gw.Write((uint)offset); - using (var GARCdatainfo = new FileStream(garcName, FileMode.Open)) - { gw.Seek(0x10, SeekOrigin.Begin); // Goto the start of the un-set 0 data we set earlier and set it. gw.Write((uint)newGARC.Length); // Write Data Offset - gw.Write((uint)(newGARC.Length + GARCdatainfo.Length)); // Write total GARC Length + gw.Write((uint)(newGARC.Length + GARCdata.Length)); // Write total GARC Length gw.Write((uint)largest); // Write Largest File stat (?) newGARC.Seek(0, SeekOrigin.End); // Goto the end so we can copy the filedata after the GARC headers. // Write in the data - GARCdatainfo.CopyTo(newGARC); // Copy the data. + GARCdata.Position = 0; + GARCdata.CopyTo(newGARC); // Copy the data. } // New File is ready to be saved (memstream newGARC) try { - File.Delete(garcName); + File.WriteAllBytes(garcPath, newGARC.ToArray()); if (label.InvokeRequired) label.Invoke((MethodInvoker)delegate { label.Visible = false; }); else { label.Visible = false; } diff --git a/pk3DS/Main.cs b/pk3DS/Main.cs index 5a26021..baa603f 100644 --- a/pk3DS/Main.cs +++ b/pk3DS/Main.cs @@ -16,6 +16,8 @@ public partial class Main : Form public Main() { InitializeComponent(); + CB_Lang.SelectedIndex = 2; + // restoreGARCs(true, "gametext", "storytext", "personal", "trpoke", "trdata", "evolution", "megaevo", "levelup", "eggmove", "item", "move", "maisonpkS", "maisontrS", "maisonpkN", "maisontrN"); this.AllowDrop = GB_Tools.AllowDrop = TB_Path.AllowDrop = true; this.DragEnter += tabMain_DragEnter; this.DragDrop += tabMain_DragDrop; @@ -23,11 +25,11 @@ public Main() GB_Tools.DragDrop += tabMain_DragDrop; TB_Path.DragEnter += tabMain_DragEnter; TB_Path.DragDrop += tabMain_DragDrop; - CB_Lang.SelectedIndex = 2; if (File.Exists("config.ini")) { string path = File.ReadAllText("config.ini"); if (path.Length > 0) openQuick(path); + } } public bool oras = false; @@ -337,21 +339,25 @@ private void B_EggMove_Click(object sender, EventArgs e) // GARC Requests public bool getGARC(string infile, string outfolder, bool PB, bool bypassExt = false) { - bool success = GARCTool.garcUnpack(infile, outfolder, bypassExt, (PB) ? pBar1 : null, null, true, bypassExt); - Console.WriteLine("Get Status: " + success.ToString()); - threads--; - return success; + try + { + bool success = GARCTool.garcUnpack(infile, outfolder, bypassExt, (PB) ? pBar1 : null, null, true, bypassExt); + Console.WriteLine("Get Status: " + success.ToString()); + threads--; + return success; + } + catch (Exception e) { Util.Error("Could not get the GARC:", e.ToString()); threads--; return false; } } public bool setGARC(string outfile, string infolder, bool PB) { - if (!Directory.Exists("temp")) Directory.CreateDirectory("temp"); - string temp = "temp" + Path.DirectorySeparatorChar + Util.getRandomFileName(); - bool success = GARCTool.garcPack(infolder, temp, (PB) ? pBar1 : null, null, true); - Console.WriteLine("Set Status: " + success.ToString()); - try { File.Delete(outfile); File.Move(temp, outfile); } - catch (Exception e) { Util.Error("Could not set the GARC back:", e.ToString()); } - threads--; - return success; + try + { + bool success = GARCTool.garcPackMS(infolder, outfile, (PB) ? pBar1 : null, null, true); + Console.WriteLine("Set Status: " + success.ToString()); + threads--; + return success; + } + catch (Exception e) { Util.Error("Could not set the GARC back:", e.ToString()); threads--; return false; } } public void threadGet(string infile, string outfolder, bool PB = true, bool bypassExt = false) { @@ -405,6 +411,17 @@ public void backupGARCs(bool overwrite, params string[] g) File.Copy(TB_Path.Text + GARC, dest); } } + public void restoreGARCs(bool ggg, params string[] g) + { + oras = ggg; + GB_Tools.Enabled = false; + foreach (string s in g) + { + string dest = File.ReadAllText("config.ini") + getGARCFileName(s); + string src = "backup" + Path.DirectorySeparatorChar + s + String.Format(" (a{0})", getGARCFileName(s).Replace(Path.DirectorySeparatorChar.ToString(), "")); + File.Copy(src, dest, true); + } + } // Text Requests internal static string[] getText(int file) diff --git a/pk3DS/Util.cs b/pk3DS/Util.cs index 544c4ae..5282839 100644 --- a/pk3DS/Util.cs +++ b/pk3DS/Util.cs @@ -580,17 +580,6 @@ internal static char[] Reverse(char[] charArray) return charArray; } - // Text File Utility - internal static string getRandomFileName() - { - Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden - int size = 8; - string s = ""; - for (int i = 0; i < size; i++) - s += Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))); - - return s; - } internal static string getExeFSFolder(string a) {