using System; using System.IO; using System.Net; using System.Windows; using System.Threading; using System.ComponentModel; using System.Diagnostics; namespace Launcher { public partial class PatchWindow : Window { private Int32 ServerID; private BackgroundWorker PatchWorker; public PatchWindow(Byte PatchServerID) { ServerID = PatchServerID; InitializeComponent(); SetupPatchWorker(); } private void SetupPatchWorker() { PatchWorker = new BackgroundWorker(); PatchWorker.DoWork += PatchWorker_DoWork; PatchWorker.RunWorkerCompleted += PatchWorker_RunWorkerCompleted; PatchWorker.RunWorkerAsync(); } private void PatchWorker_DoWork(Object Sender, DoWorkEventArgs Args) { WebClient ServerClient = new WebClient(); String ServerName = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=ServerName", App.ServerIP, ServerID)); String FolderName = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=FolderName", App.ServerIP, ServerID)); String VersionURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=VersionURL", App.ServerIP, ServerID)); String PatchesURL = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=PatchesURL", App.ServerIP, ServerID)); String VersionFile = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=VersionFile", App.ServerIP, ServerID)); String StartupParameter = ServerClient.DownloadString(String.Format("http://{0}:10002?Type=InformationRequest&ServerID={1}&InformationType=StartupParameter", App.ServerIP, ServerID)); if (!File.Exists(String.Format("{0}Launcher\\{1}", AppDomain.CurrentDomain.BaseDirectory, VersionFile))) { File.WriteAllLines(String.Format("{0}Launcher\\{1}", AppDomain.CurrentDomain.BaseDirectory, VersionFile), new String[] { "0" }); } Dispatcher.BeginInvoke((Action)(() => { Title = String.Format("{0} - {1} patcher", App.LauncherTitle, ServerName); })); Int32 ClientVersion = Convert.ToInt32(File.ReadAllLines(String.Format("{0}Launcher\\{1}", AppDomain.CurrentDomain.BaseDirectory, VersionFile))[0]); Int32 ServerVersion = Convert.ToInt32(ServerClient.DownloadString(VersionURL)); while (ClientVersion != ServerVersion) { Dispatcher.BeginInvoke((Action)(() => { Patches_Text.Content = String.Format("Working on patch ({0}/{1})", ClientVersion + 1, ServerVersion); Patches_Progress.Maximum = ServerVersion; Patches_Progress.Value = ClientVersion + 1; })); DownloadFile(PatchesURL, String.Format("Patch{0}.rar", ClientVersion + 1), false); Dispatcher.BeginInvoke((Action)(() => { Download_Text.Content = "Waiting for patch to extract..."; Download_Progress.Maximum = 100; Download_Progress.Value = 100; })); Extract PatchExtract = new Extract(); PatchExtract.FileName = String.Format("Patch{0}", ClientVersion + 1); PatchExtract.FilePath = String.Format("{0}Temp\\Patch{1}.rar", AppDomain.CurrentDomain.BaseDirectory, ClientVersion + 1); PatchExtract.SaveFolder = String.Format("{0}{1}\\", AppDomain.CurrentDomain.BaseDirectory, FolderName); PatchExtract.Progress = Extract_Progress; PatchExtract.Information = Extract_Text; PatchExtract.StartExtract(); Dispatcher.BeginInvoke((Action)(() => { Extract_Text.Content = "Waiting for download to complete..."; Extract_Progress.Maximum = 100; Extract_Progress.Value = 100; })); ClientVersion = ClientVersion + 1; } File.Delete(String.Format("{0}Launcher\\{1}", AppDomain.CurrentDomain.BaseDirectory, VersionFile)); using (var Writer = new StreamWriter(String.Format("{0}Launcher\\{1}", AppDomain.CurrentDomain.BaseDirectory, VersionFile))) { Writer.WriteLine(ClientVersion); Writer.Flush(); Writer.Close(); } Dispatcher.BeginInvoke((Action)(() => { Hide(); })); Boolean UseArguments = false; try { if (File.Exists("C:\\Token.txt")) { File.Delete("C:\\Token.txt"); } File.WriteAllLines("C:\\Token.txt", new String[] { App.Token }); } catch { UseArguments = true; } Process StartGame = new Process(); if (UseArguments) { StartGame.StartInfo.Arguments = StartupParameter.Replace("OSKTOKEN", App.Token); } else { StartGame.StartInfo.Arguments = StartupParameter.Replace(" -osk_token OSKTOKEN", ""); } StartGame.StartInfo.FileName = String.Format("{0}{1}\\Client.exe", AppDomain.CurrentDomain.BaseDirectory, FolderName); StartGame.Start(); StartGame.WaitForInputIdle(); Dispatcher.BeginInvoke((Action)(() => { Close(); })); } private void PatchWorker_RunWorkerCompleted(Object Sender, RunWorkerCompletedEventArgs Args) { } 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.Information = Download_Text; FileDownloader.Progress = Download_Progress; 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) { Dispatcher.BeginInvoke((Action)(() => { Download_Text.Content = String.Format("Error occured while getting {0}, attempting to restart in 5 seconds.", Path.GetFileName(ClientFile)); })); 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); } } } }