public SavegameQueueItem GetMostRecentSaveObject(uint profileId) { var recentStorageFile = savegameProfile.ProfileDirectory.GetFiles(SavegameHelper.GetStorageFilenameFromProfileId(profileId), SearchOption.TopDirectoryOnly).OrderByDescending(p => p.LastWriteTime).FirstOrDefault(); if (recentStorageFile != null) { var queueItem = new SavegameQueueItem() { DetectionTimestamp = DateTime.Now, WriteRetries = 0, SaveStoragePath = recentStorageFile.FullName, SaveMetafilePath = Path.Combine(Path.GetDirectoryName(recentStorageFile.FullName), "mf_" + recentStorageFile.Name), decryptionSeed = savegameProfile.EncryptionSeed, Type = savegameProfile.SaveProfileType, Comment = "[NMSGM] Automatic backup of savegame slot overwritten during restore." }; return(queueItem); } else { return(null); } }
//VERY ugly private void btnRestore_Click(object sender, EventArgs e) { if (olvBackups.SelectedItems.Count != 1) { MessageBox.Show("Please select exactly one row to export"); } else { var entryToRestore = (SavegameDatabaseEntry)olvBackups.SelectedObject; var nmsProc = Process.GetProcesses().Count(p => p.ProcessName.ToLower() == "nms"); if (nmsProc != 0) { MessageBox.Show("No Man's Sky is currently running.\r\n " + "There is an experimental feature that lets you restore savegames even when NMS is running. If you are using this you will have to re-open the ingame menu to see the restored savegame selectable for loading.\r\n\r\n" + "The restored savegame will show up as the most recent (current) save\r\n\r\n" + "In case this is not working for you please close NMS before restoring"); } // get most recent save (which will be overwritten) // we have to use the correct index!!! uint id; using (var db = new LiteDatabase(NMSGMSettings.DbFilePath)) { var saveIndex = db.GetCollection <SavegameDatabaseEntry>("SavegameIndexV1"); var objects = saveIndex.FindById(entryToRestore.Id); var sto = db.FileStorage.FindById(objects.stBlobId); id = SavegameHelper.GetProfileFromFilename(sto.Filename); } var recentSave = _main.GetMostRecentSaveById(id); bool preRestoreSaveSuccess = false; if (recentSave != null) { try { var db = new SaveStorage(); db.SaveFileToDb(recentSave, _main); preRestoreSaveSuccess = true; } catch (Exception ex) { MessageBox.Show("Automatic backup of most recent save before overwriting by restore failed. Please try again"); } } else { // hacky, we need this so restores to empty savegame dirs are possible preRestoreSaveSuccess = true; } if (preRestoreSaveSuccess) { try { var watcherPaused = _main.PauseWatcher(); var savegameProfilePath = _main.GetSavegameRootPath(); using (var db = new LiteDatabase(NMSGMSettings.DbFilePath)) { var saveIndex = db.GetCollection <SavegameDatabaseEntry>("SavegameIndexV1"); var objects = saveIndex.FindById(entryToRestore.Id); var sto = db.FileStorage.FindById(objects.stBlobId); var mf = db.FileStorage.FindById(objects.mfBlobId); var stoPath = Path.Combine(savegameProfilePath.FullName, sto.Filename); var mfPath = Path.Combine(savegameProfilePath.FullName, mf.Filename); sto.SaveAs(stoPath, true); mf.SaveAs(mfPath, true); File.SetLastWriteTime(stoPath, DateTime.Now); File.SetLastWriteTime(mfPath, DateTime.Now); } if (watcherPaused) { _main.UnpauseWatcher(); } MessageBox.Show("Restore successful"); } catch (Exception exc) { MessageBox.Show("Restore failed: " + exc.Message); } } ReloadObjectsFromDb(); } }