mirror of
https://github.com/WB3000/nusdownloader.git
synced 2024-11-16 22:59:22 +01:00
WAD Packing has been isolated into a class file.
The GUI now supports SaveAs and Deletion of contents options via checkbox.
This commit is contained in:
parent
8d62c49a99
commit
1cc04ad89a
63
NUS Downloader/Form1.Designer.cs
generated
63
NUS Downloader/Form1.Designer.cs
generated
@ -116,6 +116,8 @@
|
||||
this.europePALToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.japanNTSCJToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.koreaToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.saveaswadbox = new System.Windows.Forms.CheckBox();
|
||||
this.deletecontentsbox = new System.Windows.Forms.CheckBox();
|
||||
this.databaseStrip.SuspendLayout();
|
||||
this.tmdgpbox.SuspendLayout();
|
||||
this.ticketgpbox.SuspendLayout();
|
||||
@ -169,13 +171,14 @@
|
||||
// packbox
|
||||
//
|
||||
this.packbox.AutoSize = true;
|
||||
this.packbox.Location = new System.Drawing.Point(12, 422);
|
||||
this.packbox.Location = new System.Drawing.Point(12, 416);
|
||||
this.packbox.Name = "packbox";
|
||||
this.packbox.Size = new System.Drawing.Size(92, 17);
|
||||
this.packbox.TabIndex = 6;
|
||||
this.packbox.Text = "Pack -> WAD";
|
||||
this.packbox.UseVisualStyleBackColor = true;
|
||||
this.packbox.CheckedChanged += new System.EventHandler(this.packbox_CheckedChanged);
|
||||
this.packbox.EnabledChanged += new System.EventHandler(this.packbox_EnabledChanged);
|
||||
//
|
||||
// dlprogress
|
||||
//
|
||||
@ -191,11 +194,11 @@
|
||||
this.localuse.Checked = true;
|
||||
this.localuse.CheckState = System.Windows.Forms.CheckState.Checked;
|
||||
this.localuse.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.localuse.Location = new System.Drawing.Point(12, 468);
|
||||
this.localuse.Location = new System.Drawing.Point(104, 463);
|
||||
this.localuse.Name = "localuse";
|
||||
this.localuse.Size = new System.Drawing.Size(167, 17);
|
||||
this.localuse.Size = new System.Drawing.Size(76, 17);
|
||||
this.localuse.TabIndex = 8;
|
||||
this.localuse.Text = "Use/Keep Present Local Files";
|
||||
this.localuse.Text = "Local Files";
|
||||
this.localuse.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// NUSDownloader
|
||||
@ -249,7 +252,7 @@
|
||||
this.wadnamebox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.wadnamebox.Enabled = false;
|
||||
this.wadnamebox.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.wadnamebox.Location = new System.Drawing.Point(102, 421);
|
||||
this.wadnamebox.Location = new System.Drawing.Point(102, 416);
|
||||
this.wadnamebox.MaxLength = 99999;
|
||||
this.wadnamebox.Name = "wadnamebox";
|
||||
this.wadnamebox.Size = new System.Drawing.Size(160, 18);
|
||||
@ -258,21 +261,21 @@
|
||||
// ignoreticket
|
||||
//
|
||||
this.ignoreticket.AutoSize = true;
|
||||
this.ignoreticket.Location = new System.Drawing.Point(12, 445);
|
||||
this.ignoreticket.Location = new System.Drawing.Point(104, 440);
|
||||
this.ignoreticket.Name = "ignoreticket";
|
||||
this.ignoreticket.Size = new System.Drawing.Size(135, 17);
|
||||
this.ignoreticket.Size = new System.Drawing.Size(89, 17);
|
||||
this.ignoreticket.TabIndex = 18;
|
||||
this.ignoreticket.Text = "Ignore Ticket if Missing";
|
||||
this.ignoreticket.Text = "Ignore Ticket";
|
||||
this.ignoreticket.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// decryptbox
|
||||
//
|
||||
this.decryptbox.AutoSize = true;
|
||||
this.decryptbox.Location = new System.Drawing.Point(154, 445);
|
||||
this.decryptbox.Location = new System.Drawing.Point(199, 440);
|
||||
this.decryptbox.Name = "decryptbox";
|
||||
this.decryptbox.Size = new System.Drawing.Size(108, 17);
|
||||
this.decryptbox.Size = new System.Drawing.Size(63, 17);
|
||||
this.decryptbox.TabIndex = 19;
|
||||
this.decryptbox.Text = "Decrypt Contents";
|
||||
this.decryptbox.Text = "Decrypt";
|
||||
this.decryptbox.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// databaseButton
|
||||
@ -431,7 +434,7 @@
|
||||
// truchabox
|
||||
//
|
||||
this.truchabox.AutoSize = true;
|
||||
this.truchabox.Location = new System.Drawing.Point(184, 468);
|
||||
this.truchabox.Location = new System.Drawing.Point(199, 463);
|
||||
this.truchabox.Name = "truchabox";
|
||||
this.truchabox.Size = new System.Drawing.Size(60, 17);
|
||||
this.truchabox.TabIndex = 21;
|
||||
@ -870,7 +873,7 @@
|
||||
this.radioButton2.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
|
||||
this.radioButton2.Location = new System.Drawing.Point(78, 385);
|
||||
this.radioButton2.Name = "radioButton2";
|
||||
this.radioButton2.Size = new System.Drawing.Size(74, 31);
|
||||
this.radioButton2.Size = new System.Drawing.Size(74, 30);
|
||||
this.radioButton2.TabIndex = 13;
|
||||
this.radioButton2.UseVisualStyleBackColor = true;
|
||||
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
|
||||
@ -944,40 +947,64 @@
|
||||
this.koreaToolStripMenuItem.Size = new System.Drawing.Size(154, 22);
|
||||
this.koreaToolStripMenuItem.Text = "Korea";
|
||||
//
|
||||
// saveaswadbox
|
||||
//
|
||||
this.saveaswadbox.AutoSize = true;
|
||||
this.saveaswadbox.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.saveaswadbox.Location = new System.Drawing.Point(22, 437);
|
||||
this.saveaswadbox.Name = "saveaswadbox";
|
||||
this.saveaswadbox.Size = new System.Drawing.Size(58, 16);
|
||||
this.saveaswadbox.TabIndex = 43;
|
||||
this.saveaswadbox.Text = "SaveAs";
|
||||
this.saveaswadbox.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// deletecontentsbox
|
||||
//
|
||||
this.deletecontentsbox.AutoSize = true;
|
||||
this.deletecontentsbox.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.deletecontentsbox.Location = new System.Drawing.Point(22, 454);
|
||||
this.deletecontentsbox.Name = "deletecontentsbox";
|
||||
this.deletecontentsbox.Size = new System.Drawing.Size(62, 28);
|
||||
this.deletecontentsbox.TabIndex = 44;
|
||||
this.deletecontentsbox.Text = "Delete\r\nContents";
|
||||
this.deletecontentsbox.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(539, 492);
|
||||
this.Controls.Add(this.deletecontentsbox);
|
||||
this.Controls.Add(this.button3);
|
||||
this.Controls.Add(this.shamelessvariablelabel);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Controls.Add(this.button17);
|
||||
this.Controls.Add(this.ticketgpbox);
|
||||
this.Controls.Add(this.tmdgpbox);
|
||||
this.Controls.Add(this.truchabox);
|
||||
this.Controls.Add(this.saveaswadbox);
|
||||
this.Controls.Add(this.databaseButton);
|
||||
this.Controls.Add(this.button5);
|
||||
this.Controls.Add(this.contentModBox);
|
||||
this.Controls.Add(this.button4);
|
||||
this.Controls.Add(this.decryptbox);
|
||||
this.Controls.Add(this.ignoreticket);
|
||||
this.Controls.Add(this.wadnamebox);
|
||||
this.Controls.Add(this.button7);
|
||||
this.Controls.Add(this.truchabox);
|
||||
this.Controls.Add(this.label2);
|
||||
this.Controls.Add(this.titleversion);
|
||||
this.Controls.Add(this.button2);
|
||||
this.Controls.Add(this.decryptbox);
|
||||
this.Controls.Add(this.label1);
|
||||
this.Controls.Add(this.radioButton1);
|
||||
this.Controls.Add(this.radioButton2);
|
||||
this.Controls.Add(this.localuse);
|
||||
this.Controls.Add(this.dlprogress);
|
||||
this.Controls.Add(this.packbox);
|
||||
this.Controls.Add(this.statusbox);
|
||||
this.Controls.Add(this.ignoreticket);
|
||||
this.Controls.Add(this.downloadstartbtn);
|
||||
this.Controls.Add(this.titleidbox);
|
||||
this.Controls.Add(this.Extrasbtn);
|
||||
this.Controls.Add(this.button6);
|
||||
this.Controls.Add(this.localuse);
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
|
||||
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
|
||||
this.MaximizeBox = false;
|
||||
@ -1086,6 +1113,8 @@
|
||||
private System.Windows.Forms.ToolStripMenuItem europePALToolStripMenuItem;
|
||||
private System.Windows.Forms.ToolStripMenuItem japanNTSCJToolStripMenuItem;
|
||||
private System.Windows.Forms.ToolStripMenuItem koreaToolStripMenuItem;
|
||||
private System.Windows.Forms.CheckBox saveaswadbox;
|
||||
private System.Windows.Forms.CheckBox deletecontentsbox;
|
||||
}
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -95,6 +95,7 @@
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<Compile Include="WADPacker.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Resources\wilolgoi.png" />
|
||||
|
202
NUS Downloader/WADPacker.cs
Normal file
202
NUS Downloader/WADPacker.cs
Normal file
@ -0,0 +1,202 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace NUS_Downloader
|
||||
{
|
||||
/// <summary>
|
||||
/// Class for handling WAD Packaging.
|
||||
/// </summary>
|
||||
class WADPacker
|
||||
{
|
||||
// WAD Component Variables
|
||||
private byte[] Certsys;
|
||||
public byte[] Certs { get { return Certsys;} set { Certsys = value; CertChainSize = Certsys.Length; } }
|
||||
private byte[] tmd;
|
||||
public byte[] TMD { get { return tmd; }
|
||||
set {
|
||||
tmd = value;
|
||||
TMDContentCount = ContentCount(TMD);
|
||||
TMDSize = 484 + (TMDContentCount * 36);
|
||||
} }
|
||||
public byte[] Ticket; //{ get { return Ticket; } set { Ticket = value; } }
|
||||
private int TMDContentCount; //{ get { return TMDContentCount; } set { TMDContentCount = value; } }
|
||||
|
||||
// WAD Contents
|
||||
private byte[][] TMDContents;
|
||||
public byte[][] Contents { get { return TMDContents; }
|
||||
set {
|
||||
TMDContents = value;
|
||||
for (int a = 0; a < TMDContents.Length; a++)
|
||||
{
|
||||
DataSize += TMDContents[a].Length;
|
||||
}
|
||||
} }
|
||||
|
||||
// WAD Saving Variables
|
||||
public string Directory; //{ get { return Directory; } set { Directory = value; } }
|
||||
public string FileName; //{ get { return FileName; } set { FileName = value; } }
|
||||
|
||||
// TMD Informations
|
||||
public string[] tmdnames; //{ get { return tmdnames; } set { tmdnames = value; } }
|
||||
public string[] tmdsizes; //{ get { return tmdsizes; } set { tmdsizes = value; } }
|
||||
|
||||
// WAD Header Variables
|
||||
private const int HeaderSize = 0x20;
|
||||
private int CertChainSize; //{ get { return CertChainSize; } set { CertChainSize = value; } }
|
||||
private const int TicketSize = 0x2A4;
|
||||
private int TMDSize; //{ get { return TMDSize; } set { TMDSize = value; } }
|
||||
private int DataSize; //{ get { return DataSize; } set { DataSize = value; } }
|
||||
private byte[] WADMagic = new byte[8] { 0x00, 0x00, 0x00, 0x20, 0x49, 0x73, 0x00, 0x00 };
|
||||
private byte[] RESERVED_CONST = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
|
||||
private byte[] TIKSIZE_CONST = new byte[4] { 0x00, 0x00, 0x02, 0xA4 };
|
||||
|
||||
// Report Status back in EventHandler
|
||||
public delegate void StatusChangedEventHandler(string status);
|
||||
public event StatusChangedEventHandler StatusChanged;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Pads byte[].
|
||||
/// </summary>
|
||||
/// <param name="src">The byte[] or binary to be padded.</param>
|
||||
/// <param name="pad">How much to pad by.</param>
|
||||
/// <returns>Padded byte[]</returns>
|
||||
private long PadToMultipleOf(long src, int pad)
|
||||
{
|
||||
long len = (src + pad - 1) / pad * pad;
|
||||
return len;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an integer into its equivilant byte array.
|
||||
/// </summary>
|
||||
/// <param name="theInt">The integer</param>
|
||||
/// <param name="arrayLen">Length you desire the byte[] to be.</param>
|
||||
/// <returns></returns>
|
||||
private byte[] ConvertInttoByteArray(int theInt, int arrayLen)
|
||||
{
|
||||
byte[] resultArray = new byte[arrayLen];
|
||||
for (int i = arrayLen - 1; i >= 0; i--)
|
||||
{
|
||||
resultArray[i] = (byte)((theInt >> (8 * i)) & 0xFF);
|
||||
}
|
||||
Array.Reverse(resultArray);
|
||||
|
||||
// Fix duplication, rewrite extra to 0x00;
|
||||
if (arrayLen > 4)
|
||||
{
|
||||
for (int i = 0; i < (arrayLen - 4); i++)
|
||||
resultArray[i] = 0x00;
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the size mismatch.
|
||||
/// </summary>
|
||||
/// <param name="contentsize">The contentsize.</param>
|
||||
/// <param name="actualsize">The actualsize.</param>
|
||||
void HandleMismatch(int contentsize, int actualsize)
|
||||
{
|
||||
if (contentsize != actualsize)
|
||||
if ((contentsize - actualsize) > 16)
|
||||
StatusChanged(String.Format(" (BAD Mismatch) (Dif: {0}", (contentsize - actualsize)));
|
||||
//else
|
||||
//statusbox.Text += " (Safe Mismatch)";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns content count of TMD
|
||||
/// </summary>
|
||||
/// <param name="tmd">The TMD.</param>
|
||||
/// <returns>int Count of Contents</returns>
|
||||
private int ContentCount(byte[] tmd)
|
||||
{
|
||||
return (tmd[0x1DE] * 256) + tmd[0x1DF];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Packs the WAD file, saves it to specified location.
|
||||
/// </summary>
|
||||
public void PackWAD()
|
||||
{
|
||||
//StatusChanged("Beginning WAD Pack...");
|
||||
|
||||
if ((String.IsNullOrEmpty(Directory)) || (String.IsNullOrEmpty(FileName)))
|
||||
{
|
||||
StatusChanged("ERROR: No Directory/FileName provided!");
|
||||
return;
|
||||
}
|
||||
|
||||
FileStream wadfs = new FileStream((Directory + FileName), FileMode.Create);
|
||||
|
||||
// Seek the beginning of the WAD...
|
||||
wadfs.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Write initial part of header (WADType)
|
||||
wadfs.Write(WADMagic, 0, WADMagic.Length);
|
||||
|
||||
// Write CertChainLength
|
||||
wadfs.Seek(0x08, SeekOrigin.Begin);
|
||||
byte[] chainsize = ConvertInttoByteArray(CertChainSize, 4);
|
||||
wadfs.Write(chainsize, 0, chainsize.Length);
|
||||
|
||||
// Write res
|
||||
wadfs.Seek(0x0C, SeekOrigin.Begin);
|
||||
wadfs.Write(RESERVED_CONST, 0, RESERVED_CONST.Length);
|
||||
|
||||
// Write ticketsize
|
||||
wadfs.Seek(0x10, SeekOrigin.Begin);
|
||||
wadfs.Write(TIKSIZE_CONST, 0, TIKSIZE_CONST.Length);
|
||||
|
||||
// Write tmdsize
|
||||
wadfs.Seek(0x14, SeekOrigin.Begin);
|
||||
byte[] tmdsize = ConvertInttoByteArray(TMDSize, 4);
|
||||
wadfs.Write(tmdsize, 0, tmdsize.Length);
|
||||
|
||||
// Write data size
|
||||
wadfs.Seek(0x18, SeekOrigin.Begin);
|
||||
wadfs.Write(ConvertInttoByteArray(DataSize, 4), 0, 4);
|
||||
StatusChanged(" - WAD Header wrote (0x00)");
|
||||
|
||||
// Write cert[] to 0x40.
|
||||
wadfs.Seek(0x40, SeekOrigin.Begin);
|
||||
wadfs.Write(Certs, 0, Certs.Length);
|
||||
StatusChanged(String.Format(" - Certs wrote (0x{0})", Convert.ToString(64, 16)));
|
||||
|
||||
// Pad to next 64 byte boundary.
|
||||
wadfs.Seek(2624, SeekOrigin.Begin);
|
||||
|
||||
// Write ticket at this point...
|
||||
wadfs.Write(Ticket, 0, TicketSize);
|
||||
StatusChanged(String.Format(" - Ticket wrote (0x{0})", Convert.ToString((wadfs.Length - 0x2A4), 16)));
|
||||
|
||||
// Pad to next 64 byte boundary.
|
||||
wadfs.Seek(PadToMultipleOf(wadfs.Length, 64), SeekOrigin.Begin);
|
||||
|
||||
// Write TMD at this point...
|
||||
wadfs.Write(TMD, 0, TMDSize);
|
||||
StatusChanged(String.Format(" - TMD wrote (0x{0})", Convert.ToString((wadfs.Length - TMDSize), 16)));
|
||||
|
||||
// Add the individual contents
|
||||
for (int a = 0; a < TMDContentCount; a++)
|
||||
{
|
||||
// Pad to next 64 byte boundary...
|
||||
wadfs.Seek(PadToMultipleOf(wadfs.Length, 64), SeekOrigin.Begin);
|
||||
|
||||
wadfs.Write(Contents[a], 0, Contents[a].Length);
|
||||
|
||||
StatusChanged(String.Format(" - {0} wrote (0x{1})", tmdnames[a], Convert.ToString((wadfs.Length - Contents[a].Length), 16)));
|
||||
HandleMismatch(int.Parse(tmdsizes[a], System.Globalization.NumberStyles.HexNumber), Contents[a].Length);
|
||||
}
|
||||
|
||||
// Close filesystem...
|
||||
wadfs.Close();
|
||||
|
||||
// Finished.
|
||||
StatusChanged("WAD Created: " + FileName);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user