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

Last change on this file since 330 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

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