- adding DefaultStateForm to redirect Sessions after reloading Sessions to a different Form instead of "restart" the session (i.e. on an in Bot wizard you just want to let the user continue on the main menu) - adding to SaveSessions to save the "main menu" instead of skipping the ignored form
100 lines
3.1 KiB
C#
100 lines
3.1 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Serialization.Formatters;
|
|
using System.Text;
|
|
using TelegramBotBase.Args;
|
|
using TelegramBotBase.Base;
|
|
using TelegramBotBase.Form;
|
|
using TelegramBotBase.Interfaces;
|
|
|
|
namespace TelegramBotBase.States
|
|
{
|
|
/// <summary>
|
|
/// Is used for all complex data types. Use if other default machines are not working.
|
|
/// </summary>
|
|
public class JSONStateMachine : IStateMachine
|
|
{
|
|
public String FilePath { get; set; }
|
|
|
|
public bool Overwrite { get; set; }
|
|
|
|
public Type DefaultStateForm { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Will initialize the state machine.
|
|
/// </summary>
|
|
/// <param name="file">Path of the file and name where to save the session details.</param>
|
|
/// <param name="defaultStateForm">Type of Form which will be saved instead of Form which has <seealso cref="Attributes.IgnoreState"/> attribute declared. Needs to be subclass of <seealso cref="Form.FormBase"/>.</param>
|
|
/// <param name="overwrite">Declares of the file could be overwritten.</param>
|
|
public JSONStateMachine(String file, Type defaultStateForm = null, bool overwrite = true)
|
|
{
|
|
if (file is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(file));
|
|
}
|
|
|
|
this.DefaultStateForm = defaultStateForm ?? typeof(FormBase);
|
|
|
|
if (!this.DefaultStateForm.IsSubclassOf(typeof(FormBase)))
|
|
{
|
|
throw new ArgumentException("DefaultStateForm is not a subclass of FormBase");
|
|
}
|
|
|
|
this.FilePath = file;
|
|
this.Overwrite = overwrite;
|
|
}
|
|
|
|
public StateContainer LoadFormStates()
|
|
{
|
|
try
|
|
{
|
|
var content = System.IO.File.ReadAllText(FilePath);
|
|
|
|
var sc = Newtonsoft.Json.JsonConvert.DeserializeObject<StateContainer>(content, new JsonSerializerSettings
|
|
{
|
|
TypeNameHandling = TypeNameHandling.All,
|
|
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
|
|
}) as StateContainer;
|
|
|
|
return sc;
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
return new StateContainer();
|
|
}
|
|
|
|
public void SaveFormStates(SaveStatesEventArgs e)
|
|
{
|
|
if (System.IO.File.Exists(FilePath))
|
|
{
|
|
if (!this.Overwrite)
|
|
{
|
|
throw new Exception("File exists already.");
|
|
}
|
|
|
|
System.IO.File.Delete(FilePath);
|
|
}
|
|
|
|
try
|
|
{
|
|
var content = Newtonsoft.Json.JsonConvert.SerializeObject(e.States, Formatting.Indented, new JsonSerializerSettings
|
|
{
|
|
TypeNameHandling = TypeNameHandling.All,
|
|
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
|
|
});
|
|
|
|
System.IO.File.WriteAllText(FilePath, content);
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|