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

Last change on this file since 254 was 238, checked in by alloc, 9 years ago

Server fixes for A12

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