begin application build overhaul
This commit is contained in:
128
Services/ConfigService.cs
Normal file
128
Services/ConfigService.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using UnifiedFarmLauncher.Models;
|
||||
|
||||
namespace UnifiedFarmLauncher.Services
|
||||
{
|
||||
public class ConfigService
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
private readonly string _configPath;
|
||||
private ConfigRoot? _config;
|
||||
|
||||
public ConfigService()
|
||||
{
|
||||
var appDataPath = GetAppDataPath();
|
||||
Directory.CreateDirectory(appDataPath);
|
||||
_configPath = Path.Combine(appDataPath, "workers.json");
|
||||
}
|
||||
|
||||
private static string GetAppDataPath()
|
||||
{
|
||||
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
return Path.Combine(localAppData, "UnifiedFarmLauncher");
|
||||
}
|
||||
|
||||
public ConfigRoot Load()
|
||||
{
|
||||
if (_config != null)
|
||||
return _config;
|
||||
|
||||
if (!File.Exists(_configPath))
|
||||
{
|
||||
_config = new ConfigRoot();
|
||||
Save(_config);
|
||||
return _config;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(_configPath);
|
||||
_config = JsonSerializer.Deserialize<ConfigRoot>(json, JsonOptions) ?? new ConfigRoot();
|
||||
return _config;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to load configuration from {_configPath}: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Save(ConfigRoot? config = null)
|
||||
{
|
||||
config ??= _config ?? new ConfigRoot();
|
||||
_config = config;
|
||||
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(config, JsonOptions);
|
||||
File.WriteAllText(_configPath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to save configuration to {_configPath}: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
_config = null;
|
||||
Load();
|
||||
}
|
||||
|
||||
public WorkerConfig? GetWorker(int id)
|
||||
{
|
||||
return Load().Workers.FirstOrDefault(w => w.Id == id);
|
||||
}
|
||||
|
||||
public WorkerConfig? GetWorkerByName(string name)
|
||||
{
|
||||
return Load().Workers.FirstOrDefault(w => w.Name == name);
|
||||
}
|
||||
|
||||
public void AddWorker(WorkerConfig worker)
|
||||
{
|
||||
var config = Load();
|
||||
if (config.Workers.Any(w => w.Id == worker.Id || w.Name == worker.Name))
|
||||
throw new InvalidOperationException($"Worker with ID {worker.Id} or name '{worker.Name}' already exists");
|
||||
|
||||
config.Workers.Add(worker);
|
||||
Save(config);
|
||||
}
|
||||
|
||||
public void UpdateWorker(WorkerConfig worker)
|
||||
{
|
||||
var config = Load();
|
||||
var index = config.Workers.FindIndex(w => w.Id == worker.Id);
|
||||
if (index < 0)
|
||||
throw new InvalidOperationException($"Worker with ID {worker.Id} not found");
|
||||
|
||||
config.Workers[index] = worker;
|
||||
Save(config);
|
||||
}
|
||||
|
||||
public void DeleteWorker(int id)
|
||||
{
|
||||
var config = Load();
|
||||
var worker = config.Workers.FirstOrDefault(w => w.Id == id);
|
||||
if (worker == null)
|
||||
throw new InvalidOperationException($"Worker with ID {id} not found");
|
||||
|
||||
config.Workers.Remove(worker);
|
||||
Save(config);
|
||||
}
|
||||
|
||||
public int GetNextWorkerId()
|
||||
{
|
||||
var config = Load();
|
||||
return config.Workers.Count > 0 ? config.Workers.Max(w => w.Id) + 1 : 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user