2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-07-01 08:23:42 -04:00

Add Shipyard & Fix ShipLocker

Update File handling
Update Journal handling
Update README.md
This commit is contained in:
2024-05-10 22:26:44 +10:00
parent bc958e7679
commit 235cb2401a
17 changed files with 255 additions and 530 deletions

View File

@ -2,6 +2,7 @@ namespace Pulsar.Features;
using Observatory.Framework.Files;
using Observatory.Framework.Files.Journal;
using Observatory.Framework.Files.Journal.Odyssey;
public class EventsHub : Hub<IEventsHub>
{
@ -24,6 +25,8 @@ public class EventsHub : Hub<IEventsHub>
public async Task CargoUpdated(CargoFile cargo) => await Clients.All.CargoUpdated(cargo);
public async Task BackpackUpdated(BackpackFile backpack) => await Clients.All.BackpackUpdated(backpack);
public async Task ShipLockerUpdated(ShipLockerMaterials shipLocker) => await Clients.All.ShipLockerUpdated(shipLocker);
}
public interface IEventsHub
@ -47,4 +50,6 @@ public interface IEventsHub
Task CargoUpdated(CargoFile cargo);
Task BackpackUpdated(BackpackFile backpack);
Task ShipLockerUpdated(ShipLockerMaterials shipLocker);
}

View File

@ -1,8 +1,6 @@
using Observatory.Framework.Files;
using Observatory.Framework.Files.Journal;
using Observatory.Framework.Files.Journal.Odyssey;
using Pulsar.Features.ModulesInfo;
using Pulsar.Features.ShipLocker;
namespace Pulsar.Features;
public interface IFileHandler
@ -23,6 +21,8 @@ public class FileHandlerService(
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 JournalLogFileNameStart = "Journal.";
public static readonly string JournalLogFileNameEnd = ".log";
public static readonly string RouteFileName = "Route.json";
public static readonly string CargoFileName = "Cargo.json";
public static readonly string BackpackFileName = "Backpack.json";
@ -37,7 +37,7 @@ public class FileHandlerService(
OutfittingFileName,
ShipyardFileName,
ModulesFileName,
JournalLogFileNameRegEx,
JournalLogFileNameStart,
RouteFileName,
CargoFileName,
BackpackFileName,
@ -51,8 +51,14 @@ public class FileHandlerService(
{ StatusFileName, typeof(IJournalHandler<Observatory.Framework.Files.Status>) },
{ ModulesInfoFileName, typeof(IJournalHandler<ModuleInfoFile>) },
{ ShipLockerFileName, typeof(IJournalHandler<ShipLockerMaterials>) },
{ ShipLockerFileName, typeof(IJournalHandler<ShipLockerMaterials>) },
{ ShipyardFileName, typeof(IJournalHandler<ShipyardFile>) },
{ MarketFileName, typeof(IJournalHandler<MarketFile>) },
{ NavRouteFileName, typeof(IJournalHandler<NavRouteFile>) },
{ CargoFileName, typeof(IJournalHandler<CargoFile>) },
{ BackpackFileName, typeof(IJournalHandler<BackpackFile>) },
{ RouteFileName, typeof(IJournalHandler<NavRouteFile>) },
{ OutfittingFileName, typeof(IJournalHandler<OutfittingFile>) },
{ JournalLogFileNameStart, typeof(IJournalHandler<List<JournalBase>>) }
};
public async Task HandleFile(string path)
@ -70,13 +76,19 @@ public class FileHandlerService(
return;
}
if (Handlers.TryGetValue(match, out var type))
if (!Handlers.TryGetValue(match, out var type))
{
logger.LogInformation("Handling file {FileName} with Type {Type}", fileName);
(serviceProvider.GetRequiredService(type) as IJournalHandler)?.HandleFile(path);
logger.LogInformation("File {FileName} was not handled", fileName);
return;
}
logger.LogInformation("File {FileName} was not handled", fileName);
if (serviceProvider.GetRequiredService(type) is not IJournalHandler handler)
{
logger.LogWarning("Handler for {FileName} is not available", fileName);
return;
}
logger.LogInformation("Handling file {FileName} with Type {Type}", fileName, handler.GetType().ToString());
await handler.HandleFile(path);
}
}

View File

