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
58 changes: 23 additions & 35 deletions InscryptionAPI/Compatibility/TypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,54 @@ namespace APIPlugin;
public class IgnoreMappingAttribute : Attribute { }

[Obsolete("Unnecessary", true)]
public static class TypeMapper<S, D> where S : class where D : class
{
public static class TypeMapper<S, D> where S : class where D : class {
private static Dictionary<string, MethodInfo> _accessors = null;
private static Dictionary<string, MethodInfo> FieldAccessors
{
get
{
if (_accessors is null)
{
private static Dictionary<string, MethodInfo> 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;
}
}

private static Dictionary<string, MethodInfo> _setters = null;
private static Dictionary<string, MethodInfo> FieldSetters
{
get
{
if (_setters == null)
{
private static Dictionary<string, MethodInfo> 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 });
}
}

Expand Down
51 changes: 51 additions & 0 deletions InscryptionCommunityPatch/Card/Part1CostEmissionMaskRender.cs
Original file line number Diff line number Diff line change
@@ -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<CardDisplayer3D, SpriteRenderer> 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<SpriteRenderer>();
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;
}
}
}
2 changes: 2 additions & 0 deletions InscryptionCommunityPatch/InscryptionCommunityPatchPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class PatchPlugin : BaseUnityPlugin
internal static ConfigEntry<bool> configShowSquirrelTribeOnCards;
internal static ConfigEntry<bool> configAct3Bones;

internal static ConfigEntry<bool> configCostMask;
internal static ConfigEntry<bool> configResetEyes;
internal static ConfigEntry<bool> undeadCatEmission;

Expand Down Expand Up @@ -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.");
}

Expand Down
Loading