Files
NethackHelper/NethackHelper/SaveData.cs

209 lines
7.8 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);
SaveFile? save = null;
try {
save = YamlOptions.DeserializeFileOrDefault<SaveFile>(fileName);
} catch (YamlDotNet.Core.YamlException) {
}
if (save != null) {
this.Version = save.Version;
this.CharismaClass = save.CharismaClass;
this.Sucker = save.Sucker;
this.Properties = save.Properties;
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,
Properties = this.Properties,
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.Properties = new();
}
public string Version { get; set; } = string.Empty;
public CharismaClass CharismaClass { get; set; } = CharismaClass.DefaultClass;
public bool Sucker { get; set; }
public GameProperties Properties { 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 = [];
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 GameProperties Properties { get; set; } = new();
public List<ItemRecord> Potions { get; set; } = [];
public List<ItemRecord> Rings { get; set; } = [];
public List<ItemRecord> Scrolls { get; set; } = [];
public List<ItemRecord> Wands { get; set; } = [];
public List<ItemRecord> Spellbooks { get; set; } = [];
public List<ItemRecord> Amulets { get; set; } = [];
public List<ItemRecord> Tools { get; set; } = [];
public List<ItemRecord> Armor { get; set; } = [];
}
public struct Property {
public bool Intrinsic { get; set; }
public bool Extrinsic { get; set; }
}
public class GameProperties {
public Property ColdResistance { get; set; }
public Property DisintegrationResistance { get; set; }
public Property FireResistance { get; set; }
public Property Invisibility { get; set; }
public Property PoisonResistance { get; set; }
public Property Searching { get; set; }
public Property SeeInvisible { get; set; }
public Property ShockResistance { get; set; }
public Property SleepResistance { get; set; }
public Property Speed { get; set; }
public Property Stealth { get; set; }
public Property Telepathy { get; set; }
public Property TeleportControl { get; set; }
public Property Teleportitis { get; set; }
public Property Warning { get; set; }
public Property ProtectionFromShapeChangers { get; set; }
public Property PolymorphControl { get; set; }
public Property Polymorphitis { get; set; }
public Property MagicResistance { get; set; }
public Property Reflection { get; set; }
public Property Regeneration { get; set; }
public Property Conflict { get; set; }
public Property AggravateMonster { get; set; }
public Property MagicalBreathing { get; set; }
public Property AcidResistance { get; set; }
public Property SlowDigestion { get; set; }
public Property FreeAction { get; set; }
public Property Hunger { get; set; }
public Property Jumping { get; set; }
public Property Flying { get; set; }
public Property LifeSaving { get; set; }
public Property WaterWalking { get; set; }
public int Protection { get; set; }
public int LastPrayer { get; set; }
public bool GodAngry { get; set; }
}
public class PropertiesSource : BindingSource {
public PropertiesSource() {
this.DataSource = SaveData.Instance.Properties;
}
}
public class SaveSource : BindingSource {
public SaveSource() {
this.DataSource = SaveData.Instance;
}
}
}