Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Frozen;
using System.Text.Json;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Enums;
using SPTarkov.Server.Core.Models.Spt.Config;
Expand Down Expand Up @@ -75,22 +76,44 @@ public void Initialize()
if (acceptableFileExtensions.Contains(FileUtil.GetFileExtension(file)))
{
var type = GetConfigTypeByFilename(file);
var deserializedContent = JsonUtil.DeserializeFromFile(file, type);
if (type == null)
{
Logger.Error($"Config file: {file} has no associated ConfigTypes entry. Skipping");
continue;
}

object? deserializedContent = null;
try
{
deserializedContent = JsonUtil.DeserializeFromFile(file, type);
}
catch (JsonException ex)
{
Logger.Error($"Config file: {file} failed to deserialize");
throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed", ex);
}

if (deserializedContent == null)
{
Logger.Error($"Config file: {file} is corrupt. Use a site like: https://jsonlint.com to find the issue.");
throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed");
throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed");
}

_configs[$"spt-{FileUtil.StripExtension(file)}"] = deserializedContent;
}
}
}

private Type GetConfigTypeByFilename(string filename)
private Type? GetConfigTypeByFilename(string filename)
{
var type = Enum.GetValues<ConfigTypes>().First(en => en.GetValue().Contains(FileUtil.StripExtension(filename)));
Func<ConfigTypes, bool> filterMethod = (entry => entry.GetValue().Contains(FileUtil.StripExtension(filename)));

if (!Enum.GetValues<ConfigTypes>().Any(filterMethod))
{
return null;
}

var type = Enum.GetValues<ConfigTypes>().First(filterMethod);
return type.GetConfigType();
}
}
Loading