0
0
mirror of https://github.com/sp-tarkov/loot-dump-processor.git synced 2025-02-13 08:10:45 -05:00
bluextx 047372b6dc Refactored dependency injection and logging infrastructure
The changes include:
- Replaced custom logging with Microsoft.Extensions.Logging
- Added TarkovItemsProvider and ComposedKeyGenerator services
- Simplified configuration models by removing redundant options
- Improved dependency injection in processors and readers
- Removed unused factory methods and simplified service registration
2025-01-11 09:12:45 +03:00

113 lines
4.4 KiB
C#

using System.Collections.Frozen;
using System.Text.Json;
using LootDumpProcessor.Model.Tarkov;
using Microsoft.Extensions.Logging;
namespace LootDumpProcessor.Process
{
public class TarkovItemsProvider : ITarkovItemsProvider
{
private readonly ILogger<TarkovItemsProvider> _logger;
private readonly FrozenDictionary<string, TemplateFileItem>? _items;
private static readonly string ItemsFilePath = Path.Combine(
LootDumpProcessorContext.GetConfig().ServerLocation,
"project", "assets", "database", "templates", "items.json");
public TarkovItemsProvider(ILogger<TarkovItemsProvider> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
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;
}
}
}