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

Last change on this file since 357 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

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