source: binary-improvements/7dtd-server-fixes/src/PersistentData/PersistentContainer.cs@ 448

Last change on this file since 448 was 448, checked in by alloc, 17 months ago

24_29_42

File size: 1.8 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 const string persistentDataFileName = "/AllocsPersistentData.bin";
10
11 private Players players;
12 [OptionalField] private Attributes attributes;
13
14 public Players Players {
15 get {
16 if (players == null) {
17 players = new Players ();
18 }
19
20 return players;
21 }
22 }
23
24 public Attributes Attributes {
25 get {
26 if (attributes == null) {
27 attributes = new Attributes ();
28 }
29
30 return attributes;
31 }
32 }
33
34 private static PersistentContainer instance;
35
36 public static PersistentContainer Instance {
37 get {
38 if (instance == null) {
39 instance = new PersistentContainer ();
40 }
41
42 return instance;
43 }
44 }
45
46 private PersistentContainer () {
47 }
48
49 public void Save () {
50 lock (fileLockObject) {
51 using Stream stream = File.Open (GameIO.GetSaveGameDir () + persistentDataFileName, FileMode.Create);
52 BinaryFormatter bFormatter = new BinaryFormatter ();
53 bFormatter.Serialize (stream, this);
54 }
55 }
56
57 public static bool Load () {
58 if (!File.Exists (GameIO.GetSaveGameDir () + persistentDataFileName)) {
59 return false;
60 }
61
62 try {
63 PersistentContainer obj;
64 lock (fileLockObject) {
65 using Stream stream = File.Open (GameIO.GetSaveGameDir () + persistentDataFileName, FileMode.Open);
66 BinaryFormatter bFormatter = new BinaryFormatter ();
67 obj = (PersistentContainer)bFormatter.Deserialize (stream);
68 }
69
70 instance = obj;
71 return true;
72 } catch (Exception e) {
73 Log.Error ("Exception in PersistentContainer.Load");
74 Log.Exception (e);
75 }
76
77 return false;
78 }
79
80 private static readonly object fileLockObject = new();
81 }
82}
Note: See TracBrowser for help on using the repository browser.