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
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
13 public Players Players {
14 get {
15 if (players == null)
16 players = new Players ();
17 return players;
18 }
19 }
20
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 (GameUtils.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 (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin")) {
47 try {
48 PersistentContainer obj;
49 Stream stream = File.Open (GameUtils.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.Error ("Exception in PersistentContainer.Load");
57 Log.Exception (e);
58 }
59 }
60 return false;
61 }
62
63 }
64}
65
Note: See TracBrowser for help on using the repository browser.