Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DBML call module patches #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Binary file modified Tweaks/Assets/CustomAssemblies/TweaksAssembly.dll
Binary file not shown.
Binary file modified Tweaks/Assets/CustomAssemblies/TweaksAssembly.pdb
Binary file not shown.
14 changes: 14 additions & 0 deletions Tweaks/TweaksAssembly/DemandBasedLoading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ private static IEnumerator LoadModule(BombComponent fakeModule, BombInfo bombInf

moduleIDs.Add(moduleType);
}

var typesUpdated = false;
foreach(var moduleId in moduleIDs)
{
if(Tweaks.Instance.AllModulePatches.TryGetValue(moduleId, out var modulePatches))
{
if(modulePatches.Count == 0)
continue;
if(!typesUpdated)
ReflectedTypes.UpdateTypes();
typesUpdated = true;
modulePatches.RemoveAll(patchType => Patching.EnsurePatch(patchType.Name, patchType));
}
}

if (realModule != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
[AttributeUsage(AttributeTargets.Class)]
public class ModulePatchAttribute : HarmonyPatch
{
}
public readonly string ModuleId;

public ModulePatchAttribute(string moduleId)
{
ModuleId = moduleId;
}
}
2 changes: 1 addition & 1 deletion Tweaks/TweaksAssembly/Modules/Patches/SkinnyWiresPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Reflection.Emit;
using HarmonyLib;

[ModulePatch]
[ModulePatch("skinnyWires")]
public static class SkinnyWiresPatch
{
static bool Prepare() => ReflectedTypes.SkinnyWiresCalculateMethod != null;
Expand Down
8 changes: 5 additions & 3 deletions Tweaks/TweaksAssembly/Patching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public static bool PatchClasses(string id, out Harmony instance, params Type[] t
return success;
}

public static void EnsurePatch(string id, params Type[] types)
public static bool EnsurePatch(string id, params Type[] types)
{
if (instances.ContainsKey(id))
return;
return true;

if (PatchClasses(id, out Harmony instance, types))
var success = PatchClasses(id, out Harmony instance, types);
if (success)
instances.Add(id, instance);
return success;
}

public static Harmony ManualInstance(string id)
Expand Down
20 changes: 15 additions & 5 deletions Tweaks/TweaksAssembly/Tweaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Tweaks : MonoBehaviour
public static Action<KMGameInfo.State, KMGameInfo.State> OnStateChanged;

private Tweak[] AllTweaks = new Tweak[0];
private Type[] AllModulePatches = new Type[0];
public Dictionary<string, List<Type>> AllModulePatches = new Dictionary<string, List<Type>>();

public void Awake()
{
Expand Down Expand Up @@ -186,8 +186,8 @@ public void Awake()

ReflectedTypes.UpdateTypes();

foreach(var patchType in AllModulePatches)
Patching.EnsurePatch(patchType.Name, patchType);
foreach(var patchGroup in AllModulePatches.Values)
patchGroup.RemoveAll(patchType => Patching.EnsurePatch(patchType.Name, patchType));

ReflectedTypes.CurrencyAPIEndpointField?.SetValue(null, settings.ModuleTweaks ? "https://api.exchangerate.host" : "http://api.fixer.io");

Expand Down Expand Up @@ -315,8 +315,18 @@ private void UpdateModuleTweaksAndPatches()
var Types = Assembly.GetExecutingAssembly().GetTypes();
AllTweaks = Types.Where(type => typeof(Tweak).IsAssignableFrom(type) && !type.IsAbstract)
.Select(type => (Tweak) Activator.CreateInstance(type)).ToArray();
AllModulePatches = Types.Where(type => type.GetCustomAttributes(typeof(ModulePatchAttribute), true).Length > 0)
.ToArray();
var modulePatchTypes = Types.Where(type => type.GetCustomAttributes(typeof(ModulePatchAttribute), true).Length > 0)
.ToArray();
foreach(var pType in modulePatchTypes)
{
foreach(var attr in pType.GetCustomAttributes(typeof(ModulePatchAttribute), true))
{
var patchAttribute = (ModulePatchAttribute)attr;
if(!AllModulePatches.ContainsKey(patchAttribute.ModuleId))
AllModulePatches.Add(patchAttribute.ModuleId, new List<Type>());
AllModulePatches[patchAttribute.ModuleId].Add(pType);
}
}
}

// TODO: Remove this
Expand Down