Misc clean

This commit is contained in:
Kurt
2018-08-07 17:26:16 -07:00
parent 3ef3040c8d
commit 10c7dbbeee
9 changed files with 76 additions and 63 deletions

View File

@@ -5,15 +5,15 @@
namespace pk3DS.Core.CTR
{
// Mini Packing Util
public static class mini
public static class Mini
{
public static byte[] adjustMiniHeader(byte[] data, int headerLength)
public static byte[] AdjustMiniHeader(byte[] data, int headerLength)
{
// Adjust the header size of the mini file.
int count = BitConverter.ToUInt16(data, 2);
int[] start = new int[count];
for (int i = 0; i < count; i++)
start[i] = BitConverter.ToInt32(data, 4 + i*4);
start[i] = BitConverter.ToInt32(data, 4 + (i * 4));
int dataStart = start.Min();
if (headerLength < dataStart)
@@ -24,11 +24,12 @@ public static byte[] adjustMiniHeader(byte[] data, int headerLength)
int diff = headerLength - dataStart; // shift pointer
for (int i = 0; i < count + 1; i++)
Array.Copy(BitConverter.GetBytes(BitConverter.ToInt32(data, 4 + i * 4) + diff), 0, newData, 4 + 4 * i, 4);
Array.Copy(BitConverter.GetBytes(BitConverter.ToInt32(data, 4 + (i * 4)) + diff), 0, newData, 4 + (4 * i), 4);
return newData;
}
public static void packMini(string path, string ident, string fileName, string outExt = null, string outFolder = null, bool delete = true)
public static void PackMini(string path, string ident, string fileName, string outExt = null, string outFolder = null, bool delete = true)
{
if (outFolder == null)
{
@@ -44,7 +45,7 @@ public static void packMini(string path, string ident, string fileName, string o
Array.Copy(BitConverter.GetBytes((ushort)files.Length), 0, data, 2, 2);
int count = files.Length;
int dataOffset = 4 + 4 + count * 4;
int dataOffset = 4 + 4 + (count * 4);
// Start the data filling.
using (MemoryStream dataout = new MemoryStream())
@@ -84,7 +85,8 @@ public static void packMini(string path, string ident, string fileName, string o
if (delete)
Directory.Delete(path, true);
}
public static byte[] packMini(byte[][] fileData, string ident)
public static byte[] PackMini(byte[][] fileData, string ident)
{
// Create new Binary with the relevant header bytes
byte[] data = new byte[4];
@@ -93,7 +95,7 @@ public static byte[] packMini(byte[][] fileData, string ident)
Array.Copy(BitConverter.GetBytes((ushort)fileData.Length), 0, data, 2, 2);
int count = fileData.Length;
int dataOffset = 4 + 4 + count * 4;
int dataOffset = 4 + 4 + (count * 4);
// Start the data filling.
using (MemoryStream dataout = new MemoryStream())
@@ -129,7 +131,8 @@ public static byte[] packMini(byte[][] fileData, string ident)
}
}
}
public static bool packMini2(string path, string ident, string fileName)
public static bool PackMini2(string path, string ident, string fileName)
{
if (!Directory.Exists(path)) return false;
try
@@ -137,13 +140,14 @@ public static bool packMini2(string path, string ident, string fileName)
string[] filesToPack = Directory.GetFiles(path);
byte[][] fileData = new byte[filesToPack.Length][];
for (int i = 0; i < filesToPack.Length; i++) fileData[i] = File.ReadAllBytes(filesToPack[i]);
byte[] miniBytes = packMini(fileData, ident);
byte[] miniBytes = PackMini(fileData, ident);
File.WriteAllBytes(fileName, miniBytes);
return true;
}
catch { return false; }
}
public static void unpackMini(string path, string ident, string outFolder = null, bool delete = true)
public static void UnpackMini(string path, string ident, string outFolder = null, bool delete = true)
{
if (outFolder == null) outFolder = Path.GetDirectoryName(path);
if (!Directory.Exists(outFolder)) Directory.CreateDirectory(outFolder);
@@ -185,7 +189,14 @@ public static void unpackMini(string path, string ident, string outFolder = null
if (delete)
File.Delete(path); // File is unpacked.
}
public static byte[][] unpackMini(byte[] fileData, string identifier)
/// <summary>
/// Unpacks a BinLinkerAccessor generated file into individual arrays.
/// </summary>
/// <param name="fileData">Packed data</param>
/// <param name="identifier">Signature expected in the first two bytes (ASCII)</param>
/// <returns>Unpacked array containing all files that were packed.</returns>
public static byte[][] UnpackMini(byte[] fileData, string identifier)
{
if (fileData == null || fileData.Length < 4)
return null;
@@ -207,7 +218,8 @@ public static byte[][] unpackMini(byte[] fileData, string identifier)
}
return returnData;
}
public static string getIsMini(string path)
public static string GetIsMini(string path)
{
byte[] data = File.ReadAllBytes(path);
var fi = new FileInfo(path);
@@ -220,8 +232,8 @@ public static string getIsMini(string path)
uint length = 1338;
for (int i = 0; i < count; i++)
{
offsets[i] = BitConverter.ToUInt32(data, 4 + i * 4);
length = BitConverter.ToUInt32(data, 8 + i * 4);
offsets[i] = BitConverter.ToUInt32(data, 4 + (i * 4));
length = BitConverter.ToUInt32(data, 8 + (i * 4));
}
offsets[offsets.Length - 1] = length;

View File

@@ -153,10 +153,10 @@ public void InitializeMoves()
if (XY)
Moves = GARCMoves.Files.Select(file => new Move(file)).ToArray();
if (ORAS)
Moves = mini.unpackMini(GARCMoves.getFile(0), "WD").Select(file => new Move(file)).ToArray();
Moves = Mini.UnpackMini(GARCMoves.getFile(0), "WD").Select(file => new Move(file)).ToArray();
break;
case 7:
Moves = mini.unpackMini(GARCMoves.getFile(0), "WD").Select(file => new Move(file)).ToArray();
Moves = Mini.UnpackMini(GARCMoves.getFile(0), "WD").Select(file => new Move(file)).ToArray();
break;
}
}

View File

@@ -768,14 +768,14 @@ private void B_Move_Click(object sender, EventArgs e)
{
case 6:
bool isMini = Config.ORAS;
Moves = isMini ? mini.unpackMini(g.getFile(0), "WD") : g.Files;
Moves = isMini ? Mini.UnpackMini(g.getFile(0), "WD") : g.Files;
Invoke((Action)(() => new MoveEditor6(Moves).ShowDialog()));
g.Files = isMini ? new[] { mini.packMini(Moves, "WD") } : Moves;
g.Files = isMini ? new[] { Mini.PackMini(Moves, "WD") } : Moves;
break;
case 7:
Moves = mini.unpackMini(g.getFile(0), "WD");
Moves = Mini.UnpackMini(g.getFile(0), "WD");
Invoke((Action)(() => new MoveEditor7(Moves).ShowDialog()));
g.Files = new[] {mini.packMini(Moves, "WD")};
g.Files = new[] {Mini.PackMini(Moves, "WD")};
break;
}
g.Save();

View File

@@ -27,7 +27,7 @@ public void drawMap(int Map)
public Bitmap getMapImage(bool crop = false, bool entity = true, bool sliceArea = false)
{
// Load MM
byte[][] MM = mini.unpackMini(File.ReadAllBytes(MapMatrixes[DrawMap]), "MM");
byte[][] MM = Mini.UnpackMini(File.ReadAllBytes(MapMatrixes[DrawMap]), "MM");
var mm = OWSE.mm = new MapMatrix(MM);
// Unknown
@@ -39,7 +39,7 @@ public Bitmap getMapImage(bool crop = false, bool entity = true, bool sliceArea
{
if (mm.EntryList[i] == 0xFFFF) // Mystery Zone
continue;
byte[][] GR = mini.unpackMini(File.ReadAllBytes(MapGRs[mm.EntryList[i]]), "GR");
byte[][] GR = Mini.UnpackMini(File.ReadAllBytes(MapGRs[mm.EntryList[i]]), "GR");
mm.Entries[i] = new MapMatrix.Entry(GR[0]) {coll = new MapMatrix.Collision(GR[2])};
}
mapScale = Math.Max(1, (int)NUD_Scale.Value);
@@ -129,7 +129,7 @@ private Bitmap overlayEntities(Bitmap img)
//{
// using(var g = Graphics.FromImage(img))
// foreach (var l in OWSE.mm.LoadLines)
// try { g.DrawLine(new Pen(Color.Red, 4), l.p2 * mapScale, l.p1 * mapScale, l.p4 * mapScale, l.p3 * mapScale); }
// try { g.DrawLine(new Pen(Color.Red, 4), l.p2 * mapScale, l.p1 * mapScale, l.p4 * mapScale, l.p3 * mapScale); }
// catch {}
//}
@@ -163,7 +163,7 @@ private void hoverMap(object sender, MouseEventArgs e)
L_MapCoord.Text = string.Format("V:0x{3}{2}X:{0,3} Y:{1,3}", X, Y, Environment.NewLine, tileVal);
}
catch { }
catch { }
}
private void B_Redraw_Click(object sender, EventArgs e)
{

View File

@@ -20,7 +20,7 @@ public OWSE()
AllowDrop = true;
DragEnter += tabMain_DragEnter;
DragDrop += tabMain_DragDrop;
// Finished
openQuick(Directory.GetFiles("encdata"));
mapView.Show();
@@ -65,7 +65,7 @@ private void openQuick(string[] encdata)
zdLocations[f] = LocationNum.ToString("000") + " - " + LocationName;
rawLocations[f] = LocationName;
}
// Assign
CB_LocationID.DataSource = zdLocations;
CB_LocationID.Enabled = true;
@@ -94,7 +94,7 @@ private void getEntry()
{
if (entry < 0) return;
byte[] raw = File.ReadAllBytes(filepaths[entry]);
locationData = Core.CTR.mini.unpackMini(raw, "ZO");
locationData = Core.CTR.Mini.UnpackMini(raw, "ZO");
if (locationData == null) return;
// Read master ZD table
@@ -125,7 +125,7 @@ private void setEntry()
{
if (entry < 0) return;
if (debugToolDumping) return;
// Set the data back into the class object
// Currently only the first two files.
setZoneData(); // File 0
@@ -141,12 +141,12 @@ private void setEntry()
// Debug Check (can stay, why not.)
if (!locationData.Where((t, i) => !data[i].SequenceEqual(t)).Any())
return;
// Util.Alert("Zone has been edited!");
System.Media.SystemSounds.Asterisk.Play();
// Package the files into the permanent package file.
byte[] raw = mini.packMini(data, "ZO");
byte[] raw = Mini.PackMini(data, "ZO");
File.WriteAllBytes(filepaths[entry], raw);
}

View File

@@ -33,7 +33,7 @@ public OWSE7(lzGARCFile ed, lzGARCFile zd, lzGARCFile wd)
private readonly byte[] zoneData;
private byte[] worldData;
private readonly string[] locationList;
private void loadData()
{
// get zonedata array
@@ -107,14 +107,14 @@ private class World
private bool HasZS => _7 != null;
private bool HasZI => _8 != null;
public Script[] ZoneScripts;
public Script[] ZoneScripts;
public Script[] ZoneInfoScripts;
public World(lzGARCFile garc, int worldID)
{
int index = worldID*11;
_7 = mini.unpackMini(garc[index + 7], "ZS");
_8 = mini.unpackMini(garc[index + 8], "ZI");
_7 = Mini.UnpackMini(garc[index + 7], "ZS");
_8 = Mini.UnpackMini(garc[index + 8], "ZI");
ZoneScripts = HasZS ? _7.Select(arr => new Script(arr)).ToArray() : new Script[0];
ZoneInfoScripts = HasZI ? _8.Select(arr => new Script(arr)).ToArray() : new Script[0];

View File

@@ -215,7 +215,7 @@ private void UpdateSpeciesForm(object sender, EventArgs e)
var cur_pb = CB_TableID.SelectedIndex%2 == 0 ? PB_DayTable : PB_NightTable;
var cur_img = cur_pb.Image;
object[][] source = sender is NumericUpDown ? (object[][])nup_spec : cb_spec;
int table = Array.FindIndex(source, t => t.Contains(sender));
int slot = Array.IndexOf(source[table], sender);
@@ -255,14 +255,14 @@ private void UpdateEncounterRate(object sender, EventArgs e)
{
if (loadingdata)
return;
var cur_pb = CB_TableID.SelectedIndex%2 == 0 ? PB_DayTable : PB_NightTable;
var cur_img = cur_pb.Image;
int slot = Array.IndexOf(rate_spec, sender);
int rate = (int) ((NumericUpDown) sender).Value;
CurrentTable.Rates[slot] = rate;
using (var g = Graphics.FromImage(cur_img))
{
var pnt = new PointF(40 * slot + 10, 10);
@@ -270,7 +270,7 @@ private void UpdateEncounterRate(object sender, EventArgs e)
g.Clear(Color.Transparent);
g.DrawString($"{rate}%", font, Brushes.Black, pnt);
}
cur_pb.Image = cur_img;
var sum = TotalEncounterRate;
@@ -330,7 +330,7 @@ private void B_Save_Click(object sender, EventArgs e)
WinFormsUtil.Error("Encounter rates must add up to either 0% or 100%.");
return;
}
CurrentTable.Write();
var area = Areas[CB_LocationID.SelectedIndex];
area.Tables[CB_TableID.SelectedIndex] = CurrentTable;
@@ -363,11 +363,11 @@ private void DumpTables(object sender, EventArgs e)
File.WriteAllText(sfd.FileName, sb.ToString());
}
}
// Randomization & Bulk Modification
private void B_Randomize_Click(object sender, EventArgs e)
{
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo,
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo,
"Randomize all? Cannot undo.", "Double check Randomization settings at the bottom left."))
return;
@@ -475,8 +475,8 @@ private void ExportEncounters(string gameID, string ident)
var reg = dumpreg();
var sos = dumpsos();
File.WriteAllBytes($"encounter_{gameID}.pkl", mini.packMini(reg, ident));
File.WriteAllBytes($"encounter_{gameID}_sos.pkl", mini.packMini(sos, ident));
File.WriteAllBytes($"encounter_{gameID}.pkl", Mini.PackMini(reg, ident));
File.WriteAllBytes($"encounter_{gameID}_sos.pkl", Mini.PackMini(sos, ident));
}
private byte[][] dumpreg()
{

View File

@@ -24,20 +24,21 @@ public string GetSummary(string[] speciesList)
{
var sb = new StringBuilder();
sb.AppendLine("==========");
sb.AppendLine($"Map: {Name}");
sb.AppendLine($"Tables: {Tables.Count / 2}");
sb.Append("Map: ").AppendLine(Name);
sb.Append("Tables: ").Append(Tables.Count / 2).AppendLine();
for (int i = 0; i < Tables.Count / 2; i++)
{
sb.AppendLine($"Table {i+1} (Day):");
sb.Append("Table ").Append(i + 1).AppendLine(" (Day):");
sb.AppendLine(Tables[i*2].GetSummary(speciesList));
sb.AppendLine($"Table {i+1} (Night):");
sb.AppendLine(Tables[i*2 + 1].GetSummary(speciesList));
sb.Append("Table ").Append(i + 1).AppendLine(" (Night):");
sb.AppendLine(Tables[(i * 2) + 1].GetSummary(speciesList));
}
sb.AppendLine("==========");
return sb.ToString();
}
private const string PackIdentifier = "EA";
public static byte[] GetDayNightTableBinary(IList<EncounterTable> tables)
{
byte[][] tabs = new byte[tables.Count / 2][];
@@ -50,7 +51,7 @@ public static byte[] GetDayNightTableBinary(IList<EncounterTable> tables)
table1.CopyTo(arr, 4 + table0.Length);
tabs[i / 2] = arr;
}
return mini.packMini(tabs, PackIdentifier);
return Mini.PackMini(tabs, PackIdentifier);
}
public static Area7[] GetArray(lzGARCFile ed, ZoneData7[] zd)
@@ -62,7 +63,7 @@ public static Area7[] GetArray(lzGARCFile ed, ZoneData7[] zd)
{
areas[i] = new Area7
{
FileNumber = 9 + 11 * i,
FileNumber = 9 + (11 * i),
Zones = zd.Where(z => z.AreaIndex == i).ToArray()
};
var md = ed[areas[i].FileNumber];
@@ -72,7 +73,7 @@ public static Area7[] GetArray(lzGARCFile ed, ZoneData7[] zd)
continue;
}
byte[][] Tables = mini.unpackMini(md, PackIdentifier);
byte[][] Tables = Mini.UnpackMini(md, PackIdentifier);
areas[i].HasTables = Tables.Any(t => t.Length > 0);
if (!areas[i].HasTables)
continue;
@@ -98,7 +99,7 @@ public static Area7[] GetArray(lzGARCFile ed, ZoneData7[] zd)
/// <returns>Annotated Area Array</returns>
public static Area7[] GetArray(lzGARCFile ed, lzGARCFile zd, lzGARCFile wd, string[] locationList)
{
var Worlds = wd.Files.Select(f => mini.unpackMini(f, "WD")[0]).ToArray();
var Worlds = wd.Files.Select(f => Mini.UnpackMini(f, "WD")[0]).ToArray();
byte[][] zdfiles = zd.Files;
var worldData = zdfiles[1];

View File

@@ -138,15 +138,15 @@ internal static void openARC(string path, ProgressBar pBar1, bool recursing = fa
// Determine if it is a DARC or a Mini
// Check if Mini first
string fx = fi.Length > 10 * (1<<20) ? null : mini.getIsMini(path); // no mini is above 10MB
string fx = fi.Length > 10 * (1<<20) ? null : Mini.GetIsMini(path); // no mini is above 10MB
if (fx != null) // Is Mini Packed File
{
newFolder = folderPath + "_" + fx;
// Fetch Mini File Contents
mini.unpackMini(path, fx, newFolder, false);
Mini.UnpackMini(path, fx, newFolder, false);
// Recurse throught the extracted contents if they extract successfully
if (Directory.Exists(newFolder))
{
{
foreach (string file in Directory.GetFiles(newFolder))
openARC(file, pBar1, true);
batchRenameExtension(newFolder);
@@ -211,7 +211,7 @@ internal static void openARC(string path, ProgressBar pBar1, bool recursing = fa
Console.WriteLine($"New SARC with {sarc.SFAT.EntryCount} files.");
foreach (var z in sarc.Dump(path))
{
}
}
else if (!recursing)
@@ -295,7 +295,7 @@ private void saveARC(string path)
int index = fileName.LastIndexOf('_');
string fileNum = fileName.Substring(0, index);
string fileExt = fileName.Substring(index + 1);
// Find old file for reference...
string file;
if (File.Exists(Path.Combine(parentName, fileNum + ".bin")))
@@ -306,7 +306,7 @@ private void saveARC(string path)
file = null;
byte[] oldData = file != null ? File.ReadAllBytes(file) : null;
bool r = mini.packMini2(path, fileExt, Path.Combine(parentName, fileNum + "." + fileExt));
bool r = Mini.PackMini2(path, fileExt, Path.Combine(parentName, fileNum + "." + fileExt));
if (!r)
{
WinFormsUtil.Alert("Packing failed.");
@@ -330,9 +330,9 @@ private void saveARC(string path)
break;
// Fix pointers
byte[] update = mini.adjustMiniHeader(newData, oldPtr);
byte[] update = Mini.AdjustMiniHeader(newData, oldPtr);
File.WriteAllBytes(Path.Combine(parentName, fileNum + "." + fileExt), update);
}
}
}
break;
@@ -364,7 +364,7 @@ private void B_Reset_Click(object sender, EventArgs e)
private static void batchRenameExtension(string Folder)
{
if (!Directory.Exists(Folder))
if (!Directory.Exists(Folder))
return;
foreach (string f in Directory.GetFiles(Folder, "*", SearchOption.AllDirectories))