Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ TobiasSekan
Img
img
resx
bak
tmp
directx
mvvm
dlg
Expand All @@ -85,4 +87,4 @@ searchplugin
Noresult
wpftk
mkv
flac
flac
6 changes: 4 additions & 2 deletions Flow.Launcher.Infrastructure/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json;
Expand All @@ -16,7 +18,7 @@ static Helper()
/// <summary>
/// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy
/// </summary>
public static T NonNull<T>(this T obj)
public static T NonNull<T>(this T? obj)
{
if (obj == null)
{
Expand Down
83 changes: 48 additions & 35 deletions Flow.Launcher.Infrastructure/Storage/JsonStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Globalization;
using System.IO;
using System.Text.Json;
Expand All @@ -11,62 +12,73 @@ namespace Flow.Launcher.Infrastructure.Storage
/// </summary>
public class JsonStorage<T> where T : new()
{
protected T _data;
protected T? Data;
// 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; }
protected string FilePath { get; init; } = null!;
private string TempFilePath => $"{FilePath}.tmp";
private string BackupFilePath => $"{FilePath}.bak";
protected string DirectoryPath { get; init; } = null!;


public T Load()
{
string? serialized = null;

if (File.Exists(FilePath))
{
var serialized = File.ReadAllText(FilePath);
if (!string.IsNullOrWhiteSpace(serialized))
serialized = File.ReadAllText(FilePath);
}

if (!string.IsNullOrEmpty(serialized))
{
try
{
Deserialize(serialized);
Data = JsonSerializer.Deserialize<T>(serialized)?? TryLoadBackup() ?? LoadDefault();
}
else
catch (JsonException)
{
LoadDefault();
Data = TryLoadBackup() ?? LoadDefault();
}
}
else
{
LoadDefault();
Data = TryLoadBackup() ?? LoadDefault();
}
return _data.NonNull();
return Data.NonNull();
}

private void Deserialize(string serialized)
private T LoadDefault()
{
try
{
_data = JsonSerializer.Deserialize<T>(serialized);
}
catch (JsonException e)
if (File.Exists(FilePath))
{
LoadDefault();
Log.Exception($"|JsonStorage.Deserialize|Deserialize error for json <{FilePath}>", e);
BackupOriginFile();
}

if (_data == null)
{
LoadDefault();
}
return new T();
}

private void LoadDefault()
private T? TryLoadBackup()
{
if (File.Exists(FilePath))
if (!File.Exists(BackupFilePath))
return default;

try
{
BackupOriginFile();
var data = JsonSerializer.Deserialize<T>(File.ReadAllText(BackupFilePath));
if (data != null)
{
Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully");
File.Replace(BackupFilePath, FilePath, null);
return data;
}
return default;
}
catch (JsonException)
{
return default;
}

_data = new T();
Save();
}

private void BackupOriginFile()
Expand All @@ -82,13 +94,14 @@ private void BackupOriginFile()

public void Save()
{
string serialized = JsonSerializer.Serialize(_data, new JsonSerializerOptions() { WriteIndented = true });
string serialized = JsonSerializer.Serialize(Data, new JsonSerializerOptions
{
WriteIndented = true
});

File.WriteAllText(FilePath, serialized);
File.WriteAllText(TempFilePath, serialized);
File.Replace(TempFilePath, FilePath, BackupFilePath);
File.Delete(TempFilePath);
}
}

[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")]
public class JsonStrorage<T> : JsonStorage<T> where T : new() { }
}
2 changes: 1 addition & 1 deletion Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public PluginJsonStorage()

public PluginJsonStorage(T data) : this()
{
_data = data;
Data = data;
}
}
}
Expand Down