Skip to content

Commit f282677

Browse files
committed
Switch SOL patch to preloader instead of Harmony
1 parent 1919e57 commit f282677

8 files changed

Lines changed: 86 additions & 54 deletions

File tree

APIPatcher/APIPatcher.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>disable</Nullable>
7+
<LangVersion>latest</LangVersion>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BepInEx.Core" Version="5.4.17" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Security;
2+
using BepInEx;
3+
using BepInEx.Logging;
4+
using Mono.Cecil;
5+
using Mono.Cecil.Cil;
6+
using MonoMod.Cil;
7+
using MonoMod.Utils;
8+
9+
namespace APIPatcher;
10+
11+
public static class InscryptionAPIPatcher
12+
{
13+
public static IEnumerable<string> TargetDLLs => new[] { "Assembly-CSharp.dll" };
14+
15+
public static void Patch(AssemblyDefinition asm)
16+
{
17+
var apiPath = Directory.GetFiles(Paths.PluginPath, "InscryptionAPI.dll", SearchOption.AllDirectories)[0];
18+
using var pluginAsm = AssemblyDefinition.ReadAssembly(apiPath);
19+
20+
var eventInvoker = pluginAsm.MainModule.GetType("InscryptionAPI.InscryptionAPIPlugin").Methods.First(x => x.Name == "InvokeSOLEvent");
21+
22+
var sol = asm.MainModule.GetType("ScriptableObjectLoader`1");
23+
var allDataGetter = sol.Properties.First(x => x.Name == "AllData").GetMethod;
24+
25+
ILContext ctx = new(allDataGetter);
26+
ILCursor c = new(ctx);
27+
28+
c.Emit(OpCodes.Ldtoken, sol.GenericParameters[0]);
29+
c.Emit(OpCodes.Call, ctx.Import(typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle))));
30+
c.Emit(OpCodes.Call, asm.MainModule.ImportReference(eventInvoker));
31+
}
32+
}

