/// <summary> /// Writes game options to options data file /// </summary> /// <returns>True if write was successful, false if write failed. On failure, and error message is also printed (likely file write permission issues, etc.)</returns> public bool SaveGameOptions() { bool result = true; BinaryFormatter formatter = new BinaryFormatter(); FileStream fsOptions = null; FPEGameOptionsSaveData optionsData = mySaveLoadLogic.gatherGameOptionsSaveData(); try { fsOptions = new FileStream(optionsDataSaveFileFullPath, FileMode.Create); formatter.Serialize(fsOptions, optionsData); } catch (Exception e) { Debug.LogError("FPESaveLoadManager:: Failed to save game file(s). Reason: " + e.Message); result = false; } finally { if (fsOptions != null) { fsOptions.Close(); } } return(result); }
/// <summary> /// Restores the game options to their previously saved values /// </summary> /// <param name="data">The saved data type from which to restore game options</param> public void restoreGameOptionsData(FPEGameOptionsSaveData data) { FPEInputManager.Instance.LookSensitivity = new Vector2(data.LookSensitivity, data.LookSensitivity); FPEInputManager.Instance.LookSmoothing = data.LookSmoothing; FPEInputManager.Instance.UseGamepad = data.UseGamepad; if (FPEInteractionManagerScript.Instance != null) { FPEInteractionManagerScript.Instance.changeMouseSensitivityFromMenu(data.LookSensitivity); } }
/// <summary> /// Reads the game options from file, if file exists. /// </summary> /// <returns>True if load succeeded, false if it did not. If file did not exist, it is created and /// a warning is printed. If it exists but can not be read (e.g. file permission issue), only /// an error is printed.</returns> public bool LoadGameOptions() { bool result = true; if (File.Exists(optionsDataSaveFileFullPath)) { FPEGameOptionsSaveData optionsData = null; BinaryFormatter formatter = new BinaryFormatter(); FileStream fsOptions = null; // Try to actually read in the data from disk try { fsOptions = new FileStream(optionsDataSaveFileFullPath, FileMode.Open); optionsData = (FPEGameOptionsSaveData)formatter.Deserialize(fsOptions); mySaveLoadLogic.restoreGameOptionsData(optionsData); } catch (Exception e) { Debug.LogError("FPESaveLoadManager:: Failed to load game options file '" + optionsDataSaveFileFullPath + "'. Reason: " + e.Message); result = false; } finally { if (fsOptions != null) { fsOptions.Close(); } } } else { Debug.LogWarning("FPESaveLoadManager:: Game options file '" + optionsDataSaveFileFullPath + "' does not exist. Attempting to save existing options so a reload can occur."); SaveGameOptions(); result = false; } return(result); }
/// <summary> /// Gathers saveable data for Game Options (mouse sensitivity, look smoothing, etc.) /// </summary> /// <returns>Saveable type containing the options data</returns> public FPEGameOptionsSaveData gatherGameOptionsSaveData() { FPEGameOptionsSaveData optionsData = new FPEGameOptionsSaveData(FPEInputManager.Instance.LookSensitivity.x, FPEInputManager.Instance.LookSmoothing, FPEInputManager.Instance.UseGamepad); return(optionsData); }