2024-04-16 18:29:40 +00:00
|
|
|
using System.Collections.Concurrent;
|
2025-01-11 09:12:21 +03:00
|
|
|
using System.Text.Json;
|
2025-01-13 20:05:36 +03:00
|
|
|
using LootDumpProcessor.Model.Config;
|
2023-08-12 19:08:38 +01:00
|
|
|
using LootDumpProcessor.Model.Input;
|
|
|
|
using LootDumpProcessor.Model.Processing;
|
2025-01-11 10:52:23 +03:00
|
|
|
using LootDumpProcessor.Serializers.Json;
|
2024-04-16 18:29:40 +00:00
|
|
|
using LootDumpProcessor.Utils;
|
2025-01-11 09:12:21 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2025-01-13 20:05:36 +03:00
|
|
|
using Microsoft.Extensions.Options;
|
2023-08-12 19:08:38 +01:00
|
|
|
|
2025-01-11 12:15:01 +03:00
|
|
|
namespace LootDumpProcessor.Process.Reader.Intake;
|
|
|
|
|
2025-01-13 20:05:36 +03:00
|
|
|
public class JsonFileIntakeReader(ILogger<JsonFileIntakeReader> logger, IOptions<Config> config) : IIntakeReader
|
2023-08-12 19:08:38 +01:00
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
private readonly ILogger<JsonFileIntakeReader> _logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
2025-01-13 20:05:36 +03:00
|
|
|
private readonly Config _config = (config ?? throw new ArgumentNullException(nameof(config))).Value;
|
|
|
|
|
|
|
|
private HashSet<string> IgnoredLocations => _config
|
|
|
|
.ReaderConfig.IntakeReaderConfig.IgnoredDumpLocations.ToHashSet();
|
2023-08-12 19:08:38 +01:00
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
private readonly ConcurrentDictionary<string, int> _totalMapDumpsCounter = new();
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
public bool Read(string file, out BasicInfo basicInfo)
|
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
basicInfo = null;
|
|
|
|
string? fileData;
|
2024-08-22 15:33:27 -04:00
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
fileData = File.ReadAllText(file);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2024-08-22 15:33:27 -04:00
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
_logger.LogError(ex, "Failed to read file: {File}", file);
|
|
|
|
return false;
|
|
|
|
}
|
2024-08-22 15:33:27 -04:00
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
if (string.IsNullOrWhiteSpace(fileData))
|
|
|
|
{
|
|
|
|
_logger.LogError("Could not parse data from file: {File}", file);
|
2024-08-22 15:33:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
// 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))
|
2025-01-11 09:12:21 +03:00
|
|
|
_logger.LogError("Could not parse date from file: {File}", file);
|
2023-08-12 19:08:38 +01:00
|
|
|
|
2025-01-11 10:52:23 +03:00
|
|
|
var fi = JsonSerializer.Deserialize<RootData>(fileData, JsonSerializerSettings.Default);
|
2025-01-13 20:05:36 +03:00
|
|
|
if (fi?.Data.LocationLoot.Name != null && (!IgnoredLocations?.Contains(fi.Data.LocationLoot.Name) ?? true))
|
2023-08-12 19:08:38 +01:00
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
var mapName = fi.Data.LocationLoot.Name;
|
|
|
|
var mapId = fi.Data.LocationLoot.Id.ToLower();
|
|
|
|
|
|
|
|
var counter = _totalMapDumpsCounter.AddOrUpdate(mapName, 0, (_, current) => current);
|
|
|
|
|
2025-01-13 20:05:36 +03:00
|
|
|
var maxDumpsPerMap = _config
|
2025-01-11 09:12:21 +03:00
|
|
|
.ReaderConfig.IntakeReaderConfig?.MaxDumpsPerMap ?? 1500;
|
2023-08-13 18:14:54 +01:00
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
if (counter < maxDumpsPerMap)
|
2023-08-12 19:08:38 +01:00
|
|
|
{
|
2023-08-13 18:14:54 +01:00
|
|
|
basicInfo = new BasicInfo
|
|
|
|
{
|
2025-01-11 09:12:21 +03:00
|
|
|
Map = mapId,
|
2023-08-13 18:14:54 +01:00
|
|
|
FileHash = ProcessorUtil.HashFile(fileData),
|
|
|
|
Data = fi,
|
2024-08-22 15:33:27 -04:00
|
|
|
Date = date ?? DateTime.MinValue,
|
2023-08-13 18:14:54 +01:00
|
|
|
FileName = file
|
|
|
|
};
|
2024-08-22 15:33:27 -04:00
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
_totalMapDumpsCounter[mapName] += 1;
|
2024-05-30 16:03:33 +01:00
|
|
|
|
2025-01-11 09:12:21 +03:00
|
|
|
_logger.LogDebug("File {File} fully read, returning data", file);
|
2023-08-13 18:14:54 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-05-30 16:03:33 +01:00
|
|
|
|
2024-05-30 16:07:12 +01:00
|
|
|
// Map dump limit reached, exit
|
2025-01-11 09:12:21 +03:00
|
|
|
_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
|
|
|
}
|
|
|
|
|
2025-01-11 09:12:21 +03: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;
|
|
|
|
}
|
|
|
|
}
|