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

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 1.4 KB
Line 
1using System;
2using System.IO;
3using System.Runtime.Serialization;
4using System.Runtime.Serialization.Formatters.Binary;
5
6namespace AllocsFixes.PersistentData {
7 [Serializable]
8 public class PersistentContainer {
9 private Players players;
10 [OptionalField] private Attributes attributes;
11
12 public Players Players => players ?? (players = new Players ());
13
14 public Attributes Attributes => attributes ?? (attributes = new Attributes ());
15
16 private static PersistentContainer instance;
17
18 public static PersistentContainer Instance => instance ?? (instance = new PersistentContainer ());
19
20 private PersistentContainer () {
21 }
22
23 public void Save () {
24 Stream stream = File.Open ($"{GameIO.GetSaveGameDir ()}/AllocsPeristentData.bin", FileMode.Create);
25 BinaryFormatter bFormatter = new BinaryFormatter ();
26 bFormatter.Serialize (stream, this);
27 stream.Close ();
28 }
29
30 public static bool Load () {
31 if (!File.Exists ($"{GameIO.GetSaveGameDir ()}/AllocsPeristentData.bin")) {
32 return false;
33 }
34
35 try {
36 Stream stream = File.Open ($"{GameIO.GetSaveGameDir ()}/AllocsPeristentData.bin", FileMode.Open);
37 BinaryFormatter bFormatter = new BinaryFormatter ();
38 PersistentContainer obj = (PersistentContainer) bFormatter.Deserialize (stream);
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
47 return false;
48 }
49 }
50}
Note: See TracBrowser for help on using the repository browser.