From f5c1fe73844c6d4f00862e2a4718afeb8bf3f918 Mon Sep 17 00:00:00 2001 From: Archangel Date: Fri, 30 May 2025 14:46:07 +0200 Subject: [PATCH 1/2] Add async deserialization methods --- .../SPTarkov.Server.Core/Utils/JsonUtil.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs index 940c1e00..4ec01814 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/JsonUtil.cs @@ -112,6 +112,23 @@ public class JsonUtil } } + /// + /// Convert JSON into an object from a file asynchronously + /// + /// The JSON File to read + /// T + public async Task DeserializeFromFileAsync(string file) + { + if (!File.Exists(file)) + { + return default; + } + + await using FileStream fs = new(file, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); + + return await JsonSerializer.DeserializeAsync(fs, jsonSerializerOptionsNoIndent); + } + /// /// Convert JSON into an object from a file /// @@ -131,6 +148,24 @@ public class JsonUtil } } + /// + /// Convert JSON into an object from a file asynchronously + /// + /// The JSON File to read + /// The type of the object to deserialize to + /// object + public async Task DeserializeFromFileAsync(string file, Type type) + { + if (!File.Exists(file)) + { + return default; + } + + await using FileStream fs = new(file, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); + + return await JsonSerializer.DeserializeAsync(fs, type, jsonSerializerOptionsNoIndent); + } + /// /// Convert JSON into an object from a FileStream /// @@ -142,6 +177,17 @@ public class JsonUtil return JsonSerializer.Deserialize(fs, type, jsonSerializerOptionsNoIndent); } + /// + /// Convert JSON into an object from a FileStream asynchronously + /// + /// The file stream to deserialize + /// The type of the object to deserialize to + /// + public async Task DeserializeFromFileStreamAsync(FileStream fs, Type type) + { + return await JsonSerializer.DeserializeAsync(fs, type, jsonSerializerOptionsNoIndent); + } + /// /// Convert an object into JSON /// From 7497ed8f9fa7629a490e25c55e6501a2aac3bf2b Mon Sep 17 00:00:00 2001 From: Archangel Date: Fri, 30 May 2025 14:46:34 +0200 Subject: [PATCH 2/2] Cleanup importerutil, remove unecessary FileStream read --- .../Utils/ImporterUtil.cs | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs index 0ea9de40..f7f7646f 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/ImporterUtil.cs @@ -97,31 +97,28 @@ public class ImporterUtil { try { - using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read)) + onReadCallback?.Invoke(file); + + // Get the set method to update the object + var setMethod = GetSetMethod( + _fileUtil.StripExtension(file).ToLower(), + loadedType, + out var propertyType, + out var isDictionary + ); + + var fileDeserialized = await DeserializeFileAsync(file, propertyType); + + onObjectDeserialized?.Invoke(file, fileDeserialized); + + lock (dictionaryLock) { - onReadCallback?.Invoke(file); - - // Get the set method to update the object - var setMethod = GetSetMethod( - _fileUtil.StripExtension(file).ToLower(), - loadedType, - out var propertyType, - out var isDictionary + setMethod.Invoke( + result, + isDictionary + ? [_fileUtil.StripExtension(file), fileDeserialized] + : new[] { fileDeserialized } ); - - var fileDeserialized = await DeserializeFileAsync(fs, file, propertyType); - - onObjectDeserialized?.Invoke(file, fileDeserialized); - - lock (dictionaryLock) - { - setMethod.Invoke( - result, - isDictionary - ? [_fileUtil.StripExtension(file), fileDeserialized] - : new[] { fileDeserialized } - ); - } } } catch (Exception ex) @@ -159,14 +156,14 @@ public class ImporterUtil } } - private async Task DeserializeFileAsync(FileStream fs, string file, Type propertyType) + private async Task DeserializeFileAsync(string file, Type propertyType) { if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(LazyLoad<>)) { return CreateLazyLoadDeserialization(file, propertyType); } - return await Task.Run(() => _jsonUtil.DeserializeFromFileStream(fs, propertyType)); + return await _jsonUtil.DeserializeFromFileAsync(file, propertyType); } private object CreateLazyLoadDeserialization(string file, Type propertyType)