private void UpdateLocalGameInfoDisplays(string selectedGameName) { if (selectedGameName == null) { localSaveTimestampTextBlock.Text = ""; installDirTextBlock.Text = ""; return; } string timestampMessage = null; string installDir = null; try { installDir = savegameSync.GetLocalInstallDir(selectedGameName); SaveSpec saveSpec = SaveSpecRepository.GetRepository().GetSaveSpec(selectedGameName); if (!Directory.Exists(installDir)) { timestampMessage = "Error: install dir does not exist"; } else { DateTime timestamp = savegameSync.GetLocalSaveTimestamp(saveSpec, installDir); timestampMessage = timestamp.ToString(); } } catch (SavegameSyncException e) { timestampMessage = "Error: " + e.Message; } localSaveTimestampTextBlock.Text = timestampMessage; installDirTextBlock.Text = installDir; }
public async Task ZipAndUploadSave(string gameName) { // Copy save files from the game's install directory into a temp directory according // to the spec string installDir = localGameList.GetInstallDir(gameName); SaveSpec saveSpec = SaveSpecRepository.GetRepository().GetSaveSpec(gameName); string destDir = Path.Combine(TempDir, "saveToUpload"); FileUtils.DeleteIfExists(destDir); Directory.CreateDirectory(destDir); CopySaveFilesFromInstallDir(saveSpec, installDir, destDir); Debug.WriteLine("Dirs: " + installDir + " " + destDir); // Find the last write time of the save DateTime latestFileWriteTime = GetLocalSaveTimestamp(saveSpec, installDir); Debug.WriteLine("Latest write time: " + latestFileWriteTime); // Assign the save a guid and make it into a zip file Guid saveGuid = Guid.NewGuid(); Debug.WriteLine("Guid: " + saveGuid); string zipFile = Path.Combine(TempDir, SavegameSyncUtils.GetSavegameFileNameFromGuid(saveGuid)); FileUtils.DeleteIfExists(zipFile); ZipFile.CreateFromDirectory(destDir, zipFile); // Upload save string remoteFileName = SavegameSyncUtils.GetSavegameFileNameFromGuid(saveGuid); string fileId = await googleDriveWrapper.CreateFileAsync(remoteFileName); using (FileStream fileStream = File.OpenRead(zipFile)) { await googleDriveWrapper.UploadFileAsync(fileId, fileStream); } // Download latest version of SavegameList await ReadSavegameList(); // Add save to SavegameList savegameList.AddSave(gameName, saveGuid, latestFileWriteTime); // Upload SavegameList await WriteSavegameList(); CleanUpTempFiles(); }
public async Task DownloadAndUnzipSave(string gameName, int saveIndex) { // Download latest version of SavegameList await ReadSavegameList(); // Read file name from SavegameList List <SavegameEntry> saves = savegameList.ReadSaves(gameName); SavegameEntry save = saves[saveIndex]; Guid saveGuid = save.Guid; string saveFileName = SavegameSyncUtils.GetSavegameFileNameFromGuid(saveGuid); Debug.WriteLine("Downloading save file " + saveFileName + " with index " + saveIndex + " and timestamp " + save.Timestamp); // Download zipped save from Google Drive var files = await googleDriveWrapper.SearchFileByNameAsync(saveFileName); Debug.Assert(files.Count == 1); string saveFileId = files[0].Id; string zipFilePath = Path.Combine(TempDir, saveFileName); Directory.CreateDirectory(TempDir); using (FileStream fileStream = File.OpenWrite(zipFilePath)) { await googleDriveWrapper.DownloadFileAsync(saveFileId, fileStream); } // Unzip zipped save string tempSaveDir = Path.Combine(TempDir, "downloadedSave"); FileUtils.DeleteIfExists(tempSaveDir); ZipFile.ExtractToDirectory(zipFilePath, tempSaveDir); // Copy unzipped files/directories into game install directory string installDir = localGameList.GetInstallDir(gameName); SaveSpec saveSpec = SaveSpecRepository.GetRepository().GetSaveSpec(gameName); CopySaveFilesIntoInstallDir(saveSpec, tempSaveDir, installDir); CleanUpTempFiles(); }
private void ParseFromXmlFile(string xmlFileName) { XmlReader reader; try { reader = XmlReader.Create(xmlFileName); } catch (FileNotFoundException) { throw new SaveSpecRepositoryMissingException(); } reader.ReadToNextSibling("saveSpecRepository"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "saveSpec") { SaveSpec saveSpec = ParseSaveSpec(reader.ReadSubtree()); saveSpecs.Add(saveSpec.GameName, saveSpec); } } }
/// <summary> /// Copy the files and directories named in a SaveSpec from a source directory into a /// game's install directory, first deleting the existing copies of those items from the /// install directory. /// </summary> /// <remarks> /// Note that if an item named in the SaveSpec is present in the install directory but not /// in the source directory, this method will delete that item from the install directory. /// </remarks> private void CopySaveFilesIntoInstallDir(SaveSpec saveSpec, string sourceDir, string installDir) { foreach (string subPath in saveSpec.SavePaths) { string sourcePath = Path.Combine(sourceDir, subPath); string destPath = Path.Combine(installDir, subPath); FileUtils.DeleteIfExists(destPath); if (Directory.Exists(sourcePath)) { FileUtils.CopyDirectory(sourcePath, destPath); } else if (File.Exists(sourcePath)) { File.Copy(sourcePath, destPath); } else { Debug.WriteLine("Skipping missing subpath " + subPath + " while copying save files for " + saveSpec.GameName + " into install dir"); } } }
public DateTime GetLocalSaveTimestamp(SaveSpec saveSpec, string installDir) { DateTime timestamp = new DateTime(0); foreach (string subPath in saveSpec.SavePaths) { DateTime subPathTimestamp = new DateTime(0); string fullSubPath = Path.Combine(installDir, subPath); if (Directory.Exists(fullSubPath)) { subPathTimestamp = FileUtils.GetLatestFileWriteTime(fullSubPath); } else if (File.Exists(fullSubPath)) { subPathTimestamp = new FileInfo(fullSubPath).LastWriteTimeUtc; } if (subPathTimestamp > timestamp) { timestamp = subPathTimestamp; } } return(timestamp); }
private void CopySaveFilesFromInstallDir(SaveSpec saveSpec, string installDir, string destDir) { FileUtils.DeleteIfExists(destDir); Directory.CreateDirectory(destDir); foreach (string subPath in saveSpec.SavePaths) { string originalPath = System.IO.Path.Combine(installDir, subPath); string destPath = System.IO.Path.Combine(destDir, subPath); if (Directory.Exists(originalPath)) { FileUtils.CopyDirectory(originalPath, destPath); } else if (File.Exists(originalPath)) { File.Copy(originalPath, destPath); } else { Debug.WriteLine("Skipping missing subpath " + subPath + " while copying save files for " + saveSpec.GameName + " out of install dir"); } } }