/////////////////////////////////////////// // NUS Downloader: Database.cs // // $Rev:: $ // // $Author:: $ // // $Date:: $ // /////////////////////////////////////////// using System; using System.Windows.Forms; using System.IO; using System.Xml; using System.Drawing; namespace NUS_Downloader { class Database { private string SystemTag = "SYS"; private string IosTag = "IOS"; private string VcTag = "VC"; private string WwTag = "WW"; private string UpdateTag = "UPD"; private string[] VcConsoles = new string[11] { "C64", "GEN", "MSX", "N64", "NEO", "NES", "SMS", "SNES", "TG16", "TGCD", "ARC" }; private string databaseString; public static Image green = Properties.Resources.bullet_green; public static Image orange = Properties.Resources.bullet_orange; public static Image redorb = Properties.Resources.bullet_red; public static Image redgreen = Properties.Resources.bullet_redgreen; public static Image redorange = Properties.Resources.bullet_redorange; public void LoadDatabaseToStream(string databaseFile) { // Does it exist? if (!File.Exists(databaseFile)) throw new FileNotFoundException("I couldn't find the database file!", "database.xml"); // Load database.xml into MemoryStream databaseString = File.ReadAllText(databaseFile); /*System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[] databasebytes = encoding.GetBytes(databasestr); // Load the memory stream databaseStream = new MemoryStream(databasebytes); databaseStream.Seek(0, SeekOrigin.Begin);*/ } public string GetDatabaseVersion() { if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList DatabaseList = xDoc.GetElementsByTagName("database"); XmlAttributeCollection Attributes = DatabaseList[0].Attributes; return Attributes[0].Value; } public static string GetDatabaseVersion(string databaseString) { XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList DatabaseList = xDoc.GetElementsByTagName("database"); XmlAttributeCollection Attributes = DatabaseList[0].Attributes; return Attributes[0].Value; } public ToolStripMenuItem[] LoadSystemTitles() { if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList SystemTitlesXMLNodes = xDoc.GetElementsByTagName(SystemTag); ToolStripMenuItem[] systemTitleCollection = new ToolStripMenuItem[SystemTitlesXMLNodes.Count]; for (int x = 0; x < SystemTitlesXMLNodes.Count; x++) { ToolStripMenuItem XMLToolStripItem = new ToolStripMenuItem(); XmlAttributeCollection XMLAttributes = SystemTitlesXMLNodes[x].Attributes; string titleID = ""; string descname = ""; bool dangerous = false; bool ticket = true; XmlNodeList ChildrenOfTheNode = SystemTitlesXMLNodes[x].ChildNodes; for (int z = 0; z < ChildrenOfTheNode.Count; z++) { switch (ChildrenOfTheNode[z].Name) { case "name": descname = ChildrenOfTheNode[z].InnerText; break; case "titleID": titleID = ChildrenOfTheNode[z].InnerText; break; case "version": string[] versions = ChildrenOfTheNode[z].InnerText.Split(','); // Add to region things? if (XMLToolStripItem.DropDownItems.Count > 0) { for (int b = 0; b < XMLToolStripItem.DropDownItems.Count; b++) { if (ChildrenOfTheNode[z].InnerText != "") { ToolStripMenuItem regitem = (ToolStripMenuItem)XMLToolStripItem.DropDownItems[b]; regitem.DropDownItems.Add("Latest Version"); for (int y = 0; y < versions.Length; y++) { regitem.DropDownItems.Add("v" + versions[y]); } //regitem.DropDownItemClicked += new ToolStripItemClickedEventHandler(Application.); } } } else { XMLToolStripItem.DropDownItems.Add("Latest Version"); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < versions.Length; y++) { XMLToolStripItem.DropDownItems.Add("v" + versions[y]); } } } break; case "region": string[] regions = ChildrenOfTheNode[z].InnerText.Split(','); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < regions.Length; y++) { XMLToolStripItem.DropDownItems.Add(RegionFromIndex(Convert.ToInt32(regions[y]))); } } break; default: break; case "ticket": ticket = Convert.ToBoolean(ChildrenOfTheNode[z].InnerText); break; case "danger": dangerous = true; XMLToolStripItem.ToolTipText = ChildrenOfTheNode[z].InnerText; break; } XMLToolStripItem.Image = SelectItemImage(ticket, dangerous); if (titleID != "") { XMLToolStripItem.Text = String.Format("{0} - {1}", titleID, descname); } else { // Wait what? XMLToolStripItem.Text = descname; } } //AddToolStripItemToStrip(i, XMLToolStripItem, XMLAttributes); systemTitleCollection[x] = XMLToolStripItem; } return systemTitleCollection; } public ToolStripMenuItem[] LoadIosTitles() { if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList IosTitlesXMLNodes = xDoc.GetElementsByTagName(IosTag); ToolStripMenuItem[] iosTitleCollection = new ToolStripMenuItem[IosTitlesXMLNodes.Count]; for (int x = 0; x < IosTitlesXMLNodes.Count; x++) { ToolStripMenuItem XMLToolStripItem = new ToolStripMenuItem(); XmlAttributeCollection XMLAttributes = IosTitlesXMLNodes[x].Attributes; string titleID = ""; string descname = ""; bool dangerous = false; bool ticket = true; XmlNodeList ChildrenOfTheNode = IosTitlesXMLNodes[x].ChildNodes; for (int z = 0; z < ChildrenOfTheNode.Count; z++) { switch (ChildrenOfTheNode[z].Name) { case "name": descname = ChildrenOfTheNode[z].InnerText; break; case "titleID": titleID = ChildrenOfTheNode[z].InnerText; break; case "version": string[] versions = ChildrenOfTheNode[z].InnerText.Split(','); XMLToolStripItem.DropDownItems.Add("Latest Version"); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < versions.Length; y++) { XMLToolStripItem.DropDownItems.Add("v" + versions[y]); } } break; case "ticket": ticket = Convert.ToBoolean(ChildrenOfTheNode[z].InnerText); break; case "danger": dangerous = true; XMLToolStripItem.ToolTipText = ChildrenOfTheNode[z].InnerText; break; default: break; } XMLToolStripItem.Image = SelectItemImage(ticket, dangerous); if (titleID != "") XMLToolStripItem.Text = String.Format("{0} - {1}", titleID, descname); else XMLToolStripItem.Text = descname; //Huh } iosTitleCollection[x] = XMLToolStripItem; } return iosTitleCollection; } public ToolStripMenuItem[][] LoadVirtualConsoleTitles() { if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList VirtualConsoleXMLNodes = xDoc.GetElementsByTagName(VcTag); ToolStripMenuItem[][] vcTitleCollection = new ToolStripMenuItem[VcConsoles.Length][]; for (int j = 0; j < vcTitleCollection.Length; j++) { vcTitleCollection[j] = new ToolStripMenuItem[0]; } for (int x = 0; x < VirtualConsoleXMLNodes.Count; x++) { ToolStripMenuItem XMLToolStripItem = new ToolStripMenuItem(); XmlAttributeCollection XMLAttributes = VirtualConsoleXMLNodes[x].Attributes; string titleID = ""; string descname = ""; bool dangerous = false; bool ticket = true; XmlNodeList ChildrenOfTheNode = VirtualConsoleXMLNodes[x].ChildNodes; for (int z = 0; z < ChildrenOfTheNode.Count; z++) { switch (ChildrenOfTheNode[z].Name) { case "name": descname = ChildrenOfTheNode[z].InnerText; break; case "titleID": titleID = ChildrenOfTheNode[z].InnerText; break; case "version": string[] versions = ChildrenOfTheNode[z].InnerText.Split(','); // Add to region things? if (XMLToolStripItem.DropDownItems.Count > 0) { for (int b = 0; b < XMLToolStripItem.DropDownItems.Count; b++) { if (ChildrenOfTheNode[z].InnerText != "") { ToolStripMenuItem regitem = (ToolStripMenuItem)XMLToolStripItem.DropDownItems[b]; regitem.DropDownItems.Add("Latest Version"); for (int y = 0; y < versions.Length; y++) { regitem.DropDownItems.Add("v" + versions[y]); } //regitem.DropDownItemClicked += new ToolStripItemClickedEventHandler(deepitem_clicked); } } } else { XMLToolStripItem.DropDownItems.Add("Latest Version"); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < versions.Length; y++) { XMLToolStripItem.DropDownItems.Add("v" + versions[y]); } } } break; case "region": string[] regions = ChildrenOfTheNode[z].InnerText.Split(','); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < regions.Length; y++) { XMLToolStripItem.DropDownItems.Add(RegionFromIndex(Convert.ToInt32(regions[y]))); } } break; default: break; case "ticket": ticket = Convert.ToBoolean(ChildrenOfTheNode[z].InnerText); break; case "danger": dangerous = true; XMLToolStripItem.ToolTipText = ChildrenOfTheNode[z].InnerText; break; } XMLToolStripItem.Image = SelectItemImage(ticket, dangerous); if (titleID != "") { XMLToolStripItem.Text = String.Format("{0} - {1}", titleID, descname); } else { // Wait what? XMLToolStripItem.Text = descname; } } for (int a = 0; a < VcConsoles.Length; a++) { if (XMLAttributes[0].Value == VcConsoles[a]) { Array.Resize(ref vcTitleCollection[a], vcTitleCollection[a].Length + 1); vcTitleCollection[a][vcTitleCollection[a].Length - 1] = XMLToolStripItem; } } } return vcTitleCollection; } public ToolStripMenuItem[] LoadWiiWareTitles() { if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList WiiWareTitlesXMLNodes = xDoc.GetElementsByTagName(WwTag); ToolStripMenuItem[] wwTitleCollection = new ToolStripMenuItem[WiiWareTitlesXMLNodes.Count]; for (int x = 0; x < WiiWareTitlesXMLNodes.Count; x++) { ToolStripMenuItem XMLToolStripItem = new ToolStripMenuItem(); XmlAttributeCollection XMLAttributes = WiiWareTitlesXMLNodes[x].Attributes; string titleID = ""; string descname = ""; bool dangerous = false; bool ticket = true; XmlNodeList ChildrenOfTheNode = WiiWareTitlesXMLNodes[x].ChildNodes; for (int z = 0; z < ChildrenOfTheNode.Count; z++) { switch (ChildrenOfTheNode[z].Name) { case "name": descname = ChildrenOfTheNode[z].InnerText; break; case "titleID": titleID = ChildrenOfTheNode[z].InnerText; break; case "version": string[] versions = ChildrenOfTheNode[z].InnerText.Split(','); // Add to region things? if (XMLToolStripItem.DropDownItems.Count > 0) { for (int b = 0; b < XMLToolStripItem.DropDownItems.Count; b++) { if (ChildrenOfTheNode[z].InnerText != "") { ToolStripMenuItem regitem = (ToolStripMenuItem)XMLToolStripItem.DropDownItems[b]; regitem.DropDownItems.Add("Latest Version"); for (int y = 0; y < versions.Length; y++) { regitem.DropDownItems.Add("v" + versions[y]); } //regitem.DropDownItemClicked += new ToolStripItemClickedEventHandler(deepitem_clicked); } } } else { XMLToolStripItem.DropDownItems.Add("Latest Version"); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < versions.Length; y++) { XMLToolStripItem.DropDownItems.Add("v" + versions[y]); } } } break; case "region": string[] regions = ChildrenOfTheNode[z].InnerText.Split(','); if (ChildrenOfTheNode[z].InnerText != "") { for (int y = 0; y < regions.Length; y++) { XMLToolStripItem.DropDownItems.Add(RegionFromIndex(Convert.ToInt32(regions[y]))); } } break; default: break; case "ticket": ticket = Convert.ToBoolean(ChildrenOfTheNode[z].InnerText); break; case "danger": dangerous = true; XMLToolStripItem.ToolTipText = ChildrenOfTheNode[z].InnerText; break; } XMLToolStripItem.Image = SelectItemImage(ticket, dangerous); if (titleID != "") { XMLToolStripItem.Text = String.Format("{0} - {1}", titleID, descname); } else { // Wait what? XMLToolStripItem.Text = descname; } } wwTitleCollection[x] = XMLToolStripItem; } return wwTitleCollection; } /// /// Gathers the region based on index /// /// The index. /// XmlDocument with database inside /// Region desc private string RegionFromIndex(int index) { /* Typical Region XML * 41 (All/System) 44 (German) 45 (USA/NTSC) 46 (French) 4A (Japan) 4B (Korea) 4C (Japanese Import to Europe/Australia/PAL) 4D (American Import to Europe/Australia/PAL) 4E (Japanese Import to USA/NTSC) 50 (Europe/PAL) 51 (Korea w/ Japanese Language) 54 (Korea w/ English Language) 58 (Some Homebrew) */ if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList XMLRegionList = xDoc.GetElementsByTagName("REGIONS"); XmlNodeList ChildrenOfTheNode = XMLRegionList[0].ChildNodes; // For each child node (region node) for (int z = 0; z < ChildrenOfTheNode.Count; z++) { // Gather attributes (index='x') XmlAttributeCollection XMLAttributes = ChildrenOfTheNode[z].Attributes; // Return value of node if index matches if (Convert.ToInt32(XMLAttributes[0].Value) == index) return ChildrenOfTheNode[z].InnerText; } return "XX (Error)"; } /// /// Selects the database item image. /// /// if set to true [ticket]. /// if set to true [danger]. /// Correct Image private System.Drawing.Image SelectItemImage(bool ticket, bool danger) { // All is good, go green... if ((ticket) && (!danger)) return green; // There's no ticket, but danger is clear... if ((!ticket) && (!danger)) return orange; // DANGER WILL ROBINSON... if ((ticket) && (danger)) return redgreen; // Double bad... if ((!ticket) && (danger)) return redorange; return null; } /// /// Loads the region codes. /// public ToolStripMenuItem[] LoadRegionCodes() { /* TODO: make this check InvokeRequired... if (this.InvokeRequired) { Debug.Write("TOLDYOUSO!"); BootChecksCallback bcc = new BootChecksCallback(LoadRegionCodes); this.Invoke(bcc); return; }*/ if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList XMLRegionList = xDoc.GetElementsByTagName("REGIONS"); XmlNodeList ChildrenOfTheNode = XMLRegionList[0].ChildNodes; ToolStripMenuItem[] regionItems = new ToolStripMenuItem[ChildrenOfTheNode.Count]; // For each child node (region node) for (int z = 0; z < ChildrenOfTheNode.Count; z++) { regionItems[z] = new ToolStripMenuItem(); regionItems[z].Text = ChildrenOfTheNode[z].InnerText; } return regionItems; } public ToolStripMenuItem[] LoadScripts() { if (databaseString.Length < 1) { throw new Exception("Load the database into a memory stream first!"); } XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(databaseString); XmlNodeList ScriptXMLNodes = xDoc.GetElementsByTagName(UpdateTag); ToolStripMenuItem[] scriptCollection = new ToolStripMenuItem[ScriptXMLNodes.Count]; for (int x = 0; x < ScriptXMLNodes.Count; x++) { ToolStripMenuItem XMLToolStripItem = new ToolStripMenuItem(); XmlAttributeCollection XMLAttributes = ScriptXMLNodes[x].Attributes; XmlNodeList ChildrenOfTheNode = ScriptXMLNodes[x].ChildNodes; for (int z = 0; z < ChildrenOfTheNode.Count; z++) { switch (ChildrenOfTheNode[z].Name) { case "name": XMLToolStripItem.Text = ChildrenOfTheNode[z].InnerText; break; case "script": XMLToolStripItem.ToolTipText = ChildrenOfTheNode[z].InnerText; break; default: break; } XMLToolStripItem.Image = Properties.Resources.script_start; } scriptCollection[x] = XMLToolStripItem; } return scriptCollection; } } }