Refactoring

This commit is contained in:
Kaphotics 2016-04-18 18:21:50 -07:00
parent f39730b2b3
commit 027e0e19b1
6 changed files with 68 additions and 82 deletions

View file

@ -10,11 +10,11 @@ namespace PKHeX
// PKHeX master personal.dat
internal static readonly PersonalInfo[] PersonalAO = PersonalInfo.getPersonalArray(Properties.Resources.personal_ao, PersonalInfo.SizeAO);
private static readonly PersonalInfo[] PersonalXY = PersonalInfo.getPersonalArray(Properties.Resources.personal_xy, PersonalInfo.SizeXY);
private static readonly EggMoves[] EggMoveXY = EggMoves.getArray(Util.unpackMini(Properties.Resources.eggmove_xy, "xy"));
private static readonly Learnset[] LevelUpXY = Learnset.getArray(Util.unpackMini(Properties.Resources.lvlmove_xy, "xy"));
private static readonly EggMoves[] EggMoveAO = EggMoves.getArray(Util.unpackMini(Properties.Resources.eggmove_ao, "ao"));
private static readonly Learnset[] LevelUpAO = Learnset.getArray(Util.unpackMini(Properties.Resources.lvlmove_ao, "ao"));
private static readonly Evolutions[] Evolves = Evolutions.getArray(Util.unpackMini(Properties.Resources.evos_ao, "ao"));
private static readonly EggMoves[] EggMoveXY = EggMoves.getArray(Data.unpackMini(Properties.Resources.eggmove_xy, "xy"));
private static readonly Learnset[] LevelUpXY = Learnset.getArray(Data.unpackMini(Properties.Resources.lvlmove_xy, "xy"));
private static readonly EggMoves[] EggMoveAO = EggMoves.getArray(Data.unpackMini(Properties.Resources.eggmove_ao, "ao"));
private static readonly Learnset[] LevelUpAO = Learnset.getArray(Data.unpackMini(Properties.Resources.lvlmove_ao, "ao"));
private static readonly Evolutions[] Evolves = Evolutions.getArray(Data.unpackMini(Properties.Resources.evos_ao, "ao"));
private static readonly EncounterArea[] SlotsA;
private static readonly EncounterArea[] SlotsO;
private static readonly EncounterArea[] SlotsX;
@ -62,8 +62,8 @@ namespace PKHeX
StaticA = getSpecial(GameVersion.AS);
StaticO = getSpecial(GameVersion.OR);
var XSlots = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_x, "xy"));
var YSlots = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_y, "xy"));
var XSlots = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_x, "xy"));
var YSlots = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_y, "xy"));
// Mark Horde Encounters
foreach (var area in XSlots)
@ -81,8 +81,8 @@ namespace PKHeX
SlotsX = addXYAltTiles(XSlots, SlotsXYAlt);
SlotsY = addXYAltTiles(YSlots, SlotsXYAlt);
SlotsA = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_a, "ao"));
SlotsO = EncounterArea.getArray(Util.unpackMini(Properties.Resources.encounter_o, "ao"));
SlotsA = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_a, "ao"));
SlotsO = EncounterArea.getArray(Data.unpackMini(Properties.Resources.encounter_o, "ao"));
// Mark Rock Smash Encounters
foreach (var area in SlotsA)

View file

@ -336,4 +336,46 @@ namespace PKHeX
return d;
}
}
internal static class Data
{
internal static byte[][] unpackMini(byte[] fileData, string identifier)
{
using (var s = new MemoryStream(fileData))
using (var br = new BinaryReader(s))
{
if (identifier != new string(br.ReadChars(2)))
return null;
ushort count = br.ReadUInt16();
byte[][] returnData = new byte[count][];
uint[] offsets = new uint[count + 1];
for (int i = 0; i < count; i++)
offsets[i] = br.ReadUInt32();
uint length = br.ReadUInt32();
offsets[offsets.Length - 1] = length;
for (int i = 0; i < count; i++)
{
br.BaseStream.Seek(offsets[i], SeekOrigin.Begin);
using (MemoryStream dataout = new MemoryStream())
{
byte[] data = new byte[0];
s.CopyTo(dataout, (int)offsets[i]);
int len = (int)offsets[i + 1] - (int)offsets[i];
if (len != 0)
{
data = dataout.ToArray();
Array.Resize(ref data, len);
}
returnData[i] = data;
}
}
return returnData;
}
}
}
}

