diff --git a/InscryptionAPI/Compatibility/TypeMapper.cs b/InscryptionAPI/Compatibility/TypeMapper.cs index d7faa67c..0ce8a29a 100644 --- a/InscryptionAPI/Compatibility/TypeMapper.cs +++ b/InscryptionAPI/Compatibility/TypeMapper.cs @@ -10,27 +10,22 @@ namespace APIPlugin; public class IgnoreMappingAttribute : Attribute { } [Obsolete("Unnecessary", true)] -public static class TypeMapper where S : class where D : class -{ +public static class TypeMapper where S : class where D : class { private static Dictionary _accessors = null; - private static Dictionary FieldAccessors - { - get - { - if (_accessors is null) - { + private static Dictionary FieldAccessors { + get { + if (_accessors is null) { _accessors = new(); - foreach (var field in AccessTools.GetDeclaredFields(typeof(S)).Where(x => !x.GetCustomAttributes(typeof(IgnoreMappingAttribute), false).Any())) - { - var accessor = new DynamicMethodDefinition("get_" + field.Name, typeof(object), new Type[] { typeof(S) }); + foreach (var _field in AccessTools.GetDeclaredFields(typeof(S)).Where(x => !x.GetCustomAttributes(typeof(IgnoreMappingAttribute), false).Any())) { + var accessor = new DynamicMethodDefinition("get_" + _field.Name, typeof(object), new Type[] { typeof(S) }); var il = accessor.GetILProcessor(); il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Ldfld, accessor.Module.ImportReference(field)); - if (field.FieldType.IsValueType) - il.Emit(OpCodes.Box, field.FieldType); + il.Emit(OpCodes.Ldfld, accessor.Module.ImportReference(_field)); + if (_field.FieldType.IsValueType) + il.Emit(OpCodes.Box, _field.FieldType); il.Emit(OpCodes.Ret); - _accessors.Add(field.Name, accessor.Generate()); + _accessors.Add(_field.Name, accessor.Generate()); } } return _accessors; @@ -38,38 +33,31 @@ private static Dictionary FieldAccessors } private static Dictionary _setters = null; - private static Dictionary FieldSetters - { - get - { - if (_setters == null) - { + private static Dictionary FieldSetters { + get { + if (_setters == null) { _setters = new(); - foreach (var field in AccessTools.GetDeclaredFields(typeof(D))) - { - var setter = new DynamicMethodDefinition("set_" + field.Name, typeof(void), new Type[] { typeof(D), typeof(object) }); + foreach (var _field in AccessTools.GetDeclaredFields(typeof(D))) { + var setter = new DynamicMethodDefinition("set_" + _field.Name, typeof(void), new Type[] { typeof(D), typeof(object) }); var il = setter.GetILProcessor(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Unbox_Any, setter.Module.ImportReference(field.FieldType)); - il.Emit(OpCodes.Stfld, setter.Module.ImportReference(field)); + il.Emit(OpCodes.Unbox_Any, setter.Module.ImportReference(_field.FieldType)); + il.Emit(OpCodes.Stfld, setter.Module.ImportReference(_field)); il.Emit(OpCodes.Ret); - _setters.Add(field.Name, setter.Generate()); + _setters.Add(_field.Name, setter.Generate()); } } return _setters; } } - public static D Convert(S source, D destination) - { - foreach (var field in FieldAccessors) - { - object val = field.Value.Invoke(null, new object[] { source }); - if (val is not null && FieldSetters.ContainsKey(field.Key)) - { - FieldSetters[field.Key].Invoke(null, new object[] { destination, val }); + public static D Convert(S source, D destination) { + foreach (var _field in FieldAccessors) { + object val = _field.Value.Invoke(null, new object[] { source }); + if (val is not null && FieldSetters.ContainsKey(_field.Key)) { + FieldSetters[_field.Key].Invoke(null, new object[] { destination, val }); } } diff --git a/InscryptionCommunityPatch/Card/Part1CostEmissionMaskRender.cs b/InscryptionCommunityPatch/Card/Part1CostEmissionMaskRender.cs new file mode 100644 index 00000000..d4149a4c --- /dev/null +++ b/InscryptionCommunityPatch/Card/Part1CostEmissionMaskRender.cs @@ -0,0 +1,51 @@ +using DiskCardGame; +using HarmonyLib; +using System; +using System.Collections.Generic; +using System.Text; +using UnityEngine; + +namespace InscryptionCommunityPatch.Card; + +[HarmonyPatch] +public class Part1CostEmissionMaskRender { + public static readonly Dictionary CostEmissionMaskRenderers = new(); + + public static SpriteRenderer Verify3DCostEmissionMaskRenderer(CardDisplayer3D cardDisplayer, bool emissionEnabled) { + // add entry for new CardDisplayer3D's + if (!CostEmissionMaskRenderers.TryGetValue(cardDisplayer, out SpriteRenderer result)) { + //PatchPlugin.Logger.LogDebug("[Verify3DCostEmissionMaskRenderer] Add cost mask renderer to CardDisplay3D"); + GameObject obj = GameObject.Instantiate(cardDisplayer.costRenderer.gameObject, cardDisplayer.transform); + obj.name = "CostEmissionMask"; + obj.layer = cardDisplayer.emissivePortraitRenderer.gameObject.layer; + result = obj.GetComponent(); + result.color = Color.black; + result.sortingOrder = 100; + CostEmissionMaskRenderers.Add(cardDisplayer, result); + } + + if (result == null) { + PatchPlugin.Logger.LogWarning("[Verify3DCostEmissionMaskRenderer] Could not find/create SpriteRenderer for CardDisplayer3D instance"); + } + else { + // disable if config is false ; otherwise toggle based on appearance of emissive portrait + result.gameObject.SetActive(PatchPlugin.configCostMask.Value && emissionEnabled); + } + + return result; + } + + [HarmonyPrefix, HarmonyPatch(typeof(CardDisplayer3D), nameof(CardDisplayer3D.Awake))] + private static void AddCostEmissionMaskOnAwake(CardDisplayer3D __instance) { + Verify3DCostEmissionMaskRenderer(__instance, false); + } + + [HarmonyPriority(Priority.Last), HarmonyPostfix, HarmonyPatch(typeof(CardDisplayer3D), nameof(CardDisplayer3D.DisplayInfo))] + private static void UpdateCostEmissionMask(CardDisplayer3D __instance) { + SpriteRenderer rend = Verify3DCostEmissionMaskRenderer(__instance, __instance.emissivePortraitRenderer.gameObject.activeSelf); + if (rend != null) { + //PatchPlugin.Logger.LogDebug("[UpdateCostEmissionMask] Update Cost emission mask"); + rend.sprite = __instance.costRenderer.sprite; + } + } +} diff --git a/InscryptionCommunityPatch/InscryptionCommunityPatchPlugin.cs b/InscryptionCommunityPatch/InscryptionCommunityPatchPlugin.cs index 71937425..1ea6974d 100644 --- a/InscryptionCommunityPatch/InscryptionCommunityPatchPlugin.cs +++ b/InscryptionCommunityPatch/InscryptionCommunityPatchPlugin.cs @@ -31,6 +31,7 @@ public class PatchPlugin : BaseUnityPlugin internal static ConfigEntry configShowSquirrelTribeOnCards; internal static ConfigEntry configAct3Bones; + internal static ConfigEntry configCostMask; internal static ConfigEntry configResetEyes; internal static ConfigEntry undeadCatEmission; @@ -107,6 +108,7 @@ private void Awake() configFullDebug = Config.Bind("General", "Full Debug", true, "If true, displays all debug logs in the console."); configTestState = Config.Bind("General", "Test Mode", false, "Puts the game into test mode. This will cause (among potentially other things) a new run to spawn a number of cards into your opening deck that will demonstrate card behaviors."); configResetEyes = Config.Bind("Act 1", "Reset Red Eyes", false, "Resets Leshy's eyes to normal if they were turned red due to a boss fight's grizzly bear sequence."); + configCostMask = Config.Bind("Act 1", "Render Costs Above Emission", true, "Applies a mask to card emissions that prevents them from covering play costs when rendering in-game."); undeadCatEmission = Config.Bind("General", "Undead Cat Emission", false, "If true, Undead Cat will have a forced red emission."); }