public void preStartup() { ScarletUtil.testConnection(this); ip = ScarletUtil.getExternalIP(); AppDomain.CurrentDomain.ProcessExit += new EventHandler(onProcessExit); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { Root = this.installDirectory.Replace(@"\", @"/"); if (!Directory.Exists(Root)) { status = 11; return; } status = 8; // Loads Server Repos XML XDocument xmlRepo = XDocument.Load(this.ServerRepo + "/repo.xml"); // Saves it to Scarlet's APPDATA Folder string AppDataScarlet = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Scarlet/"; // Create Directory & Save Repo XML Directory.CreateDirectory(AppDataScarlet); xmlRepo.Save(AppDataScarlet + "repo.xml", SaveOptions.None); xmlRepo = XDocument.Load(AppDataScarlet + "repo.xml"); status = 12; // Fetches ModRoot Directory from Server, or Defaults to /Server_Mods/ // Don't use /Mods/ as a folder as your clan will bug you to ask where their custom 1337 mods have gone! // Eg. Root + /Mods_AAF/ ModsRoot = Root + "/@Server_Mods/"; foreach (XElement xelement in xmlRepo.Descendants((XName)"modDir")) { ModsDirName = xelement.Element((XName)"name").Value; ModsRoot = Root + "/" + xelement.Element((XName)"name").Value + "/"; } status = 4; // Create Directory if it doesn't already exist (Creates ModRoot) if (!Directory.Exists(ModsRoot)) { Directory.CreateDirectory(ModsRoot); } status = 5; ///////////////////////////////////////////////////////////////////////////////// // Deletes all Directories other than ones that exist in the Server Repos List // ///////////////////////////////////////////////////////////////////////////////// // Fetches all Dirs from XML List <string> dbDirs = new List <string>(); foreach (XElement dir in xmlRepo.Descendants((XName)"directory")) { dbDirs.Add(Root + dir.Element((XName)"name").Value); } // Fetches all Dirs from Local List <string> allDirs = new List <string>(); foreach (string dir in Directory.GetDirectories(ModsRoot, "*", SearchOption.AllDirectories)) { allDirs.Add(dir.Replace("\\", "/")); } // Delete the difference foreach (string dir in Enumerable.Except <string>((IEnumerable <string>)allDirs, (IEnumerable <string>)dbDirs, (IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase)) { Vendor.FileOperationAPIWrapper.MoveToRecycleBin(dir); } status = 6; /////////////////////////////////////////////////////////////////////////// // Deletes all Files other than ones that exist in the Server Repos List // /////////////////////////////////////////////////////////////////////////// // Fetches all Files from XML List <string> dbFiles = new List <string>(); foreach (XElement file in xmlRepo.Descendants((XName)"file")) { dbFiles.Add(Root + file.Element((XName)"name").Value); } // Fetches all Files from Local List <string> allFiles = new List <string>(); foreach (string file in Directory.GetFiles(ModsRoot, "*", SearchOption.AllDirectories)) { allFiles.Add(file.Replace("\\", "/")); } // Delete the difference foreach (string file in Enumerable.Except <string>((IEnumerable <string>)allFiles, (IEnumerable <string>)dbFiles, (IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase)) { Vendor.FileOperationAPIWrapper.MoveToRecycleBin(file); } status = 7; int filesDone = 0; int fileList = xmlRepo.Descendants("file").Count(); foreach (XContainer xcontainer in xmlRepo.Descendants("directory")) { string str2 = xcontainer.Element("name").Value; Directory.CreateDirectory(Root + str2); } foreach (var xelement in xmlRepo.Descendants("file")) { if (backgroundWorker1.CancellationPending) { e.Cancel = true; break; } else { string str2 = xelement.Element("name").Value; string a = xelement.Element("hash").Value; string sUrlToReadFileFrom = this.ServerRepo + "/" + str2; string str3 = Root + str2; double percent = (double)filesDone / fileList; if (Root != null) { using (MD5.Create()) { if (System.IO.File.Exists(Root + str2)) { this.Invoke(new Action(() => { downloadLbl_Controller(percent, 1, str2); })); FileStream stream = System.IO.File.OpenRead(Root + str2); string b = ScarletUtil.HashFile(stream); stream.Close(); if (!string.Equals(a, b)) { File.Delete(str3); this.Invoke(new Action(() => { downloadLbl_Controller(percent, 0, str2); })); downloadFile(sUrlToReadFileFrom, str3, percent, fileList); wsm.Send("(" + ip + ") " + Username + " is Fetching File: " + str2 + " to"); } else { wsm.Send("(" + ip + ") " + Username + " Verified File: " + str2 + " to"); } } else { this.Invoke(new Action(() => { downloadLbl_Controller(percent, 0, str2); })); downloadFile(sUrlToReadFileFrom, str3, percent, fileList); wsm.Send("(" + ip + ") " + Username + " is Fetching File: " + str2 + " to"); } } } filesDone++; backgroundWorker1.ReportProgress((int)(percent * 710)); } } if (filesDone == fileList) { status = 10; } else { status = 9; } }