public async Task <T> LoadAsync() { if (!File.Exists(_file)) { return(await AskUserForStateIfUserEditableAsync()); } string text; if (_flags.HasFlag(StateHandlerFlags.UseCipher)) { text = await Env.Cipher.TryDecryptFileAsync(_file); if (text == null) { await AskUserToDeleteAsync(); text = ""; } } else { text = File.ReadAllText(_file); } _hash = Helper.GetSha256(text); if ((Program.Reset || string.IsNullOrWhiteSpace(text)) && _flags.HasFlag(StateHandlerFlags.UserEditable)) { return(await AskUserForStateAsync(text)); } if (string.IsNullOrWhiteSpace(text)) { return(UseCurrentState()); } return(await ParseTextAsync(text)); }
public IStateHandler <T> Create <T>(T state, string file, StateHandlerFlags flags) where T : IState { if (flags.HasFlag(StateHandlerFlags.Exportable)) { _exportableStateFiles.Add(new StateFile(file, flags)); } return(new JsonStateHandler <T>(state, file, flags)); }
private byte[] _hash = new byte[0]; // We use this to check for changes. public JsonStateHandler(T state, string file, StateHandlerFlags flags) { _state = state; _file = Path.IsPathRooted(file) ? file : Path.Combine(Env.Config.DataDir, file + (flags.HasFlag(StateHandlerFlags.UseCipher) ? "" : ".json")); _flags = flags; if (_flags.HasFlag(StateHandlerFlags.SavePeriodically)) { Env.App.AddSaveAction(SaveAsync); } }