/// <summary> /// Simply checks for and returns the scene index from the core save file, per the last manual save operation. /// </summary> /// <returns>The last manually saved scene index, or invalidSceneIndexToken if no such save was present.</returns> private int fetchLastSavedGameSceneIndex() { int lastSavedGameSceneIndex = invalidSceneIndexToken; FileStream fsCore = null; FPESceneSaveData loadedSceneData = null; BinaryFormatter formatter = new BinaryFormatter(); try { fsCore = new FileStream(fullCoreSaveFileFullPath, FileMode.Open); loadedSceneData = (FPESceneSaveData)formatter.Deserialize(fsCore); lastSavedGameSceneIndex = loadedSceneData.LastSavedSceneIndex; } catch (Exception e) { Debug.LogError("FPESaveLoadManager:: Failed to load core save file '" + fullCoreSaveFileFullPath + "'. Reason: " + e.Message); } finally { if (fsCore != null) { fsCore.Close(); } } return(lastSavedGameSceneIndex); }
/// <summary> /// Checks to see if there is a valid core save file present. /// </summary> /// <returns>True if a core save file exists and is considered valid to load. False if no such file exists.</returns> public bool SavedGameExists() { bool result = false; FPESceneSaveData loadedSceneData = null; BinaryFormatter formatter = new BinaryFormatter(); FileStream fsCore = null; try { // If core file exists, read in contents and see if save game is valid if (File.Exists(fullCoreSaveFileFullPath)) { fsCore = new FileStream(fullCoreSaveFileFullPath, FileMode.Open); loadedSceneData = (FPESceneSaveData)formatter.Deserialize(fsCore); if (loadedSceneData.LastSavedSceneIndex != invalidSceneIndexToken) { result = true; } //else //{ // Debug.Log("FPESaveLoadManager.SavedGameExists():: Saved game core file at '" + fullCoreSaveFileFullPath + "' was invalid, scene index was '" + loadedSceneData.LastSavedSceneIndex + "'"); //} } // If it does not exist, that's fine, it will be created when player saves their game else { result = false; } } catch (Exception e) { Debug.LogError("FPESaveLoadManager.SavedGameExists():: Exception occurred while trying to open '" + fullCoreSaveFileFullPath + "'. Reason: " + e.Message); result = false; } finally { if (fsCore != null) { fsCore.Close(); } } return(result); }
/// <summary> /// Saves all relevant game data per the scene and specified fullSave value. /// </summary> /// <param name="fullSave">If true, 'last manually saved scene' and player location data are also saved. If false, these are skipped and just the other scene object data is saved.</param> /// <returns>True if operation was a success. False if there was an error. In the case of an error, an exception likely occured, and an exception message will be printed.</returns> private IEnumerator saveDataForCurrentScene(bool fullSave) { float operationStartTime = Time.time; bool result = true; BinaryFormatter formatter = new BinaryFormatter(); FileStream fsCore = null; FileStream fsPlayer = null; FileStream fsInventory = null; FileStream fsLevel = null; FPESceneSaveData sceneData = mySaveLoadLogic.gatherSceneData(); FPEInventorySaveData inventoryData = mySaveLoadLogic.gatherInventorySaveData(); FPEPlayerStateSaveData playerLocationData = mySaveLoadLogic.gatherPlayerData(); FPEInventoryWorldSaveData[] worldInventoryData = mySaveLoadLogic.gatherInventoryInWorld(); FPEPickupWorldSaveData[] worldPickupData = mySaveLoadLogic.gatherPickupsInWorld(); FPETriggerSaveData[] triggerSaveData = mySaveLoadLogic.gatherTriggerData(); FPEActivateSaveData[] activateSaveData = mySaveLoadLogic.gatherActivateTypeData(); FPEAttachedNoteSaveData[] attachedNoteSaveData = mySaveLoadLogic.gatherAttachedNoteTypeData(); FPEAudioDiaryPlayedStateSaveData[] playedDiarySaveData = mySaveLoadLogic.gatherAudioDiaryPlayedStateData(); FPEJournalSaveData[] journalSaveData = mySaveLoadLogic.gatherJournalSaveData(); FPEDoorSaveData[] doorSaveData = mySaveLoadLogic.gatherDoorTypeData(); FPEGenericObjectSaveData[] genericSaveData = mySaveLoadLogic.gatherGenericSaveTypeData(); // // Your additional Custom Save/Load logic for custom save data types goes here // // Try to write the gathered data to applicable save files on disk try { // We only want to save player location data on manual save operation. When "auto-saving" on change of scene via Doorway, we just want to save the non-player level data. if (fullSave) { // We also only want to write to core save file on a manual save operation. Auto-saves on level change do not count as a "full save" fsCore = new FileStream(fullCoreSaveFileFullPath, FileMode.Create); formatter.Serialize(fsCore, sceneData); fsPlayer = new FileStream(fullPlayerLocationDataSaveFileFullPath, FileMode.Create); formatter.Serialize(fsPlayer, playerLocationData); fsInventory = new FileStream(fullInventoryDataSaveFileFullPath, FileMode.Create); formatter.Serialize(fsInventory, inventoryData); } fsLevel = new FileStream(autoSavePath + "/" + levelDataFilePrefix + sceneData.LastSavedSceneIndex + levelDataFilePostfix, FileMode.Create); formatter.Serialize(fsLevel, worldInventoryData); formatter.Serialize(fsLevel, worldPickupData); formatter.Serialize(fsLevel, triggerSaveData); formatter.Serialize(fsLevel, activateSaveData); formatter.Serialize(fsLevel, attachedNoteSaveData); formatter.Serialize(fsLevel, playedDiarySaveData); formatter.Serialize(fsLevel, journalSaveData); formatter.Serialize(fsLevel, doorSaveData); formatter.Serialize(fsLevel, genericSaveData); // If performing a full save, we also want to flush all previous FULL save data from full directory, and replace it will the stuff we just saved to auto if (fullSave) { copyAllLevelDataFromPathToPath(autoSavePath, fullSavePath); } } catch (Exception e) { Debug.LogError("FPESaveLoadManager:: Failed to save game file(s). Did you make a call the SaveGame() before ever calling StartANewGame()? Reason: " + e.Message); result = false; } finally { if (fsCore != null) { fsCore.Close(); } if (fsLevel != null) { fsLevel.Close(); } if (fsPlayer != null) { fsPlayer.Close(); } if (fsInventory != null) { fsInventory.Close(); } } // Last thing is to clear the progress flag to indicate we're done savingInProgress = false; yield return(result); }
/// <summary> /// This function removes all level save files and resets the core save file back to the invalid token index. After calling this function, SavedGameExists() will return false. /// </summary> /// <returns>True if starting a new game worked. False if something went wrong (e.g. file permissions issue, etc.)</returns> public bool StartANewGame() { bool result = true; BinaryFormatter formatter = new BinaryFormatter(); FileStream fsCore = null; FPESceneSaveData sceneData = new FPESceneSaveData(invalidSceneIndexToken); try { // On first play, directories will not exist if (Directory.Exists(fullSavePath) == false) { Directory.CreateDirectory(fullSavePath); } if (Directory.Exists(autoSavePath) == false) { Directory.CreateDirectory(autoSavePath); } // Reset Core file fsCore = new FileStream(fullCoreSaveFileFullPath, FileMode.Create); formatter.Serialize(fsCore, sceneData); // Delete player data and inventory data from both auto and full directories try { // Full if (File.Exists(fullPlayerLocationDataSaveFileFullPath)) { File.Delete(fullPlayerLocationDataSaveFileFullPath); } if (File.Exists(fullInventoryDataSaveFileFullPath)) { File.Delete(fullInventoryDataSaveFileFullPath); } } catch (Exception e) { //Debug.LogError("FPESaveLoadManager.StartANewGame():: Failed to delete either player save file ('" + fullPlayerLocationDataSaveFileFullPath + "'/'" + autoPlayerLocationDataSaveFileFullPath + "') or inventory save file ('" + fullInventoryDataSaveFileFullPath + "'/'" + autoInventoryDataSaveFileFullPath + "') (or some combination). Reason: " + e.Message); Debug.LogError("FPESaveLoadManager.StartANewGame():: Failed to delete either player save file ('" + fullPlayerLocationDataSaveFileFullPath + "') or inventory save file ('" + fullInventoryDataSaveFileFullPath + "') (or some combination). Reason: " + e.Message); } finally { } // Delete all level save files in auto directory deleteAllLevelDataFromPath(autoSavePath); // Delete all level save files in full directory deleteAllLevelDataFromPath(fullSavePath); } catch (Exception e) { Debug.LogError("FPESaveLoadManager.StartANewGame():: Failed to create core save game file '" + fullCoreSaveFileFullPath + "'. Reason: " + e.Message); result = false; } finally { if (fsCore != null) { fsCore.Close(); } } return(result); }