InscryptionAPI.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InscryptionAPI", "InscryptionAPI\InscryptionAPI.csproj", "{8C7A7246-7033-4E9F-BCC7-4E76D6E5F679}"
44
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIPatcher", "APIPatcher\APIPatcher.csproj", "{CF5A34DF-24FC-4356-BD87-0FBDD9D352C6}"
6+
EndProject
57
Global
68
GlobalSection(SolutionConfigurationPlatforms) = preSolution
79
Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
1214
{8C7A7246-7033-4E9F-BCC7-4E76D6E5F679}.Debug|Any CPU.Build.0 = Debug|Any CPU
1315
{8C7A7246-7033-4E9F-BCC7-4E76D6E5F679}.Release|Any CPU.ActiveCfg = Release|Any CPU
1416
{8C7A7246-7033-4E9F-BCC7-4E76D6E5F679}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{CF5A34DF-24FC-4356-BD87-0FBDD9D352C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{CF5A34DF-24FC-4356-BD87-0FBDD9D352C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{CF5A34DF-24FC-4356-BD87-0FBDD9D352C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{CF5A34DF-24FC-4356-BD87-0FBDD9D352C6}.Release|Any CPU.Build.0 = Release|Any CPU
1521
EndGlobalSection
1622
EndGlobal

InscryptionAPI/Ascension/ChallengeManager.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ public static class ChallengeManager
2020

2121
public static event Action<List<AscensionChallengeInfo>> ModifyAscensionChallengeList;
2222

23-
private static bool _hasLoaded = false;
24-
25-
public static List<AscensionChallengeInfo> AllInfo
23+
static ChallengeManager()
2624
{
27-
get
25+
InscryptionAPIPlugin.ScriptableObjectLoaderLoad += static type =>
2826
{
29-
return BaseGameChallenges.Concat(newInfos).ToList();
30-
}
27+
if (type == typeof(AscensionChallengeInfo))
28+
{
29+
ScriptableObjectLoader<AscensionChallengeInfo>.allData = AllInfo;
30+
}
31+
};
3132
}
3233

34+
public static List<AscensionChallengeInfo> AllInfo = BaseGameChallenges.ToList();
35+
3336
public static bool IsStackable(AscensionChallenge id)
3437
{
3538
return stackableMap.ContainsKey(id) ? stackableMap[id] : false;
@@ -42,12 +45,12 @@ public static AscensionChallengeInfo Add(string pluginGuid, AscensionChallengeIn
4245
newInfos.Add(info);
4346

4447
ModifyAscensionChallengeList?.Invoke(newInfos);
48+
newInfos.Sort((a, b) => unlockLevelMap[a.challengeType] - unlockLevelMap[b.challengeType]);
49+
AllInfo = BaseGameChallenges.Concat(newInfos).ToList();
4550

4651
stackableMap.Add(info.challengeType, stackable);
4752
unlockLevelMap.Add(info.challengeType, unlockLevel);
4853

49-
_hasLoaded = false; // Force a reload in case something happened out of the expected order
50-
5154
return info;
5255
}
5356

@@ -77,18 +80,6 @@ public static AscensionChallengeInfo Add(
7780
return Add(pluginGuid, info, unlockLevel, stackable);
7881
}
7982

80-
[HarmonyPatch(typeof(AscensionChallengesUtil), "GetInfo")]
81-
[HarmonyPrefix]
82-
private static void CardLoadPrefix()
83-
{
84-
if (!_hasLoaded)
85-
{
86-
// Sort the new challenges in order of unlocks
87-
newInfos.Sort((a, b) => unlockLevelMap[a.challengeType] - unlockLevelMap[b.challengeType]);
88-
ScriptableObjectLoader<AscensionChallengeInfo>.allData = AllInfo;
89-
}
90-
}
91-
9283
[HarmonyPatch(typeof(AscensionUnlockSchedule), "ChallengeIsUnlockedForLevel")]
9384
[HarmonyPostfix]
9485
public static void IsCustomChallengeUnlocked(ref bool __result, int level, AscensionChallenge challenge)

InscryptionAPI/Card/AbilityManager.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public FullAbility(Ability id, AbilityInfo info, Type behaviour, Texture texture
4141

4242
static AbilityManager()
4343
{
44+
InscryptionAPIPlugin.ScriptableObjectLoaderLoad += static type =>
45+
{
46+
if (type == typeof(AbilityInfo))
47+
{
48+
ScriptableObjectLoader<AbilityInfo>.allData = AllAbilityInfos;
49+
}
50+
};
4451
NewAbilities.CollectionChanged += static (_, _) =>
4552
{
4653
AllAbilities = BaseGameAbilities.Concat(NewAbilities).ToList();
@@ -100,14 +107,6 @@ public static AbilityInfo New(string guid, string rulebookName, string rulebookD
100107

101108
public static void Remove(Ability id) => NewAbilities.Remove(NewAbilities.FirstOrDefault(x => x.Id == id));
102109
public static void Remove(FullAbility ability) => NewAbilities.Remove(ability);
103-
104-
[HarmonyPrefix]
105-
[HarmonyPatch(typeof(ScriptableObjectLoader<UnityObject>), nameof(ScriptableObjectLoader<UnityObject>.LoadData))]
106-
[SuppressMessage("Member Access", "Publicizer001", Justification = "Need to set internal list of abilities")]
107-
private static void AbilityLoadPrefix()
108-
{
109-
ScriptableObjectLoader<AbilityInfo>.allData = AllAbilityInfos;
110-
}
111110

112111
[HarmonyReversePatch(HarmonyReversePatchType.Original)]
113112
[HarmonyPatch(typeof(AbilitiesUtil), nameof(AbilitiesUtil.LoadAbilityIcon))]

InscryptionAPI/Card/CardManager.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ internal static void SyncCardList()
2323

2424
static CardManager()
2525
{
26+
InscryptionAPIPlugin.ScriptableObjectLoaderLoad += static type =>
27+
{
28+
if (type == typeof(CardInfo))
29+
{
30+
ScriptableObjectLoader<CardInfo>.allData = AllCardsCopy;
31+
}
32+
};
2633
NewCards.CollectionChanged += static (_, _) =>
2734
{
2835
SyncCardList();
@@ -46,12 +53,4 @@ static CardManager()
4653

4754
return retval;
4855
}
49-
50-
[HarmonyPrefix]
51-
[HarmonyPatch(typeof(ScriptableObjectLoader<UnityObject>), nameof(ScriptableObjectLoader<UnityObject>.LoadData))]
52-
[SuppressMessage("Member Access", "Publicizer001", Justification = "Need to set internal list of cards")]
53-
private static void CardLoadPrefix()
54-
{
55-
ScriptableObjectLoader<CardInfo>.allData = AllCardsCopy;
56-
}
5756
}

InscryptionAPI/InscryptionAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="BepInEx.Analyzers" Version="1.0.7">
21+
<PackageReference Include="BepInEx.Analyzers" Version="1.0.8">
2222
<PrivateAssets>all</PrivateAssets>
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
</PackageReference>

InscryptionAPI/InscryptionAPIPlugin.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
using BepInEx;
44
using BepInEx.Logging;
5-
using DiskCardGame;
65
using HarmonyLib;
76
using InscryptionAPI.Card;
7+
using System.Runtime.CompilerServices;
8+
9+
[assembly: InternalsVisibleTo("Assembly-CSharp")]
810

911
namespace InscryptionAPI;
1012

@@ -20,27 +22,16 @@ public class InscryptionAPIPlugin : BaseUnityPlugin
2022

2123
private readonly Harmony HarmonyInstance = new(ModGUID);
2224

23-
private static readonly (Type, string)[] ObjectLoaderTypes =
25+
public static event Action<Type> ScriptableObjectLoaderLoad;
26+
internal static void InvokeSOLEvent(Type type)
2427
{
25-
(typeof(AscensionChallengeInfo), "Ascension/Challenges"),
26-
(typeof(BoonData), "Boons"),
27-
(typeof(CommandLineTextSegment), ""),
28-
(typeof(HoloMapWorldData), "Map/HoloMapWorlds"),
29-
(typeof(StarterDeckInfo), ""),
30-
(typeof(ItemData), "Consumables")
31-
};
28+
ScriptableObjectLoaderLoad?.Invoke(type);
29+
}
3230

3331
public void OnEnable()
3432
{
3533
Logger = base.Logger;
36-
37-
var loaderType = typeof(ScriptableObjectLoader<>);
38-
foreach (var type in ObjectLoaderTypes)
39-
{
40-
Logger.LogMessage($"Loading {type.Item1.FullName}...");
41-
AccessTools.DeclaredMethod(loaderType.MakeGenericType(type.Item1), "LoadData").Invoke(null, new object[] { type.Item2 });
42-
}
43-
34+
4435
HarmonyInstance.PatchAll(typeof(InscryptionAPIPlugin).Assembly);
4536
}
4637

0 commit comments

Comments
 (0)