private void ImportSavegamesCache(IsolatedStorageFile isf) { List <CartridgeSavegame> cSavegames = new List <CartridgeSavegame>(); string[] gwsFiles = isf.GetFileNames(PathToSavegames + "/*.gws"); if (gwsFiles != null) { // For each file, imports its metadata. foreach (string file in gwsFiles) { string path = PathToSavegames + "/" + file; try { cSavegames.Add(CartridgeSavegame.FromIsoStore(path, isf)); } catch (FileNotFoundException) { // No associated meta-data or the file does not exist. // Let the store decide what to do. App.Current.Model.CartridgeStore.OnUnknownSavegame(path); } catch (Exception ex) { // Outputs the exception. System.Diagnostics.Debug.WriteLine("CartridgeTag: WARNING: Exception during savegame import."); DebugUtils.DumpException(ex); } } } // Sets the savegame list. _savegames.AddRange(cSavegames); RaisePropertyChanged("Savegames"); }
/// <summary> /// Makes a savegame of the currently playing cartridge, eventually /// prompting the user to customize the metadata of the cartridge. /// </summary> /// <param name="isAutoSave">If true, no prompting is done, and /// the savegame is marked as autosave.</param> public void SaveGame(bool isAutoSave) { // Gets a new random CartridgeSavegame. CartridgeTag tag = GetCurrentTag(); CartridgeSavegame cs = new CartridgeSavegame(tag) { IsAutosave = isAutoSave }; // Performs the savegame. _appViewModel.Model.Core.SaveAsync(cs) .ContinueWith(t => { // If the savegame failed, display a message. if (!t.Result) { MessageBox.Show("An error occured while preparing the savegame. Please try again.", "Error", MessageBoxButton.OK); return; } // If this is a manual save, shows a message box. if (!isAutoSave) { // What happens next depends on the result of this message box. ShowNewSavegameMessageBox(cs); } else { // Adds the savegame to the tag. tag.AddSavegame(cs); } }, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); }
/// <summary> /// Exports a savegame to the isolated storage and adds it to this tag. /// </summary> /// <param name="cs">The savegame to add.</param> public void AddSavegame(CartridgeSavegame cs) { // Sanity check: a savegame with similar name should // not exist. if (Savegames.Any(c => c.Name == cs.Name)) { System.Diagnostics.Debug.WriteLine("CartridgeTag: Renaming new savegame because an old one with same name exists: " + cs.Name); // What's the last savegame following the pattern "name (n)"? int dbl = GetLastSavegameNameInteger(cs.Name, " ({0})"); // Renames the savegame. using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { cs.Rename(this, cs.Name + " (" + ++dbl + ")", isf); } } // Makes sure the savegame is exported to the cache. cs.ExportToIsoStore(); // Adds the savegame. _savegames.Add(cs); // Notifies of a change. RaisePropertyChanged("Savegames"); }
/// <summary> /// Makes a savegame of the currently playing cartridge, eventually /// prompting the user to customize the metadata of the cartridge. /// </summary> /// <param name="isAutoSave">If true, no prompting is done, and /// the savegame is marked as autosave.</param> public void SaveGame(bool isAutoSave) { // Gets a new random CartridgeSavegame. CartridgeTag tag = GetCurrentTag(); CartridgeSavegame cs = new CartridgeSavegame(tag) { IsAutosave = isAutoSave }; // Performs the savegame. _appViewModel.Model.Core.SaveAsync(cs) .ContinueWith(t => { // If this is a manual save, shows a message box. if (!isAutoSave) { // What happens next depends on the result of this message box. ShowNewSavegameMessageBox(cs); } else { // Adds the savegame to the tag. tag.AddSavegame(cs); } }, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); }
/// <summary> /// Saves the game to a CartridgeSavegame object asynchronously. /// </summary> /// <param name="cs">The CartridgeSavegame representing the savegame.</param> /// <returns></returns> public System.Threading.Tasks.Task SaveAsync(CartridgeSavegame cs) { return(System.Threading.Tasks.Task.Factory.StartNew(() => { WaitForGameState(EngineGameState.Playing); Save(cs); })); }
/// <summary> /// Makes a savegame of the currently playing cartridge and prompts the user for a name. /// </summary> public void SaveAndPrompt() { // Creates a savegame container CartridgeTag tag = GetCurrentTag(); CartridgeSavegame cs = new CartridgeSavegame(tag); // Saves the game, displays a prompt, and a message in case of error. SaveCore(tag, cs, (t,c) => ShowNewSavegameMessageBox(c), true); }
/// <summary> /// Saves the game to a CartridgeSavegame object. /// </summary> /// <param name="cs">The CartridgeSavegame representing the savegame.</param> public void Save(CartridgeSavegame cs) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (System.IO.Stream fs = cs.CreateOrReplace(isf)) { Save(fs, cs.Name); } } }
/// <summary> /// Removes a savegame's contents from the isolated storage and removes /// it from this tag. /// </summary> public void RemoveSavegame(CartridgeSavegame cs) { // Removes the savegame. _savegames.Remove(cs); // Makes sure the savegame is cleared from cache. cs.RemoveFromIsoStore(); // Notifies of a change. RaisePropertyChanged("Savegames"); }
private void OnSavegameChanged(CartridgeSavegame cs) { if (cs == null) { Name = null; HashColor = default(Color); } else { Name = cs.Name; HashColor = cs.HashColor; } }
/// <summary> /// Exports a savegame to the isolated storage and adds it to this tag if it is not already there. /// </summary> /// <param name="cs"></param> public void RefreshOrAddSavegame(CartridgeSavegame cs) { if (_savegames.Contains(cs)) { // Refresh cs.ExportToIsoStore(); // Notifies of a change. RaisePropertyChanged("Savegames"); } else { AddSavegame(cs); } }
/// <summary> /// Initializes quick and auto savegames for a new game session. /// </summary> /// <param name="tag">Cartridge being played</param> /// <param name="savegameCandidate">Savegame that started the game session, or null if it is a new game.</param> public void InitSessionSavegames(CartridgeTag tag, CartridgeSavegame savegameCandidate) { // Inits the session's quick save from the restored savegame if it is a quicksave, or makes a new one if not. if (savegameCandidate != null && savegameCandidate.IsQuicksave) { _quickSaves[tag] = savegameCandidate; } else { CreateQuickSavegame(tag); } // Inits the session's auto save from the restored savegame if it is an autosave, or makes a new one if not. if (savegameCandidate != null && savegameCandidate.IsAutosave) { _autoSaves[tag] = savegameCandidate; } else { CreateAutoSavegame(tag); } }
private void ShowNewSavegameMessageBox(CartridgeSavegame cs) { if (cs == null) { throw new ArgumentNullException("cs"); } // Creates a custom message box. CustomMessageBox cmb = new CustomMessageBox() { Caption = "Savegame", Content = new Controls.SavegameMessageBoxContentControl() { Savegame = cs }, LeftButtonContent = "OK", RightButtonContent = "Cancel" }; // Adds event handlers. cmb.Dismissed += new EventHandler<DismissedEventArgs>(OnSavegameCustomMessageBoxDismissed); // Shows the message box. _appViewModel.MessageBoxManager.AcceptAndShow(cmb); }
/// <summary> /// Constructs a history entry of a particular type, populating /// its fields with the contents of a cartridge tag and a cartridge /// savegame, and using <code>DateTime.Now</code> as timestamp. /// </summary> /// <param name="type"></param> /// <param name="cartTag"></param> /// <param name="savegame"></param> public HistoryEntry(HistoryEntry.Type type, CartridgeTag cartTag, CartridgeSavegame savegame) : this(type, cartTag) { RelatedSavegameHashColor = savegame.HashColor; RelatedSavegameName = savegame.Name; }
private bool AcceptSavegame(string filename) { // Copies this savegame to the content folders of each cartridge // whose name matches the cartridge name in the savegame metadata. System.Diagnostics.Debug.WriteLine("CartridgeStore: Trying to accept savegame " + filename); // Refreshes the progress. string businessTag = "accept:" + filename; _isBusyAggregator[businessTag] = true; // Gets the cartridge this savegame is associated with. bool isAborted = false; WF.Player.Core.Formats.GWS.Metadata saveMetadata = null; try { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isf.FileExists(filename)) { System.Diagnostics.Debug.WriteLine("CartridgeStore: WARNING: Savegame file not found: " + filename); isAborted = true; } if (!isAborted) { using (IsolatedStorageFileStream fs = isf.OpenFile(filename, System.IO.FileMode.Open)) { saveMetadata = WF.Player.Core.Formats.GWS.LoadMetadata(fs); } } } } catch (Exception ex) { // Logs this. DebugUtils.DumpException(ex, "loading savegame metadata", true); // Abort! isAborted = true; } bool foundMatch = false; if (!isAborted) { // For each matching tag, creates an associated savegame and copies the file to each // tag's content folder. List <CartridgeTag> matches; lock (_syncRoot) { matches = Items.Where(ct => ct.Title == saveMetadata.CartridgeName).ToList(); } if (matches.Count > 0) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { foreach (CartridgeTag tag in matches) { // Creates a savegame. CartridgeSavegame save = new CartridgeSavegame(tag, saveMetadata, System.IO.Path.GetFileName(filename)); // Copies the new file to the right isolated storage. if (filename != save.SavegameFile) { isf.CopyFile(filename, save.SavegameFile, true); } // Adds the savegame to its tag. tag.AddSavegame(save); foundMatch = true; } } } } // Refreshes the progress. _isBusyAggregator[businessTag] = false; return(!isAborted && foundMatch); }
/// <summary> /// Saves the game to a CartridgeSavegame object. /// </summary> /// <param name="cs">The CartridgeSavegame representing the savegame.</param> public void Save(CartridgeSavegame cs) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (System.IO.Stream fs = cs.CreateOrReplace(isf)) { Save(fs); } } }
/// <summary> /// Saves the game to a CartridgeSavegame object asynchronously. /// </summary> /// <param name="cs">The CartridgeSavegame representing the savegame.</param> /// <returns></returns> public System.Threading.Tasks.Task SaveAsync(CartridgeSavegame cs) { return System.Threading.Tasks.Task.Factory.StartNew(() => { WaitForLegalState(); Save(cs); }); }
/// <summary> /// Called when a game started. /// </summary> /// <param name="tag">Cartridge that started.</param> /// <param name="savegame">Optional savegame restored when the game started.</param> public void HandleGameStarted(CartridgeTag tag, CartridgeSavegame savegame = null) { // Resets the session quick save. SavegameManager.InitSessionSavegames(tag, savegame); }
/// <summary> /// Exports a savegame to the isolated storage and adds it to this tag. /// </summary> /// <param name="cs">The savegame to add.</param> public void AddSavegame(CartridgeSavegame cs) { // Sanity check: a cartridge with similar name should // not exist. if (Savegames.Any(c => c.Name == cs.Name)) { throw new InvalidOperationException("A savegame with the same name already exists for this savegame."); } // Makes sure the savegame is exported to the cache. cs.ExportToIsoStore(); // Adds the savegame. _savegames.Add(cs); // Notifies of a change. RaisePropertyChanged("Savegames"); }
/// <summary> /// Saves the game to a CartridgeSavegame object. /// </summary> /// <param name="cs">The CartridgeSavegame representing the savegame.</param> /// <returns>True if the savegame was a success, false if an error occured.</returns> public bool Save(CartridgeSavegame cs) { try { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (System.IO.Stream fs = cs.CreateOrReplace(isf)) { Save(fs, cs.Name); } } return true; } catch (Exception) { return false; } }
/// <summary> /// Adds a savegame to this tag. /// </summary> /// <param name="cs">The savegame to add.</param> public void AddSavegame(CartridgeSavegame cs) { // Makes sure the savegame is exported to the cache. cs.ExportToCache(); // Adds the savegame. _savegames.Add(cs); // Notifies of a change. RaisePropertyChanged("Savegames"); }
/// <summary> /// Adds a history entry for a game that has been saved. /// </summary> /// <param name="tag"></param> /// <param name="savegame"></param> public void AddSavedGame(CartridgeTag tag, CartridgeSavegame savegame) { Add(new HistoryEntry(HistoryEntry.Type.Saved, tag, savegame)); }
/// <summary> /// Saves the game to a CartridgeSavegame object asynchronously. /// </summary> /// <param name="cs">The CartridgeSavegame representing the savegame.</param> /// <returns></returns> public System.Threading.Tasks.Task<bool> SaveAsync(CartridgeSavegame cs) { return System.Threading.Tasks.Task.Factory.StartNew<bool>(() => { WaitForGameState(EngineGameState.Playing); return Save(cs); }); }
/// <summary> /// Navigates the app to the game main page of a cartridge and restores a /// savegame. /// </summary> public void NavigateToGameHome(string filename, CartridgeSavegame savegame) { NavigateCore(new Uri(String.Format( "/Views/GameHomePage.xaml?{0}={1}&{2}={3}", GameHomeViewModel.CartridgeFilenameKey, filename, GameHomeViewModel.SavegameFilenameKey, savegame.SavegameFile), UriKind.Relative)); }
private void ResumeGame(CartridgeSavegame savegame) { // Resumes the game! App.Current.ViewModel.NavigateToGameHome(Cartridge.Filename, savegame); }
private CartridgeSavegame CreateSavegame(CartridgeTag tag, string nameRoot, string suffixFormat, bool isQuickSave, bool isAutoSave, Dictionary<CartridgeTag, CartridgeSavegame> dict) { if (!isQuickSave && !isAutoSave) { throw new InvalidOperationException("Savegame must be either quick save or auto save"); } // Makes a savegame. string intPattern = " {0}"; int saveId = tag.GetLastSavegameNameInteger(nameRoot, intPattern) + 1; CartridgeSavegame cs = new CartridgeSavegame(tag, nameRoot + String.Format(intPattern, saveId)) { IsQuicksave = isQuickSave, IsAutosave = isAutoSave }; // Sets it as the current save for the tag. dict[tag] = cs; // Returns return cs; }
private void SaveCore( CartridgeTag tag, CartridgeSavegame cs, Action<CartridgeTag, CartridgeSavegame> continuation, bool displayError) { try { // Performs the savegame. _appViewModel.Model.Core.SaveAsync(cs) .ContinueWith(t => { // Continues. continuation(tag, cs); }, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); } catch (Exception ex) { // Keeps track of the error. DebugUtils.DumpException(ex); // Displays an error if needed. if (displayError) { MessageBox.Show("An error occured while preparing the savegame. Please try again.", "Error", MessageBoxButton.OK); } } }
/// <summary> /// Starts the protocol of saving the current game. /// </summary> public void SaveGame(bool isAutoSave) { // Gets a new random CartridgeSavegame. CartridgeTag tag = Model.CartridgeStore.GetCartridgeTag(Model.Core.Cartridge); CartridgeSavegame cs = new CartridgeSavegame(tag) { IsAutosave = isAutoSave }; // Displays a message box. System.Windows.MessageBox.Show("The savegame " + cs.Name + "will be now saved."); // Shows progress to the user. App.Current.ViewModel.SetSystemTrayProgressIndicator("Saving game..."); // Performs the savegame. Model.Core.Save(cs); // Adds the savegame to the tag. tag.AddSavegame(cs); // Shows progress to the user. App.Current.ViewModel.SetSystemTrayProgressIndicator(isVisible: false); }