diff --git a/Patches/MarbleControllerPatches.cs b/Patches/MarbleControllerPatches.cs index 36e4ba2..0d6c092 100644 --- a/Patches/MarbleControllerPatches.cs +++ b/Patches/MarbleControllerPatches.cs @@ -1,6 +1,7 @@ using HarmonyLib; using MIU; using System; +using System.Collections.Generic; using UnityEngine; namespace CustomCosmeticLoader.Patches @@ -41,37 +42,40 @@ namespace CustomCosmeticLoader.Patches [HarmonyPatch(typeof(MarbleController), nameof(MarbleController.ApplyCosmetics))] internal class MarbleControllerApplyCosmeticsPatch { - static void Prefix(ref ReplayCosmetics cosmetics) + static void Prefix(MarbleController __instance, ref ReplayCosmetics cosmetics) { // Actually, we can let this one run even if the custom skins are disabled //if (!Config.enabled) // return; - if (!Config.overrideReplayCosmetics) + if (Config.overrideReplayCosmetics) + { + cosmetics.Skin = CosmeticManager.MySkin.Id; + cosmetics.Trail = CosmeticManager.MyTrail.Id; + cosmetics.Respawn = CosmeticManager.MyRespawn.Id; + cosmetics.Hat = CosmeticManager.MyHat.Id; + cosmetics.Blast = CosmeticManager.MyBlast.Id; + } + + if (!Config.enabled || Config.skinNameToHijack == "*") return; - cosmetics.Skin = CosmeticManager.MySkin.Id; - cosmetics.Trail = CosmeticManager.MyTrail.Id; - cosmetics.Respawn = CosmeticManager.MyRespawn.Id; - cosmetics.Hat = CosmeticManager.MyHat.Id; - cosmetics.Blast = CosmeticManager.MyBlast.Id; - } + string replayName = null; - static void Postfix(MarbleController __instance, ReplayCosmetics cosmetics) - { - if (!Config.enabled) - return; + GamePlayManager.Get().GetCurrentReplays(delegate (List replays) + { + if (replays.Count > 0) + replayName = replays[0].Player; + }); - if (Config.skinNameToHijack == "*") - return; + Shared.Log("MarbleControllerApplyCosmeticsPatch Replay name: " + replayName); - if (!Config.inAllReplays) - return; + if (Config.otherPlayers.ContainsKey(replayName) || replayName == Player.Current.Name) + cosmetics.Skin = Config.skinNameToHijack; - MarbleHolder mHolder = __instance.GetComponentInChildren(); - string actualSkinId = mHolder.CosmeticSet.skin; - mHolder.SetMarble(Shared.SkinToHijack); - mHolder.CosmeticSet.skin = actualSkinId; + // HACK - call this early so that SetMarble is aware of it + // Normally it would be called right AFTER ApplyCosmetics + __instance.AddMode(MarbleController.ReplayMode); } } } diff --git a/Patches/MarbleHolderPatches.cs b/Patches/MarbleHolderPatches.cs index 616b986..880be43 100644 --- a/Patches/MarbleHolderPatches.cs +++ b/Patches/MarbleHolderPatches.cs @@ -1,6 +1,8 @@ using HarmonyLib; +using MIU; using Parse.Common.Internal; using System; +using System.Collections.Generic; using System.Reflection; using UnityEngine; @@ -36,22 +38,60 @@ namespace CustomCosmeticLoader.Patches if (!Config.inCosmeticMenu && __instance == CosmeticPanel.cosmHolder) return; + MarbleController controller = MarbleHolderValues.MbcField.GetValue(__instance) as MarbleController; + if (controller == null) + return; + Texture2D skin = null; if (NetworkManager.IsMultiplayer) { - MarbleController controller = MarbleHolderValues.MbcField.GetValue(__instance) as MarbleController; - if (controller == null) - return; - if (!controller.isMyClientMarble()) + if (controller.isMyClientMarble()) + { + skin = Config.skins[Config.currentSkin]; + } + else { if (!Config.otherPlayers.ContainsKey(controller.nickname)) return; skin = Config.skins[Config.otherPlayers[controller.nickname]]; } } + else + { + if (controller.InGhostMode || + controller.InMode(MarbleController.ReplayMode) || + controller.name.StartsWith("Marble Ghost")) + { + string replayName = null; - Shared.ApplyCustomTexture(__instance.currentMarble, skin); + GamePlayManager.Get().GetCurrentReplays(delegate (List replays) + { + if (replays.Count > 0) + replayName = replays[0].Player; + }); + + Shared.Log("MarbleHolderSetMarblePatch Replay name: " + replayName); + if (replayName == Player.Current.Name) + { + skin = Config.skins[Config.currentSkin]; + } + else + { + if (Config.otherPlayers.ContainsKey(replayName)) + skin = Config.skins[Config.otherPlayers[replayName]]; + } + } + else + { + skin = Config.skins[Config.currentSkin]; + } + } + + if (skin != null) + { + Shared.ApplyCustomTexture(__instance.currentMarble, skin); + } } } diff --git a/Shared.cs b/Shared.cs index 3edc55a..b58aa8b 100644 --- a/Shared.cs +++ b/Shared.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.Remoting.Messaging; using UnityEngine; namespace CustomCosmeticLoader @@ -42,6 +43,12 @@ namespace CustomCosmeticLoader public static void ApplyCustomTexture(GameObject marbleObject, Texture2D skin = null) { + if (marbleObject == null) + { + Shared.Log("ApplyCustomTexture - marbleObject is null"); + return; + } + if (skin == null) skin = Config.skins[Config.currentSkin]; @@ -49,8 +56,15 @@ namespace CustomCosmeticLoader for (int i = 0; i < componentsInChildren.Length; i++) { Material[] materials = componentsInChildren[i].materials; + if (materials == null) + continue; foreach (Material material in materials) - material.mainTexture = skin; + { + if (material == null) + continue; + if (material.mainTexture != null) + material.mainTexture = skin; + } } }