Last change
on this file since 417 was 402, checked in by alloc, 22 months ago |
- Major refactoring
- Using Utf8Json for (de)serialization
- Moving APIs to REST
- Removing dependencies from WebServer and MapRenderer to ServerFixes
|
File size:
1.4 KB
|
Rev | Line | |
---|
[142] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
| 3 | using System.Runtime.Serialization;
|
---|
| 4 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
| 5 |
|
---|
[325] | 6 | namespace AllocsFixes.PersistentData {
|
---|
[143] | 7 | [Serializable]
|
---|
[325] | 8 | public class PersistentContainer {
|
---|
[144] | 9 | private Players players;
|
---|
[325] | 10 | [OptionalField] private Attributes attributes;
|
---|
[144] | 11 |
|
---|
[391] | 12 | public Players Players => players ?? (players = new Players ());
|
---|
[325] | 13 |
|
---|
[391] | 14 | public Attributes Attributes => attributes ?? (attributes = new Attributes ());
|
---|
[144] | 15 |
|
---|
[142] | 16 | private static PersistentContainer instance;
|
---|
| 17 |
|
---|
[391] | 18 | public static PersistentContainer Instance => instance ?? (instance = new PersistentContainer ());
|
---|
[325] | 19 |
|
---|
| 20 | private PersistentContainer () {
|
---|
[142] | 21 | }
|
---|
| 22 |
|
---|
[325] | 23 | public void Save () {
|
---|
[402] | 24 | Stream stream = File.Open ($"{GameIO.GetSaveGameDir ()}/AllocsPeristentData.bin", FileMode.Create);
|
---|
[142] | 25 | BinaryFormatter bFormatter = new BinaryFormatter ();
|
---|
| 26 | bFormatter.Serialize (stream, this);
|
---|
| 27 | stream.Close ();
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[325] | 30 | public static bool Load () {
|
---|
[402] | 31 | if (!File.Exists ($"{GameIO.GetSaveGameDir ()}/AllocsPeristentData.bin")) {
|
---|
[326] | 32 | return false;
|
---|
[146] | 33 | }
|
---|
[325] | 34 |
|
---|
[326] | 35 | try {
|
---|
[402] | 36 | Stream stream = File.Open ($"{GameIO.GetSaveGameDir ()}/AllocsPeristentData.bin", FileMode.Open);
|
---|
[326] | 37 | BinaryFormatter bFormatter = new BinaryFormatter ();
|
---|
[391] | 38 | PersistentContainer obj = (PersistentContainer) bFormatter.Deserialize (stream);
|
---|
[326] | 39 | stream.Close ();
|
---|
| 40 | instance = obj;
|
---|
| 41 | return true;
|
---|
| 42 | } catch (Exception e) {
|
---|
| 43 | Log.Error ("Exception in PersistentContainer.Load");
|
---|
| 44 | Log.Exception (e);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[146] | 47 | return false;
|
---|
[142] | 48 | }
|
---|
| 49 | }
|
---|
[325] | 50 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.