Ejemplo n.º 1
0
        public async Task <Dictionary <string, List <SavegameEntry> > > GetMissingSaveEntriesAsync()
        {
            Dictionary <string, List <SavegameEntry> > result = new Dictionary <string, List <SavegameEntry> >();

            await ReadSavegameList();

            foreach (string gameName in savegameList.GetGames())
            {
                List <SavegameEntry> saves = savegameList.ReadSaves(gameName);
                foreach (SavegameEntry save in saves)
                {
                    string saveFileName = SavegameSyncUtils.GetSavegameFileNameFromGuid(save.Guid);
                    List <Google.Apis.Drive.v3.Data.File> files = await googleDriveWrapper.SearchFileByNameAsync(saveFileName);

                    Debug.Assert(files.Count <= 1);
                    if (files.Count == 0)
                    {
                        if (!result.ContainsKey(gameName))
                        {
                            result.Add(gameName, new List <SavegameEntry>());
                        }
                        result[gameName].Add(save);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task DownloadSpecificSaveFileAsync(string gameName, int saveIndex)
        {
            await ReadSavegameList();

            List <SavegameEntry> saves = savegameList.ReadSaves(gameName);
            SavegameEntry        save  = saves[saveIndex];
            string saveFileName        = SavegameSyncUtils.GetSavegameFileNameFromGuid(save.Guid);

            string outputPath = GetSpecificSaveFileDownloadPath(gameName, saveIndex);

            FileUtils.DeleteIfExists(outputPath);
            await DownloadSaveFileToPathAsync(saveFileName, outputPath);
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        public async Task DeleteGameFromCloud(string gameName)
        {
            await ReadSavegameList();

            List <SavegameEntry> saves = savegameList.ReadSaves(gameName);

            savegameList.DeleteGame(gameName);
            await WriteSavegameList();

            foreach (SavegameEntry save in saves)
            {
                string saveFileName = SavegameSyncUtils.GetSavegameFileNameFromGuid(save.Guid);
                int    filesDeleted = await googleDriveWrapper.DeleteAllFilesWithNameAsync(saveFileName);

                Debug.Assert(filesDeleted == 1);
            }
        }
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
        public async Task <List <string> > GetOrphanedSaveFileNames()
        {
            List <string> orphanedSaveFileNames = new List <string>();

            await ReadSavegameList();

            var files = await googleDriveWrapper.GetAllFilesAsync();

            foreach (var file in files)
            {
                bool foundFile = false;

                // Special case for the SavegameList file, which should be the only file that is
                // not a save file itself.
                if (file.Name == SavegameListFileName)
                {
                    foundFile = true;
                }

                foreach (string gameName in savegameList.GetGames())
                {
                    foreach (SavegameEntry entry in savegameList.ReadSaves(gameName))
                    {
                        string entryFileName = SavegameSyncUtils.GetSavegameFileNameFromGuid(entry.Guid);
                        if (entryFileName == file.Name)
                        {
                            foundFile = true;
                        }
                    }
                }

                if (!foundFile)
                {
                    orphanedSaveFileNames.Add(file.Name);
                }
            }
            return(orphanedSaveFileNames);
        }
Ejemplo n.º 7
0
        public async Task DeleteSave(string gameName, int saveIndex)
        {
            // Download latest version of SavegameList
            await ReadSavegameList();

            // Get guid of zipped save file to use later
            List <SavegameEntry> saves = savegameList.ReadSaves(gameName);
            SavegameEntry        save  = saves[saveIndex];
            Guid saveGuid = save.Guid;

            Debug.WriteLine("Deleting save file with guid " + saveGuid + ", index " + saveIndex + ", and timestamp " + save.Timestamp);

            // Delete save from SavegameList
            savegameList.DeleteSave(gameName, saveIndex);

            // Upload SavegameList
            await WriteSavegameList();

            // Delete zipped save file
            string saveFileName = SavegameSyncUtils.GetSavegameFileNameFromGuid(saveGuid);
            int    filesDeleted = await googleDriveWrapper.DeleteAllFilesWithNameAsync(saveFileName);

            Debug.Assert(filesDeleted == 1);
        }