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

Last change on this file since 173 was 146, checked in by alloc, 10 years ago

Fixes

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 {
38 Stream stream = File.Open (StaticDirectories.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Create);
39 BinaryFormatter bFormatter = new BinaryFormatter ();
40 bFormatter.Serialize (stream, this);
41 stream.Close ();
42 }
43
44 public static bool Load ()
45 {
46 if (File.Exists (StaticDirectories.GetSaveGameDir () + "/AllocsPeristentData.bin")) {
[146]47 try {
48 PersistentContainer obj;
49 Stream stream = File.Open (StaticDirectories.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Open);
50 BinaryFormatter bFormatter = new BinaryFormatter ();
51 obj = (PersistentContainer)bFormatter.Deserialize (stream);
52 stream.Close ();
53 instance = obj;
54 return true;
55 } catch (Exception e) {
56 Log.Out ("Exception in PersistentContainer.Load: " + e.Message);
57 }
58 }
59 return false;
[142]60 }
61
62 }
63}
64
Note: See TracBrowser for help on using the repository browser.