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

Last change on this file since 454 was 448, checked in by alloc, 17 months ago

24_29_42

File size: 1.8 KB
RevLine 
[142]1using System;
2using System.IO;
3using System.Runtime.Serialization;
4using System.Runtime.Serialization.Formatters.Binary;
5
[325]6namespace AllocsFixes.PersistentData {
[143]7 [Serializable]
[325]8 public class PersistentContainer {
[448]9 private const string persistentDataFileName = "/AllocsPersistentData.bin";
10
[144]11 private Players players;
[325]12 [OptionalField] private Attributes attributes;
[144]13
14 public Players Players {
15 get {
[325]16 if (players == null) {
[144]17 players = new Players ();
[325]18 }
19
[144]20 return players;
21 }
22 }
23
[325]24 public Attributes Attributes {
[273]25 get {
26 if (attributes == null) {
[325]27 attributes = new Attributes ();
[273]28 }
[325]29
[273]30 return attributes;
31 }
32 }
33
[142]34 private static PersistentContainer instance;
35
36 public static PersistentContainer Instance {
37 get {
38 if (instance == null) {
39 instance = new PersistentContainer ();
40 }
[325]41
[142]42 return instance;
43 }
44 }
45
[325]46 private PersistentContainer () {
[142]47 }
[448]48
[325]49 public void Save () {
[448]50 lock (fileLockObject) {
51 using Stream stream = File.Open (GameIO.GetSaveGameDir () + persistentDataFileName, FileMode.Create);
52 BinaryFormatter bFormatter = new BinaryFormatter ();
53 bFormatter.Serialize (stream, this);
54 }
[142]55 }
56
[325]57 public static bool Load () {
[448]58 if (!File.Exists (GameIO.GetSaveGameDir () + persistentDataFileName)) {
[326]59 return false;
[146]60 }
[325]61
[326]62 try {
63 PersistentContainer obj;
[448]64 lock (fileLockObject) {
65 using Stream stream = File.Open (GameIO.GetSaveGameDir () + persistentDataFileName, FileMode.Open);
66 BinaryFormatter bFormatter = new BinaryFormatter ();
67 obj = (PersistentContainer)bFormatter.Deserialize (stream);
68 }
69
[326]70 instance = obj;
71 return true;
72 } catch (Exception e) {
73 Log.Error ("Exception in PersistentContainer.Load");
74 Log.Exception (e);
75 }
76
[146]77 return false;
[142]78 }
[448]79
80 private static readonly object fileLockObject = new();
[142]81 }
[325]82}
Note: See TracBrowser for help on using the repository browser.