コード例 #1
0
        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));
        }
コード例 #2
0
 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));
 }
コード例 #3
0
        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);
            }
        }