forked from new-frontiers-14/frontier-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.SaveLoad.cs
68 lines (56 loc) · 2.23 KB
/
Program.SaveLoad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Content.Server.Power.Pow3r;
using static Content.Server.Power.Pow3r.PowerState;
namespace Pow3r
{
internal sealed partial class Program
{
private void LoadFromDisk()
{
if (!File.Exists("data.json"))
return;
var dat = JsonSerializer.Deserialize<DiskDat>(File.ReadAllBytes("data.json"), SerializerOptions);
if (dat == null)
return;
_paused = dat.Paused;
_currentSolver = dat.Solver;
_state = new PowerState
{
Networks = GenIdStorage.FromEnumerable(dat.Networks.Select(n => (n.Id, n))),
Supplies = GenIdStorage.FromEnumerable(dat.Supplies.Select(s => (s.Id, s))),
Loads = GenIdStorage.FromEnumerable(dat.Loads.Select(l => (l.Id, l))),
Batteries = GenIdStorage.FromEnumerable(dat.Batteries.Select(b => (b.Id, b)))
};
_displayLoads = dat.Loads.ToDictionary(n => n.Id, _ => new DisplayLoad());
_displaySupplies = dat.Supplies.ToDictionary(n => n.Id, _ => new DisplaySupply());
_displayBatteries = dat.Batteries.ToDictionary(n => n.Id, _ => new DisplayBattery());
_displayNetworks = dat.Networks.ToDictionary(n => n.Id, _ => new DisplayNetwork());
RefreshLinks();
}
private void SaveToDisk()
{
var data = new DiskDat
{
Paused = _paused,
Solver = _currentSolver,
Loads = _state.Loads.Values.ToList(),
Batteries = _state.Batteries.Values.ToList(),
Networks = _state.Networks.Values.ToList(),
Supplies = _state.Supplies.Values.ToList()
};
File.WriteAllBytes("data.json", JsonSerializer.SerializeToUtf8Bytes(data, SerializerOptions));
}
private sealed class DiskDat
{
public bool Paused;
public int Solver;
public List<Load> Loads;
public List<Network> Networks;
public List<Supply> Supplies;
public List<Battery> Batteries;
}
}
}