using System; using System.Linq; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Threading; using System.Windows; using System.Net; namespace Launcher { public partial class SplashWindow : Window { private WebClient ServerClient = new WebClient(); private BackgroundWorker UpdateWorker; public SplashWindow() { InitializeComponent(); } private void Window_Initialized(Object Sender, EventArgs Args) { if (!Directory.Exists(String.Format("{0}Temp\\", AppDomain.CurrentDomain.BaseDirectory))) { Directory.CreateDirectory(String.Format("{0}Temp\\", AppDomain.CurrentDomain.BaseDirectory)); } if (!Directory.Exists(String.Format("{0}Launcher\\", AppDomain.CurrentDomain.BaseDirectory))) { Directory.CreateDirectory(String.Format("{0}Launcher\\", AppDomain.CurrentDomain.BaseDirectory)); } try { String Response = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=Ping", App.ServerIP)); if (Response != "Pong") { MessageBox.Show("The server sent an invalid ping response.", " Launcher", MessageBoxButton.OK); Environment.Exit(0); } } catch { MessageBox.Show("Connection to Patch Server has Failed. Please try again later.", " Launcher", MessageBoxButton.OK); Environment.Exit(0); } App.LauncherTitle = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherTitle", App.ServerIP, 0)); Title = String.Format("{0} - Splash screen", App.LauncherTitle); SetupUpdateWorker(); } private void SetupUpdateWorker() { UpdateWorker = new BackgroundWorker(); UpdateWorker.DoWork += UpdateWorker_DoWork; UpdateWorker.RunWorkerCompleted += UpdateWorker_RunWorkerCompleted; UpdateWorker.RunWorkerAsync(); } private void UpdateWorker_DoWork(Object Sender, DoWorkEventArgs Args) { if (Environment.CommandLine.Contains("a7fb7e0f-067f-45f7-9d25-48b9828a2f76")) { Process[] RunningLaunchers = Process.GetProcessesByName("Launcher").Where(ID => ID.Id != AppDomain.CurrentDomain.Id).ToArray(); foreach (Process RunningLauncher in RunningLaunchers) { RunningLauncher.Kill(); } Thread.Sleep(3000); File.Delete(String.Format("{0}\\Launcher.exe", AppDomain.CurrentDomain.BaseDirectory)); File.Copy(String.Format("{0}\\Update.exe", AppDomain.CurrentDomain.BaseDirectory), String.Format("{0}\\Launcher.exe", AppDomain.CurrentDomain.BaseDirectory)); Process LauncherProcess = new Process(); LauncherProcess.StartInfo.Arguments = "fe81a4d3-a4cd-4ff5-8db7-e6aa799e88bf"; LauncherProcess.StartInfo.FileName = String.Format("{0}\\Launcher.exe", AppDomain.CurrentDomain.BaseDirectory); LauncherProcess.Start(); Process.GetCurrentProcess().Kill(); } else if (!Environment.CommandLine.Contains("fe81a4d3-a4cd-4ff5-8db7-e6aa799e88bf")) { Process[] RunningLaunchers = Process.GetProcessesByName("Update").Where(ID => ID.Id != AppDomain.CurrentDomain.Id).ToArray(); foreach (Process RunningLauncher in RunningLaunchers) { RunningLauncher.Kill(); } if (File.Exists(String.Format("{0}\\Update.exe", AppDomain.CurrentDomain.BaseDirectory))) { File.Delete(String.Format("{0}\\Update.exe", AppDomain.CurrentDomain.BaseDirectory)); } String LauncherSIPURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherSIPURL", App.ServerIP, 0)); String LauncherUURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherUURL", App.ServerIP, 0)); DownloadFile(LauncherSIPURL.Replace(String.Concat("/", Path.GetFileName(LauncherSIPURL)), ""), Path.GetFileName(LauncherSIPURL), true); DownloadFile(LauncherUURL.Replace(String.Concat("/", Path.GetFileName(LauncherUURL)), ""), Path.GetFileName(LauncherUURL), true); Process StartNewUpdate = new Process(); StartNewUpdate.StartInfo.Arguments = "a7fb7e0f-067f-45f7-9d25-48b9828a2f76"; StartNewUpdate.StartInfo.FileName = String.Format("{0}\\Update.exe", AppDomain.CurrentDomain.BaseDirectory); StartNewUpdate.Start(); Process.GetCurrentProcess().Kill(); } else { String LauncherVGURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherVGURL", App.ServerIP, 0)); String LauncherMGURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherMGURL", App.ServerIP, 0)); String VideoGUID = ServerClient.DownloadString(LauncherVGURL); String MusicGUID = ServerClient.DownloadString(LauncherMGURL); if (!File.Exists(String.Format("{0}Launcher\\VideoGUID.txt", AppDomain.CurrentDomain.BaseDirectory))) { File.Delete(String.Format("{0}Video.mp4", AppDomain.CurrentDomain.BaseDirectory)); File.WriteAllLines(String.Format("{0}Launcher\\VideoGUID.txt", AppDomain.CurrentDomain.BaseDirectory), new String[] { VideoGUID }); } else if (File.ReadAllLines(String.Format("{0}Launcher\\VideoGUID.txt", AppDomain.CurrentDomain.BaseDirectory))[0] != VideoGUID) { File.Delete(String.Format("{0}Video.mp4", AppDomain.CurrentDomain.BaseDirectory)); } if (!File.Exists(String.Format("{0}Launcher\\MusicGUID.txt", AppDomain.CurrentDomain.BaseDirectory))) { File.Delete(String.Format("{0}Music.mp3", AppDomain.CurrentDomain.BaseDirectory)); File.WriteAllLines(String.Format("{0}Launcher\\MusicGUID.txt", AppDomain.CurrentDomain.BaseDirectory), new String[] { MusicGUID }); } else if (File.ReadAllLines(String.Format("{0}Launcher\\MusicGUID.txt", AppDomain.CurrentDomain.BaseDirectory))[0] != VideoGUID) { File.Delete(String.Format("{0}Music.mp3", AppDomain.CurrentDomain.BaseDirectory)); } String LauncherVURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherVURL", App.ServerIP, 0)); String LauncherMURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=LauncherMURL", App.ServerIP, 0)); if (!File.Exists(String.Format("{0}Video.mp4", AppDomain.CurrentDomain.BaseDirectory))) { DownloadFile(LauncherVURL.Replace(String.Concat("/", Path.GetFileName(LauncherVURL)), ""), Path.GetFileName(LauncherVURL), true); } if (!File.Exists(String.Format("{0}Music.mp3", AppDomain.CurrentDomain.BaseDirectory))) { DownloadFile(LauncherMURL.Replace(String.Concat("/", Path.GetFileName(LauncherMURL)), ""), Path.GetFileName(LauncherMURL), true); } } } private void DownloadFile(String StartPath, String ClientFile, Boolean Move) { if (!File.Exists(String.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, ClientFile))) { if (File.Exists(String.Format("{0}Temp\\{1}", AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ClientFile)))) { File.Delete(String.Format("{0}Temp\\{1}", AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ClientFile))); } Download FileDownloader = new Download(); FileDownloader.Address = new Uri(String.Format("{0}/{1}", StartPath, ClientFile)); FileDownloader.FileName = Path.GetFileNameWithoutExtension(ClientFile); FileDownloader.SavePath = String.Format("{0}Temp\\{1}", AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ClientFile)); FileDownloader.StartDownload(); while (!FileDownloader.HasErrored && !FileDownloader.IsFinished) { Thread.Sleep(1000); } if (FileDownloader.HasErrored) { Thread.Sleep(5000); DownloadFile(StartPath, ClientFile, Move); } if (FileDownloader.IsFinished) { if (!Directory.Exists(String.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, ClientFile))) { Directory.CreateDirectory(String.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, ClientFile.Replace(Path.GetFileName(ClientFile), ""))); } if (Move) { File.Move(String.Format("{0}Temp\\{1}", AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ClientFile)), String.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, ClientFile)); } } } else { File.Delete(String.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, ClientFile)); DownloadFile(StartPath, ClientFile, Move); } } private void UpdateWorker_RunWorkerCompleted(Object Sender, RunWorkerCompletedEventArgs Args) { LoginWindow LS = new LoginWindow(); LS.Show(); Dispatcher.BeginInvoke((Action)(() => { Close(); })); } } }