/// <summary> /// Saves data. /// </summary> /// <typeparam name="T">Data type</typeparam> /// <param name="data">Data to save</param> public void Save <T>(T data) { if (File.Exists(FilePath)) { File.Delete(FilePath); } using (FileStream stream = File.OpenWrite(FilePath)) { BinaryFormatter bf = new BinaryFormatter(); var surrogateSelector = new SurrogateSelector(); Vector3Surrogate v3ss = new Vector3Surrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); bf.SurrogateSelector = surrogateSelector; bf.Serialize(stream, data); // Calling the stream.Close() is not necessary when using // the 'using' statement. When the execution leaves the // stream's scope, the Dispose method is called automatically // by stream.Close(). stream.Close(); } }
public void Save <T>(T data) { if (File.Exists(FilePath)) { File.Delete(FilePath); } using (FileStream stream = File.OpenWrite(FilePath)) { BinaryFormatter bf = new BinaryFormatter(); var surrogateSelector = new SurrogateSelector(); Vector3Surrogate v3ss = new Vector3Surrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); bf.SurrogateSelector = surrogateSelector; bf.Serialize(stream, data); // Calling stream.Close() not necessary when using 'using'. stream.Close(); } }
public T Load <T>() { T data = default(T); if (File.Exists(FilePath)) { // If we are not using the 'using' statement, we have to make sure that // the stream is correctly closed in case of an Exception being thrown. // The finally block makes sure that the steam is closed correctly in // every case. FileStream stream = File.OpenRead(FilePath); try { BinaryFormatter bf = new BinaryFormatter(); var surrogateSelector = new SurrogateSelector(); Vector3Surrogate v3ss = new Vector3Surrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); bf.SurrogateSelector = surrogateSelector; data = (T)bf.Deserialize(stream); } catch (SerializationException e) { Debug.LogError("Deserialization failed!"); } catch (Exception e) { Debug.LogException(e); } finally { stream.Close(); } } return(data); }
public void Save <T>(T data) { if (File.Exists(FilePath)) { File.Delete(FilePath); } using (FileStream stream = File.OpenWrite(FilePath)) { BinaryFormatter bf = new BinaryFormatter(); var surrogateSelector = new SurrogateSelector(); Vector3Surrogate v3ss = new Vector3Surrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); bf.SurrogateSelector = surrogateSelector; bf.Serialize(stream, data); // Called automatically inside using statement, so this is moot // stream.Close(); } }
public T Load <T>() { T data = default(T); if (File.Exists(FilePath)) { // If not using 'using', stream must be closed correctly. // The finally block makes sure to do that in every case. FileStream stream = File.OpenRead(FilePath); try { BinaryFormatter bf = new BinaryFormatter(); var surrogateSelector = new SurrogateSelector(); Vector3Surrogate v3ss = new Vector3Surrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); bf.SurrogateSelector = surrogateSelector; data = (T)bf.Deserialize(stream); } catch (SerializationException e) { Debug.LogError("Deserialization failed!"); } catch (Exception e) { Debug.LogException(e); } finally { stream.Close(); } } return(data); }