Show custom skins when watching replays

This commit is contained in:
Terry Hearst 2023-09-10 16:54:51 -04:00
parent 8d5de1a6c1
commit 7c35b2e5da
3 changed files with 84 additions and 26 deletions

View file

@ -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<Replay> 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<MarbleHolder>();
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);
}
}
}

View file

@ -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<Replay> 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);
}
}
}

View file

@ -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;
}
}
}