public bool CheckForUpdates(bool forceCheck = false) { if (_needUpdates.Count == 0 || forceCheck == true) { try { using var client = new WebClient(); using var stream = client.OpenRead("http://google.com/generate_204");// check connection (maybe pointless?) var branch = "master"; //var files = new string[5] { "SmartHunter/Game/Config/MemoryConfig.cs", "SmartHunter/Game/Config/PlayerDataConfig.cs", "SmartHunter/Game/Config/MonsterDataConfig.cs", "SmartHunter/Game/Config/LocalizationConfig.cs", "SmartHunter/bin/Debug/SmartHunter.exe" }; UpdateNode[] nodes = new UpdateNode[5] { new UpdateNode("", "SmartHunter/Game/Config/MemoryConfig.cs", "Memory.json", "", false), new UpdateNode("", "SmartHunter/Game/Config/PlayerDataConfig.cs", "PlayerData.json", "", false), new UpdateNode("", "SmartHunter/Game/Config/MonsterDataConfig.cs", "MonsterData.json", "", false), new UpdateNode("", "SmartHunter/Game/Config/LocalizationConfig.cs", "en-US.json", "", false), new UpdateNode("", "SmartHunter/bin/Debug/SmartHunter.exe", "SmartHunter.exe", "", true) }; foreach (UpdateNode node in nodes) { string apiUrl = $"{_apiEndpoint}{node.FilePath}"; client.Dispose(); client.Headers["User-Agent"] = _dummyUserAgent; string apiResponseStr = client.DownloadString(apiUrl); JArray json = JArray.Parse(apiResponseStr); if (json.Count > 0) { JObject lastCommit = (JObject)json[0]; string hash = (string)lastCommit["sha"]; if (!ConfigHelper.Versions.Values.GetType().GetField(Path.GetFileNameWithoutExtension(node.FilePath)).GetValue(ConfigHelper.Versions.Values).Equals(hash)) { Log.WriteLine($"Found a new version of '{Path.GetFileName(node.FileName)}'"); node.Hash = hash; if (node.NeedDownload) { node.DownloadUrl = $"{_apiRaw}/{branch}/{node.FilePath}"; } _needUpdates.Add(node); } } } } catch { Log.WriteLine($"An error has occured while searching for updates... Resuming the normal flow of the application!"); return(false); } } return(_needUpdates.Count > 0); }
public bool DownloadUpdates() { try { using var client = new WebClient(); while (_needUpdates.Count > 0) { UpdateNode node = _needUpdates.First(); string hash = node.Hash; string filePath = node.FilePath; string fileName = node.FileName; string fileNamePath = Path.GetFileName(filePath); string fileNamePathWithNoExtension = Path.GetFileNameWithoutExtension(filePath); if (node.NeedDownload) { string url = node.DownloadUrl; Log.WriteLine($"Downloading file '{fileNamePath}'"); client.Dispose(); client.Headers["User-Agent"] = _dummyUserAgent; if (Path.GetExtension(fileNamePath).Equals(".exe")) { client.DownloadFile(url, $"{fileNamePathWithNoExtension}_NEW.exe"); } else { File.Delete(fileName); client.DownloadFile(url, fileNamePath); } } else { Log.WriteLine($"Deleting file '{fileName}'"); File.Delete(fileName); } ConfigHelper.Versions.Values.GetType().GetField(fileNamePathWithNoExtension).SetValue(ConfigHelper.Versions.Values, hash); ConfigHelper.Versions.Save(); _needUpdates.Remove(node); } } catch { return(false); } return(true); }
public bool DownloadUpdates() { try { using (var client = new WebClient()) { while (NeedUpdates.Count > 0) { UpdateNode node = NeedUpdates.First(); string hash = node.hash; string name = node.fileName; string url = node.downloadUrl; string nameWithNoExtension = Path.GetFileNameWithoutExtension(name); Log.WriteLine($"Downloading file '{name}'"); client.Dispose(); client.Headers["User-Agent"] = dummyUserAgent; if (Path.GetExtension(name).Equals(".exe")) { client.DownloadFile(url, $"{nameWithNoExtension}_{hash}.exe"); } else { client.DownloadFile(url, name); } ConfigHelper.Versions.Values.GetType().GetField(nameWithNoExtension).SetValue(ConfigHelper.Versions.Values, hash); ConfigHelper.Versions.Save(); NeedUpdates.Remove(node); } } } catch { return(false); } return(true); }