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

84 lines
2.9 KiB
C#
Raw Normal View History

using System.Collections.Concurrent;
using System.Text.Json;
using LootDumpProcessor;
2023-08-12 19:08:38 +01:00
using LootDumpProcessor.Model.Input;
using LootDumpProcessor.Model.Processing;
using LootDumpProcessor.Process.Reader.Intake;
using LootDumpProcessor.Utils;
using Microsoft.Extensions.Logging;
2023-08-12 19:08:38 +01:00
public class JsonFileIntakeReader(ILogger<JsonFileIntakeReader> logger) : IIntakeReader
2023-08-12 19:08:38 +01:00
{
private readonly ILogger<JsonFileIntakeReader> _logger = logger ?? throw new ArgumentNullException(nameof(logger));
private readonly HashSet<string>? _ignoredLocations = LootDumpProcessorContext.GetConfig()
.ReaderConfig.IntakeReaderConfig?.IgnoredDumpLocations.ToHashSet();
2023-08-12 19:08:38 +01:00
private readonly ConcurrentDictionary<string, int> _totalMapDumpsCounter = new();
2023-08-12 19:08:38 +01:00
public bool Read(string file, out BasicInfo basicInfo)
{
basicInfo = null;
string? fileData;
try
{
fileData = File.ReadAllText(file);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to read file: {File}", file);
return false;
}
if (string.IsNullOrWhiteSpace(fileData))
{
_logger.LogError("Could not parse data from file: {File}", file);
return false;
}
// If the file format changes, it may affect the date parser
2023-08-13 18:14:54 +01:00
if (!FileDateParser.TryParseFileDate(file, out var date))
{
_logger.LogError("Could not parse date from file: {File}", file);
}
2023-08-12 19:08:38 +01:00
var fi = JsonSerializer.Deserialize<RootData>(fileData);
if (fi?.Data?.LocationLoot?.Name != null && (!_ignoredLocations?.Contains(fi.Data.LocationLoot.Name) ?? true))
2023-08-12 19:08:38 +01:00
{
var mapName = fi.Data.LocationLoot.Name;
var mapId = fi.Data.LocationLoot.Id.ToLower();
var counter = _totalMapDumpsCounter.AddOrUpdate(mapName, 0, (_, current) => current);
var maxDumpsPerMap = LootDumpProcessorContext.GetConfig()
.ReaderConfig.IntakeReaderConfig?.MaxDumpsPerMap ?? 1500;
2023-08-13 18:14:54 +01:00
if (counter < maxDumpsPerMap)
2023-08-12 19:08:38 +01:00
{
2023-08-13 18:14:54 +01:00
basicInfo = new BasicInfo
{
Map = mapId,
2023-08-13 18:14:54 +01:00
FileHash = ProcessorUtil.HashFile(fileData),
Data = fi,
Date = date ?? DateTime.MinValue,
2023-08-13 18:14:54 +01:00
FileName = file
};
_totalMapDumpsCounter[mapName] += 1;
_logger.LogDebug("File {File} fully read, returning data", file);
2023-08-13 18:14:54 +01:00
return true;
}
2024-05-30 16:07:12 +01:00
// Map dump limit reached, exit
_logger.LogDebug("Ignoring file {File} as the file cap for map {MapId} has been reached", file, mapId);
2024-05-30 16:07:12 +01:00
return false;
2023-08-12 19:08:38 +01:00
}
_logger.LogWarning(
"File {File} was not eligible for dump data; it did not contain a location name or it was on the ignored locations config",
file);
2023-08-12 19:08:38 +01:00
return false;
}
}