2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/Backpack/BackpackService.cs
Ben Parsons 579b2b115d Fix issues with Journal handling
Implement basic database
Handle startup events
only send events after the most recent LoadGame
2024-05-25 16:19:07 +10:00

46 lines
1.5 KiB
C#

namespace Pulsar.Features.Backpack;
using Observatory.Framework.Files;
public interface IBackpackService : IJournalHandler<BackpackFile>;
public class BackpackService(IOptions<PulsarConfiguration> options, IEventHubContext hub, ILogger<BackpackService> logger) : IBackpackService
{
public async Task<BackpackFile> Get()
{
var filePath = Path.Combine(options.Value.JournalDirectory, FileName);
if (!FileHelper.ValidateFile(filePath))
{
return new BackpackFile();
}
await using var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var backpack = await JsonSerializer.DeserializeAsync<BackpackFile>(file);
if (backpack != null) return backpack;
logger.LogWarning("Failed to deserialize backpack file {File}", filePath);
return new BackpackFile();
}
public async Task HandleFile(string path, CancellationToken token = new ())
{
if (!FileHelper.ValidateFile(path))
{
return;
}
var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var backpack = await JsonSerializer.DeserializeAsync<BackpackFile>(file, cancellationToken: token);
if (backpack == null)
{
logger.LogWarning("Failed to deserialize backpack {FilePath}", file);
return;
}
await hub.Clients.All.BackpackUpdated(backpack);
}
public string FileName => FileHandlerService.BackpackFileName;
}