//extract files private bool doExtract(dlc currentdlc) { try { FastZip fz = new FastZip(); fz.ExtractZip(Path.GetTempPath() + currentdlc.filename, path + "\\data_win\\", ""); return(true); } catch (ZipException ex) { //MessageBox.Show("error extracting " + currentdlc.filename + " from path " //+ Path.GetTempPath() + currentdlc.filename + " to path " + path + "\\data_win\\" + ex.Message); return(false); } catch (FileNotFoundException) { return(false); } }
//extract files private bool doExtract(dlc currentdlc) { try { FastZip fz = new FastZip(); fz.ExtractZip(Path.GetTempPath() + currentdlc.filename, path + "\\data_win\\", ""); return true; } catch (ZipException ex) { //MessageBox.Show("error extracting " + currentdlc.filename + " from path " //+ Path.GetTempPath() + currentdlc.filename + " to path " + path + "\\data_win\\" + ex.Message); return false; } catch (FileNotFoundException) { return false; } }
//this function will check the dlc.xml doc for client and map updates public void checkXML() { //create a webclient to download xml WebClient c = new WebClient(); String dlcxml = ""; try { c.Headers.Add("Content-Type", "application/xml"); dlcxml = c.DownloadString("http://vengeful-spirit.com/ofp2-dlc/dlc.xml"); } catch (System.Net.WebException ex) { labelStatus.Text = "Status: Failed to connect to server"; } try { //if we have an xml doc lets start parsing it if (dlcxml != "") { XmlDocument doc = new XmlDocument(); doc.LoadXml(dlcxml); //lets check client xml settings first XmlNodeList client = doc.SelectNodes("dlcdata/client"); foreach (XmlNode entry in client) { if (entry.NodeType == XmlNodeType.Element && entry.FirstChild.NodeType == XmlNodeType.Element) { foreach (XmlNode node in entry) { try { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "version") { clientVersion = Convert.ToDouble(node.InnerText.ToString(), CultureInfo.InvariantCulture); } if (node.Name == "news") { clientNews = node.InnerText; } if (node.Name == "url") { clientURL = node.InnerText; } if (node.Name == "tickRate") { tickRate = Convert.ToInt32(node.InnerText, CultureInfo.InvariantCulture); } } } catch (XmlException ex) { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText("Problem with XML - An Administrator will be resolving this shortly: " + ex.Message + "\n"); }); } } } } //do we need to update? if (clientVersion > version & !updateNotify) { clientUpdate(); } //get list of dlc packs and mirros mydlc = new List<dlc>(); XmlNodeList dlclist = doc.SelectNodes("dlcdata/dlcpack"); foreach (XmlNode entry in dlclist) { dlc tempdlc = new dlc(); if (entry.NodeType == XmlNodeType.Element) { foreach (XmlNode node in entry) { try { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "name") { tempdlc.name = node.InnerText; } if (node.Name == "revision") { tempdlc.revision = Convert.ToDouble(node.InnerText, CultureInfo.InvariantCulture); } if (node.Name == "mirror") { tempdlc.mirrors(node.InnerText); } if (node.Name == "uid") { tempdlc.uid = Convert.ToInt32(node.InnerText, CultureInfo.InvariantCulture); } if (node.Name == "remove") { tempdlc.removePath(node.InnerText); } if (node.Name == "notes") { tempdlc.notes = node.InnerText; } } } catch (XmlException ex) { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText("Problem with XML - An Administrator will be resolving this shortly: " + ex.Message + "\n"); }); } } } //do we need to update this DLC? int revision = 0; RegistryKey dlcInfo = Registry.CurrentUser.OpenSubKey("Software\\VSDLC\\MapPack" + tempdlc.uid); if (dlcInfo == null) { mydlc.Add(tempdlc); } else { revision = Convert.ToInt32(dlcInfo.GetValue("revision"), CultureInfo.InvariantCulture); } if (tempdlc.revision > revision && revision > 0) { mydlc.Add(tempdlc); //dlcInfo.Close(); } } } } catch (XmlException ex) { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText("Problem with XML - An Administrator will be resolving this shortly: " + ex.Message + "\n"); }); } }
//download files private void doDownload(dlc currentdlc, DoWorkEventArgs e) { //check if mirror is working if (currentdlc.isThereAMirror) { //take care of any remove operations foreach (string todel in currentdlc.remove) { if (todel != null) { string fullPath = path + "\\data_win\\" + todel; try { if(Directory.Exists(fullPath)) { Directory.Delete(fullPath,true); } } catch(Exception ex) { } } } //start downloading string sFilePathToWriteFileTo = Path.GetTempPath().ToString() + currentdlc.filename; HttpWebRequest request; HttpWebResponse response; Uri url = new Uri(currentdlc.foundMirror); Int64 iSize = 0; //add WebException here try { request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); response = (System.Net.HttpWebResponse)request.GetResponse(); response.Close(); if (response.ContentType == "text/html; charset=utf-8") { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(" - Mirror: " + currentdlc.foundMirror + "- Failed \n"); }); return; } iSize = response.ContentLength; } catch (WebException ex) { //MessageBox.Show(ex.Message); this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(" - Failed: " + ex.Message + "\n"); }); return; } //initial size of file //counter for progress Int64 iRunningByteTotal = 0; this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(" - Mirror: " + currentdlc.foundMirror); }); using (System.Net.WebClient client = new System.Net.WebClient()) { using (System.IO.Stream streamRemote = client.OpenRead(url)) { using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None)) { //loop the steam and get the file ino the byte buffer int iByteSize = 0; byte[] byteBuffer = new byte[iSize]; while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) { if (backgroundWorkerDownload.CancellationPending) { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText("\n\n"); progressBar.Value = 0; }); return; } streamLocal.Write(byteBuffer, 0, iByteSize); iRunningByteTotal += iByteSize; //calculate progress out of a base 100 double dIndex = (double)(iRunningByteTotal); double dTotal = (double)byteBuffer.Length; double dProgressPercentage = (dIndex / dTotal); int iProgressPercentage = (int)(dProgressPercentage * 100); //update progress bar backgroundWorkerDownload.ReportProgress(iProgressPercentage); } //clean up the file stream streamLocal.Close(); } //close the connection to the server streamRemote.Close(); //did DLC update properly? if (doExtract(currentdlc)) { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(" - Finished "); if (currentdlc.notes != null && currentdlc.notes != "") { richTextBox1.AppendText("Readme: " + currentdlc.notes + "\n"); } else { richTextBox1.AppendText("\n"); } }); //update registry with UID and revision RegistryKey dlcInfo = Registry.CurrentUser.OpenSubKey("Software\\VSDLC\\MapPack" + currentdlc.uid,true); //if key does not exists we create it here if (dlcInfo == null) { dlcInfo = Registry.CurrentUser.CreateSubKey("Software\\VSDLC\\MapPack" + currentdlc.uid); dlcInfo.SetValue("revision", currentdlc.revision); dlcInfo.Close(); } else { try { dlcInfo.SetValue("revision", currentdlc.revision); dlcInfo.Close(); } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); } } } else { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(" - Failed \n"); }); } } } } else { this.Invoke((MethodInvoker)delegate { richTextBox1.AppendText(" - No valid mirrors found \n"); }); return; } }
//this function will check the dlc.xml doc for client and map updates public void checkXML() { //create a webclient to download xml WebClient c = new WebClient(); String dlcxml = ""; try { c.Headers.Add("Content-Type", "application/xml"); dlcxml = c.DownloadString("http://vengeful-spirit.com/ofp2-dlc/dlc.xml"); } catch (System.Net.WebException ex) { labelStatus.Text = "Status: Failed to connect to server"; } try { //if we have an xml doc lets start parsing it if (dlcxml != "") { XmlDocument doc = new XmlDocument(); doc.LoadXml(dlcxml); //lets check client xml settings first XmlNodeList client = doc.SelectNodes("dlcdata/client"); foreach (XmlNode entry in client) { if (entry.NodeType == XmlNodeType.Element && entry.FirstChild.NodeType == XmlNodeType.Element) { foreach (XmlNode node in entry) { try { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "version") { clientVersion = Convert.ToDouble(node.InnerText.ToString(), CultureInfo.InvariantCulture); } if (node.Name == "news") { clientNews = node.InnerText; } if (node.Name == "url") { clientURL = node.InnerText; } if (node.Name == "tickRate") { tickRate = Convert.ToInt32(node.InnerText, CultureInfo.InvariantCulture); } } } catch (XmlException ex) { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText("Problem with XML - An Administrator will be resolving this shortly: " + ex.Message + "\n"); }); } } } } //do we need to update? if (clientVersion > version & !updateNotify) { clientUpdate(); } //get list of dlc packs and mirros mydlc = new List <dlc>(); XmlNodeList dlclist = doc.SelectNodes("dlcdata/dlcpack"); foreach (XmlNode entry in dlclist) { dlc tempdlc = new dlc(); if (entry.NodeType == XmlNodeType.Element) { foreach (XmlNode node in entry) { try { if (node.NodeType == XmlNodeType.Element) { if (node.Name == "name") { tempdlc.name = node.InnerText; } if (node.Name == "revision") { tempdlc.revision = Convert.ToDouble(node.InnerText, CultureInfo.InvariantCulture); } if (node.Name == "mirror") { tempdlc.mirrors(node.InnerText); } if (node.Name == "uid") { tempdlc.uid = Convert.ToInt32(node.InnerText, CultureInfo.InvariantCulture); } if (node.Name == "remove") { tempdlc.removePath(node.InnerText); } if (node.Name == "notes") { tempdlc.notes = node.InnerText; } } } catch (XmlException ex) { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText("Problem with XML - An Administrator will be resolving this shortly: " + ex.Message + "\n"); }); } } } //do we need to update this DLC? int revision = 0; RegistryKey dlcInfo = Registry.CurrentUser.OpenSubKey("Software\\VSDLC\\MapPack" + tempdlc.uid); if (dlcInfo == null) { mydlc.Add(tempdlc); } else { revision = Convert.ToInt32(dlcInfo.GetValue("revision"), CultureInfo.InvariantCulture); } if (tempdlc.revision > revision && revision > 0) { mydlc.Add(tempdlc); //dlcInfo.Close(); } } } } catch (XmlException ex) { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText("Problem with XML - An Administrator will be resolving this shortly: " + ex.Message + "\n"); }); } }
//download files private void doDownload(dlc currentdlc, DoWorkEventArgs e) { //check if mirror is working if (currentdlc.isThereAMirror) { //take care of any remove operations foreach (string todel in currentdlc.remove) { if (todel != null) { string fullPath = path + "\\data_win\\" + todel; try { if (Directory.Exists(fullPath)) { Directory.Delete(fullPath, true); } } catch (Exception ex) { } } } //start downloading string sFilePathToWriteFileTo = Path.GetTempPath().ToString() + currentdlc.filename; HttpWebRequest request; HttpWebResponse response; Uri url = new Uri(currentdlc.foundMirror); Int64 iSize = 0; //add WebException here try { request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url); response = (System.Net.HttpWebResponse)request.GetResponse(); response.Close(); if (response.ContentType == "text/html; charset=utf-8") { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText(" - Mirror: " + currentdlc.foundMirror + "- Failed \n"); }); return; } iSize = response.ContentLength; } catch (WebException ex) { //MessageBox.Show(ex.Message); this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText(" - Failed: " + ex.Message + "\n"); }); return; } //initial size of file //counter for progress Int64 iRunningByteTotal = 0; this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText(" - Mirror: " + currentdlc.foundMirror); }); using (System.Net.WebClient client = new System.Net.WebClient()) { using (System.IO.Stream streamRemote = client.OpenRead(url)) { using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None)) { //loop the steam and get the file ino the byte buffer int iByteSize = 0; byte[] byteBuffer = new byte[iSize]; while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) { if (backgroundWorkerDownload.CancellationPending) { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText("\n\n"); progressBar.Value = 0; }); return; } streamLocal.Write(byteBuffer, 0, iByteSize); iRunningByteTotal += iByteSize; //calculate progress out of a base 100 double dIndex = (double)(iRunningByteTotal); double dTotal = (double)byteBuffer.Length; double dProgressPercentage = (dIndex / dTotal); int iProgressPercentage = (int)(dProgressPercentage * 100); //update progress bar backgroundWorkerDownload.ReportProgress(iProgressPercentage); } //clean up the file stream streamLocal.Close(); } //close the connection to the server streamRemote.Close(); //did DLC update properly? if (doExtract(currentdlc)) { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText(" - Finished "); if (currentdlc.notes != null && currentdlc.notes != "") { richTextBox1.AppendText("Readme: " + currentdlc.notes + "\n"); } else { richTextBox1.AppendText("\n"); } }); //update registry with UID and revision RegistryKey dlcInfo = Registry.CurrentUser.OpenSubKey("Software\\VSDLC\\MapPack" + currentdlc.uid, true); //if key does not exists we create it here if (dlcInfo == null) { dlcInfo = Registry.CurrentUser.CreateSubKey("Software\\VSDLC\\MapPack" + currentdlc.uid); dlcInfo.SetValue("revision", currentdlc.revision); dlcInfo.Close(); } else { try { dlcInfo.SetValue("revision", currentdlc.revision); dlcInfo.Close(); } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); } } } else { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText(" - Failed \n"); }); } } } } else { this.Invoke((MethodInvoker) delegate { richTextBox1.AppendText(" - No valid mirrors found \n"); }); return; } }