Initial commit
This commit is contained in:
149
NethackHelper/SaveData.cs
Normal file
149
NethackHelper/SaveData.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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() ?? [],
|
||||
};
|
||||
|
||||
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.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 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user