View file

@ -82,16 +82,6 @@ namespace PKHeX
RTB_Code.AppendText((writeoffset + i * 4 + 0x20000000).ToString("X8") + " ");
RTB_Code.AppendText(BitConverter.ToUInt32(newdata,i*4).ToString("X8") + Environment.NewLine);
}
// Mat's Code - Unfinished
//for (int i = 0; i < newdata.Length / (4); i++)
//{
// // Add Operator
// RTB_Code.AppendText("00000001 "); // 01 00 00 00
// RTB_Code.AppendText((writeoffset + i * 4).ToString("X8") + " ");
// RTB_Code.AppendText(BitConverter.ToUInt32(newdata,i*4).ToString("X8") + Environment.NewLine);
//}
}
private void B_Clear_Click(object sender, EventArgs e)
{
@ -132,7 +122,7 @@ namespace PKHeX
if (RTB_Code.Text.Length < 1) return;
byte[] ncf = new byte[4 + (RTB_Code.Lines.Length-1)*3*4];
Array.Copy(BitConverter.GetBytes(ncf.Length - 4), ncf, 4);
BitConverter.GetBytes(ncf.Length - 4).CopyTo(ncf, 4);
for (int i = 0; i < RTB_Code.Lines.Length-1; i++)
{
@ -140,9 +130,9 @@ namespace PKHeX
string[] rip = line.Split(new[] {" "}, StringSplitOptions.None);
// Write the 3 u32's to an array.
Array.Copy(BitConverter.GetBytes(uint.Parse(rip[0], NumberStyles.HexNumber)),0,ncf,4+i*12+0,4);
Array.Copy(BitConverter.GetBytes(uint.Parse(rip[1], NumberStyles.HexNumber)),0,ncf,4+i*12+4,4);
Array.Copy(BitConverter.GetBytes(uint.Parse(rip[2], NumberStyles.HexNumber)),0,ncf,4+i*12+8,4);
BitConverter.GetBytes(uint.Parse(rip[0], NumberStyles.HexNumber)).CopyTo(ncf,4+i*12+0);
BitConverter.GetBytes(uint.Parse(rip[1], NumberStyles.HexNumber)).CopyTo(ncf,4+i*12+4);
BitConverter.GetBytes(uint.Parse(rip[2], NumberStyles.HexNumber)).CopyTo(ncf,4+i*12+8);
}
SaveFileDialog sfd = new SaveFileDialog {FileName = "code.bin", Filter = "Code File|*.bin"};
@ -193,8 +183,8 @@ namespace PKHeX
for (int i = 0; i < newcyber.Length - 0x200; i += 4)
{
// Skip Party and Boxes
if (i == 0x14200) i += 260 * 6 + 4; // +4 to skip over party count
if (i == boxoffset) i += 232 * 30 * 31;
if (i == Main.SAV.Party) i += 260 * 6 + 4; // +4 to skip over party count
if (i == Main.SAV.Box) i += 232 * 30 * 31;
if (BitConverter.ToUInt32(cybersav, i) == BitConverter.ToUInt32(newcyber, i)) continue;
result += (0x20000000 + i).ToString("X8") + " ";
@ -228,7 +218,7 @@ namespace PKHeX
{
result +=
Environment.NewLine +
"--- Segment " + (lines / 128 + 1) + " ---" +
$"--- Segment {lines / 128 + 1} ---" +
Environment.NewLine + Environment.NewLine;
}
if (lines > 10000) goto toomany;
@ -246,7 +236,7 @@ namespace PKHeX
{
result +=
Environment.NewLine +
"--- Segment " + (lines / 128 + 1) + " ---" +
$"--- Segment {lines / 128 + 1} ---" +
Environment.NewLine + Environment.NewLine;
}
if (lines > 10000) goto toomany;
@ -269,7 +259,7 @@ namespace PKHeX
{
result +=
Environment.NewLine +
"--- Segment " + (lines / 128 + 1) + " ---" +
$"--- Segment {lines / 128 + 1} ---" +
Environment.NewLine + Environment.NewLine;
}
if (lines > 10000) goto toomany;
@ -277,18 +267,13 @@ namespace PKHeX
}
if ((lines / 128 > 0) && CHK_Break.Checked)
{
Util.Alert($"{1 + lines/128} Code Segments.",
$"{lines} Lines.");
}
Util.Alert($"{1 + lines/128} Code Segments.", $"{lines} Lines.");
RTB_Code.Text = result;
return;
toomany:
{
Util.Alert("Too many differences detected.",
"Export your save instead.");
}
Util.Alert("Too many differences detected.", "Export your save instead.");
}
// Import

