public void TryLoadState() { string JSONdata = ""; try { //HACK: despite having try/catch block, the statements in if crash whole editor on linux if (File.Exists(Application.persistentDataPath + "/save.dat")) { JSONdata = File.ReadAllText(Application.persistentDataPath + "/save.dat"); stateData = JsonUtility.FromJson <GameSaveState>(JSONdata); } else { throw new IOException("Save file not found! Loading default scene..."); } } catch (IOException ioe) { //if IO error occured, load default scene and create new stateData stateData = new GameSaveState(); Debug.Log(ioe.Message); return; } catch (Exception e) { Debug.Log(e.Message); return; } SceneManager.LoadScene(stateData.lastScene); }
public History(Guid gameId) { Id = Guid.NewGuid(); GameId = gameId; State = new GameSaveState(); DateStarted = DateTimeOffset.Now; }
public void GameStateReq(Player fromPlayer) { Log.DebugFormat("GameStateReq From {0}", fromPlayer); try { var ps = new GameSaveState().Create(Program.GameEngine, fromPlayer); var str = JsonConvert.SerializeObject(ps, Formatting.None); Program.Client.Rpc.GameState(fromPlayer, str); } catch (Exception e) { Log.Error("GameStateReq Error", e); } }
private static void ReadSteam() { if (SteamManager.Initialized) { try { if (SteamRemoteStorage.FileExists("progress.txt")) { byte[] array = new byte[SteamRemoteStorage.GetFileSize("progress.txt")]; int count = SteamRemoteStorage.FileRead("progress.txt", array, array.Length); string @string = Encoding.UTF8.GetString(array, 0, count); if (@string.Length > 0) { GameSaveState gameSaveState = savedState = JsonUtility.FromJson <GameSaveState>(@string); } } } catch (Exception exception) { Debug.LogException(exception); } } }
public static GameSaveState LoadFromText(string text) { GameSaveState gameSave = null; XmlSerializer serializer = new XmlSerializer(typeof(GameSaveState)); StringReader stream = null; try { // Try to load the save file stream = new StringReader(text); gameSave = serializer.Deserialize(stream) as GameSaveState; } catch { // If we fail to load the file just return null gameSave = null; } finally { // Make sure we close the stream if (stream != null) { stream.Close(); } } return(gameSave); }
public static GameSaveState Load(string path) { GameSaveState gameSave = null; XmlSerializer serializer = new XmlSerializer(typeof(GameSaveState)); FileStream stream = null; try { // Try to load the save file stream = new FileStream(path, FileMode.Open); gameSave = serializer.Deserialize(stream) as GameSaveState; } catch { // If we fail to load the file just return null gameSave = null; } finally { // Make sure we close the stream if (stream != null) { stream.Close(); } } return(gameSave); }
private IEnumerator LoadGameStateHelper() { // Set our first checkpoint to the default start point if (_saveData == null) { LastCheckPoint = _currentLevel.DefaultStartpoint; } // Clear out player inventory Inventory.Clear(); // Get the saved data _saveData = GameSaveState.Load(GameSaveStatePath); // Do nothing if we failed to load a game save if (_saveData == null) { _saveData = new GameSaveState(); Checkpoint lastCheckpoint = LastCheckPoint.GetComponent <Checkpoint>(); if (lastCheckpoint != null) { _saveData.Checkpoint = lastCheckpoint.Location; } yield break; } // Load up the correct level if (!string.IsNullOrEmpty(SaveData.LevelName) && !SaveData.LevelName.Equals(SceneManager.GetActiveScene().name)) { Instantiate(Resources.Load("Prefabs/User Interface/Loading Screen")); // NOTE: WE DON'T USE THE OBJECT POOL, SINCE WE ONLY NEED THE 1 SceneManager.LoadScene(SaveData.LevelName); } // Move player to his last checkpoint. bool checkpointFound = false; GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("Respawn"); foreach (GameObject obj in spawnPoints) { Checkpoint checkpoint = obj.GetComponent <Checkpoint>(); if (checkpoint != null && checkpoint.Location == _saveData.Checkpoint) { Player.transform.position = checkpoint.transform.position; LastCheckPoint = checkpoint.transform; checkpointFound = true; break; } } if (!checkpointFound) { Debug.LogWarning("Saved checkpoint not found!"); LastCheckPoint = _currentLevel.DefaultStartpoint; } // Reload the player's items foreach (InventoryItem invItem in _saveData.InventoryState.ItemsHeld) { Inventory.AddItem(InventoryItemFactory.CreateFromType(invItem.Type, invItem.Quantity)); } // Reload the player's weapons foreach (WeaponSaveState weaponSave in _saveData.InventoryState.WeaponsHeld) { string weaponName = "Prefabs/Items/Weapons/InHand/" + weaponSave.WeaponType.ToString().Substring(7); // NOTE: WE MAY WANT TO POOL HERE, BUT I DON'T THINK IT HAPPENS ENOUGH TO BE WORTH IT/NOT SURE IT MAKES SENSE GameObject createdWeapon = (GameObject)Instantiate(Resources.Load(weaponName), _currentLevel.OffscreenPosition, Quaternion.identity); if (createdWeapon == null) { Debug.LogWarning("Failed to load weapon: " + weaponName); continue; } Weapon newWeapon = createdWeapon.GetComponent <Weapon>(); newWeapon.Quantity = weaponSave.Quantity; Inventory.Weapons.Add(newWeapon); } // Display the correct weapon for (int i = 0; i < Inventory.Items.Count; i++) { if (UI.CurrentWeapon == _saveData.InventoryState.CurrentWeapon) { break; } UI.CycleToNextWeapon(); } UI.RefreshWeaponWheel(); yield return(null); }
public void Save() { XmlSerializer serializer = new XmlSerializer(typeof(GameSaveState)); using(TextWriter writer = new StreamWriter(GameStateSaver.FilePath())) { GameSaveState gss = new GameSaveState(); foreach (Chapter chapter in StateChapterSelect.Instance.Chapters) { gss.Chapters.Add(chapter.ToChapterSaveState()); } serializer.Serialize(writer, gss); writer.Close(); } Debug.Log("Save game state"); }
public void Reset() { List<string> files = System.IO.Directory.GetFiles(Application.persistentDataPath, "*.xml").ToList(); if(files != null && files.Count != 0) { foreach (string file in files) { File.Delete(file); } } //Unlocking partially tutorial level XmlSerializer serializer = new XmlSerializer(typeof(GameSaveState)); using(TextWriter writer = new StreamWriter(GameStateSaver.FilePath())) { GameSaveState gss = new GameSaveState(); for (int i = 0; i < StateChapterSelect.Instance.Chapters.Length; i++) { ChapterSaveState resetChapter = new ChapterSaveState(); resetChapter.LevelNumber = StateChapterSelect.Instance.Chapters[i].LevelNumber; if(StateChapterSelect.Instance.Chapters[i].LevelNumber == 0) { for (int j = 0; j < resetChapter.JigsawPeicesUnlocked.Length; j++) { resetChapter.JigsawPeicesUnlocked[j] = 1.0f; } resetChapter.JigsawPeicesUnlocked[5] = 0.8f; resetChapter.UnLocked = true; } gss.Chapters.Add(resetChapter); } serializer.Serialize(writer, gss); writer.Close(); } Debug.Log("Reset game state"); }
public History() { State = new GameSaveState(); Id = Guid.NewGuid(); }