Skip to content

Commit 604e007

Browse files
fix spine swap event handlers
1 parent 727131a commit 604e007

2 files changed

Lines changed: 77 additions & 14 deletions

File tree

COTL_API/CustomSkins/CustomSkinManager.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -590,28 +590,29 @@ public static void ChangeSelectedPlayerSpine(string name, int playerId = 0)
590590
{
591591
var isCoopChange = CoopManager.CoopActive && playerId == 1;
592592
var instance = isCoopChange ? PlayerFarming.players[1] : PlayerFarming.Instance;
593-
LogWarning("player instance hotswap spine: " + instance + "! Actions may not work as intended until a location change!");
593+
LogWarning("player instance hotswap spine: " + instance + "!");
594594

595595
var spineOverride = isCoopChange ? SelectedSpine2 : SelectedSpine;
596596
if (spineOverride == "") return;
597597
if (!CustomPlayerSpines.ContainsKey(spineOverride)) return;
598598
if (CustomPlayerSpines[spineOverride] == null) return;
599599

600-
var selectedSpineSkin = spineOverride.Split(['/'], 2)[1];
601-
var runtimeSkeletonAsset = CustomPlayerSpines[spineOverride];
602-
instance.Spine.skeletonDataAsset = runtimeSkeletonAsset;
603-
instance.Spine.initialSkinName = selectedSpineSkin;
604-
instance.Spine.Initialize(true);
600+
// var selectedSpineSkin = spineOverride.Split(['/'], 2)[1];
601+
// var runtimeSkeletonAsset = CustomPlayerSpines[spineOverride];
602+
// instance.Spine.skeletonDataAsset = runtimeSkeletonAsset;
603+
// instance.Spine.initialSkinName = selectedSpineSkin;
604+
// instance.Spine.Initialize(true);
605605

606-
//this.anim.AnimationState.Event += new Spine.AnimationState.TrackEntryEventDelegate(this.SpineEventHandler);
607-
//enable the spine animator event tracker after replacing spine
608-
instance.simpleSpineAnimator.anim.AnimationState.Event -=
609-
instance.simpleSpineAnimator.SpineEventHandler;
610-
instance.simpleSpineAnimator.anim.AnimationState.Event +=
611-
instance.simpleSpineAnimator.SpineEventHandler;
606+
// //this.anim.AnimationState.Event += new Spine.AnimationState.TrackEntryEventDelegate(this.SpineEventHandler);
607+
// //enable the spine animator event tracker after replacing spine
608+
// instance.simpleSpineAnimator.anim.AnimationState.Event -=
609+
// instance.simpleSpineAnimator.SpineEventHandler;
610+
// instance.simpleSpineAnimator.anim.AnimationState.Event +=
611+
// instance.simpleSpineAnimator.SpineEventHandler;
612+
instance.Start(); //rerun the start prefix patch
612613

613614
NotificationCentre.Instance.PlayGenericNotification(
614-
"<color=\"yellow\">Warning: Spine hotswap detected!</color> Actions may not work as intended until a location change.",
615+
"<color=\"yellow\">Experimental: Spine hotswap detected!</color> If you encounter any issues, please report them in the <color=\"yellow\">COTL Modding Discord</color>.",
615616
NotificationBase.Flair.Winter);
616617
}
617618
}

COTL_API/CustomSkins/CustomSkinPatches.cs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System.Collections;
2+
using System.Reflection;
23
using HarmonyLib;
34
using Lamb.UI;
45
using LeTai.Asset.TranslucentImage;
6+
using Sirenix.Serialization.Utilities;
57
using Spine;
8+
using Spine.Unity;
69
using src.Alerts;
710
using UnityEngine;
811
using UnityEngine.Experimental.Rendering;
@@ -248,20 +251,79 @@ private static bool PlayerFarming_Start(PlayerFarming __instance)
248251
if (!CustomPlayerSpines.ContainsKey(spineOverride)) return true;
249252
if (CustomPlayerSpines[spineOverride] == null) return true;
250253

