-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use reflection instead of dependency ExtendedVariantMode.dll
- Loading branch information
1 parent
3487afb
commit 440f3a5
Showing
3 changed files
with
28 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 28 additions & 22 deletions
50
CelesteTAS-EverestInterop/Source/Utils/ExtendedVariantsUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using ExtendedVariants.Module; | ||
using Celeste.Mod; | ||
using MonoMod.Utils; | ||
|
||
namespace TAS.Utils; | ||
|
||
internal static class ExtendedVariantsUtils { | ||
private static readonly Lazy<bool> installed = new(() => ModUtils.GetModule("ExtendedVariantMode") != null); | ||
|
||
// enum value might be different between different ExtendedVariantMode version | ||
private static readonly Lazy<object> upsideDownVariant = | ||
new(() => Enum.Parse(typeof(ExtendedVariantsModule.Variant), "UpsideDown")); | ||
|
||
private static readonly Lazy<object> superDashingVariant = | ||
new(() => Enum.Parse(typeof(ExtendedVariantsModule.Variant), "SuperDashing")); | ||
|
||
private static bool upsideDown { | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
get => (bool) ExtendedVariantsModule.Instance.TriggerManager.GetCurrentVariantValue( | ||
(ExtendedVariantsModule.Variant) upsideDownVariant.Value); | ||
private static readonly Lazy<EverestModule> module = new(() => ModUtils.GetModule("ExtendedVariantMode")); | ||
private static readonly Lazy<object> triggerManager = new(() => module.Value?.GetFieldValue<object>("TriggerManager")); | ||
|
||
private static readonly Lazy<FastReflectionDelegate> getCurrentVariantValue = new(() => | ||
triggerManager.Value?.GetType().GetMethodInfo("GetCurrentVariantValue")?.GetFastDelegate()); | ||
|
||
private static readonly Lazy<Type> variantType = | ||
new(() => module.Value?.GetType().Assembly.GetType("ExtendedVariants.Module.ExtendedVariantsModule+Variant")); | ||
|
||
// enum value might be different between different ExtendedVariantMode version, so we have to parse from string | ||
private static readonly Lazy<object> upsideDownVariant = new(ParseVariant("UpsideDown")); | ||
private static readonly Lazy<object> superDashingVariant = new(ParseVariant("SuperDashing")); | ||
|
||
private static Func<object> ParseVariant(string value) { | ||
return () => { | ||
try { | ||
return variantType.Value == null ? null : Enum.Parse(variantType.Value, value); | ||
} catch (Exception e) { | ||
e.LogException($"Parsing Variant.{value} Failed."); | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
public static bool UpsideDown => installed.Value && upsideDown; | ||
public static bool UpsideDown => GetCurrentVariantValue(upsideDownVariant); | ||
public static bool SuperDashing => GetCurrentVariantValue(superDashingVariant); | ||
|
||
private static bool superDashing { | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
get => (bool) ExtendedVariantsModule.Instance.TriggerManager.GetCurrentVariantValue( | ||
(ExtendedVariantsModule.Variant) superDashingVariant.Value); | ||
private static bool GetCurrentVariantValue(Lazy<object> variant) { | ||
return variant.Value is { } value && (bool?) getCurrentVariantValue.Value?.Invoke(triggerManager.Value, value) == true; | ||
} | ||
|
||
public static bool SuperDashing => installed.Value && superDashing; | ||
} |
Binary file not shown.