using System; using System.IO; using System.Runtime.Serialization; using System.Xml; using TelegramBotBase.Args; using TelegramBotBase.Base; using TelegramBotBase.Form; using TelegramBotBase.Interfaces; namespace TelegramBotBase.States; public class XmlStateMachine : IStateMachine { /// /// Will initialize the state machine. /// /// Path of the file and name where to save the session details. /// /// Type of Form which will be saved instead of Form which has /// attribute declared. Needs to be subclass of /// . /// /// Declares of the file could be overwritten. public XmlStateMachine(string file, Type fallbackStateForm = null, bool overwrite = true) { FallbackStateForm = fallbackStateForm; if (FallbackStateForm != null && !FallbackStateForm.IsSubclassOf(typeof(FormBase))) { throw new ArgumentException("FallbackStateForm is not a subclass of FormBase"); } FilePath = file ?? throw new ArgumentNullException(nameof(file)); Overwrite = overwrite; } public string FilePath { get; set; } public bool Overwrite { get; set; } public Type FallbackStateForm { get; } public StateContainer LoadFormStates() { try { var serializer = new DataContractSerializer(typeof(StateContainer)); using (var reader = new StreamReader(FilePath)) { using (var xml = new XmlTextReader(reader)) { var sc = serializer.ReadObject(xml) as StateContainer; return sc; } } } catch { } return new StateContainer(); } public void SaveFormStates(SaveStatesEventArgs e) { if (File.Exists(FilePath)) { if (!Overwrite) { throw new Exception("File exists already."); } File.Delete(FilePath); } try { var serializer = new DataContractSerializer(typeof(StateContainer)); using (var sw = new StreamWriter(FilePath)) { using (var writer = new XmlTextWriter(sw)) { writer.Formatting = Formatting.Indented; // indent the Xml so it’s human readable serializer.WriteObject(writer, e.States); writer.Flush(); } } } catch { } } }