Files
NethackHelper/NethackHelper/SaveData.cs
2025-09-24 23:54:20 -05:00

178 lines
6.6 KiB
C#

namespace NethackHelper {
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
public class SaveData {
private const string SAVE_NAME = "savedata.yaml";
private static SaveData? _instance;
public static SaveData Instance {
get {
_instance ??= new();
return _instance;
}
}
private SaveData() {
var fileName = Path.Combine(Application.UserAppDataPath, SAVE_NAME);
var save = YamlOptions.DeserializeFileOrDefault<SaveFile>(fileName);
if (save != null) {
this.Version = save.Version;
this.CharismaClass = save.CharismaClass;
this.Sucker = save.Sucker;
this.Intrinsics = save.Intrinsics;
if (save.Potions != null && save.Potions.Count > 0) {
this.Potions = new(save.Potions);
}
if (save.Rings != null && save.Rings.Count > 0) {
this.Rings = new(save.Rings);
}
if (save.Scrolls != null && save.Scrolls.Count > 0) {
this.Scrolls = new(save.Scrolls);
}
if (save.Wands != null && save.Wands.Count > 0) {
this.Wands = new(save.Wands);
}
if (save.Spellbooks != null && save.Spellbooks.Count > 0) {
this.Spellbooks = new(save.Spellbooks);
}
if (save.Amulets != null && save.Amulets.Count > 0) {
this.Amulets = new(save.Amulets);
}
if (save.Tools != null && save.Tools.Count > 0) {
this.Tools = new(save.Tools);
}
if (save.Armor != null && save.Armor.Count > 0) {
this.Armor = new(save.Armor);
}
}
}
public void Save() {
var fileName = Path.Combine(Application.UserAppDataPath, SAVE_NAME);
var save = new SaveFile() {
Version = this.Version,
CharismaClass = this.CharismaClass,
Sucker = this.Sucker,
Intrinsics = this.Intrinsics,
Potions = this.Potions?.Export() ?? [],
Rings = this.Rings?.Export() ?? [],
Scrolls = this.Scrolls?.Export() ?? [],
Wands = this.Wands?.Export() ?? [],
Spellbooks = this.Spellbooks?.Export() ?? [],
Amulets = this.Amulets?.Export() ?? [],
Tools = this.Tools?.Export() ?? [],
Armor = this.Armor?.Export() ?? [],
};
YamlOptions.SerializeToFile(fileName, save);
}
public void Reset(string version) {
this.Version = version;
this.CharismaClass = CharismaClass.DefaultClass;
this.Sucker = false;
this.Potions = null;
this.Rings = null;
this.Scrolls = null;
this.Wands = null;
this.Spellbooks = null;
this.Amulets = null;
this.Tools = null;
this.Armor = null;
this.Intrinsics = new();
}
public string Version { get; set; } = string.Empty;
public CharismaClass CharismaClass { get; set; } = CharismaClass.DefaultClass;
public bool Sucker { get; set; }
public Intrinsics Intrinsics { get; set; } = new();
public IdentificationRecord? Potions { get; set; }
public IdentificationRecord? Rings { get; set; }
public IdentificationRecord? Scrolls { get; set; }
public IdentificationRecord? Wands { get; set; }
public IdentificationRecord? Spellbooks { get; set; }
public IdentificationRecord? Amulets { get; set; }
public IdentificationRecord? Tools { get; set; }
public IdentificationRecord? Armor { get; set; }
}
public struct ItemRecord {
public string Name { get; set; }
public string? Appearance { get; set; }
}
public class IdentificationRecord {
private readonly Dictionary<string, ItemRecord> identifications = new();
public IdentificationRecord(List<ItemRecord> records) {
this.identifications = records.ToDictionary(record => record.Name);
}
public bool HasIdentified(string name) {
return identifications.ContainsKey(name);
}
public bool TryGetValue(string name, out ItemRecord record) {
return identifications.TryGetValue(name, out record);
}
public List<ItemRecord> Export() {
return identifications.Values.ToList();
}
}
public class SaveFile {
public string Version { get; set; } = string.Empty;
public CharismaClass CharismaClass { get; set; } = CharismaClass.DefaultClass;
public bool Sucker { get; set; }
public Intrinsics Intrinsics { get; set; } = new();
public List<ItemRecord> Potions { get; set; } = new();
public List<ItemRecord> Rings { get; set; } = new();
public List<ItemRecord> Scrolls { get; set; } = new();
public List<ItemRecord> Wands { get; set; } = new();
public List<ItemRecord> Spellbooks { get; set; } = new();
public List<ItemRecord> Amulets { get; set; } = new();
public List<ItemRecord> Tools { get; set; } = new();
public List<ItemRecord> Armor { get; set; } = new();
}
public class Intrinsics {
public bool ColdResistance { get; set; }
public bool DisintegrationResistance { get; set; }
public bool FireResistance { get; set; }
public bool Invisible { get; set; }
public bool PoisonResistance { get; set; }
public bool Searching { get; set; }
public bool SeeInvisible { get; set; }
public bool ShockResistance { get; set; }
public bool SleepResistance { get; set; }
public bool Speed { get; set; }
public bool Stealth { get; set; }
public bool Telepathy { get; set; }
public bool TeleportControl { get; set; }
public bool Teleportitis { get; set; }
public bool Warning { get; set; }
public int Protection { get; set; }
public int LastPrayer { get; set; }
}
public class IntrinsicsSource : BindingSource {
public IntrinsicsSource() {
this.DataSource = SaveData.Instance.Intrinsics;
}
}
public class SaveSource : BindingSource {
public SaveSource() {
this.DataSource = SaveData.Instance;
}
}
}