@ -26,7 +26,10 @@ public class FileWatcherService(IOptions<PulsarConfiguration> options, IFileHand
{
foreach (var file in watcher.GetDirectoryContents(""))
{
if (!file.Name.EndsWith(".json") || file.IsDirectory) continue;
if (file.IsDirectory || !file.Name.EndsWith(".json") && !(file.Name.StartsWith(FileHandlerService.JournalLogFileNameStart) && file.Name.EndsWith(FileHandlerService.JournalLogFileNameEnd)))
{
continue;
}
var existing = FileDates.GetOrAdd(file.PhysicalPath, file.LastModified);
@ -40,7 +43,7 @@ public class FileWatcherService(IOptions<PulsarConfiguration> options, IFileHand
private void Watch()
{
watcher.Watch("**/*.json").RegisterChangeCallback(HandleFileChanged, null);
watcher.Watch("*.*").RegisterChangeCallback(HandleFileChanged, null);
}
public Task StopAsync(CancellationToken cancellationToken)

View File

@ -11,34 +11,9 @@ public class ModulesInfoService(
{
public string FileName => FileHandlerService.ModulesInfoFileName;
public bool ValidateFile(string filePath)
{
if (!File.Exists(filePath))
{
logger.LogWarning("Journal file {JournalFile} does not exist", filePath);
return false;
}
var fileInfo = new FileInfo(filePath);
if (!string.Equals(fileInfo.Name, FileName, StringComparison.InvariantCultureIgnoreCase))
{
logger.LogWarning("Journal file {name} is not valid");
return false;
}
if (fileInfo.Length == 0)
{
logger.LogWarning("Journal file {name} is empty", filePath);
return false;
}
return true;
}
public async Task HandleFile(string filePath)
{
if (!ValidateFile(filePath))
if (!FileHelper.ValidateFile(filePath))
{
return;
}
@ -59,7 +34,7 @@ public class ModulesInfoService(
{
var moduleInfoFile = Path.Combine(options.Value.JournalDirectory, FileName);
if (!ValidateFile(moduleInfoFile))
if (!FileHelper.ValidateFile(moduleInfoFile))
{
return new ModuleInfoFile();
}

View File

@ -4,43 +4,45 @@ using Observatory.Framework.Files.Journal.Odyssey;
public interface IShipLockerService : IJournalHandler<ShipLockerMaterials>;
public class ShipLockerService(ILogger<ShipLockerService> logger)
public class ShipLockerService(ILogger<ShipLockerService> logger, IOptions<PulsarConfiguration> options,
IEventHubContext hub)
: IShipLockerService
{
public string FileName => FileHandlerService.ShipLockerFileName;
public bool ValidateFile(string filePath)
public async Task<ShipLockerMaterials> Get()
{
if (!File.Exists(filePath))
var shipLockerFile = Path.Combine(options.Value.JournalDirectory, FileName);
if (!FileHelper.ValidateFile(shipLockerFile))
{
logger.LogWarning("Journal file {JournalFile} does not exist", filePath);
return false;
return new ShipLockerMaterials();
}
var fileInfo = new FileInfo(filePath);
await using var file = File.Open(shipLockerFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var shipLocker = await JsonSerializer.DeserializeAsync<ShipLockerMaterials>(file);
if (shipLocker != null) return shipLocker;
if (!string.Equals(fileInfo.Name, FileName, StringComparison.InvariantCultureIgnoreCase))
{
logger.LogWarning("Journal file {name} is not valid");
return false;
}
if (fileInfo.Length == 0)
{
logger.LogWarning("Journal file {name} is empty", filePath);
return false;
}
return true;
logger.LogWarning("Failed to deserialize ship locker file {ShipLockerFile}", shipLockerFile);
return new ShipLockerMaterials();
}
public Task<ShipLockerMaterials> Get()
public async Task HandleFile(string filePath)
{
throw new NotImplementedException();
}
if (!FileHelper.ValidateFile(filePath))
{
return;
}
public Task HandleFile(string filePath)
{
throw new NotImplementedException();
var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var shipLocker = await JsonSerializer.DeserializeAsync<ShipLockerMaterials>(file);
if (shipLocker == null)
{
logger.LogWarning("Failed to deserialize status file {FilePath}", filePath);
return;
}
await hub.Clients.All.ShipLockerUpdated(shipLocker);
}
}

View File

@ -0,0 +1,47 @@
using Observatory.Framework.Files;
namespace Pulsar.Features.Shipyard;
public interface IShipyardService : IJournalHandler<ShipyardFile>;
public class ShipyardService(ILogger<ShipyardService> logger, IOptions<PulsarConfiguration> options,
IEventHubContext hub) : IShipyardService
{
public string FileName => FileHandlerService.ShipyardFileName;
public async Task<ShipyardFile> Get()
{
var shipyardFile = Path.Combine(options.Value.JournalDirectory, FileName);
if (!FileHelper.ValidateFile(shipyardFile))
{
return new ShipyardFile();
}
await using var file = File.Open(shipyardFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var shipyard = await JsonSerializer.DeserializeAsync<ShipyardFile>(file);
if (shipyard != null) return shipyard;
logger.LogWarning("Failed to deserialize shipyard file {ShipyardFile}", shipyardFile);
return new ShipyardFile();
}
public async Task HandleFile(string path)
{
if (!FileHelper.ValidateFile(path))
{
return;
}
var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var shipyard = await JsonSerializer.DeserializeAsync<ShipyardFile>(file);
if (shipyard == null)
{
logger.LogWarning("Failed to deserialize status file {FilePath}", path);
return;
}
await hub.Clients.All.ShipyardUpdated(shipyard);
}
}

View File

@ -40,7 +40,7 @@ public class StatusService
public async Task HandleFile(string filePath)
{
if (!ValidateFile(filePath))
if (!FileHelper.ValidateFile(filePath))
{
return;
}
@ -61,7 +61,7 @@ public class StatusService
{
var statusFile = Path.Combine(options.Value.JournalDirectory, FileName);
if (!ValidateFile(statusFile))
if (!FileHelper.ValidateFile(statusFile))
{
return new Status();
}