42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using ModVersionChecker.managers.interfaces;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace ModVersionChecker.managers.filesystem
|
|
{
|
|
public class AppsManager
|
|
{
|
|
private readonly string FilePath = Path.Combine(AppContext.BaseDirectory, "data", "apps.json");
|
|
|
|
public List<AppConfig> Load()
|
|
{
|
|
if (!File.Exists(FilePath))
|
|
return new List<AppConfig>();
|
|
var json = File.ReadAllText(FilePath);
|
|
return JsonSerializer.Deserialize<List<AppConfig>>(json) ?? new();
|
|
}
|
|
|
|
public void Save(List<AppConfig> apps)
|
|
{
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
var json = JsonSerializer.Serialize(apps, options);
|
|
File.WriteAllText(FilePath, json);
|
|
}
|
|
|
|
public void Upsert(AppConfig app)
|
|
{
|
|
var apps = Load();
|
|
var index = apps.FindIndex(a => a.Id == app.Id);
|
|
if (index >= 0)
|
|
{
|
|
apps[index] = app;
|
|
}
|
|
else
|
|
{
|
|
apps.Add(app);
|
|
}
|
|
Save(apps);
|
|
}
|
|
}
|
|
} |