source: binary-improvements2/7dtd-server-fixes/src/PersistentData/PersistentContainer.cs@ 391

Last change on this file since 391 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 1.4 KB
RevLine 
[142]1using System;
2using System.IO;
3using System.Runtime.Serialization;
4using System.Runtime.Serialization.Formatters.Binary;
5
[325]6namespace AllocsFixes.PersistentData {
[143]7 [Serializable]
[325]8 public class PersistentContainer {
[144]9 private Players players;
[325]10 [OptionalField] private Attributes attributes;
[144]11
[391]12 public Players Players => players ?? (players = new Players ());
[325]13
[391]14 public Attributes Attributes => attributes ?? (attributes = new Attributes ());
[144]15
[142]16 private static PersistentContainer instance;
17
[391]18 public static PersistentContainer Instance => instance ?? (instance = new PersistentContainer ());
[325]19
20 private PersistentContainer () {
[142]21 }
22
[325]23 public void Save () {
[369]24 Stream stream = File.Open (GameIO.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Create);
[142]25 BinaryFormatter bFormatter = new BinaryFormatter ();
26 bFormatter.Serialize (stream, this);
27 stream.Close ();
28 }
29
[325]30 public static bool Load () {
[369]31 if (!File.Exists (GameIO.GetSaveGameDir () + "/AllocsPeristentData.bin")) {
[326]32 return false;
[146]33 }
[325]34
[326]35 try {
[369]36 Stream stream = File.Open (GameIO.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Open);
[326]37 BinaryFormatter bFormatter = new BinaryFormatter ();
[391]38 PersistentContainer obj = (PersistentContainer) bFormatter.Deserialize (stream);
[326]39 stream.Close ();
40 instance = obj;
41 return true;
42 } catch (Exception e) {
43 Log.Error ("Exception in PersistentContainer.Load");
44 Log.Exception (e);
45 }
46
[146]47 return false;
[142]48 }
49 }
[325]50}
Note: See TracBrowser for help on using the repository browser.