Made new SetTextThreadSafe function

Script mode (file version) will now load correctly, even subdirectories
Script mode now:
- auto-enables WAD packing
- changes output directory to new directory (output_<name of script>)
- auto-deletes leftovers
This commit is contained in:
gb.luke 2010-07-05 02:00:18 +00:00
parent 8d98f6db4d
commit e69139247e
2 changed files with 89 additions and 20 deletions

View File

@ -32,6 +32,8 @@ namespace NUS_Downloader
int type, ToolStripMenuItem additionitem, XmlAttributeCollection attributes); int type, ToolStripMenuItem additionitem, XmlAttributeCollection attributes);
private delegate void WriteStatusCallback(string Update); private delegate void WriteStatusCallback(string Update);
private delegate void BootChecksCallback(); private delegate void BootChecksCallback();
private delegate void SetEnableForDownloadCallback(bool enabled);
private delegate void SetTextThreadSafeCallback(System.Windows.Forms.Control what, string setto);
// Images do not compare unless globalized... // Images do not compare unless globalized...
private Image green = Properties.Resources.bullet_green; private Image green = Properties.Resources.bullet_green;
@ -799,15 +801,15 @@ namespace NUS_Downloader
try try
{ {
if (!statusbox.Lines[0].StartsWith(" ---")) if (!statusbox.Lines[0].StartsWith(" ---"))
statusbox.Text = " --- " + titleidbox.Text + " ---"; SetTextThreadSafe(statusbox, " --- " + titleidbox.Text + " ---");
} }
catch // No lines present... catch // No lines present...
{ {
statusbox.Text = " --- " + titleidbox.Text + " ---"; SetTextThreadSafe(statusbox, " --- " + titleidbox.Text + " ---");
} }
} }
else else
statusbox.Text += "\r\n --- " + titleidbox.Text + " ---"; SetTextThreadSafe(statusbox, statusbox.Text + "\r\n --- " + titleidbox.Text + " ---");
// Handle SaveAs here so it shows up properly... // Handle SaveAs here so it shows up properly...
if (!(String.IsNullOrEmpty(WAD_Saveas_Filename))) if (!(String.IsNullOrEmpty(WAD_Saveas_Filename)))
@ -827,24 +829,36 @@ namespace NUS_Downloader
NUSDownloader.RunWorkerAsync(); NUSDownloader.RunWorkerAsync();
} }
private void SetTextThreadSafe(System.Windows.Forms.Control what, string setto)
{
if (this.InvokeRequired)
{
SetTextThreadSafeCallback sttscb = new SetTextThreadSafeCallback(SetTextThreadSafe);
this.Invoke(sttscb, new object[] { what, setto });
return;
}
what.Text = setto;
}
private void NUSDownloader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) private void NUSDownloader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{ {
// Preparations for Downloading // Preparations for Downloading
Control.CheckForIllegalCrossThreadCalls = false; Control.CheckForIllegalCrossThreadCalls = false; // this function would need major rewriting to get rid of this...
if (!(script_mode)) if (!(script_mode))
WriteStatus("Starting NUS Download. Please be patient!"); WriteStatus("Starting NUS Download. Please be patient!");
SetEnableforDownload(false); SetEnableforDownload(false);
downloadstartbtn.Text = "Starting NUS Download!"; downloadstartbtn.Text = "Starting NUS Download!";
// Prevent crossthread issues
string titleid = titleidbox.Text;
// Creates the directory // Creates the directory
CreateTitleDirectory(); if (!script_mode)
CreateTitleDirectory();
// Wii / DSi // Wii / DSi
bool wiimode = (consoleCBox.SelectedIndex == 0); bool wiimode = (consoleCBox.SelectedIndex == 0);
string titleid = titleidbox.Text;
// Set UserAgent to Wii value // Set UserAgent to Wii value
generalWC.Headers.Add("User-Agent", "wii libnup/1.0"); generalWC.Headers.Add("User-Agent", "wii libnup/1.0");
@ -858,6 +872,9 @@ namespace NUS_Downloader
else else
titledirectory = Path.Combine(CURRENT_DIR, (titleid + "v" + titleversion.Text)); titledirectory = Path.Combine(CURRENT_DIR, (titleid + "v" + titleversion.Text));
if (script_mode)
titledirectory = Path.Combine(CURRENT_DIR, "output_" + Path.GetFileNameWithoutExtension(script_filename));
downloadstartbtn.Text = "Prerequisites: (0/2)"; downloadstartbtn.Text = "Prerequisites: (0/2)";
// Windows 7? // Windows 7?
@ -888,6 +905,14 @@ namespace NUS_Downloader
downloadstartbtn.Text = "Prerequisites: (1/2)"; downloadstartbtn.Text = "Prerequisites: (1/2)";
dlprogress.Value = 50; dlprogress.Value = 50;
if (script_mode)
{
packbox.Checked = true;
packbox_CheckedChanged("scripted", new EventArgs());
deletecontentsbox.Checked = true;
wadnamebox.Enabled = false;
}
// Download CETK after tmd... // Download CETK after tmd...
bool ticket_exists = true; bool ticket_exists = true;
try try
@ -1165,6 +1190,8 @@ namespace NUS_Downloader
/// </summary> /// </summary>
private void DeleteTitleDirectory() private void DeleteTitleDirectory()
{ {
if (script_mode)
return;
// Get placement directory early... // Get placement directory early...
string titledirectory; string titledirectory;
if (titleversion.Text == "") if (titleversion.Text == "")
@ -1254,6 +1281,9 @@ namespace NUS_Downloader
packer.tmdnames = GetContentNames(packer.TMD, contentcount); packer.tmdnames = GetContentNames(packer.TMD, contentcount);
packer.tmdsizes = GetContentSizes(packer.TMD, contentcount); packer.tmdsizes = GetContentSizes(packer.TMD, contentcount);
if (script_mode)
UpdatePackedName();
if (wadnamebox.Text.Contains("[v]") == true) if (wadnamebox.Text.Contains("[v]") == true)
wadnamebox.Text = wadnamebox.Text.Replace("[v]", "v" + titleversion.Text); wadnamebox.Text = wadnamebox.Text.Replace("[v]", "v" + titleversion.Text);
@ -1874,9 +1904,8 @@ namespace NUS_Downloader
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...");
Control.CheckForIllegalCrossThreadCalls = false;
script_mode = true; script_mode = true;
statusbox.Text = ""; 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!
@ -1996,6 +2025,10 @@ namespace NUS_Downloader
private void LoadRegionCodes() private void LoadRegionCodes()
{ {
// TODO: make this check InvokeRequired... // TODO: make this check InvokeRequired...
if (this.InvokeRequired)
{
Debug.Write("TOLDYOUSO!");
}
XmlDocument xDoc = new XmlDocument(); XmlDocument xDoc = new XmlDocument();
xDoc.Load("database.xml"); xDoc.Load("database.xml");
@ -2065,6 +2098,12 @@ namespace NUS_Downloader
/// <param name="enabled">if set to <c>true</c> [enabled].</param> /// <param name="enabled">if set to <c>true</c> [enabled].</param>
private void SetEnableforDownload(bool enabled) private void SetEnableforDownload(bool enabled)
{ {
if (this.InvokeRequired)
{
SetEnableForDownloadCallback sefdcb = new SetEnableForDownloadCallback(SetEnableforDownload);
this.Invoke(sefdcb, new object[] { enabled });
return;
}
// Disable things the user should not mess with during download... // Disable things the user should not mess with during download...
downloadstartbtn.Enabled = enabled; downloadstartbtn.Enabled = enabled;
titleidbox.Enabled = enabled; titleidbox.Enabled = enabled;
@ -2150,7 +2189,7 @@ namespace NUS_Downloader
string title_name = null; string title_name = null;
if ((titleidbox.Enabled == true) && (packbox.Checked == true)) if ((titleidbox.Enabled == true || script_mode == true) && (packbox.Checked == true))
{ {
if (titleversion.Text != "") if (titleversion.Text != "")
{ {
@ -2902,10 +2941,11 @@ namespace NUS_Downloader
/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param> /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
private void RunScript(object sender, System.ComponentModel.DoWorkEventArgs e) private void RunScript(object sender, System.ComponentModel.DoWorkEventArgs e)
{ {
//Control.CheckForIllegalCrossThreadCalls = false;
script_mode = true; script_mode = true;
statusbox.Text = ""; SetTextThreadSafe(statusbox, "");
WriteStatus("Starting script download. Please be patient!"); 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; string[] NUS_Entries;
if (script_filename != "\000") if (script_filename != "\000")
{ {
@ -2925,15 +2965,15 @@ namespace NUS_Downloader
// don't let the delete issue reappear... // don't let the delete issue reappear...
if (string.IsNullOrEmpty(title_info[0])) if (string.IsNullOrEmpty(title_info[0]))
break; break;
titleidbox.Text = title_info[0]; SetTextThreadSafe(titleidbox, title_info[0]);
titleversion.Text = SetTextThreadSafe(titleversion,
Convert.ToString(256* Convert.ToString(256*
(byte.Parse(title_info[1].Substring(0, 2), (byte.Parse(title_info[1].Substring(0, 2),
System.Globalization.NumberStyles.HexNumber))); System.Globalization.NumberStyles.HexNumber))));
titleversion.Text = SetTextThreadSafe(titleversion,
Convert.ToString(Convert.ToInt32(titleversion.Text) + Convert.ToString(Convert.ToInt32(titleversion.Text) +
byte.Parse(title_info[1].Substring(2, 2), byte.Parse(title_info[1].Substring(2, 2),
System.Globalization.NumberStyles.HexNumber)); System.Globalization.NumberStyles.HexNumber)));
button3_Click("Scripter", EventArgs.Empty); button3_Click("Scripter", EventArgs.Empty);
@ -3102,10 +3142,22 @@ namespace NUS_Downloader
nus_script_item.Image = Properties.Resources.script_go; nus_script_item.Image = Properties.Resources.script_go;
folder_item.DropDownItems.Add(nus_script_item); folder_item.DropDownItems.Add(nus_script_item);
// TODO: OnItemClicked... nus_script_item.Click += new EventHandler(nus_script_item_Click);
}
scriptsLocalMenuEntry.DropDownItems.Add(folder_item);
} }
scriptsLocalMenuEntry.DropDownItems.Add(folder_item); // Add scripts in \scripts\
foreach (string nusscript in Directory.GetFiles(Path.Combine(CURRENT_DIR, "scripts"), "*.nus", SearchOption.TopDirectoryOnly))
{
FileInfo finfo = new FileInfo(nusscript);
ToolStripMenuItem nus_script_item = new ToolStripMenuItem();
nus_script_item.Text = finfo.Name;
nus_script_item.Image = Properties.Resources.script_go;
scriptsLocalMenuEntry.DropDownItems.Add(nus_script_item);
nus_script_item.Click += new EventHandler(nus_script_item_Click);
} }
} }
@ -3222,5 +3274,21 @@ namespace NUS_Downloader
saveaswadbtn.Text = String.Empty; saveaswadbtn.Text = String.Empty;
saveaswadbtn.ImageAlign = ContentAlignment.MiddleCenter; saveaswadbtn.ImageAlign = ContentAlignment.MiddleCenter;
} }
}
void nus_script_item_Click(object sender, EventArgs e)
{
ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
string folderpath = "";
if (!tsmi.OwnerItem.Equals(this.scriptsLocalMenuEntry))
{
folderpath = Path.Combine(tsmi.OwnerItem.Text, folderpath);
}
folderpath = Path.Combine(this.CURRENT_DIR, Path.Combine("scripts", Path.Combine(folderpath, tsmi.Text)));
script_filename = folderpath;
BackgroundWorker scripter = new BackgroundWorker();
scripter.DoWork += new DoWorkEventHandler(RunScript);
scripter.RunWorkerAsync();
}
} }
} }

View File

@ -114,6 +114,7 @@
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="Windows7Taskbar.cs" /> <Compile Include="Windows7Taskbar.cs" />
<None Include="TODO" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Resources\wilolgoi.png" /> <None Include="Resources\wilolgoi.png" />