Skip to content
Open
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions XtraPlzNoCrashes/Patches/TemplateCollectionServicePatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Reflection.Emit;

namespace XtraPlzNoCrashes.Patches;

[HarmonyPatch]
public static class TemplateCollectionServicePatches
{
[HarmonyTranspiler, HarmonyPatch(typeof(TemplateCollectionService), nameof(TemplateCollectionService.Load))]
public static IEnumerable<CodeInstruction> DistinctLoad(IEnumerable<CodeInstruction> instructions)
{
foreach (var ins in instructions)
{
yield return ins;
if (ins.opcode == OpCodes.Call && ins.operand is MethodInfo mi && mi.Name == nameof(Enumerable.SelectMany))
{
yield return new CodeInstruction(
OpCodes.Call,
typeof(TemplateCollectionServicePatches)
.Method(nameof(DistinctAssets))
);
}
}
}

static IEnumerable<AssetRef<BlueprintAsset>> DistinctAssets(IEnumerable<AssetRef<BlueprintAsset>> assetRefs) =>
assetRefs.Distinct(BlueprintAssetRefEqualizer.Instance);

class BlueprintAssetRefEqualizer : IEqualityComparer<AssetRef<BlueprintAsset>>
{

public static readonly BlueprintAssetRefEqualizer Instance = new();

public bool Equals(AssetRef<BlueprintAsset> x, AssetRef<BlueprintAsset> y) => x.Path.ToLowerInvariant().Equals(y.Path.ToLowerInvariant());

public int GetHashCode(AssetRef<BlueprintAsset> obj) => obj.Path.ToLowerInvariant().GetHashCode();

}

}
2 changes: 1 addition & 1 deletion XtraPlzNoCrashes/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Name": "XtraPlzNoCrashes",
"Version": "10.0.0",
"Version": "10.0.1",
"Id": "XtraPlzNoCrashes",
"MinimumGameVersion": "1.0.0",
"Description": "Support for mods so game constraints don't crash them.",
Expand Down