mirror of
https://github.com/sp-tarkov/loot-dump-processor.git
synced 2025-02-13 09:50:44 -05:00

* Improved thread safety and async processing in dump processor components * Removed unused _processedDumps field and simplified variable scope in QueuePipeline * Refactored service registration into dedicated extension methods * Added configuration binding and environment variables support * Refactored collector initialization to use dependency injection * Refactored data storage to use dependency injection * Refactored configuration models to use records and added validation * Refactored static loot configuration to use dependency injection The changes include: - Moved static weapon IDs and forced items from LootDumpProcessorContext to ForcedStatic record - Added ForcedStatic configuration injection in StaticLootProcessor and StaticContainerProcessor - Improved immutability by using read-only collections in ForcedStatic model - Simplified LootDumpProcessorContext by removing unused methods * Refactored configuration access to use dependency injection consistently * Fixed ForcedStatic configuration * Refactored forced items configuration to use async provider pattern The changes introduce a new `IForcedItemsProvider` abstraction to handle loading and caching of forced static and loose loot configurations. This improves the code by: 1. Making configuration loading asynchronous 2. Implementing caching of loaded configurations 3. Centralizing forced items configuration access 4. Removing direct file system dependencies from processors 5. Improving testability through dependency injection The change also updates related processors and interfaces to use async/await pattern consistently. * Refactored loose loot processor to use async forced items provider * Reorganized processor and service components into dedicated namespaces
116 lines
4.2 KiB
C#
116 lines
4.2 KiB
C#
using System.Collections.Frozen;
|
|
using System.Text.Json;
|
|
using LootDumpProcessor.Model.Config;
|
|
using LootDumpProcessor.Model.Tarkov;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace LootDumpProcessor.Process.Services.TarkovItemsProvider;
|
|
|
|
public class TarkovItemsProvider : ITarkovItemsProvider
|
|
{
|
|
private readonly ILogger<TarkovItemsProvider> _logger;
|
|
private readonly FrozenDictionary<string, TemplateFileItem>? _items;
|
|
private readonly Config _config;
|
|
|
|
private string ItemsFilePath => Path.Combine(
|
|
_config.ServerLocation,
|
|
"project", "assets", "database", "templates", "items.json");
|
|
|
|
public TarkovItemsProvider(ILogger<TarkovItemsProvider> logger, IOptions<Config> config)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
_config = (config ?? throw new ArgumentNullException(nameof(config))).Value;
|
|
|
|
try
|
|
{
|
|
var jsonContent = File.ReadAllText(ItemsFilePath);
|
|
_items = (JsonSerializer.Deserialize<Dictionary<string, TemplateFileItem>>(jsonContent)
|
|
?? throw new InvalidOperationException()).ToFrozenDictionary();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to load server items from {ItemsPath}", ItemsFilePath);
|
|
throw new InvalidOperationException(
|
|
"The server items couldn't be found or loaded. Check server config is pointing to the correct place.",
|
|
ex);
|
|
}
|
|
}
|
|
|
|
public bool IsBaseClass(string tpl, string baseclassId)
|
|
{
|
|
if (_items == null)
|
|
{
|
|
_logger.LogError("The server items are null. Check server config is pointing to the correct place.");
|
|
throw new InvalidOperationException(
|
|
"The server items couldn't be found or loaded. Check server config is pointing to the correct place.");
|
|
}
|
|
|
|
if (!_items.TryGetValue(tpl, out var itemTemplate))
|
|
{
|
|
_logger.LogError(
|
|
"Item template '{Tpl}' with base class id '{BaseclassId}' was not found in the server items!", tpl,
|
|
baseclassId);
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(itemTemplate.Parent))
|
|
return false;
|
|
|
|
return itemTemplate.Parent == baseclassId || IsBaseClass(itemTemplate.Parent, baseclassId);
|
|
}
|
|
|
|
public bool IsQuestItem(string tpl)
|
|
{
|
|
if (_items == null)
|
|
{
|
|
_logger.LogError("The server items are null. Check server config is pointing to the correct place.");
|
|
throw new InvalidOperationException(
|
|
"The server items couldn't be found or loaded. Check server config is pointing to the correct place.");
|
|
}
|
|
|
|
if (!_items.TryGetValue(tpl, out var itemTemplate))
|
|
{
|
|
_logger.LogError("Item template '{Tpl}' was not found in the server items!", tpl);
|
|
return false;
|
|
}
|
|
|
|
return itemTemplate.Props.QuestItem;
|
|
}
|
|
|
|
public string? MaxDurability(string tpl)
|
|
{
|
|
if (_items == null)
|
|
{
|
|
_logger.LogError("The server items are null. Check server config is pointing to the correct place.");
|
|
throw new InvalidOperationException(
|
|
"The server items couldn't be found or loaded. Check server config is pointing to the correct place.");
|
|
}
|
|
|
|
if (!_items.TryGetValue(tpl, out var itemTemplate))
|
|
{
|
|
_logger.LogError("Item template '{Tpl}' was not found in the server items!", tpl);
|
|
return null;
|
|
}
|
|
|
|
return itemTemplate.Props.MaxDurability?.ToString() ?? string.Empty;
|
|
}
|
|
|
|
public string? AmmoCaliber(string tpl)
|
|
{
|
|
if (_items == null)
|
|
{
|
|
_logger.LogError("The server items are null. Check server config is pointing to the correct place.");
|
|
throw new InvalidOperationException(
|
|
"The server items couldn't be found or loaded. Check server config is pointing to the correct place.");
|
|
}
|
|
|
|
if (!_items.TryGetValue(tpl, out var itemTemplate))
|
|
{
|
|
_logger.LogError("Item template '{Tpl}' was not found in the server items!", tpl);
|
|
return null;
|
|
}
|
|
|
|
return itemTemplate.Props.Caliber;
|
|
}
|
|
} |