254+
__instance.simpleSpineAnimator = __instance.GetComponentInChildren<SimpleSpineAnimator>();
255+
//attempt to collect the event delegates via reflection
256+
MulticastDelegate delegates;
257+
try {
258+
LogWarning("Reflection start BEFORE");
259+
var target = __instance.simpleSpineAnimator.anim.AnimationState;
260+
var field = target.GetType().GetField("Event", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
261+
LogWarning("Reflection field 1");
262+
263+
delegates = field.GetValue(target) as MulticastDelegate;
264+
265+
LogWarning("Reflection field 2 delegate success, size is " + delegates.GetInvocationList().Length);
266+
delegates.GetInvocationList().ForEach(x => __instance.simpleSpineAnimator.anim.AnimationState.Event += x as Spine.AnimationState.TrackEntryEventDelegate);
267+
268+
269+
}
270+
catch (Exception e) {
271+
delegates = null;
272+
LogWarning(e.ToString());
273+
}
274+
251275
var selectedSpineSkin = spineOverride.Split(['/'], 2)[1];
252276
var runtimeSkeletonAsset = CustomPlayerSpines[spineOverride];
253-
__instance.simpleSpineAnimator = __instance.GetComponentInChildren<SimpleSpineAnimator>();
254277
__instance.Spine.skeletonDataAsset = runtimeSkeletonAsset;
255278
__instance.Spine.initialSkinName = selectedSpineSkin;
256279
__instance.Spine.Initialize(true);
257280

258281
//this.anim.AnimationState.Event += new Spine.AnimationState.TrackEntryEventDelegate(this.SpineEventHandler);
259282
//enable the spine animator event tracker after replacing spine
283+
try {
284+
if (delegates != null)
285+
{
286+
287+
foreach (var delegateX in delegates.GetInvocationList())
288+
{
289+
LogWarning("Attempting to Reapply animation handler - " + delegateX.GetMethodInfo().DeclaringType + "." + delegateX.GetMethodInfo().Name);
290+
__instance.simpleSpineAnimator.anim.AnimationState.Event -= delegateX as Spine.AnimationState.TrackEntryEventDelegate;
291+
__instance.simpleSpineAnimator.anim.AnimationState.Event += delegateX as Spine.AnimationState.TrackEntryEventDelegate;
292+
}
293+
}
294+
}
295+
catch (Exception e) {
296+
LogWarning("Error trying to add animation handlers! Some actions may not work! " + e.ToString());
297+
}
260298
__instance.simpleSpineAnimator.anim.AnimationState.Event -=
261299
__instance.simpleSpineAnimator.SpineEventHandler;
262300
__instance.simpleSpineAnimator.anim.AnimationState.Event +=
263301
__instance.simpleSpineAnimator.SpineEventHandler;
264302

303+
// try
304+
// {
305+
// LogWarning("Reapplying Animation Handlers");
306+
// __instance.simpleSpineAnimator.anim.AnimationState.Event += __instance.playerController.HandleAnimationStateEvent;
307+
// __instance.simpleSpineAnimator.anim.AnimationState.Event += __instance.playerWeapon.HandleAnimationStateEvent;
308+
309+
// }
310+
// catch (Exception e)
311+
// {
312+
// LogWarning("Error adding animation handlers! Some actions may not work! " + e.ToString());
313+
// }
314+
//remove player shaders to prevent random sprites from showing
315+
__instance.Spine.CustomMaterialOverride.Clear();
316+
__instance.Spine.CustomSlotMaterials.Clear();
317+
318+
var allShaderRenderers = __instance.GetComponentsInChildren<SkeletonRendererCustomMaterials>();
319+
foreach (var shaderRenderer in allShaderRenderers)
320+
{
321+
shaderRenderer.customMaterialOverrides.Clear();
322+
shaderRenderer.customSlotMaterials.Clear();
323+
shaderRenderer.enabled = false;
324+
}
325+
326+
265327
LogInfo("PLAYERFARMING_START: Loaded Custom Spine " + spineOverride + " with skin " + selectedSpineSkin + " For player ID " + __instance.playerID);
266328

267329
return true;

0 commit comments

Comments
 (0)