public static void initialize(CProgressBar TruckersMPUpdateProgress, Label TruckersMPUpdateProgressLabel) { if (Properties.Settings.Default.StartSteam) { if (Process.GetProcessesByName("Steam").Length == 0) { Microsoft.Win32.RegistryKey steamKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Valve\\Steam"); if (steamKey != null) { string SteamExe = (string)steamKey.GetValue("SteamExe"); if (SteamExe != null) { Process.Start(SteamExe); } } } } Launcher.ETS2Installed = false; Launcher.ATSInstalled = false; Microsoft.Win32.RegistryKey readKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\TruckersMP"); if (readKey != null) { Launcher.TruckersMPLocation = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TruckersMP"; Launcher.ETS2Location = (string)readKey.GetValue("InstallLocationETS2"); Launcher.ATSLocation = (string)readKey.GetValue("InstallLocationATS"); if (System.IO.Directory.Exists(Launcher.ETS2Location)) { Launcher.ETS2Installed = true; } if (System.IO.Directory.Exists(Launcher.ATSLocation)) { Launcher.ATSInstalled = true; } if (!System.IO.Directory.Exists(Launcher.TruckersMPLocation)) { DialogResult dialogResult = MessageBox.Show("Unable to locate TruckersMP.\n\nDo you want to install it now?\n(required)", "TruckersMP Launcher", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { System.Threading.ThreadPool.QueueUserWorkItem(delegate { TruckersMP.install(TruckersMPUpdateProgress, TruckersMPUpdateProgressLabel); }); } else { Environment.Exit(1); } } else { if (!(Launcher.ETS2Installed || Launcher.ATSInstalled)) { DialogResult dialogResult = MessageBox.Show("There seems to be a problem with your TruckersMP install.\n\nDo you want to reinstall TruckersMP?\n(required)", "TruckersMP Launcher", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { System.Threading.ThreadPool.QueueUserWorkItem(delegate { TruckersMP.install(TruckersMPUpdateProgress, TruckersMPUpdateProgressLabel, true); }); } else { Environment.Exit(1); } } else { System.Threading.ThreadPool.QueueUserWorkItem(delegate { TruckersMP.integrityCheck(TruckersMPUpdateProgress, TruckersMPUpdateProgressLabel); }); } } } else { DialogResult dialogResult = MessageBox.Show("TruckersMP has not been installed!\n\nPlease run the TruckersMP installer once.\n\nWant to do it now? ", "TruckersMP Launcher", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { System.Diagnostics.Process.Start("http://truckersmp.com/en_US/download"); Environment.Exit(1); } else { Environment.Exit(1); } } }
public static void install(CProgressBar TruckersMPUpdateProgress, Label TruckersMPUpdateProgressLabel, bool reinstall = false) { Launcher.working = true; JArray liveFiles = new JArray(); //Get files from TMP using (WebClient client = new WebClient()) { try { JObject requestData = JObject.Parse(client.DownloadString("http://update.ets2mp.com/files.json")); liveFiles = (JArray)requestData["Files"]; } catch (WebException) { Console.WriteLine("Unable to connect to TruckersMP Update API. Cannot install TMP!"); Launcher.working = false; return; } } if (reinstall) { if (Directory.Exists(Launcher.TruckersMPLocation)) { Directory.Delete(Launcher.TruckersMPLocation, true); } } if (!Directory.Exists(Launcher.TruckersMPLocation)) { Directory.CreateDirectory(Launcher.TruckersMPLocation); } TruckersMPUpdateProgress.Visible = true; TruckersMPUpdateProgressLabel.Visible = true; TruckersMPUpdateProgress.Maximum = 100; try { System.Threading.ThreadPool.QueueUserWorkItem(async delegate { foreach (var file in liveFiles) { string filePath = (string)file["FilePath"]; string localPath = ((string)file["FilePath"]).Replace("/", "\\"); string[] s = filePath.Split('/'); string fileName = s[(s.Length - 1)]; if (!Directory.Exists(Launcher.TruckersMPLocation)) { Directory.CreateDirectory(Launcher.TruckersMPLocation); } if (s.Length == 3) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1]); } } if (s.Length == 4) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2]); } } if (s.Length == 5) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3]); } } if (s.Length == 6) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3] + "\\" + s[4])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3] + "\\" + s[4]); } } TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "Downloading " + fileName + "...")); using (WebClient downloadClient = new WebClient()) { downloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs e) { TruckersMPUpdateProgress.Value = e.ProgressPercentage; }); await downloadClient.DownloadFileTaskAsync(new Uri("http://download.ets2mp.com/files" + filePath), Launcher.TruckersMPLocation + localPath); } } TruckersMPUpdateProgress.Value = 100; Launcher.working = false; TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Visible = false)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "TruckersMP Installed!")); System.Threading.Thread.Sleep(6000); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Visible = false)); }); } catch (WebException) { Console.WriteLine("An error occured downloading files from TruckersMP. Unable to update!"); Launcher.working = false; return; } return; }
private static void update(CProgressBar TruckersMPUpdateProgress, Label TruckersMPUpdateProgressLabel, List <string> mismatchedFiles, bool runGame = false, String game = "") { Launcher.working = true; TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Visible = true)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Visible = true)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "Updating TruckersMP...")); TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Maximum = 100)); try { System.Threading.ThreadPool.QueueUserWorkItem(async delegate { foreach (var file in mismatchedFiles) { string downloadFile = (file.Replace("_backup", "")).Replace("\\", "/"); string[] s = file.Split('\\'); string fileName = s[(s.Length - 1)]; if (!Directory.Exists(Launcher.TruckersMPLocation)) { Directory.CreateDirectory(Launcher.TruckersMPLocation); } if (s.Length == 3) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1]); } } if (s.Length == 4) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2]); } } if (s.Length == 5) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3]); } } if (s.Length == 6) { if (!Directory.Exists(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3] + "\\" + s[4])) { Directory.CreateDirectory(Launcher.TruckersMPLocation + "\\" + s[1] + "\\" + s[2] + "\\" + s[3] + "\\" + s[4]); } } TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "Downloading " + fileName + "...")); using (WebClient downloadClient = new WebClient()) { downloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs e) { TruckersMPUpdateProgress.Value = e.ProgressPercentage; }); await downloadClient.DownloadFileTaskAsync(new Uri("http://download.ets2mp.com/files" + downloadFile), Launcher.TruckersMPLocation + file); } } TruckersMPUpdateProgress.Value = 100; Launcher.working = false; TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Visible = false)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "TruckersMP Updated!")); if (runGame) { if (game == "ETS2MP" || game.Equals("play_ets2mp_btn")) { Game.runETS2MP(); } else if (game == "ATSMP" || game.Equals("play_atsmp_btn")) { Game.runATSMP(); } } System.Threading.Thread.Sleep(6000); if (!Launcher.working) { TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Visible = false)); } }); } catch (WebException) { Console.WriteLine("An error occured downloading files from TruckersMP. Unable to update!"); Launcher.working = false; return; } }
public static void integrityCheck(CProgressBar TruckersMPUpdateProgress, Label TruckersMPUpdateProgressLabel, bool runGame = false, String game = "") { JArray liveFiles = new JArray(); Dictionary <string, string> localFiles = new Dictionary <string, string>(); List <string> mismatchedFiles = new List <string>(); Launcher.working = true; //Get files from TMP using (WebClient client = new WebClient()) { try { JObject requestData = JObject.Parse(client.DownloadString("http://update.ets2mp.com/files.json")); liveFiles = (JArray)requestData["Files"]; } catch (WebException) { Console.WriteLine("Unable to connect to TruckersMP Update API. Cannot check TMP integrity!"); if (runGame) { if (game == "ETS2MP" || game.Equals("play_ets2mp_btn")) { Game.runETS2MP(); } else if (game == "ATSMP" || game.Equals("play_atsmp_btn")) { Game.runATSMP(); } } return; } } int step = 0; TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Visible = true)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Visible = true)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "Checking mod integrity...")); //Get local files try { var files = System.IO.Directory.GetFiles(Launcher.TruckersMPLocation, "*.*", System.IO.SearchOption.AllDirectories); TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Maximum = (files.Length))); foreach (var file in files) { FileInfo info = new FileInfo(file); string key = info.FullName; var checksum = MD5(key); key = key.Replace(Launcher.TruckersMPLocation, ""); localFiles.Add(key, checksum); step = step + 1; TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Value = step)); } } catch { Console.WriteLine("Unable to load local files. Cannot check TMP integrity!"); if (runGame) { if (game == "ETS2MP" || game.Equals("play_ets2mp_btn")) { Game.runETS2MP(); } else if (game == "ATSMP" || game.Equals("play_atsmp_btn")) { Game.runATSMP(); } } return; } //Compare results try { foreach (var file in liveFiles) { string filePath = ((string)file["FilePath"]).Replace("/", "\\"); if (!localFiles.ContainsKey(filePath)) { mismatchedFiles.Add(filePath); continue; } string localHash = localFiles[filePath]; if (!(filePath.Contains("ui") || filePath.Contains("fonts"))) { if ((string)file["Md5"] != localHash) { mismatchedFiles.Add(filePath); } continue; } else { string[] s = filePath.Split('.'); string backupFile = s[0] + "_backup." + s[1]; if (File.Exists(Launcher.TruckersMPLocation + backupFile)) { string backupHash = MD5(Launcher.TruckersMPLocation + backupFile); if ((string)file["Md5"] != backupHash) { mismatchedFiles.Add(backupFile); } continue; } else { if ((string)file["Md5"] != localHash) { mismatchedFiles.Add(filePath); } continue; } } } } catch { Console.WriteLine("An error occured comparing files. Cannot check TMP integrity!"); if (runGame) { if (game == "ETS2MP" || game.Equals("play_ets2mp_btn")) { Game.runETS2MP(); } else if (game == "ATSMP" || game.Equals("play_atsmp_btn")) { Game.runATSMP(); } } return; } if (mismatchedFiles.Count != 0) { DialogResult dialogResult = MessageBox.Show("Your install of TruckersMP is outdated, has modified files or is missing files.\n\nDo you want to update/redownload them?\n(Required to start multiplayer)", "TruckersMP Launcher", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { TruckersMP.update(TruckersMPUpdateProgress, TruckersMPUpdateProgressLabel, mismatchedFiles, runGame, game); return; } } else { if (runGame) { if (game == "ETS2MP" || game.Equals("play_ets2mp_btn")) { Game.runETS2MP(); } else if (game == "ATSMP" || game.Equals("play_atsmp_btn")) { Game.runATSMP(); } } } Launcher.working = false; TruckersMPUpdateProgress.Invoke((MethodInvoker)(() => TruckersMPUpdateProgress.Visible = false)); TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Text = "Integrity check done!")); System.Threading.Thread.Sleep(6000); if (!Launcher.working) { TruckersMPUpdateProgressLabel.Invoke((MethodInvoker)(() => TruckersMPUpdateProgressLabel.Visible = false)); } }