2017-02-07 00:21:39 +00:00
|
|
|
|
using System;
|
2017-02-12 20:03:32 +00:00
|
|
|
|
using System.Globalization;
|
2017-02-07 00:21:39 +00:00
|
|
|
|
using System.IO;
|
2020-12-30 05:40:42 +00:00
|
|
|
|
using System.Text.Json;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
2014-12-15 14:58:49 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Infrastructure.Storage
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Serialize object using json format.
|
|
|
|
|
|
/// </summary>
|
2021-05-11 19:10:03 +00:00
|
|
|
|
public class JsonStorage<T> where T : new()
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2021-02-14 10:08:30 +00:00
|
|
|
|
protected T _data;
|
2017-02-07 00:21:39 +00:00
|
|
|
|
// need a new directory name
|
|
|
|
|
|
public const string DirectoryName = "Settings";
|
|
|
|
|
|
public const string FileSuffix = ".json";
|
|
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
|
public string DirectoryPath { get; set; }
|
|
|
|
|
|
|
2016-04-21 00:53:21 +00:00
|
|
|
|
|
2017-02-06 22:04:52 +00:00
|
|
|
|
public T Load()
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2016-04-21 00:53:21 +00:00
|
|
|
|
if (File.Exists(FilePath))
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2021-05-11 19:10:03 +00:00
|
|
|
|
var serialized = File.ReadAllText(FilePath);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(serialized))
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2021-05-11 19:10:03 +00:00
|
|
|
|
Deserialize(serialized);
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
2016-04-21 00:53:21 +00:00
|
|
|
|
else
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2016-04-21 00:53:21 +00:00
|
|
|
|
LoadDefault();
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2016-04-21 00:53:21 +00:00
|
|
|
|
LoadDefault();
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
2017-02-13 09:38:48 +00:00
|
|
|
|
return _data.NonNull();
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-11 19:10:03 +00:00
|
|
|
|
private void Deserialize(string serialized)
|
2014-12-15 14:58:49 +00:00
|
|
|
|
{
|
2016-04-21 00:53:21 +00:00
|
|
|
|
try
|
2015-01-23 13:52:46 +00:00
|
|
|
|
{
|
2021-05-11 19:10:03 +00:00
|
|
|
|
_data = JsonSerializer.Deserialize<T>(serialized);
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
2017-02-20 12:16:49 +00:00
|
|
|
|
catch (JsonException e)
|
2016-04-21 00:53:21 +00:00
|
|
|
|
{
|
|
|
|
|
|
LoadDefault();
|
2021-05-11 19:10:03 +00:00
|
|
|
|
Log.Exception($"|JsonStorage.Deserialize|Deserialize error for json <{FilePath}>", e);
|
2016-04-21 00:53:21 +00:00
|
|
|
|
}
|
2017-02-13 09:38:48 +00:00
|
|
|
|
|
2021-06-21 02:34:07 +00:00
|
|
|
|
if (_data == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
LoadDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void LoadDefault()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(FilePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
BackupOriginFile();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_data = new T();
|
|
|
|
|
|
Save();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void BackupOriginFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
var timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff", CultureInfo.CurrentUICulture);
|
|
|
|
|
|
var directory = Path.GetDirectoryName(FilePath).NonNull();
|
|
|
|
|
|
var originName = Path.GetFileNameWithoutExtension(FilePath);
|
|
|
|
|
|
var backupName = $"{originName}-{timestamp}{FileSuffix}";
|
|
|
|
|
|
var backupPath = Path.Combine(directory, backupName);
|
|
|
|
|
|
File.Copy(FilePath, backupPath, true);
|
|
|
|
|
|
// todo give user notification for the backup process
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
|
{
|
|
|
|
|
|
string serialized = JsonSerializer.Serialize(_data, new JsonSerializerOptions() { WriteIndented = true });
|
|
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(FilePath, serialized);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-21 11:56:20 +00:00
|
|
|
|
[Obsolete("Deprecated as of Flow Launcher v1.8.0, on 2021.06.21. " +
|
|
|
|
|
|
"This is used only for Everything plugin v1.4.9 or below backwards compatibility")]
|
2021-06-21 11:04:19 +00:00
|
|
|
|
public class JsonStrorage<T> : JsonStorage<T> where T : new() { }
|
2014-12-15 14:58:49 +00:00
|
|
|
|
}
|