0
0
mirror of https://github.com/sp-tarkov/loot-dump-processor.git synced 2025-02-13 08:50:44 -05:00
bluextx 465ad95cb5 Refactored code to use System.Text.Json and improved code style
The changes include:
- Replaced Newtonsoft.Json with System.Text.Json for serialization
- Removed redundant JsonProperty attributes and simplified model classes
- Converted static fields to constants where appropriate
- Improved code formatting and readability
- Simplified method bodies and expressions
- Removed unused imports and cleaned up namespaces
2025-01-11 10:52:23 +03:00

112 lines
4.0 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;
}
}