Skip to content

Commit

Permalink
Artifacts, Races and GameEvents are reloaded from xml on new game; re…
Browse files Browse the repository at this point in the history
…apply mod's patches
  • Loading branch information
Tyler-IN committed Mar 31, 2022
1 parent 32fac90 commit e21f661
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions DistantWorlds2.ModLoader.Core/GameDataDefinitionPatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public static class GameDataDefinitionPatching
new(nameof(CharacterRoom), () => Galaxy.CharacterRoomsStatic)
});

private static readonly ImmutableDictionary<string, Func<object>> LateStaticDefs = ImmutableDictionary.CreateRange(
new KeyValuePair<string, Func<object>>[]
{
new(nameof(Race), () => Galaxy.RacesStatic),
new(nameof(Artifact), () => Galaxy.ArtifactsStatic),
new(nameof(GameEvent), () => Galaxy.GameEventsStatic)
});

private static readonly ImmutableDictionary<string, Func<Galaxy, object>> InstanceDefs = ImmutableDictionary.CreateRange(
new KeyValuePair<string, Func<Galaxy, object>>[]
{
Expand Down Expand Up @@ -142,7 +150,7 @@ public static void ApplyContentPatches(string dataPath, Galaxy galaxy)
foreach (var dataFilePath in Directory.EnumerateFiles(absPath, "*.yml", SearchOption.AllDirectories))
{
if (dataFilePath is null) continue;
Console.WriteLine($"Parsing {dataFilePath} for instance definitions");
Console.WriteLine($"Parsing {dataFilePath} for late static and instance definitions");
using var s = File.Open(dataFilePath, FileMode.Open, FileAccess.Read);
var ys = LoadYaml(s);
foreach (var yd in ys)
Expand Down Expand Up @@ -181,9 +189,19 @@ public static void ApplyContentPatches(string dataPath, Galaxy galaxy)
Console.Error.WriteLine($"Unknown definition type {typeStr} @ {keyScalar.Start}");
continue;
}

if (LateStaticDefs.TryGetValue(typeStr, out var getStaticDefs))
{
DefIdFields.TryGetValue(typeStr, out var idFieldName);

if (StaticDefs.ContainsKey(typeStr))
var defs = getStaticDefs();

if (typeof(IndexedList<>).MakeGenericType(type).IsInstanceOfType(defs))
PatchIndexedDefinitions(type, defs, valueSeq, idFieldName);
else
PatchDefinitions(type, defs, valueSeq, idFieldName);
continue;
}

if (GalaxyDefs.TryGetValue(typeStr, out var getGlxDef))
{
Expand All @@ -203,6 +221,9 @@ public static void ApplyContentPatches(string dataPath, Galaxy galaxy)
continue;
}

if (StaticDefs.ContainsKey(typeStr))
continue;

Console.Error.WriteLine($"Can't find defs for {typeStr} @ {keyScalar.Start}");
}
else
Expand Down

0 comments on commit e21f661

Please sign in to comment.