using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Net; using System.IO; namespace GamerServices { public class Downloader { public class DownloadInfo { public string Path; public string URL; public string Version; public int GameID; public int TotalActions; public byte Action; } public FormMain Main; public List FilesToDownload; public System.Threading.Thread DownloadThread; private static System.Net.WebClient DownloadClient; public Downloader(FormMain main) { Main = main; FilesToDownload = new List(); if (DownloadThread != null && DownloadThread.IsAlive) DownloadThread.Abort(); DownloadThread = new System.Threading.Thread(new System.Threading.ThreadStart(Update)); DownloadThread.Start(); } private static int CurrentGameUpdateing = -1; private void Update() { DownloadClient = new WebClient(); DownloadClient.Proxy = null; DownloadClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFinished); DownloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadBusy); while (true) { if (Main.Games != null && Main.Games.Length > 0) { lock(Main.Games){ foreach (Game g in Main.Games) { if (g != null) { if (g.Status == Game.GameStatus.DOWNLOADING) { bool isdownloading = false; lock (FilesToDownload) { for (int i = 0; i < FilesToDownload.Count; i++) { if (FilesToDownload[i].GameID == g.GameID) { isdownloading = true; break; } } } if (!isdownloading) { Main.Popup("Download finished!", g.Name, 0); g.Status = Game.GameStatus.INSTALLED; Main.SetGameData(g.GameID, g.Name, g.Status); } } } } } } if (FilesToDownload.Count > 0 && CurrentGameUpdateing == -1) { DownloadInfo info = null; lock (FilesToDownload) { info = FilesToDownload[0]; try { CurrentGameUpdateing = -1; switch (info.Action) { case 0: if (File.Exists("Games/" + info.GameID + "/" + info.Path)) File.Delete("Games/" + info.GameID + "/" + info.Path); DownloadClient.DownloadFileAsync(new Uri(info.URL.Replace("\\", "/")), "Games/" + info.GameID + "/" + info.Path, null); CurrentGameUpdateing = info.GameID; break; case 1: if (File.Exists("Games/" + info.GameID + "/" + info.Path)) File.Delete("Games/" + info.GameID + "/" + info.Path); File.Create("Games/" + info.GameID + "/" + info.Path); UpdateText((short)info.GameID, ""); SafeDownloadList(true); break; case 2: if (Directory.Exists("Games/" + info.GameID + "/" + info.Path)) Directory.Delete("Games/" + info.GameID + "/" + info.Path, true); if (File.Exists("Games/" + info.GameID + "/" + info.Path)) File.Delete("Games/" + info.GameID + "/" + info.Path); UpdateText((short)info.GameID, ""); SafeDownloadList(true); break; case 3: if (Directory.Exists("Games/" + info.GameID + "/" + info.Path)) Directory.Delete("Games/" + info.GameID + "/" + info.Path, true); Directory.CreateDirectory("Games/" + info.GameID + "/" + info.Path); UpdateText((short)info.GameID, ""); SafeDownloadList(true); break; } } catch(Exception e) { CurrentGameUpdateing = -1; SafeDownloadList(true); } } } System.Threading.Thread.Sleep(200); } } public void SafeDownloadList(bool del){ if (del && FilesToDownload.Count > 0) { lock(FilesToDownload){ FilesToDownload.RemoveAt(0); } } if (FilesToDownload.Count > 0) { string data = ""; DownloadInfo[] sort = null; lock (FilesToDownload) { sort = FilesToDownload.OrderBy(DownloadInfo => DownloadInfo.GameID).ToArray(); } short lastID = -1; foreach (DownloadInfo info in sort) { if (info.GameID != lastID) { data += "}\n\n" + info.GameID + " " + info.Version + "\n{\n"; lastID = (short)info.GameID; } data += "\t" + info.Path + "\n"; data += "\t" + info.Action + "\n"; } if (data.Length > 0) { data = data.Substring(3) + (lastID != -1 ? "}" : ""); try { File.WriteAllText("downloading.txt", data); } catch { } } } else { try { File.WriteAllText("downloading.txt", ""); } catch { } } } private void UpdateText(short gameID, string add) { if (Main.Games == null || Main.Games.Length == 0) return; lock (Main.Games) { int res = 0; int totalfiles = 0; lock (FilesToDownload) { for (int i = 0; i < FilesToDownload.Count; i++) { if (FilesToDownload[i].GameID == gameID) { res++; if (FilesToDownload[i].TotalActions > totalfiles) totalfiles = FilesToDownload[i].TotalActions; } } } Game g = Main.GetGame(gameID); if (g != null) { g.Status = res > 0 ? Game.GameStatus.DOWNLOADING : Game.GameStatus.INSTALLED; if (res > 0) { double per = 100.0 - ((double)res / (double)totalfiles * 100.0); per = per - per % 0.1; Main.SetGameData(gameID, g.Name + " " + per + "%" + add, g.Status); } else { Main.Popup("Download finished!", g.Name, 0); Main.SetGameData(gameID, g.Name, g.Status); } } else { } } } private void DownloadFinished(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { UpdateText((short)CurrentGameUpdateing, ""); SafeDownloadList(true); for (int i = 0; i < FilesToDownload.Count; i++) { if (FilesToDownload[i].GameID == CurrentGameUpdateing) { CurrentGameUpdateing = -1; return; } } Game g = Main.GetGame((short)CurrentGameUpdateing); if (g != null) { Main.Popup("Download finished!", g.Name, 0); g.Status = Game.GameStatus.INSTALLED; Main.SetGameData(g.GameID, g.Name, g.Status); CurrentGameUpdateing = -1; } } private void DownloadBusy(object sender, DownloadProgressChangedEventArgs e) { UpdateText((short)CurrentGameUpdateing, " " + e.ProgressPercentage.ToString() + "%"); } public bool UpdateParser(int GameID, string curver){ System.Net.WebClient client = new WebClient(); client.Proxy = null; string[] FileData = null; int actions = 0; try { FileData = new StreamReader(client.OpenRead("http://" + "gs.mrag.nl/download.php?gid=" + GameID + "&file=updates.txt")).ReadToEnd().Replace("\r", "").Split('\n'); } catch(Exception e) { return false; } lock (FilesToDownload) { string currenthandelingrev = ""; List filesdone = new List(); foreach (string line in FileData){ if (line.Length > 0) { string[] args = null; if (line.Contains("=")) args = line.Split('='); if (line.TrimStart('\t', ' ').StartsWith("#")){ }else if (args[0] == "VER"){ if (currenthandelingrev.Length == 0){ currenthandelingrev = args[1]; File.WriteAllText("Games/" + GameID + "/version.txt", currenthandelingrev); Game g = Main.GetGame((short)GameID); if (g != null) g.Version = currenthandelingrev; } if (args[1] == curver) { break; } }else{ if (!filesdone.Contains(args[1])) { filesdone.Add(args[1]); DownloadInfo info = new DownloadInfo(); info.Action = 0; info.GameID = GameID; info.URL = "http://" + "gs.mrag.nl/download.php?gid=" + GameID + "&file=" + args[1]; info.Path = args[1]; info.Version = currenthandelingrev; switch (args[0]) { case "ADD": info.Action = 0; break; case "MNF": info.Action = 1; break; case "DEL": info.Action = 2; break; case "MDR": info.Action = 3; break; } FilesToDownload.Add(info); actions++; } } } } for (int i = FilesToDownload.Count - 1; i > 0; i--) { if (FilesToDownload[i].GameID == GameID) { Game g = Main.GetGame((short)GameID); if (g != null) g.Status = Game.GameStatus.DOWNLOADING; FilesToDownload[i].TotalActions += actions; } } for (int i = 0; i < FilesToDownload.Count; i++) { System.Diagnostics.Debug.Print(FilesToDownload[i].Action + " = " + FilesToDownload[i].Path); } } return true; } } }