Program.SaveLoad.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.Json;
  5. using Content.Server.Power.Pow3r;
  6. using static Content.Server.Power.Pow3r.PowerState;
  7. namespace Pow3r
  8. {
  9. internal sealed partial class Program
  10. {
  11. private void LoadFromDisk()
  12. {
  13. if (!File.Exists("data.json"))
  14. return;
  15. var dat = JsonSerializer.Deserialize<DiskDat>(File.ReadAllBytes("data.json"), SerializerOptions);
  16. if (dat == null)
  17. return;
  18. _paused = dat.Paused;
  19. _currentSolver = dat.Solver;
  20. _state = new PowerState
  21. {
  22. Networks = GenIdStorage.FromEnumerable(dat.Networks.Select(n => (n.Id, n))),
  23. Supplies = GenIdStorage.FromEnumerable(dat.Supplies.Select(s => (s.Id, s))),
  24. Loads = GenIdStorage.FromEnumerable(dat.Loads.Select(l => (l.Id, l))),
  25. Batteries = GenIdStorage.FromEnumerable(dat.Batteries.Select(b => (b.Id, b)))
  26. };
  27. _displayLoads = dat.Loads.ToDictionary(n => n.Id, _ => new DisplayLoad());
  28. _displaySupplies = dat.Supplies.ToDictionary(n => n.Id, _ => new DisplaySupply());
  29. _displayBatteries = dat.Batteries.ToDictionary(n => n.Id, _ => new DisplayBattery());
  30. _displayNetworks = dat.Networks.ToDictionary(n => n.Id, _ => new DisplayNetwork());
  31. RefreshLinks();
  32. }
  33. private void SaveToDisk()
  34. {
  35. var data = new DiskDat
  36. {
  37. Paused = _paused,
  38. Solver = _currentSolver,
  39. Loads = _state.Loads.Values.ToList(),
  40. Batteries = _state.Batteries.Values.ToList(),
  41. Networks = _state.Networks.Values.ToList(),
  42. Supplies = _state.Supplies.Values.ToList()
  43. };
  44. File.WriteAllBytes("data.json", JsonSerializer.SerializeToUtf8Bytes(data, SerializerOptions));
  45. }
  46. private sealed class DiskDat
  47. {
  48. public bool Paused;
  49. public int Solver;
  50. public List<Load> Loads;
  51. public List<Network> Networks;
  52. public List<Supply> Supplies;
  53. public List<Battery> Batteries;
  54. }
  55. }
  56. }