Fix Patch gathering and file copying

This commit is contained in:
Kurt
2015-06-26 07:26:46 -07:00
parent 7abd8e8af7
commit 0ddaf6952f
2 changed files with 79 additions and 20 deletions

View File

@@ -35,6 +35,8 @@ private void InitializeComponent()
this.CHK_Lang = new System.Windows.Forms.CheckBox();
this.RTB_GARCs = new System.Windows.Forms.RichTextBox();
this.label2 = new System.Windows.Forms.Label();
this.B_CheckAll = new System.Windows.Forms.Button();
this.B_CheckNone = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// CHKLB_GARCs
@@ -44,7 +46,7 @@ private void InitializeComponent()
this.CHKLB_GARCs.FormattingEnabled = true;
this.CHKLB_GARCs.Location = new System.Drawing.Point(12, 12);
this.CHKLB_GARCs.Name = "CHKLB_GARCs";
this.CHKLB_GARCs.Size = new System.Drawing.Size(150, 289);
this.CHKLB_GARCs.Size = new System.Drawing.Size(150, 259);
this.CHKLB_GARCs.TabIndex = 0;
//
// textBox1
@@ -94,7 +96,7 @@ private void InitializeComponent()
this.RTB_GARCs.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.RTB_GARCs.Location = new System.Drawing.Point(168, 90);
this.RTB_GARCs.Name = "RTB_GARCs";
this.RTB_GARCs.Size = new System.Drawing.Size(75, 160);
this.RTB_GARCs.Size = new System.Drawing.Size(75, 181);
this.RTB_GARCs.TabIndex = 5;
this.RTB_GARCs.Text = "";
//
@@ -107,11 +109,35 @@ private void InitializeComponent()
this.label2.TabIndex = 6;
this.label2.Text = "Extra GARCs:";
//
// B_CheckAll
//
this.B_CheckAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.B_CheckAll.Location = new System.Drawing.Point(12, 278);
this.B_CheckAll.Name = "B_CheckAll";
this.B_CheckAll.Size = new System.Drawing.Size(75, 23);
this.B_CheckAll.TabIndex = 7;
this.B_CheckAll.Text = "Check All";
this.B_CheckAll.UseVisualStyleBackColor = true;
this.B_CheckAll.Click += new System.EventHandler(this.B_CheckAll_Click);
//
// B_CheckNone
//
this.B_CheckNone.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.B_CheckNone.Location = new System.Drawing.Point(87, 278);
this.B_CheckNone.Name = "B_CheckNone";
this.B_CheckNone.Size = new System.Drawing.Size(75, 23);
this.B_CheckNone.TabIndex = 8;
this.B_CheckNone.Text = "Check None";
this.B_CheckNone.UseVisualStyleBackColor = true;
this.B_CheckNone.Click += new System.EventHandler(this.B_CheckNone_Click);
//
// Patch
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 312);
this.Controls.Add(this.B_CheckNone);
this.Controls.Add(this.B_CheckAll);
this.Controls.Add(this.label2);
this.Controls.Add(this.RTB_GARCs);
this.Controls.Add(this.CHK_Lang);
@@ -139,5 +165,7 @@ private void InitializeComponent()
private System.Windows.Forms.CheckBox CHK_Lang;
private System.Windows.Forms.RichTextBox RTB_GARCs;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button B_CheckAll;
private System.Windows.Forms.Button B_CheckNone;
}
}

View File

