From 50552927d7df795d86bd7fa28fd440c7471c446f Mon Sep 17 00:00:00 2001 From: ucorpor Date: Sun, 31 Mar 2024 18:14:30 +0400 Subject: [PATCH] console version support --- ibf-unpack/IbfUnpack.cs | 75 ++++++++++++++++----------- ibf-unpack/MainForm.Designer.cs | 66 ++++++++++++++++++----- ibf-unpack/MainForm.cs | 14 ++++- ibf-unpack/Properties/AssemblyInfo.cs | 4 +- 4 files changed, 115 insertions(+), 44 deletions(-) diff --git a/ibf-unpack/IbfUnpack.cs b/ibf-unpack/IbfUnpack.cs index ee225a0..392e11d 100644 --- a/ibf-unpack/IbfUnpack.cs +++ b/ibf-unpack/IbfUnpack.cs @@ -14,10 +14,10 @@ public static void Unpack(string archivePath) Directory.CreateDirectory(systemDir); Stream stream = File.OpenRead(archivePath); - int filenameLength = stream.ReadByte() * 2; + int filenameLength = stream.ReadByte(); while (filenameLength > 0) { - string filename = ReadString(stream, filenameLength).Replace("\0", string.Empty); + string filename = ReadString(stream, filenameLength * 2).Replace("\0", string.Empty); string filepath = Path.Combine(systemDir, filename); File.WriteAllText(filepath, string.Empty); @@ -31,54 +31,65 @@ public static void Unpack(string archivePath) int remained = fileLength - fileLength / chunkSize * chunkSize; ReadAndWriteBytes(stream, remained, filepath); - filenameLength = stream.ReadByte() * 2; + filenameLength = stream.ReadByte(); } stream.Close(); } - public static void UnpackV2(string archivePath) + public static void Unpack2(string archivePath, bool isBigEndian = false) { string systemDir = Path.Combine(Path.GetDirectoryName(archivePath), "System"); Directory.CreateDirectory(systemDir); Stream stream = File.OpenRead(archivePath); - int filenameLength = stream.ReadByte() * 2; - while (filenameLength > 0) + int filenameLength = stream.ReadByte(); + while (stream.Position < stream.Length) { - string filename = ReadString(stream, filenameLength).Replace("\0", string.Empty); - string filepath = Path.Combine(systemDir, filename); - File.WriteAllText(filepath, string.Empty); - - List file = new List(); - byte prevB = byte.MaxValue; - while (true) + if (filenameLength > 0) { - byte b = ReadBytes(stream, 1)[0]; - if ((prevB == 0x0D || prevB == 0x5B) && b == 0x0) + string filename = ReadString(stream, filenameLength * 2, isBigEndian).Replace("\0", string.Empty); + string filepath = Path.Combine(systemDir, filename); + File.WriteAllText(filepath, string.Empty); + + List file = new List(); + byte prevB = byte.MaxValue; + while (stream.Position < stream.Length) { - file.Add(prevB); - file.Add(b); - break; + byte b = ReadByte(stream); + if ((isBigEndian == false && prevB != 0x0 && b == 0x0) + || (isBigEndian == true && prevB == 0x0 && b != 0x0)) + { + file.Add(prevB); + file.Add(b); + break; + } + prevB = b; } - prevB = b; - } - while (true) - { - byte[] symbol = ReadBytes(stream, 2); - if (symbol[0] == 0x0 && symbol[1] == 0x0) + while (stream.Position < stream.Length) { - WriteBytes(file.ToArray(), filepath); - break; + byte[] symbol = ReadBytes(stream, 2); + if (symbol[0] == 0x0 && symbol[1] == 0x0) + { + WriteBytes(file.ToArray(), filepath); + break; + } + file.AddRange(symbol); } - file.AddRange(symbol); } - filenameLength = stream.ReadByte() * 2; + filenameLength = stream.ReadByte(); } stream.Close(); } + private static byte ReadByte(Stream stream) + { + byte[] bytes = new byte[1]; + stream.Read(bytes, 0, 1); + return bytes[0]; + } + private static byte[] ReadBytes(Stream stream, int length) { byte[] bytes = new byte[length]; @@ -86,9 +97,15 @@ private static byte[] ReadBytes(Stream stream, int length) return bytes; } - private static string ReadString(Stream stream, int length) + private static string ReadString(Stream stream, int length, bool isBigEndian = false) { byte[] bytes = ReadBytes(stream, length); + + if (isBigEndian) + { + return Encoding.BigEndianUnicode.GetString(bytes); + } + return Encoding.Unicode.GetString(bytes); } diff --git a/ibf-unpack/MainForm.Designer.cs b/ibf-unpack/MainForm.Designer.cs index 1a32612..0a52d5a 100644 --- a/ibf-unpack/MainForm.Designer.cs +++ b/ibf-unpack/MainForm.Designer.cs @@ -37,6 +37,9 @@ private void InitializeComponent() this.unpackPage = new System.Windows.Forms.TabPage(); this.makePage = new System.Windows.Forms.TabPage(); this.makeBtn = new System.Windows.Forms.Button(); + this.b1Rb = new System.Windows.Forms.RadioButton(); + this.consoleRb = new System.Windows.Forms.RadioButton(); + this.b2Rb = new System.Windows.Forms.RadioButton(); this.tabControl1.SuspendLayout(); this.unpackPage.SuspendLayout(); this.makePage.SuspendLayout(); @@ -44,7 +47,7 @@ private void InitializeComponent() // // pathBtn // - this.pathBtn.Location = new System.Drawing.Point(262, 5); + this.pathBtn.Location = new System.Drawing.Point(251, 3); this.pathBtn.Name = "pathBtn"; this.pathBtn.Size = new System.Drawing.Size(96, 23); this.pathBtn.TabIndex = 0; @@ -56,14 +59,14 @@ private void InitializeComponent() // this.pathTxt.Location = new System.Drawing.Point(6, 5); this.pathTxt.Name = "pathTxt"; - this.pathTxt.Size = new System.Drawing.Size(250, 20); + this.pathTxt.Size = new System.Drawing.Size(231, 20); this.pathTxt.TabIndex = 1; // // unpackBtn // - this.unpackBtn.Location = new System.Drawing.Point(171, 31); + this.unpackBtn.Location = new System.Drawing.Point(165, 55); this.unpackBtn.Name = "unpackBtn"; - this.unpackBtn.Size = new System.Drawing.Size(187, 23); + this.unpackBtn.Size = new System.Drawing.Size(182, 21); this.unpackBtn.TabIndex = 2; this.unpackBtn.Text = "Unpack archive"; this.unpackBtn.UseVisualStyleBackColor = true; @@ -72,7 +75,7 @@ private void InitializeComponent() // renameCb // this.renameCb.AutoSize = true; - this.renameCb.Location = new System.Drawing.Point(6, 35); + this.renameCb.Location = new System.Drawing.Point(3, 33); this.renameCb.Name = "renameCb"; this.renameCb.Size = new System.Drawing.Size(159, 17); this.renameCb.TabIndex = 3; @@ -82,7 +85,7 @@ private void InitializeComponent() // linkLabel1 // this.linkLabel1.AutoSize = true; - this.linkLabel1.Location = new System.Drawing.Point(196, 104); + this.linkLabel1.Location = new System.Drawing.Point(181, 133); this.linkLabel1.Name = "linkLabel1"; this.linkLabel1.Size = new System.Drawing.Size(191, 13); this.linkLabel1.TabIndex = 4; @@ -97,19 +100,22 @@ private void InitializeComponent() this.tabControl1.Location = new System.Drawing.Point(11, 12); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; - this.tabControl1.Size = new System.Drawing.Size(375, 89); + this.tabControl1.Size = new System.Drawing.Size(361, 118); this.tabControl1.TabIndex = 5; // // unpackPage // + this.unpackPage.Controls.Add(this.b2Rb); + this.unpackPage.Controls.Add(this.consoleRb); this.unpackPage.Controls.Add(this.renameCb); + this.unpackPage.Controls.Add(this.b1Rb); this.unpackPage.Controls.Add(this.pathBtn); this.unpackPage.Controls.Add(this.pathTxt); this.unpackPage.Controls.Add(this.unpackBtn); this.unpackPage.Location = new System.Drawing.Point(4, 22); this.unpackPage.Name = "unpackPage"; this.unpackPage.Padding = new System.Windows.Forms.Padding(3); - this.unpackPage.Size = new System.Drawing.Size(367, 63); + this.unpackPage.Size = new System.Drawing.Size(353, 92); this.unpackPage.TabIndex = 0; this.unpackPage.Text = "Unpack"; this.unpackPage.UseVisualStyleBackColor = true; @@ -120,7 +126,7 @@ private void InitializeComponent() this.makePage.Location = new System.Drawing.Point(4, 22); this.makePage.Name = "makePage"; this.makePage.Padding = new System.Windows.Forms.Padding(3); - this.makePage.Size = new System.Drawing.Size(367, 63); + this.makePage.Size = new System.Drawing.Size(353, 92); this.makePage.TabIndex = 1; this.makePage.Text = "Make"; this.makePage.UseVisualStyleBackColor = true; @@ -129,17 +135,50 @@ private void InitializeComponent() // this.makeBtn.Location = new System.Drawing.Point(6, 6); this.makeBtn.Name = "makeBtn"; - this.makeBtn.Size = new System.Drawing.Size(355, 51); + this.makeBtn.Size = new System.Drawing.Size(341, 80); this.makeBtn.TabIndex = 0; - this.makeBtn.Text = "Make archive..."; + this.makeBtn.Text = "Make archive...\r\n(PC version only)"; this.makeBtn.UseVisualStyleBackColor = true; this.makeBtn.Click += new System.EventHandler(this.makeBtn_Click); // + // b1Rb + // + this.b1Rb.AutoSize = true; + this.b1Rb.Checked = true; + this.b1Rb.Location = new System.Drawing.Point(165, 32); + this.b1Rb.Name = "b1Rb"; + this.b1Rb.Size = new System.Drawing.Size(76, 17); + this.b1Rb.TabIndex = 4; + this.b1Rb.TabStop = true; + this.b1Rb.Text = "PC version"; + this.b1Rb.UseVisualStyleBackColor = true; + // + // consoleRb + // + this.consoleRb.AutoSize = true; + this.consoleRb.Location = new System.Drawing.Point(247, 32); + this.consoleRb.Name = "consoleRb"; + this.consoleRb.Size = new System.Drawing.Size(100, 17); + this.consoleRb.TabIndex = 5; + this.consoleRb.Text = "Console version"; + this.consoleRb.UseVisualStyleBackColor = true; + // + // b2Rb + // + this.b2Rb.AutoSize = true; + this.b2Rb.Location = new System.Drawing.Point(3, 57); + this.b2Rb.Name = "b2Rb"; + this.b2Rb.Size = new System.Drawing.Size(81, 17); + this.b2Rb.TabIndex = 6; + this.b2Rb.Text = "Bioshock 2 "; + this.b2Rb.UseVisualStyleBackColor = true; + this.b2Rb.Visible = false; + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(398, 128); + this.ClientSize = new System.Drawing.Size(384, 155); this.Controls.Add(this.tabControl1); this.Controls.Add(this.linkLabel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; @@ -168,6 +207,9 @@ private void InitializeComponent() private System.Windows.Forms.TabPage unpackPage; private System.Windows.Forms.TabPage makePage; private System.Windows.Forms.Button makeBtn; + private System.Windows.Forms.RadioButton b1Rb; + private System.Windows.Forms.RadioButton b2Rb; + private System.Windows.Forms.RadioButton consoleRb; } } diff --git a/ibf-unpack/MainForm.cs b/ibf-unpack/MainForm.cs index 5cd47f4..711e8d4 100644 --- a/ibf-unpack/MainForm.cs +++ b/ibf-unpack/MainForm.cs @@ -42,7 +42,18 @@ private void unpackBtn_Click(object sender, EventArgs e) try { string path = pathTxt.Text.Trim(); - IbfUnpack.Unpack(path); + if (consoleRb.Checked) + { + IbfUnpack.Unpack2(path, true); + } + else if (b2Rb.Checked) + { + IbfUnpack.Unpack2(path); + } + else + { + IbfUnpack.Unpack(path); + } if (renameCb.Checked) File.Move(path, $"{path}.backup"); @@ -110,5 +121,6 @@ private void makeBtn_Click(object sender, EventArgs e) Cursor = Cursors.Default; } } + } } diff --git a/ibf-unpack/Properties/AssemblyInfo.cs b/ibf-unpack/Properties/AssemblyInfo.cs index 7947186..e682a2e 100644 --- a/ibf-unpack/Properties/AssemblyInfo.cs +++ b/ibf-unpack/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.0.0")] -[assembly: AssemblyFileVersion("1.3.0.0")] +[assembly: AssemblyVersion("1.4.0.0")] +[assembly: AssemblyFileVersion("1.4.0.0")]