From 989f391b94eed63eaa140f19922feec65e36ac6e Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 20 Nov 2016 20:34:32 -0800 Subject: [PATCH] Continued refactoring Shifting away from unpacking every garc file to disk storage (instead operate with RAM) --- pk3DS/Game/GameConfig.cs | 4 ++- pk3DS/Main.cs | 38 ++++++++++++++++----------- pk3DS/Subforms/Gen6/EggMoveEditor6.cs | 17 ++++++------ pk3DS/Subforms/Gen6/LevelUpEditor6.cs | 18 +++++++------ pk3DS/Subforms/Gen6/MoveEditor6.cs | 33 +++++------------------ pk3DS/Subforms/Gen6/RSTE.cs | 2 +- pk3DS/Subforms/Gen7/EggMoveEditor7.cs | 17 ++++++------ pk3DS/Subforms/Gen7/LevelUpEditor7.cs | 18 +++++++------ pk3DS/Subforms/Gen7/MoveEditor7.cs | 30 +++++---------------- 9 files changed, 76 insertions(+), 101 deletions(-) diff --git a/pk3DS/Game/GameConfig.cs b/pk3DS/Game/GameConfig.cs index 84fff6e..63cc859 100644 --- a/pk3DS/Game/GameConfig.cs +++ b/pk3DS/Game/GameConfig.cs @@ -137,7 +137,9 @@ public void InitializeMoves() } public GARCFile getGARCData(string file) { - return new GARCFile(getMemGARC(file), getGARCReference(file), getGARCPath(file)); + var gr = getGARCReference(file); + gr = gr.LanguageVariant ? gr.getRelativeGARC(Language, gr.Name) : gr; + return new GARCFile(getMemGARC(file), gr, getGARCPath(file)); } private string getGARCPath(string file) { diff --git a/pk3DS/Main.cs b/pk3DS/Main.cs index 99ed495..8a86972 100644 --- a/pk3DS/Main.cs +++ b/pk3DS/Main.cs @@ -607,19 +607,23 @@ private void B_Move_Click(object sender, EventArgs e) if (threadActive()) return; new Thread(() => { - string[] files = { "move" }; - fileGet(files); + var g = Config.GARCMoves; + byte[][] Moves; switch (Config.Generation) { case 6: - Invoke((Action)(() => new MoveEditor6().ShowDialog())); + bool mini = Config.ORAS; + Moves = mini ? CTR.mini.unpackMini(g.getFile(0), "WD") : g.Files; + Invoke((Action)(() => new MoveEditor6(Moves).ShowDialog())); + g.Files = mini ? new[] { CTR.mini.packMini(Moves, "WD") } : Moves; break; case 7: - Invoke((Action)(() => new MoveEditor7().ShowDialog())); + Moves = CTR.mini.unpackMini(g.getFile(0), "WD"); + Invoke((Action)(() => new MoveEditor7(Moves).ShowDialog())); + g.Files = new[] {CTR.mini.packMini(Moves, "WD")}; break; } - Invoke((Action)(() => new MoveEditor6().ShowDialog())); - fileSet(files); + g.Save(); }).Start(); } private void B_LevelUp_Click(object sender, EventArgs e) @@ -627,18 +631,19 @@ private void B_LevelUp_Click(object sender, EventArgs e) if (threadActive()) return; new Thread(() => { - string[] files = { "levelup", "move" }; - fileGet(files); + var g = Config.getGARCData("levelup"); + byte[][] d = g.Files; switch (Config.Generation) { case 6: - Invoke((Action)(() => new LevelUpEditor6().ShowDialog())); + Invoke((Action)(() => new LevelUpEditor6(d).ShowDialog())); break; case 7: - Invoke((Action)(() => new LevelUpEditor7().ShowDialog())); + Invoke((Action)(() => new LevelUpEditor7(d).ShowDialog())); break; } - fileSet(files); + g.Files = d; + g.Save(); }).Start(); } private void B_EggMove_Click(object sender, EventArgs e) @@ -646,18 +651,19 @@ private void B_EggMove_Click(object sender, EventArgs e) if (threadActive()) return; new Thread(() => { - string[] files = { "eggmove", "move" }; - fileGet(files); + var g = Config.getGARCData("eggmove"); + byte[][] d = g.Files; switch (Config.Generation) { case 6: - Invoke((Action)(() => new EggMoveEditor6().ShowDialog())); + Invoke((Action)(() => new EggMoveEditor6(d).ShowDialog())); break; case 7: - Invoke((Action)(() => new EggMoveEditor7().ShowDialog())); + Invoke((Action)(() => new EggMoveEditor7(d).ShowDialog())); break; } - fileSet(files); + g.Files = d; + g.Save(); }).Start(); } private void B_TitleScreen_Click(object sender, EventArgs e) diff --git a/pk3DS/Subforms/Gen6/EggMoveEditor6.cs b/pk3DS/Subforms/Gen6/EggMoveEditor6.cs index 87c8b5b..57acc15 100644 --- a/pk3DS/Subforms/Gen6/EggMoveEditor6.cs +++ b/pk3DS/Subforms/Gen6/EggMoveEditor6.cs @@ -12,9 +12,10 @@ namespace pk3DS { public partial class EggMoveEditor6 : Form { - public EggMoveEditor6() + public EggMoveEditor6(byte[][] infiles) { InitializeComponent(); + files = infiles; string[] specieslist = Main.getText(TextName.SpeciesNames); specieslist[0] = movelist[0] = ""; @@ -31,7 +32,7 @@ public EggMoveEditor6() CB_Species.DataSource = newlist; CB_Species.SelectedIndex = 0; } - private readonly string[] files = Directory.GetFiles("eggmove"); + private readonly byte[][] files; private readonly byte[] data = File.ReadAllBytes(Directory.GetFiles("personal", "*.*", SearchOption.TopDirectoryOnly).Last()); private int entry = -1; private readonly string[] movelist = Main.getText(TextName.MoveNames); @@ -63,10 +64,10 @@ private void getList() PB_MonSprite.Image = (Bitmap)Resources.ResourceManager.GetObject(filename); dgv.Rows.Clear(); - byte[] input = File.ReadAllBytes(files[entry]); + byte[] input = files[entry]; if (input.Length == 0) return; pkm = new EggMoves6(input); - if (pkm.Count < 1) { File.WriteAllBytes(files[entry], new byte[0]); return; } + if (pkm.Count < 1) { files[entry] = new byte[0]; return; } dgv.Rows.Add(pkm.Count); // Fill Entries @@ -86,7 +87,7 @@ private void setList() } pkm.Moves = moves.ToArray(); - File.WriteAllBytes(files[entry], pkm.Write()); + files[entry] = pkm.Write(); } private void changeEntry(object sender, EventArgs e) @@ -115,7 +116,7 @@ private void B_RandAll_Click(object sender, EventArgs e) banned = banned.Concat(new[] { 165, 621 }).ToArray(); // Struggle, Hyperspace Fury // Move Stats - Move[] moveTypes = MoveEditor6.getMoves(); + Move[] moveTypes = Main.Config.Moves; // Personal Stats byte[] personalData = File.ReadAllBytes(Directory.GetFiles("personal").Last()); @@ -193,7 +194,7 @@ private void formClosing(object sender, FormClosingEventArgs e) } private void calcStats() { - Move[] MoveData = MoveEditor6.getMoves(); + Move[] MoveData = Main.Config.Moves; byte[] personalData = File.ReadAllBytes(Directory.GetFiles("personal").Last()); @@ -204,7 +205,7 @@ private void calcStats() int species = 0; for (int i = 0; i < Main.Config.MaxSpeciesID; i++) { - byte[] movedata = File.ReadAllBytes(files[i]); + byte[] movedata = files[i]; if (movedata.Length <= 2) continue; int movecount = BitConverter.ToUInt16(movedata, 0); if (movecount == 65535 || movecount < 0) diff --git a/pk3DS/Subforms/Gen6/LevelUpEditor6.cs b/pk3DS/Subforms/Gen6/LevelUpEditor6.cs index dc87fda..a763d8e 100644 --- a/pk3DS/Subforms/Gen6/LevelUpEditor6.cs +++ b/pk3DS/Subforms/Gen6/LevelUpEditor6.cs @@ -12,9 +12,10 @@ namespace pk3DS { public partial class LevelUpEditor6 : Form { - public LevelUpEditor6() + public LevelUpEditor6(byte[][] infiles) { InitializeComponent(); + files = infiles; string[] species = Main.getText(TextName.SpeciesNames); string[][] AltForms = Main.Config.Personal.getFormList(species, Main.Config.MaxSpeciesID); int[] baseForm, formVal; @@ -36,7 +37,8 @@ public LevelUpEditor6() CB_Species.DataSource = newlist; CB_Species.SelectedIndex = 0; } - private readonly string[] files = Directory.GetFiles("levelup"); + + private readonly byte[][] files; private readonly byte[] data = File.ReadAllBytes(Directory.GetFiles("personal", "*.*", SearchOption.TopDirectoryOnly).Last()); private int entry = -1; private readonly string[] movelist = Main.getText(TextName.MoveNames); @@ -76,8 +78,8 @@ private void getList() PB_MonSprite.Image = (Bitmap)Resources.ResourceManager.GetObject(filename); dgv.Rows.Clear(); - byte[] input = File.ReadAllBytes(files[entry]); - if (input.Length <= 4) { File.WriteAllBytes(files[entry], BitConverter.GetBytes(-1)); return; } + byte[] input = files[entry]; + if (input.Length <= 4) { files[entry] = BitConverter.GetBytes(-1); return; } pkm = new Learnset6(input); dgv.Rows.Add(pkm.Count); @@ -111,7 +113,7 @@ private void setList() } pkm.Moves = moves.ToArray(); pkm.Levels = levels.ToArray(); - File.WriteAllBytes(files[entry], pkm.Write()); + files[entry] = pkm.Write(); } private void changeEntry(object sender, EventArgs e) @@ -143,7 +145,7 @@ private void B_RandAll_Click(object sender, EventArgs e) banned = banned.Concat(new[] { 165, 621 }).ToArray(); // Struggle, Hyperspace Fury // Move Stats - Move[] moveTypes = MoveEditor6.getMoves(); + Move[] moveTypes = Main.Config.Moves; // Personal Stats byte[] personalData = File.ReadAllBytes(Directory.GetFiles("personal").Last()); @@ -240,7 +242,7 @@ private void CHK_TypeBias_CheckedChanged(object sender, EventArgs e) public void calcStats() // Debug Function { - Move[] MoveData = MoveEditor6.getMoves(); + Move[] MoveData = Main.Config.Moves; byte[] personalData = File.ReadAllBytes(Directory.GetFiles("personal").Last()); @@ -250,7 +252,7 @@ private void CHK_TypeBias_CheckedChanged(object sender, EventArgs e) int stab = 0; for (int i = 0; i < Main.Config.MaxSpeciesID; i++) { - byte[] movedata = File.ReadAllBytes(files[i]); + byte[] movedata = files[i]; int movecount = (movedata.Length - 4) / 4; if (movecount == 65535) continue; diff --git a/pk3DS/Subforms/Gen6/MoveEditor6.cs b/pk3DS/Subforms/Gen6/MoveEditor6.cs index 6c36c0d..ff7e837 100644 --- a/pk3DS/Subforms/Gen6/MoveEditor6.cs +++ b/pk3DS/Subforms/Gen6/MoveEditor6.cs @@ -6,11 +6,9 @@ namespace pk3DS { public partial class MoveEditor6 : Form { - public MoveEditor6() + public MoveEditor6(byte[][] infiles) { - if (Main.Config.ORAS) - CTR.mini.unpackMini(Directory.GetFiles("move")[0], "WD"); - + files = infiles; movelist[0] = ""; sortedmoves = (string[])movelist.Clone(); Array.Sort(sortedmoves); @@ -18,7 +16,7 @@ public MoveEditor6() InitializeComponent(); Setup(); } - private string[] files = Directory.GetFiles("move"); + private byte[][] files; private readonly string[] types = Main.getText(TextName.Types); private readonly string[] moveflavor = Main.getText(TextName.MoveFlavor); private readonly string[] movelist = Main.getText(TextName.MoveNames); @@ -61,7 +59,6 @@ private void Setup() CB_Inflict.Items.Add("Special"); CB_Move.Items.RemoveAt(0); - files = Directory.GetFiles("move"); CB_Move.SelectedIndex = 0; } private int entry = -1; @@ -74,7 +71,7 @@ private void changeEntry(object sender, EventArgs e) private void getEntry() { if (entry < 1) return; - byte[] data = File.ReadAllBytes(files[entry]); + byte[] data = files[entry]; { string flavor = moveflavor[entry].Replace("\\n", Environment.NewLine); RTB.Text = flavor; @@ -125,7 +122,7 @@ private void getEntry() private void setEntry() { if (entry < 1) return; - byte[] data = File.ReadAllBytes(files[entry]); + byte[] data = files[entry]; { data[0x00] = (byte)CB_Type.SelectedIndex; data[0x01] = (byte)CB_Quality.SelectedIndex; @@ -161,14 +158,11 @@ private void setEntry() data[0x21] = (byte)NUD_0x21.Value; // end, the other bytes aren't used. } - File.WriteAllBytes(files[entry], data); + files[entry] = data; } private void formClosing(object sender, FormClosingEventArgs e) { setEntry(); - - if (Main.Config.ORAS) - CTR.mini.packMini("move", "WD", "0", ".bin"); } private void B_RandAll_Click(object sender, EventArgs e) @@ -190,20 +184,5 @@ private void B_RandAll_Click(object sender, EventArgs e) } Util.Alert("Moves have been randomized!"); } - - internal static Move[] getMoves() - { - if (Main.Config.ORAS) - CTR.mini.unpackMini(Directory.GetFiles("move")[0], "WD"); - string[] f2 = Directory.GetFiles("move"); - Move[] moves = new Move[f2.Length]; - for (int i = 0; i < f2.Length; i++) - moves[i] = new Move(File.ReadAllBytes(f2[i])); - - if (Main.Config.ORAS) - CTR.mini.packMini("move", "WD", "0", ".bin"); - - return moves; - } } } \ No newline at end of file diff --git a/pk3DS/Subforms/Gen6/RSTE.cs b/pk3DS/Subforms/Gen6/RSTE.cs index b367261..a994659 100644 --- a/pk3DS/Subforms/Gen6/RSTE.cs +++ b/pk3DS/Subforms/Gen6/RSTE.cs @@ -557,7 +557,7 @@ private void Randomize() List GymE4Types = new List(); // Fetch Move Stats for more difficult randomization - var moveData = MoveEditor6.getMoves(); + var moveData = Main.Config.Moves; int[] moveList = Enumerable.Range(1, movelist.Length - 1).ToArray(); int mctr = 0; Util.Shuffle(moveList); diff --git a/pk3DS/Subforms/Gen7/EggMoveEditor7.cs b/pk3DS/Subforms/Gen7/EggMoveEditor7.cs index 9f0b051..bc0348e 100644 --- a/pk3DS/Subforms/Gen7/EggMoveEditor7.cs +++ b/pk3DS/Subforms/Gen7/EggMoveEditor7.cs @@ -12,9 +12,10 @@ namespace pk3DS { public partial class EggMoveEditor7 : Form { - public EggMoveEditor7() + public EggMoveEditor7(byte[][] infiles) { InitializeComponent(); + files = infiles; string[] species = Main.getText(TextName.SpeciesNames); string[][] AltForms = Main.Config.Personal.getFormList(species, Main.Config.MaxSpeciesID); string[] specieslist = Main.Config.Personal.getPersonalEntryList(AltForms, species, Main.Config.MaxSpeciesID, out baseForms, out formVal); @@ -37,7 +38,7 @@ public EggMoveEditor7() CB_Species.SelectedIndex = 0; } - private readonly string[] files = Directory.GetFiles("eggmove"); + private readonly byte[][] files; private int entry = -1; private readonly string[] movelist = Main.getText(TextName.MoveNames); private bool dumping; @@ -73,10 +74,10 @@ private void getList() PB_MonSprite.Image = (Bitmap)Resources.ResourceManager.GetObject(filename); dgv.Rows.Clear(); - byte[] input = File.ReadAllBytes(files[entry]); + byte[] input = files[entry]; pkm = new EggMoves7(input); NUD_FormTable.Value = pkm.FormTableIndex; - if (pkm.Count < 1) { File.WriteAllBytes(files[entry], new byte[0]); return; } + if (pkm.Count < 1) { files[entry] = new byte[0]; return; } dgv.Rows.Add(pkm.Count); // Fill Entries @@ -96,7 +97,7 @@ private void setList() } pkm.Moves = moves.ToArray(); - File.WriteAllBytes(files[entry], pkm.Write()); + files[entry] = pkm.Write(); } private void changeEntry(object sender, EventArgs e) @@ -117,7 +118,7 @@ private void B_RandAll_Click(object sender, EventArgs e) int[] banned = new[] { 165, 621, 464 }.Concat(Legal.Z_Moves).ToArray(); // Struggle, Hyperspace Fury, Dark Void // Move Stats - Move[] moveTypes = MoveEditor6.getMoves(); + Move[] moveTypes = Main.Config.Moves; // Set up Randomized Moves int[] randomMoves = Enumerable.Range(1, movelist.Length - 1).Select(i => i).ToArray(); @@ -188,14 +189,14 @@ private void B_Goto_Click(object sender, EventArgs e) private void calcStats() { - Move[] MoveData = MoveEditor6.getMoves(); + Move[] MoveData = Main.Config.Moves; int movectr = 0; int max = 0; int spec = 0; int stab = 0; for (int i = 0; i < Main.Config.MaxSpeciesID; i++) { - byte[] movedata = File.ReadAllBytes(files[i]); + byte[] movedata = files[i]; int movecount = BitConverter.ToUInt16(movedata, 2); if (movecount == 65535) continue; diff --git a/pk3DS/Subforms/Gen7/LevelUpEditor7.cs b/pk3DS/Subforms/Gen7/LevelUpEditor7.cs index eb5f6c3..210eafe 100644 --- a/pk3DS/Subforms/Gen7/LevelUpEditor7.cs +++ b/pk3DS/Subforms/Gen7/LevelUpEditor7.cs @@ -12,9 +12,10 @@ namespace pk3DS { public partial class LevelUpEditor7 : Form { - public LevelUpEditor7() + public LevelUpEditor7(byte[][] infiles) { InitializeComponent(); + files = infiles; string[] species = Main.getText(TextName.SpeciesNames); string[][] AltForms = Main.Config.Personal.getFormList(species, Main.Config.MaxSpeciesID); string[] specieslist = Main.Config.Personal.getPersonalEntryList(AltForms, species, Main.Config.MaxSpeciesID, out baseForms, out formVal); @@ -35,7 +36,8 @@ public LevelUpEditor7() CB_Species.DataSource = newlist; CB_Species.SelectedIndex = 0; } - private readonly string[] files = Directory.GetFiles("levelup"); + + private readonly byte[][] files; private int entry = -1; private readonly string[] movelist = Main.getText(TextName.MoveNames); private bool dumping; @@ -78,8 +80,8 @@ private void getList() PB_MonSprite.Image = (Bitmap)Resources.ResourceManager.GetObject(filename); dgv.Rows.Clear(); - byte[] input = File.ReadAllBytes(files[entry]); - if (input.Length <= 4) { File.WriteAllBytes(files[entry], BitConverter.GetBytes(-1)); return; } + byte[] input = files[entry]; + if (input.Length <= 4) { files[entry] = BitConverter.GetBytes(-1); return; } pkm = new Learnset7(input); dgv.Rows.Add(pkm.Count); @@ -113,7 +115,7 @@ private void setList() } pkm.Moves = moves.ToArray(); pkm.Levels = levels.ToArray(); - File.WriteAllBytes(files[entry], pkm.Write()); + files[entry] = pkm.Write(); } private void changeEntry(object sender, EventArgs e) @@ -137,7 +139,7 @@ private void B_RandAll_Click(object sender, EventArgs e) int[] banned = new[] { 165, 621, 464 }.Concat(Legal.Z_Moves).ToArray(); // Struggle, Hyperspace Fury, Dark Void // Move Stats - Move[] moveTypes = MoveEditor6.getMoves(); + Move[] moveTypes = Main.Config.Moves; // Set up Randomized Moves int[] randomMoves = Enumerable.Range(1, movelist.Length - 1).Select(i => i).ToArray(); @@ -224,14 +226,14 @@ private void CHK_TypeBias_CheckedChanged(object sender, EventArgs e) public void calcStats() // Debug Function { - Move[] MoveData = MoveEditor6.getMoves(); + Move[] MoveData = Main.Config.Moves; int movectr = 0; int max = 0; int spec = 0; int stab = 0; for (int i = 0; i < Main.Config.MaxSpeciesID; i++) { - byte[] movedata = File.ReadAllBytes(files[i]); + byte[] movedata = files[i]; int movecount = (movedata.Length - 4) / 4; if (movecount == 65535) continue; diff --git a/pk3DS/Subforms/Gen7/MoveEditor7.cs b/pk3DS/Subforms/Gen7/MoveEditor7.cs index 1a8db45..1ea0af0 100644 --- a/pk3DS/Subforms/Gen7/MoveEditor7.cs +++ b/pk3DS/Subforms/Gen7/MoveEditor7.cs @@ -6,9 +6,9 @@ namespace pk3DS { public partial class MoveEditor7 : Form { - public MoveEditor7() + public MoveEditor7(byte[][] infiles) { - CTR.mini.unpackMini(Directory.GetFiles("move")[0], "WD"); + files = infiles; movelist[0] = ""; sortedmoves = (string[])movelist.Clone(); @@ -17,7 +17,7 @@ public MoveEditor7() InitializeComponent(); Setup(); } - private string[] files = Directory.GetFiles("move"); + private byte[][] files; private readonly string[] types = Main.getText(TextName.Types); private readonly string[] moveflavor = Main.getText(TextName.MoveFlavor); private readonly string[] movelist = Main.getText(TextName.MoveNames); @@ -60,7 +60,6 @@ private void Setup() CB_Inflict.Items.Add("Special"); CB_Move.Items.RemoveAt(0); - files = Directory.GetFiles("move"); CB_Move.SelectedIndex = 0; } private int entry = -1; @@ -73,7 +72,7 @@ private void changeEntry(object sender, EventArgs e) private void getEntry() { if (entry < 1) return; - byte[] data = File.ReadAllBytes(files[entry]); + byte[] data = files[entry]; { string flavor = moveflavor[entry].Replace("\\n", Environment.NewLine); RTB.Text = flavor; @@ -124,7 +123,7 @@ private void getEntry() private void setEntry() { if (entry < 1) return; - byte[] data = File.ReadAllBytes(files[entry]); + byte[] data = files[entry]; { data[0x00] = (byte)CB_Type.SelectedIndex; data[0x01] = (byte)CB_Quality.SelectedIndex; @@ -160,14 +159,11 @@ private void setEntry() data[0x21] = (byte)NUD_0x21.Value; // end, the other bytes aren't used. } - File.WriteAllBytes(files[entry], data); + files[entry] = data; } private void formClosing(object sender, FormClosingEventArgs e) { setEntry(); - - if (Main.Config.ORAS) - CTR.mini.packMini("move", "WD", "0", ".bin"); } private void B_RandAll_Click(object sender, EventArgs e) @@ -189,19 +185,5 @@ private void B_RandAll_Click(object sender, EventArgs e) } Util.Alert("Moves have been randomized!"); } - - internal static Move[] getMoves() - { - CTR.mini.unpackMini(Directory.GetFiles("move")[0], "WD"); - string[] f2 = Directory.GetFiles("move"); - Move[] moves = new Move[f2.Length]; - for (int i = 0; i < f2.Length; i++) - moves[i] = new Move(File.ReadAllBytes(f2[i])); - - if (Main.Config.ORAS) - CTR.mini.packMini("move", "WD", "0", ".bin"); - - return moves; - } } } \ No newline at end of file