View file

@ -343,7 +343,7 @@ namespace PKHeX
// Summarize
string filename = pk6.Nickname;
if (pk6.Nickname != Main.specieslist[pk6.Species] && Main.specieslist[pk6.Species] != null)
filename += " (" + Main.specieslist[pk6.Species] + ")";
filename += $" ({Main.specieslist[pk6.Species]})";
response[0] = $"{filename} [{Main.abilitylist[pk6.Ability]}] lv{pk6.Stat_Level} @ {Main.itemlist[pk6.HeldItem]} -- {Main.natures[pk6.Nature]}";
response[1] = $"{Main.movelist[pk6.Move1]} / {Main.movelist[pk6.Move2]} / {Main.movelist[pk6.Move3]} / {Main.movelist[pk6.Move4]}";
response[2] = string.Format(
@ -514,7 +514,7 @@ namespace PKHeX
internal static uint getPSV(uint PID)
{
return Convert.ToUInt16((PID >> 16 ^ PID & 0xFFFF) >> 4);
return (PID >> 16 ^ PID & 0xFFFF) >> 4;
}
internal static uint getTSV(uint TID, uint SID)
{

View file

@ -471,45 +471,6 @@ namespace PKHeX
}
return cbList;
}
internal static byte[][] unpackMini(byte[] fileData, string ident)
{
using (var s = new MemoryStream(fileData))
using (var br = new BinaryReader(s))
{
string fx = new string(br.ReadChars(2));
if (fx != ident) return null;
ushort count = br.ReadUInt16();
byte[][] returnData = new byte[count][];
uint[] offsets = new uint[count + 1];
for (int i = 0; i < count; i++)
offsets[i] = br.ReadUInt32();
uint length = br.ReadUInt32();
offsets[offsets.Length - 1] = length;
for (int i = 0; i < count; i++)
{
br.BaseStream.Seek(offsets[i], SeekOrigin.Begin);
using (MemoryStream dataout = new MemoryStream())
{
byte[] data = new byte[0];
s.CopyTo(dataout, (int)offsets[i]);
int len = (int)offsets[i + 1] - (int)offsets[i];
if (len != 0)
{
data = dataout.ToArray();
Array.Resize(ref data, len);
}
returnData[i] = data;
}
}
return returnData;
}
}
// QR Utility
internal static byte[] getQRData()

View file

@ -293,9 +293,7 @@ namespace PKHeX
private void mainMenuCodeGen(object sender, EventArgs e)
{
// Open Code Generator
PK6 formdata = null;
if (verifiedPKX()) formdata = preparepkx();
CodeGenerator CodeGen = new CodeGenerator(formdata.Data);
CodeGenerator CodeGen = new CodeGenerator(verifiedPKX() ? preparepkx().Data : null);
CodeGen.ShowDialog();
byte[] data = CodeGen.returnArray;
@ -793,13 +791,13 @@ namespace PKHeX
// Enable Secondary Tools
GB_SAVtools.Enabled = B_JPEG.Enabled = true;
Menu_ExportSAV.Enabled = B_VerifyCHK.Enabled = SAV.Exportable;
Menu_CodeGenerator.Enabled = !SAV.ORASDEMO && SAV.Exportable;
setBoxNames(); // Display the Box Names
setPKXBoxes(); // Reload all of the PKX Windows
// Version Exclusive Editors
GB_SUBE.Visible = !SAV.ORAS;
B_OpenSecretBase.Visible = SAV.ORAS;
if (SAV.Box > -1)
{