Scripts mostly working all around (naming todo, etc)

This commit is contained in:
givememystuffplease 2010-08-11 01:28:39 +00:00
parent 534b2a502c
commit 1f14032ca7
3 changed files with 182 additions and 105 deletions

View File

@ -64,9 +64,9 @@ namespace NUS_Downloader
private string WAD_Saveas_Filename; private string WAD_Saveas_Filename;
// TODO: OOP scripting // TODO: OOP scripting
private string script_filename; /*private string script_filename;
private bool script_mode = false; private bool script_mode = false;
private string[] nusentries; private string[] nusentries;*/
// Proxy stuff... // Proxy stuff...
private string proxy_url; private string proxy_url;
@ -124,10 +124,10 @@ namespace NUS_Downloader
if ((args.Length == 1) && (File.Exists(args[0]))) if ((args.Length == 1) && (File.Exists(args[0])))
{ {
script_filename = args[0]; string script_content = File.ReadAllText(args[0]);
BackgroundWorker scripter = new BackgroundWorker(); BackgroundWorker scripter = new BackgroundWorker();
scripter.DoWork += new DoWorkEventHandler(RunScript); scripter.DoWork += new DoWorkEventHandler(RunScriptBg);
scripter.RunWorkerAsync(); scripter.RunWorkerAsync(script_content);
} }
} }
@ -484,7 +484,7 @@ namespace NUS_Downloader
WriteStatus("Running with your current settings will produce no output!", errorcolor); WriteStatus("Running with your current settings will produce no output!", errorcolor);
WriteStatus(" - To amend this, look below and check an output type.", errorcolor); WriteStatus(" - To amend this, look below and check an output type.", errorcolor);
return; return;
} }/*
else if (!(script_mode)) else if (!(script_mode))
{ {
try try
@ -496,7 +496,7 @@ namespace NUS_Downloader
{ {
SetTextThreadSafe(statusbox, " --- " + titleidbox.Text + " ---"); SetTextThreadSafe(statusbox, " --- " + titleidbox.Text + " ---");
} }
} }*/
else else
WriteStatus(" --- " + titleidbox.Text + " ---"); WriteStatus(" --- " + titleidbox.Text + " ---");
@ -532,8 +532,8 @@ namespace NUS_Downloader
private void NUSDownloader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) private void NUSDownloader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{ {
Control.CheckForIllegalCrossThreadCalls = false; // this function would need major rewriting to get rid of this... Control.CheckForIllegalCrossThreadCalls = false; // this function would need major rewriting to get rid of this...
if (!(script_mode))
WriteStatus("Starting NUS Download. Please be patient!", infocolor); WriteStatus("Starting NUS Download. Please be patient!", infocolor);
SetEnableforDownload(false); SetEnableforDownload(false);
downloadstartbtn.Text = "Starting NUS Download!"; downloadstartbtn.Text = "Starting NUS Download!";
@ -575,7 +575,7 @@ namespace NUS_Downloader
} }
catch (Exception ex) catch (Exception ex)
{ {
WriteStatus("Uhoh, the download bombed: \"" + ex.Message + " ):\"", errorcolor); WriteStatus("Download failed: \"" + ex.Message + " ):\"", errorcolor);
} }
if (iosPatchCheckbox.Checked == true) { // Apply patches then... if (iosPatchCheckbox.Checked == true) { // Apply patches then...
@ -697,9 +697,6 @@ namespace NUS_Downloader
if (IsWin7()) if (IsWin7())
dlprogress.ShowInTaskbar = false; dlprogress.ShowInTaskbar = false;
if (script_mode)
statusbox.Text = "";
} }
private void consoleCBox_SelectedIndexChanged(object sender, EventArgs e) private void consoleCBox_SelectedIndexChanged(object sender, EventArgs e)
@ -1221,13 +1218,14 @@ namespace NUS_Downloader
return titlename; return titlename;
} }
/*
private void upditem_itemclicked(object sender, ToolStripItemClickedEventArgs e) private void upditem_itemclicked(object sender, ToolStripItemClickedEventArgs e)
{ {
WriteStatus("Preparing to run download script..."); WriteStatus("Preparing to run download script...");
script_mode = true; //script_mode = true;
SetTextThreadSafe(statusbox, ""); SetTextThreadSafe(statusbox, "");
WriteStatus("Starting script download. Please be patient!"); //WriteStatus("Starting script download. Please be patient!");
string[] NUS_Entries = e.ClickedItem.AccessibleDescription.Split('\n'); string[] NUS_Entries = e.ClickedItem.AccessibleDescription.Split('\n');
// TODO: Find somewhere better to put this. AND FAST! // TODO: Find somewhere better to put this. AND FAST!
for (int i = 0; i < NUS_Entries.Length; i++) for (int i = 0; i < NUS_Entries.Length; i++)
@ -1239,7 +1237,7 @@ namespace NUS_Downloader
BackgroundWorker scripter = new BackgroundWorker(); BackgroundWorker scripter = new BackgroundWorker();
scripter.DoWork += new DoWorkEventHandler(RunScript); scripter.DoWork += new DoWorkEventHandler(RunScript);
scripter.RunWorkerAsync(); scripter.RunWorkerAsync();
} }*/
public void DatabaseItem_Clicked(object sender, ToolStripItemClickedEventArgs e) public void DatabaseItem_Clicked(object sender, ToolStripItemClickedEventArgs e)
{ {
@ -1517,7 +1515,7 @@ namespace NUS_Downloader
string title_name = null; string title_name = null;
if ((titleidbox.Enabled == true || script_mode == true) && (packbox.Checked == true)) if ((titleidbox.Enabled == true) && (packbox.Checked == true))
{ {
if (titleversion.Text != "") if (titleversion.Text != "")
{ {
@ -2072,94 +2070,96 @@ namespace NUS_Downloader
if (ofd.ShowDialog() != DialogResult.Cancel) if (ofd.ShowDialog() != DialogResult.Cancel)
{ {
script_filename = ofd.FileName; string script_content = File.ReadAllText(ofd.FileName);
BackgroundWorker scripter = new BackgroundWorker(); BackgroundWorker scripter = new BackgroundWorker();
scripter.DoWork += new DoWorkEventHandler(RunScript); scripter.DoWork += new DoWorkEventHandler(RunScriptBg);
scripter.RunWorkerAsync(); scripter.RunWorkerAsync(script_content);
} }
} }
/// <summary>
/// Runs a NUS script (BG).
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
private void RunScriptBg(object sender, System.ComponentModel.DoWorkEventArgs e)
{
RunScript(e.Argument.ToString());
/*
script_mode = true;
SetTextThreadSafe(statusbox, "");
WriteStatus("Starting script download. Please be patient!");
if (!File.Exists(Path.Combine(CURRENT_DIR, "output_" + Path.GetFileNameWithoutExtension(script_filename))))
Directory.CreateDirectory(Path.Combine(CURRENT_DIR, "output_" + Path.GetFileNameWithoutExtension(script_filename)));
string[] NUS_Entries;
if (script_filename != "\000")
{
NUS_Entries = File.ReadAllLines(script_filename);
}
else
{
NUS_Entries = nusentries;
}
WriteStatus(String.Format(" - Script loaded ({0} Titles)", NUS_Entries.Length));
/// <summary> for (int a = 0; a < NUS_Entries.Length; a++)
/// Runs a NUS script (BG). {
/// </summary> // Download the title
/// <param name="sender">The sender.</param> WriteStatus(String.Format("===== Running Download ({0}/{1}) =====", a + 1, NUS_Entries.Length));
/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param> string[] title_info = NUS_Entries[a].Split(' ');
private void RunScript(object sender, System.ComponentModel.DoWorkEventArgs e) // don't let the delete issue reappear...
{ if (string.IsNullOrEmpty(title_info[0]))
script_mode = true; break;
SetTextThreadSafe(statusbox, "");
WriteStatus("Starting script download. Please be patient!");
if (!File.Exists(Path.Combine(CURRENT_DIR, "output_" + Path.GetFileNameWithoutExtension(script_filename))))
Directory.CreateDirectory(Path.Combine(CURRENT_DIR, "output_" + Path.GetFileNameWithoutExtension(script_filename)));
string[] NUS_Entries;
if (script_filename != "\000")
{
NUS_Entries = File.ReadAllLines(script_filename);
}
else
{
NUS_Entries = nusentries;
}
WriteStatus(String.Format(" - Script loaded ({0} Titles)", NUS_Entries.Length));
for (int a = 0; a < NUS_Entries.Length; a++) // WebClient configuration
{ WebClient nusWC = new WebClient();
// Download the title nusWC = ConfigureWithProxy(nusWC);
WriteStatus(String.Format("===== Running Download ({0}/{1}) =====", a + 1, NUS_Entries.Length)); nusWC.Headers.Add("User-Agent", "wii libnup/1.0"); // Set UserAgent to Wii value
string[] title_info = NUS_Entries[a].Split(' ');
// don't let the delete issue reappear...
if (string.IsNullOrEmpty(title_info[0]))
break;
// WebClient configuration // Create\Configure NusClient
WebClient nusWC = new WebClient(); libWiiSharp.NusClient nusClient = new libWiiSharp.NusClient();
nusWC = ConfigureWithProxy(nusWC); nusClient.ConfigureNusClient(nusWC);
nusWC.Headers.Add("User-Agent", "wii libnup/1.0"); // Set UserAgent to Wii value nusClient.UseLocalFiles = localuse.Checked;
nusClient.ContinueWithoutTicket = true;
nusClient.Debug += new EventHandler<libWiiSharp.MessageEventArgs>(nusClient_Debug);
// Create\Configure NusClient libWiiSharp.StoreType[] storeTypes = new libWiiSharp.StoreType[1];
libWiiSharp.NusClient nusClient = new libWiiSharp.NusClient(); // There's no harm in outputting everything i suppose
nusClient.ConfigureNusClient(nusWC); storeTypes[0] = libWiiSharp.StoreType.All;
nusClient.UseLocalFiles = localuse.Checked;
nusClient.ContinueWithoutTicket = true;
nusClient.Debug += new EventHandler<libWiiSharp.MessageEventArgs>(nusClient_Debug);
libWiiSharp.StoreType[] storeTypes = new libWiiSharp.StoreType[1]; int title_version = int.Parse(title_info[1], System.Globalization.NumberStyles.HexNumber);
// There's no harm in outputting everything i suppose
storeTypes[0] = libWiiSharp.StoreType.All;
int title_version = int.Parse(title_info[1], System.Globalization.NumberStyles.HexNumber); string wadName = NameFromDatabase(title_info[0]);
if (wadName != null)
wadName = OfficialWADNaming(wadName);
else
wadName = title_info[0] + "-NUS-v" + title_version + ".wad";
string wadName = NameFromDatabase(title_info[0]); nusClient.DownloadTitle(title_info[0], title_version.ToString(), Path.Combine(CURRENT_DIR, ("output_" + Path.GetFileNameWithoutExtension(script_filename))), wadName, storeTypes);
if (wadName != null)
wadName = OfficialWADNaming(wadName);
else
wadName = title_info[0] + "-NUS-v" + title_version + ".wad";
nusClient.DownloadTitle(title_info[0], title_version.ToString(), Path.Combine(CURRENT_DIR, ("output_" + Path.GetFileNameWithoutExtension(script_filename))), wadName, storeTypes); /*
SetTextThreadSafe(titleidbox, title_info[0]);
SetTextThreadSafe(titleversion,
Convert.ToString(256*
(byte.Parse(title_info[1].Substring(0, 2),
System.Globalization.NumberStyles.HexNumber))));
SetTextThreadSafe(titleversion,
Convert.ToString(Convert.ToInt32(titleversion.Text) +
byte.Parse(title_info[1].Substring(2, 2),
System.Globalization.NumberStyles.HexNumber)));
/* button3_Click("Scripter", EventArgs.Empty);
SetTextThreadSafe(titleidbox, title_info[0]);
SetTextThreadSafe(titleversion,
Convert.ToString(256*
(byte.Parse(title_info[1].Substring(0, 2),
System.Globalization.NumberStyles.HexNumber))));
SetTextThreadSafe(titleversion,
Convert.ToString(Convert.ToInt32(titleversion.Text) +
byte.Parse(title_info[1].Substring(2, 2),
System.Globalization.NumberStyles.HexNumber)));
button3_Click("Scripter", EventArgs.Empty); Thread.Sleep(1000);
Thread.Sleep(1000); while (NUSDownloader.IsBusy)
{
while (NUSDownloader.IsBusy) Thread.Sleep(1000);
{ }
Thread.Sleep(1000); }
} */ script_mode = false;
} WriteStatus("Script completed!");*/
script_mode = false; }
WriteStatus("Script completed!");
}
private void scriptsbutton_Click(object sender, EventArgs e) private void scriptsbutton_Click(object sender, EventArgs e)
{ {
@ -2360,10 +2360,10 @@ namespace NUS_Downloader
folderpath = Path.Combine(tsmi.OwnerItem.Text, folderpath); folderpath = Path.Combine(tsmi.OwnerItem.Text, folderpath);
} }
folderpath = Path.Combine(this.CURRENT_DIR, Path.Combine("scripts", Path.Combine(folderpath, tsmi.Text))); folderpath = Path.Combine(this.CURRENT_DIR, Path.Combine("scripts", Path.Combine(folderpath, tsmi.Text)));
script_filename = folderpath; string script_content = File.ReadAllText(folderpath);
BackgroundWorker scripter = new BackgroundWorker(); BackgroundWorker scripter = new BackgroundWorker();
scripter.DoWork += new DoWorkEventHandler(RunScript); scripter.DoWork += new DoWorkEventHandler(RunScriptBg);
scripter.RunWorkerAsync(); scripter.RunWorkerAsync(script_content);
} }
private void saveaswadbtn_Click(object sender, EventArgs e) private void saveaswadbtn_Click(object sender, EventArgs e)
@ -2416,7 +2416,7 @@ namespace NUS_Downloader
ToolStripMenuItem[] scriptItems = databaseObj.LoadScripts(); ToolStripMenuItem[] scriptItems = databaseObj.LoadScripts();
for (int a = 0; a < scriptItems.Length; a++) for (int a = 0; a < scriptItems.Length; a++)
{ {
scriptItems[a].DropDownItemClicked += new ToolStripItemClickedEventHandler(ScriptItem_Clicked); scriptItems[a].Click += new EventHandler(ScriptItem_Clicked);
AddToolStripItemToStrip(scriptsDatabaseToolStripMenuItem, scriptItems[a]); AddToolStripItemToStrip(scriptsDatabaseToolStripMenuItem, scriptItems[a]);
//SystemMenuList.DropDownItems.Add(systemItems[a]); //SystemMenuList.DropDownItems.Add(systemItems[a]);
@ -2426,14 +2426,16 @@ namespace NUS_Downloader
SetPropertyThreadSafe(scriptsDatabaseToolStripMenuItem, true, "Visible"); SetPropertyThreadSafe(scriptsDatabaseToolStripMenuItem, true, "Visible");
} }
public void ScriptItem_Clicked(object sender, ToolStripItemClickedEventArgs e) public void ScriptItem_Clicked(object sender, EventArgs e)
{// STILL TODO {
// Scripts from database are stored in tooltips...
ToolStripMenuItem tsmi = (ToolStripMenuItem)sender; ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
string folderpath = "\000"; string script_content = tsmi.ToolTipText;
script_filename = folderpath; // Debug.WriteLine(script_content);
BackgroundWorker scripter = new BackgroundWorker(); BackgroundWorker scripter = new BackgroundWorker();
scripter.DoWork += new DoWorkEventHandler(RunScript); scripter.DoWork += new DoWorkEventHandler(RunScriptBg);
scripter.RunWorkerAsync(); scripter.RunWorkerAsync(script_content);
} }
void ReorganizePreviousFolderStructure(object sender, DoWorkEventArgs e) void ReorganizePreviousFolderStructure(object sender, DoWorkEventArgs e)
@ -2528,5 +2530,77 @@ namespace NUS_Downloader
{ {
WriteStatus(" - Operation complete!"); WriteStatus(" - Operation complete!");
} }
private void RunScript(string scriptstr)
{
// Form and folder stuffs
SetTextThreadSafe(statusbox, "");
WriteStatus("Starting script download. Please be patient!");
string scriptdir = Path.Combine(Path.Combine(CURRENT_DIR, "scripts"), RandomString(7) + "_output"); //TODO: Nonrandom naming
if (!File.Exists(scriptdir))
Directory.CreateDirectory(scriptdir);
Debug.WriteLine(" - Output: " + scriptdir.Replace(CURRENT_DIR, ""));
// Parse entries
string[] NUS_Entries = scriptstr.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
WriteStatus(String.Format(" - Script loaded ({0} Titles)", NUS_Entries.Length));
for (int a = 0; a < NUS_Entries.Length; a++)
{
// Download the title
WriteStatus(String.Format("===== Running Download ({0}/{1}) =====", a + 1, NUS_Entries.Length));
string[] title_info = NUS_Entries[a].Split(' ');
// don't let the delete issue reappear...
if (string.IsNullOrEmpty(title_info[0]))
continue;
// WebClient configuration
WebClient nusWC = new WebClient();
nusWC = ConfigureWithProxy(nusWC);
nusWC.Headers.Add("User-Agent", "wii libnup/1.0"); // Set UserAgent to Wii value
// Create\Configure NusClient
libWiiSharp.NusClient nusClient = new libWiiSharp.NusClient();
nusClient.ConfigureNusClient(nusWC);
nusClient.UseLocalFiles = localuse.Checked;
nusClient.ContinueWithoutTicket = true;
nusClient.Debug += new EventHandler<libWiiSharp.MessageEventArgs>(nusClient_Debug);
libWiiSharp.StoreType[] storeTypes = new libWiiSharp.StoreType[1];
// There's no harm in outputting everything i suppose
storeTypes[0] = libWiiSharp.StoreType.All;
int title_version = int.Parse(title_info[1], System.Globalization.NumberStyles.HexNumber);
string wadName = NameFromDatabase(title_info[0]);
if (wadName != null)
wadName = OfficialWADNaming(wadName);
else
wadName = title_info[0] + "-NUS-v" + title_version + ".wad";
nusClient.DownloadTitle(title_info[0], title_version.ToString(), scriptdir, wadName, storeTypes);
}
WriteStatus("Script completed!");
}
// Random string function for temp foldernames in RunScript.
// Probably going to be removed TODO
private readonly Random _rng = new Random();
private const string _chars = "abcdefghijklmnopqrstuvwxyz";
private string RandomString(int size)
{
char[] buffer = new char[size];
for (int i = 0; i < size; i++)
{
buffer[i] = _chars[_rng.Next(_chars.Length)];
}
return new string(buffer);
}
} }
} }

