private void RebuildFile() { if (File.Exists(path)) { File.Delete(path); } if (!Directory.Exists(root + Path.DirectorySeparatorChar + profileName)) { Directory.CreateDirectory(root + Path.DirectorySeparatorChar + profileName); } IFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) { try { formatter.Serialize(stream, dict); } catch (SerializationException e) { Debug.LogException(e); stream.Close(); } catch (Exception e) { Debug.LogException(e); stream.Close(); } } }
public void Save() { IFormatter formatter = new BinaryFormatter(); try { Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); try { formatter.Serialize(stream, dict); } catch (SerializationException e) { Debug.LogException(e); } catch (Exception e) { Debug.LogException(e); } finally { stream.Close(); } } catch (Exception) { RebuildFile(); Save(); } }
public DataContainer(string filename, string profile = "default") { profileName = profile; path = root + Path.DirectorySeparatorChar + profile + Path.DirectorySeparatorChar + filename; if (File.Exists(path)) { IFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) try { dict = (Dictionary <string, T>)formatter.Deserialize(stream); } catch (SerializationException e) { Debug.LogException(e); RebuildFile(); } } else { RebuildFile(); } if (Application.isPlaying && SaveOnQuit.Instances < 1) { QuitObject = new GameObject("FilePrefs_QuitObject", typeof(SaveOnQuit)); } }