2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/FileHandlerService.cs
Ben Parsons b44d822eb9 Rework JournalHandler Again
Reduce Code Dupe
Finished ModulesInfo
Started ShipLocker
2024-04-21 23:40:44 +10:00

80 lines
2.7 KiB
C#

using Pulsar.Features.ModulesInfo;
using Pulsar.Features.ShipLocker;
namespace Pulsar.Features;
public interface IFileHandler
{
Task HandleFile(string path);
}
public interface IFileHandlerService : IFileHandler;
public class FileHandlerService(
ILogger<FileHandlerService> logger,
IStatusService statusService,
IShipLockerService shipLockerService,
IModulesInfoService modulesService) : IFileHandlerService
{
public static readonly string MarketFileName = "Market.json";
public static readonly string StatusFileName = "Status.json";
public static readonly string OutfittingFileName = "Outfitting.json";
public static readonly string ShipyardFileName = "Shipyard.json";
public static readonly string ModulesFileName = "Modules.json";
public static readonly string JournalLogFileNameRegEx = @"Journal\.\d\d\d\d-\d\d-\d\dT\d+\.\d\d\.log";
public static readonly string JournalLogFileName = "Journal.*.log";
public static readonly string RouteFileName = "Route.json";
public static readonly string CargoFileName = "Cargo.json";
public static readonly string BackpackFileName = "Backpack.json";
public static readonly string ModulesInfoFileName = "ModulesInfo.json";
public static readonly string ShipLockerFileName = "ShipLocker.json";
public static readonly string NavRouteFileName = "NavRoute.json";
public static readonly string[] AllFileNames =
[
MarketFileName,
StatusFileName,
OutfittingFileName,
ShipyardFileName,
ModulesFileName,
JournalLogFileNameRegEx,
RouteFileName,
CargoFileName,
BackpackFileName,
ModulesInfoFileName,
ShipLockerFileName,
NavRouteFileName
];
private readonly Dictionary<string, IJournalHandler> Handlers = new()
{
{ StatusFileName, statusService },
{ ModulesInfoFileName, modulesService },
{ ShipLockerFileName, shipLockerService }
};
public async Task HandleFile(string path)
{
var fileInfo = new FileInfo(path);
var fileName = fileInfo.Name;
// only scan the file if we recognize it
var match = AllFileNames.FirstOrDefault(
f => fileName.StartsWith(f, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrWhiteSpace(match))
{
logger.LogWarning("File {FileName} is not recognized", fileName);
return;
}
if (Handlers.TryGetValue(match, out var handler))
{
logger.LogInformation("Handling file {FileName}", fileName);
await handler.HandleFile(fileInfo.Name);
return;
}
logger.LogInformation("File {FileName} was not handled", fileName);
}
}