Skip to content

Commit 04435d1

Browse files
committed
Core: FactionTemplateDB is more forgiving also yields a warning message if data is missing.
1 parent a8cffef commit 04435d1

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

Core/Database/FactionTemplateDB.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using SharedLib;
1+
using Microsoft.Extensions.Logging;
2+
3+
using SharedLib;
24
using SharedLib.Data;
35

6+
using System;
47
using System.Collections.Frozen;
58

69
using static Newtonsoft.Json.JsonConvert;
@@ -13,12 +16,28 @@ public sealed class FactionTemplateDB
1316
{
1417
public FrozenDictionary<int, int> Factions { get; }
1518

16-
public FactionTemplateDB(DataConfig dataConfig)
19+
public const string FileName = "factiontemplates.json";
20+
21+
public FactionTemplateDB(ILogger<FactionTemplateDB> logger, DataConfig dataConfig)
1722
{
18-
FactionTemplate[] data = DeserializeObject<FactionTemplate[]>(
19-
ReadAllText(Join(dataConfig.ExpDbc, "factiontemplates.json")))!;
23+
string path = Join(dataConfig.ExpDbc, FileName);
24+
25+
FactionTemplate[] data = [];
26+
27+
try
28+
{
29+
string json = ReadAllText(path);
30+
if (!string.IsNullOrWhiteSpace(json))
31+
{
32+
data = DeserializeObject<FactionTemplate[]>(json) ?? [];
33+
}
34+
}
35+
catch (Exception ex)
36+
{
37+
logger.LogWarning($"Failed to load {FileName}: {ex.Message}");
38+
logger.LogWarning("AdhocNPC profiles with 'Auto NPC Route' features will not work properly!");
39+
}
2040

21-
Factions = data
22-
.ToFrozenDictionary(c => c.Id, c => c.FriendGroup);
41+
Factions = data.ToFrozenDictionary(c => c.Id, c => c.FriendGroup);
2342
}
2443
}

0 commit comments

Comments
 (0)