TelegramBotFramework/TelegramBotBase/States/SimpleJSONStateMachine.cs
Максим Човнюк 367de14a1a
Some checks failed
build nuget workflow for TelegramBotBase project / Build-TelegramBotBase (x64, linux) (push) Failing after 41s
update dependencies and add workflow for publish nuget
2024-12-05 16:16:49 +05:00

87 lines
2.4 KiB
C#

using System;
using System.IO;
using System.Text.Json;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.States;
/// <summary>
/// Is used for simple object structures like classes, lists or basic datatypes without generics and other compiler
/// based data types.
/// </summary>
public class SimpleJsonStateMachine : IStateMachine
{
/// <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="fallbackStateForm">
/// 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 SimpleJsonStateMachine(string file, Type fallbackStateForm = null, bool overwrite = true)
{
FallbackStateForm = fallbackStateForm;
if (FallbackStateForm != null && !FallbackStateForm.IsSubclassOf(typeof(FormBase)))
{
throw new ArgumentException($"{nameof(FallbackStateForm)} is not a subclass of {nameof(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 content = File.ReadAllText(FilePath);
var sc = JsonSerializer.Deserialize<StateContainer>(content);
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 content = JsonSerializer.Serialize(e.States, new JsonSerializerOptions() {
WriteIndented = true
});
File.WriteAllText(FilePath, content);
}
catch
{
}
}
}