public GameVersion GetVersionFromFile() { GameVersion result; if (!this.VersionFileExist()) { result = new GameVersion { Version = BaseVersion }; using (var stream = File.CreateText(_basePathToVersionFile)) { var jsonToWrite = JsonConvert.SerializeObject(result); stream.Write(jsonToWrite); } return result; } else { result = new GameVersion { Version = BaseVersion }; var jsonToRead = File.ReadAllText(_basePathToVersionFile); result = JsonConvert.DeserializeObject<GameVersion>(jsonToRead); } return result; }
public GameVersion UpdateVersionInFile(GameVersion gameVersion) { GameVersion result; var jsonToWrite = JsonConvert.SerializeObject(gameVersion); File.WriteAllText(_basePathToVersionFile, jsonToWrite); var jsonToRead = File.ReadAllText(_basePathToVersionFile); result = JsonConvert.DeserializeObject<GameVersion>(jsonToRead); return result; }
public bool DownloadFileFromServer(GameVersion gameVersion) { this.SetupCurrentCulture(); var fileToDownload = $"my-game-patch-{gameVersion.Version}.zip"; var locationOfFileToSave = Path.Combine(_basePath, fileToDownload); var restRequest = new RestRequest(fileToDownload, Method.GET); _restClient.DownloadData(restRequest).SaveAs(locationOfFileToSave); return File.Exists(locationOfFileToSave); }
public void UnzipDownloadedFiles(GameVersion gameVersion) { this.SetupCurrentCulture(); var fileToUnzip = $"my-game-patch-{gameVersion.Version}.zip"; var locationOfUnzip = Path.Combine(_basePath, fileToUnzip); using (var streamToUnzip = File.OpenRead(locationOfUnzip)) { using (var zipInputStream = new ZipInputStream(streamToUnzip)) { var zipEntry = zipInputStream.GetNextEntry(); while (zipEntry != null) { var entryFileName = zipEntry.Name; byte[] buffer = new byte[4096]; var fullZipToPath = Path.Combine(_basePath, entryFileName).ToLower(); var directoryName = Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } if (!fullZipToPath.Contains(".txt") && !fullZipToPath.Contains(".exe") && !fullZipToPath.Contains(".cfg")) { zipEntry = zipInputStream.GetNextEntry(); } else { using (var streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipInputStream, streamWriter, buffer); } zipEntry = zipInputStream.GetNextEntry(); } } } } }