From b3ad618392fbfaf976cc6aa1024ab1836645f204 Mon Sep 17 00:00:00 2001 From: Terry Hearst Date: Sun, 27 Aug 2023 02:44:35 -0400 Subject: [PATCH] Config system, configurable texture and hijack skin --- Config.cs | 84 ++++++++++++++++++++++++++++++ PatchEntryPoint.cs | 19 ++++--- Patches/MarbleControllerPatches.cs | 23 +++++--- Patches/MarbleHolderPatches.cs | 11 ++-- 4 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 Config.cs diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..652c75e --- /dev/null +++ b/Config.cs @@ -0,0 +1,84 @@ +using I2.Loc.SimpleJSON; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using UnityEngine; + +namespace CustomCosmeticLoader +{ + public class Config + { + public const string CONFIG_FILE_NAME = "config.json"; + public const string SKINS_DIR_NAME = "skins"; + + public static bool enabled = true; + public static string currentSkin; + public static string skinToHijack = "Swirl_M"; // Mirage, simple skin with nice properties + + public static Dictionary skins = new Dictionary(); + + public static void Init() + { + if (File.Exists(GetConfigPath())) + ReadConfig(); + else + SaveConfig(); + } + + public static string GetConfigPath() + { + return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_FILE_NAME); + } + + private static void ReadConfig() + { + if (!File.Exists(GetConfigPath())) + return; + + JSONNode data; + try + { + data = JSON.Parse(File.ReadAllText(GetConfigPath())); + } + catch (Exception e) + { + Debug.LogError("Couldn't load CustomCosmeticLoader config!"); + Debug.LogException(e); + return; + } + + if (data["enabled"] != null) + enabled = data["enabled"].AsBool; + + if (data["currentSkin"] != null) + currentSkin = data["currentSkin"].Value; + if (currentSkin == null || !skins.ContainsKey(currentSkin)) + { + if (skins.Count == 0) + { + Debug.LogWarning("No custom skins found, disabling"); + enabled = false; + return; + } + currentSkin = skins.Keys.ToList()[0]; + } + + if (data["skinToHijack"] != null) + skinToHijack = data["skinToHijack"].Value; + } + + public static void SaveConfig() + { + JSONNode node = new JSONClass + { + { "enabled", enabled ? "true" : "false" }, + { "currentSkin", currentSkin }, + { "skinToHijack", skinToHijack } + }; + + File.WriteAllText(GetConfigPath(), node.ToString()); + } + } +} diff --git a/PatchEntryPoint.cs b/PatchEntryPoint.cs index 1618b39..fbdf384 100644 --- a/PatchEntryPoint.cs +++ b/PatchEntryPoint.cs @@ -11,8 +11,6 @@ namespace CustomCosmeticLoader { private static bool IsPatched = false; - public static Texture2D skin; - public static void Start() { SceneManager.sceneLoaded += beginPatch; @@ -26,15 +24,24 @@ namespace CustomCosmeticLoader IsPatched = true; SceneManager.sceneLoaded -= beginPatch; LoadCosmetics(); + Config.Init(); } } private static void LoadCosmetics() { - string skinFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - skinFile = Path.Combine(skinFile, "skin.png"); - skin = new Texture2D(2, 2); - skin.LoadImage(File.ReadAllBytes(skinFile)); + string skinsPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + skinsPath = Path.Combine(skinsPath, "skins"); + string printStr = "Loaded custom skins: "; + foreach (string skinFile in Directory.GetFiles(skinsPath)) + { + string skinName = Path.GetFileNameWithoutExtension(skinFile); + printStr += skinName + ", "; + Texture2D skinTexture = new Texture2D(2, 2); + skinTexture.LoadImage(File.ReadAllBytes(skinFile)); + Config.skins.Add(skinName, skinTexture); + } + Debug.Log(printStr); } } } diff --git a/Patches/MarbleControllerPatches.cs b/Patches/MarbleControllerPatches.cs index e061e8a..64dd6cf 100644 --- a/Patches/MarbleControllerPatches.cs +++ b/Patches/MarbleControllerPatches.cs @@ -1,5 +1,6 @@ using HarmonyLib; using System; +using UnityEngine; namespace CustomCosmeticLoader.Patches { @@ -7,30 +8,38 @@ namespace CustomCosmeticLoader.Patches [HarmonyPatch("ApplyMyCosmetics")] internal class MarbleControllerApplyMyCosmeticsPatch { - private static Cosmetic mirageSkin = null; + private static Cosmetic hijackSkin = null; [HarmonyPostfix] static void Postfix(MarbleController __instance) { - if (mirageSkin == null) + if (!Config.enabled) + return; + + if (Config.skinToHijack == "*") + return; + + if (hijackSkin == null) { - // We hijack one skin (Mirage) since it's one of the basic skins with no animations/different UVs/etc foreach (Cosmetic skin in CosmeticManager.Skins) { - if (skin.Id == "Swirl_M") + if (skin.Id == Config.skinToHijack) { - mirageSkin = skin; + hijackSkin = skin; break; } } - if (mirageSkin == null) //whut + if (hijackSkin == null) + { + Debug.LogWarning("Couldn't hijack skin " + Config.skinToHijack); return; + } } MarbleHolder mHolder = __instance.MHolder; string actualSkinId = mHolder.CosmeticSet.skin; - mHolder.SetMarble(mirageSkin); + mHolder.SetMarble(hijackSkin); // 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 d2ebfab..9948809 100644 --- a/Patches/MarbleHolderPatches.cs +++ b/Patches/MarbleHolderPatches.cs @@ -10,17 +10,18 @@ namespace CustomCosmeticLoader.Patches { static void Postfix(MarbleHolder __instance, Cosmetic marbleObject) { - if (marbleObject.Id != "Swirl_M") - return; // I only hijack this one skin, "Mirage" + if (!Config.enabled) + return; + + if (marbleObject.Id != Config.skinToHijack && Config.skinToHijack != "*") + 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 = PatchEntryPoint.skin; - } + material.mainTexture = Config.skins[Config.currentSkin]; } } }