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

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

fixes 8_10_13_1

File size: 1.6 KB
RevLine 
[142]1using System;
2using System.IO;
3using System.Runtime.Serialization;
4using System.Runtime.Serialization.Formatters.Binary;
5
6namespace AllocsFixes.PersistentData
7{
[143]8 [Serializable]
9 public class PersistentContainer
[142]10 {
[144]11 private Players players;
[273]12 [OptionalField]
13 private Attributes attributes;
[144]14
15 public Players Players {
16 get {
17 if (players == null)
18 players = new Players ();
19 return players;
20 }
21 }
22
[273]23 public Attributes Attributes
24 {
25 get {
26 if (attributes == null) {
27 attributes = new Attributes();
28 }
29 return attributes;
30 }
31 }
32
[142]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 {
[238]50 Stream stream = File.Open (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Create);
[142]51 BinaryFormatter bFormatter = new BinaryFormatter ();
52 bFormatter.Serialize (stream, this);
53 stream.Close ();
54 }
55
56 public static bool Load ()
57 {
[238]58 if (File.Exists (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin")) {
[146]59 try {
60 PersistentContainer obj;
[238]61 Stream stream = File.Open (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Open);
[146]62 BinaryFormatter bFormatter = new BinaryFormatter ();
63 obj = (PersistentContainer)bFormatter.Deserialize (stream);
64 stream.Close ();
65 instance = obj;
66 return true;
67 } catch (Exception e) {
[230]68 Log.Error ("Exception in PersistentContainer.Load");
69 Log.Exception (e);
[146]70 }
71 }
72 return false;
[142]73 }
74
75 }
76}
77
Note: See TracBrowser for help on using the repository browser.