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

Last change on this file since 315 was 273, checked in by alloc, 9 years ago

fixes 8_10_13_1

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{
8 [Serializable]
9 public class PersistentContainer
10 {
11 private Players players;
12 [OptionalField]
13 private Attributes attributes;
14
15 public Players Players {
16 get {
17 if (players == null)
18 players = new Players ();
19 return players;
20 }
21 }
22
23 public Attributes Attributes
24 {
25 get {
26 if (attributes == null) {
27 attributes = new Attributes();
28 }
29 return attributes;
30 }
31 }
32
33 private static PersistentContainer instance;
34
35 public static PersistentContainer Instance {
36 get {
37 if (instance == null) {
38 instance = new PersistentContainer ();
39 }
40 return instance;
41 }
42 }
43
44 private PersistentContainer ()
45 {
46 }
47
48 public void Save ()
49 {
50 Stream stream = File.Open (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Create);
51 BinaryFormatter bFormatter = new BinaryFormatter ();
52 bFormatter.Serialize (stream, this);
53 stream.Close ();
54 }
55
56 public static bool Load ()
57 {
58 if (File.Exists (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin")) {
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 }
76}
77
Note: See TracBrowser for help on using the repository browser.