@@ -18,7 +18,7 @@ public Patch()
CHKLB_GARCs.Items.Add(s);
}
internal static bool patchExeFS(string path, string[] oldstr, string[] newstr, ref string result, string outPath = null)
internal static bool patchExeFS(string path, string[] oldstr, string[] newstr, string oldROM, string newROM, ref string result, string outPath = null)
{
int ctr = 0;
if (oldstr.Length != newstr.Length)
@@ -28,66 +28,86 @@ internal static bool patchExeFS(string path, string[] oldstr, string[] newstr, r
}
string text = File.ReadAllText(path, Encoding.Unicode);
if (!text.Contains(newROM))
{
result = "ExeFS\\.code.bin is not a patchable ExeFS (no rom2: found).";
return false;
}
for (int i = 0; i < oldstr.Length; i++)
{
if (!text.Contains(oldstr[i]))
string oldString = (oldROM + oldstr[i]).Replace(Path.DirectorySeparatorChar, '/');
string patchedStr = (newROM + oldstr[i]).Replace(Path.DirectorySeparatorChar, '/');
string newString = (newROM + newstr[i]).Replace(Path.DirectorySeparatorChar, '/');
bool old = text.Contains(oldString);
bool patched = text.Contains(patchedStr);
if (!old && !patched)
result += "Does not contain " + oldstr + Environment.NewLine;
else
ctr++;
text = text.Replace(oldstr[i], newstr[i]);
if (old)
text = text.Replace(oldString, newString);
if (patched)
text = text.Replace(patchedStr, newString);
}
if (ctr == 0)
{ result = "Did not find the old path strings to replace."; return false; }
result += String.Format("Redirected {0} file paths.", ctr);
Directory.CreateDirectory(Directory.GetParent(outPath).Name);
File.WriteAllText(outPath ?? path, text, Encoding.Unicode);
return true;
}
internal static string exportGARCs(string[] garcPaths, string[] newPaths, string parentRomFS, string patchFolder)
{
if (Directory.Exists(patchFolder))
Directory.Delete(patchFolder, true);
Directory.CreateDirectory(patchFolder);
// Stuff files into new patch folder
for (int i = 0; i < garcPaths.Length; i++)
{
if ((garcPaths[i] ?? "").Length == 0) continue;
string folder = Path.GetDirectoryName(newPaths[i]);
string oldPath = parentRomFS + garcPaths[i];
string newPath = patchFolder + newPaths[i];
string folder = Path.GetDirectoryName(newPath);
Directory.CreateDirectory(folder);
File.Copy(Path.Combine(parentRomFS, garcPaths[i]), folder);
File.Copy(oldPath, newPath);
}
return patchFolder;
}
private void B_PatchCIA_Click(object sender, EventArgs e)
{
string patchFolder = String.Format("{0} ({1})", "Patch", (DateTime.Now).ToString("yy-MM-dd@HH-mm-ss"));
try
{
string[] garcs = getGARCs();
string[] garcPaths = getPaths(garcs);
const string oldROM = "rom:/a/";
const string newROM = "rom2:/a";
const string oldROM = "rom:";
const string newROM = "rom2:";
const string oldA = "\\a\\";
const string newA = "\\a";
string[] newPaths = (string[]) garcPaths.Clone();
// Patch the reference
for (int i = 0; i < newPaths.Length; i++)
{
int posA = newPaths[i].LastIndexOf(oldROM, StringComparison.Ordinal);
newPaths[i] = (posA == -1) ? null : newPaths[i].Remove(posA, oldROM.Length).Insert(posA, newROM);
int posA = newPaths[i].LastIndexOf(oldA, StringComparison.Ordinal);
newPaths[i] = (posA == -1) ? null : newPaths[i].Remove(posA, oldA.Length).Insert(posA, newA);
}
string patchFolder = String.Format("{0} ({1})", "Patch", new DateTime().ToString("yyMMdd@HHmmss"));
string result = "";
if (!patchExeFS(Main.ExeFSPath, garcPaths, newPaths, ref result, Path.Combine(patchFolder, ".code.bin")))
return;
string ExeFS = Directory.GetFiles(Main.ExeFSPath)[0];
if (!File.Exists(ExeFS) || !Path.GetFileNameWithoutExtension(ExeFS).Contains("code")) { throw new Exception("No .code.bin detected."); }
if (!patchExeFS(ExeFS, garcPaths, newPaths, oldROM, newROM, ref result, Path.Combine(patchFolder, ".code.bin")))
throw new Exception(result);
Util.Alert("Patch contents saved to:" + Environment.NewLine + exportGARCs(garcPaths, newPaths, Main.RomFSPath, patchFolder), result);
}
catch (Exception ex)
{ Util.Error("Could not create patch:", ex.ToString()); }
{
Util.Error("Could not create patch:", ex.ToString());
Directory.Delete(patchFolder, true);
}
}
private string[] getGARCs()
@@ -109,7 +129,7 @@ private string[] getPaths(string[] sc)
bool languages = CHK_Lang.Checked;
StringCollection paths = new StringCollection();
foreach (string s in sc)
if (!languages || (s != "gametext" || s != "storytext"))
if (!languages || (s != "gametext" && s != "storytext"))
paths.Add(Main.getGARCFileName(s, Main.Language));
else
for (int l = 0; l < 8; l++)
@@ -119,5 +139,16 @@ private string[] getPaths(string[] sc)
paths.CopyTo(garcs, 0);
return garcs;
}
private void B_CheckAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < CHKLB_GARCs.Items.Count; i++)
CHKLB_GARCs.SetItemChecked(i, true);
}
private void B_CheckNone_Click(object sender, EventArgs e)
{
for (int i = 0; i < CHKLB_GARCs.Items.Count; i++)
CHKLB_GARCs.SetItemChecked(i, false);
}
}
}