View File

@ -360,8 +360,11 @@ namespace libWiiSharp
throw new Exception("Downloading Ticket Failed:\n" + ex.Message); throw new Exception("Downloading Ticket Failed:\n" + ex.Message);
} }
storeDecrypted = false; if (!(File.Exists(Path.Combine(outputDir, "cetk"))))
storeWad = false; {
storeDecrypted = false;
storeWad = false;
}
} }
fireProgress(10); fireProgress(10);
@ -409,7 +412,7 @@ namespace libWiiSharp
//Check SHA1 //Check SHA1
byte[] newSha = s.ComputeHash(decryptedContent); byte[] newSha = s.ComputeHash(decryptedContent);
if (!Shared.CompareByteArrays(newSha, tmd.Contents[i].Hash)) if (!Shared.CompareByteArrays(newSha, tmd.Contents[i].Hash))
{ fireDebug(@"/!\ /!\ /!\ Hashes do not match /!\ /!\ /!\"); throw new Exception(string.Format("Content #{0}: Hashes do not match!", i)); } { fireDebug(@"/!\ /!\ Hashes do not match /!\ /!\"); throw new Exception(string.Format("Content #{0}: Hashes do not match!", i)); }
//Write Decrypted Content //Write Decrypted Content
File.WriteAllBytes(Path.Combine(outputDir, (tmd.Contents[i].ContentID.ToString("x8") + ".app")), decryptedContent); File.WriteAllBytes(Path.Combine(outputDir, (tmd.Contents[i].ContentID.ToString("x8") + ".app")), decryptedContent);