diff --git a/Config.cs b/Config.cs index b89d691..a04cdc5 100644 --- a/Config.cs +++ b/Config.cs @@ -15,12 +15,14 @@ namespace CustomCosmeticLoader public const string PROPERTY_ENABLED = "enabled"; public const string PROPERTY_CURRENT_SKIN = "currentSkin"; - public const string PROPERTY_SKIN_TO_HIJACK = "skinToHijack"; + public const string PROPERTY_SKIN_NAME_TO_HIJACK = "skinNameToHijack"; + public const string PROPERTY_IN_MAIN_MENU = "inMainMenu"; public const string PROPERTY_IN_COSMETIC_MENU = "inCosmeticMenu"; public static bool enabled = true; public static string currentSkin; - public static string skinToHijack = "Swirl_M"; // Mirage, simple skin with nice properties + public static string skinNameToHijack = "Swirl_M"; // Mirage, simple skin with nice properties + public static bool inMainMenu = true; public static bool inCosmeticMenu = false; public static Dictionary skins = new Dictionary(); @@ -71,8 +73,11 @@ namespace CustomCosmeticLoader currentSkin = skins.Keys.ToList()[0]; } - if (data[PROPERTY_SKIN_TO_HIJACK] != null) - skinToHijack = data[PROPERTY_SKIN_TO_HIJACK].Value; + if (data[PROPERTY_SKIN_NAME_TO_HIJACK] != null) + skinNameToHijack = data[PROPERTY_SKIN_NAME_TO_HIJACK].Value; + + if (data[PROPERTY_IN_MAIN_MENU] != null) + inMainMenu = data[PROPERTY_IN_MAIN_MENU].AsBool; if (data[PROPERTY_IN_COSMETIC_MENU] != null) inCosmeticMenu = data[PROPERTY_IN_COSMETIC_MENU].AsBool; @@ -84,7 +89,8 @@ namespace CustomCosmeticLoader { { PROPERTY_ENABLED, enabled ? "true" : "false" }, { PROPERTY_CURRENT_SKIN, currentSkin }, - { PROPERTY_SKIN_TO_HIJACK, skinToHijack }, + { PROPERTY_SKIN_NAME_TO_HIJACK, skinNameToHijack }, + { PROPERTY_IN_MAIN_MENU, inMainMenu ? "true" : "false" }, { PROPERTY_IN_COSMETIC_MENU, inCosmeticMenu ? "true" : "false" }, }; diff --git a/Patches/MainMenuPanelPatches.cs b/Patches/MainMenuPanelPatches.cs new file mode 100644 index 0000000..9654a13 --- /dev/null +++ b/Patches/MainMenuPanelPatches.cs @@ -0,0 +1,32 @@ +using HarmonyLib; +using System; + +namespace CustomCosmeticLoader.Patches +{ + [HarmonyPatch(typeof(MainMenuPanel), nameof(MainMenuPanel.SetupImages), new Type[] { typeof(bool) })] + internal class MainMenuPanelSetupImagesPatch + { + static void Postfix(MainMenuPanel __instance) + { + if (!Config.enabled) + return; + + if (!Config.inMainMenu) + return; + + if (Config.skinNameToHijack == "*") + return; + + if (Shared.SkinToHijack == null) + return; + + if (__instance.cosmeticDisplay) + { + __instance.cosmeticDisplay.Clear(); + GameObjectAllocator.Deinitialize(__instance.cosmeticDisplay.gameObject); + __instance.cosmeticDisplay.Setup(Shared.SkinToHijack, CosmeticType.Skin, true, true); + Shared.ApplyCustomTexture(__instance.cosmeticDisplay.CachedCosmeticGameObject); + } + } + } +} diff --git a/Patches/MarbleControllerPatches.cs b/Patches/MarbleControllerPatches.cs index 5f82dd8..77c9245 100644 --- a/Patches/MarbleControllerPatches.cs +++ b/Patches/MarbleControllerPatches.cs @@ -7,38 +7,21 @@ namespace CustomCosmeticLoader.Patches [HarmonyPatch(typeof(MarbleController), nameof(MarbleController.ApplyMyCosmetics))] internal class MarbleControllerApplyMyCosmeticsPatch { - private static Cosmetic hijackSkin = null; - [HarmonyPostfix] static void Postfix(MarbleController __instance) { if (!Config.enabled) return; - if (Config.skinToHijack == "*") + if (Config.skinNameToHijack == "*") return; - if (hijackSkin == null) - { - foreach (Cosmetic skin in CosmeticManager.Skins) - { - if (skin.Id == Config.skinToHijack) - { - hijackSkin = skin; - break; - } - } - - if (hijackSkin == null) - { - Debug.LogWarning("Couldn't hijack skin " + Config.skinToHijack); - return; - } - } + if (Shared.SkinToHijack == null) + return; MarbleHolder mHolder = __instance.MHolder; string actualSkinId = mHolder.CosmeticSet.skin; - mHolder.SetMarble(hijackSkin); + mHolder.SetMarble(Shared.SkinToHijack); // Setting the skin id here allows others in multiplayer/replays to see your normal skin mHolder.CosmeticSet.skin = actualSkinId; } diff --git a/Patches/MarbleHolderPatches.cs b/Patches/MarbleHolderPatches.cs index 40954ab..c1a2f95 100644 --- a/Patches/MarbleHolderPatches.cs +++ b/Patches/MarbleHolderPatches.cs @@ -12,19 +12,13 @@ namespace CustomCosmeticLoader.Patches if (!Config.enabled) return; - if (marbleObject.Id != Config.skinToHijack && Config.skinToHijack != "*") + if (marbleObject.Id != Config.skinNameToHijack && Config.skinNameToHijack != "*") return; if (!Config.inCosmeticMenu && __instance == CosmeticPanel.cosmHolder) return; - MeshRenderer[] componentsInChildren = __instance.currentMarble.GetComponentsInChildren(); - for (int i = 0; i < componentsInChildren.Length; i++) - { - Material[] materials = componentsInChildren[i].materials; - foreach (Material material in materials) - material.mainTexture = Config.skins[Config.currentSkin]; - } + Shared.ApplyCustomTexture(__instance.currentMarble); } } } diff --git a/Shared.cs b/Shared.cs new file mode 100644 index 0000000..77a062f --- /dev/null +++ b/Shared.cs @@ -0,0 +1,51 @@ +using System; +using UnityEngine; + +namespace CustomCosmeticLoader +{ + public static class Shared + { + private static Cosmetic skinToHijack; + private static bool noSkinToHijack = false; + + public static Cosmetic SkinToHijack + { + get + { + if (noSkinToHijack) + return null; + if (skinToHijack == null) + { + DetermineSkinToHijack(); + if (skinToHijack == null) + Debug.LogWarning("Couldn't hijack skin " + Config.skinNameToHijack); + } + + return skinToHijack; + } + } + + private static void DetermineSkinToHijack() + { + foreach (Cosmetic skin in CosmeticManager.Skins) + { + if (skin.Id == Config.skinNameToHijack) + { + skinToHijack = skin; + break; + } + } + } + + public static void ApplyCustomTexture(GameObject marbleObject) + { + MeshRenderer[] componentsInChildren = marbleObject.GetComponentsInChildren(); + for (int i = 0; i < componentsInChildren.Length; i++) + { + Material[] materials = componentsInChildren[i].materials; + foreach (Material material in materials) + material.mainTexture = Config.skins[Config.currentSkin]; + } + } + } +}