More mongo (#450)

* Remove debug, doesn't really work

* Convert Handbook to MongoId's

* Make traders in Database keyed to MongoId rather than string
This commit is contained in:
Jesse
2025-07-05 14:41:57 +02:00
committed by GitHub
parent 7dc5bb106d
commit bd7d60e5ab
7 changed files with 61 additions and 49 deletions
@@ -44,7 +44,7 @@ public class TraderController(
var traders = databaseService.GetTraders();
foreach (var (traderId, trader) in traders)
{
if (traderId == Traders.LIGHTHOUSEKEEPER || traderId == "ragfair")
if (traderId == Traders.LIGHTHOUSEKEEPER)
{
continue;
}
@@ -7,13 +7,7 @@ public readonly struct MongoId : IEquatable<MongoId>
{
private readonly string? _stringId;
public MongoId(
string? id,
// TODO: TEMPORARY REMOVE ME WHEN DONE!!!!
[CallerFilePath] string callerFilePath = "",
[CallerMemberName] string methodName = "",
[CallerLineNumber] int callerLineNumber = 0
)
public MongoId(string? id)
{
// Handle null strings, various id's are null either by BSG or by our own doing with LINQ
if (string.IsNullOrEmpty(id))
@@ -26,15 +20,13 @@ public readonly struct MongoId : IEquatable<MongoId>
if (id.Length != 24)
{
// TODO: Items.json root item has an empty parentId property
Console.WriteLine(
$"Critical MongoId error [{callerFilePath}::{methodName} L{callerLineNumber}]: Incorrect length. id: {id}"
);
Console.WriteLine($"Critical MongoId error: Incorrect length. id: {id}");
}
if (!IsValidMongoId(id))
{
Console.WriteLine(
$"Critical MongoId error [{callerFilePath}::{methodName} L{callerLineNumber}]: Incorrect format. Must be a hexadecimal [a-f0-9] of 24 characters. id: {id}"
$"Critical MongoId error: Incorrect format. Must be a hexadecimal [a-f0-9] of 24 characters. id: {id}"
);
}
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using SPTarkov.Server.Core.Models.Common;
namespace SPTarkov.Server.Core.Models.Eft.Common.Tables;
@@ -20,11 +21,11 @@ public record HandbookCategory
public Dictionary<string, object>? ExtensionData { get; set; }
[JsonPropertyName("Id")]
public string? Id { get; set; }
public MongoId Id { get; set; }
[JsonPropertyName("ParentId")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public string? ParentId { get; set; }
public MongoId? ParentId { get; set; }
[JsonPropertyName("Icon")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
@@ -43,11 +44,11 @@ public record HandbookItem
public Dictionary<string, object>? ExtensionData { get; set; }
[JsonPropertyName("Id")]
public string? Id { get; set; }
public MongoId Id { get; set; }
[JsonPropertyName("ParentId")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public string? ParentId { get; set; }
public MongoId ParentId { get; set; }
[JsonPropertyName("Price")]
public double? Price { get; set; }
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
@@ -21,7 +22,7 @@ public record DatabaseTables
public Templates.Templates? Templates { get; set; }
public Dictionary<string, Trader> Traders { get; set; }
public Dictionary<MongoId, Trader> Traders { get; set; }
public Globals? Globals { get; set; }
@@ -328,7 +328,7 @@ public class DatabaseService(
}
/// <returns> assets/database/traders/ </returns>
public Dictionary<string, Trader> GetTraders()
public Dictionary<MongoId, Trader> GetTraders()
{
if (_databaseServer.GetTables().Traders == null)
{
@@ -3,6 +3,7 @@ using System.Security.Cryptography;
using System.Text;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
using SPTarkov.Server.Core.Models.Spt.Server;
using SPTarkov.Server.Core.Models.Utils;
@@ -128,22 +129,8 @@ public class DatabaseImporter(
VerifyDatabase
);
// TODO: Fix loading of traders, so their full path is not included as the key
var tempTraders = new Dictionary<string, Trader>();
// temp fix for trader keys
foreach (var trader in dataToImport.Traders)
{
// fix string for key
var tempKey = trader.Key.Split("/").Last();
tempTraders.Add(tempKey, trader.Value);
}
timer.Stop();
dataToImport.Traders = tempTraders;
_logger.Info(_serverLocalisationService.GetText("importing_database_finish"));
_logger.Debug($"Database import took {timer.ElapsedMilliseconds}ms");
_databaseServer.SetTables(dataToImport);
@@ -1,7 +1,9 @@
using System.Collections;
using System.Collections.Frozen;
using System.Linq.Expressions;
using System.Reflection;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils.Json;
@@ -169,26 +171,55 @@ public class ImporterUtil(ISptLogger<ImporterUtil> _logger, FileUtil _fileUtil,
{
try
{
var setMethod = GetSetMethod(
directory.Split("/").Last().Replace("_", ""),
loadedType,
out var matchedProperty,
out var isDictionary
);
var directoryName = directory.Split("/").Last().Replace("_", "");
var loadedData = await LoadRecursiveAsync(
$"{directory}/",
matchedProperty,
onReadCallback,
onObjectDeserialized
);
lock (dictionaryLock)
if (MongoId.IsValidMongoId(directoryName))
{
setMethod.Invoke(
result,
isDictionary ? [directory, loadedData] : new[] { loadedData }
// For trader MongoId directories, we need to get the parent property. Get parent directory name to find the property
var parentDirectory = directory.Substring(0, directory.LastIndexOf('/'));
var parentName = parentDirectory.Split("/").Last().Replace("_", "");
GetSetMethod(parentName, loadedType, out var matchedProperty, out _);
var loadedData = await LoadRecursiveAsync(
$"{directory}/",
matchedProperty,
onReadCallback,
onObjectDeserialized
);
lock (dictionaryLock)
{
// Traders already have a dictionary, so we only need to handle this here
if (result is IDictionary dictionary)
{
dictionary[new MongoId(directoryName)] = loadedData;
}
}
}
else
{
var setMethod = GetSetMethod(
directoryName,
loadedType,
out var matchedProperty,
out var isDictionary
);
var loadedData = await LoadRecursiveAsync(
$"{directory}/",
matchedProperty,
onReadCallback,
onObjectDeserialized
);
lock (dictionaryLock)
{
setMethod.Invoke(
result,
isDictionary ? [directory, loadedData] : new[] { loadedData }
);
}
}
}
catch (Exception ex)