Load custom skin from skin.png

This commit is contained in:
Terry Hearst 2023-08-26 17:59:50 -04:00
parent 4886e1c257
commit 1c3e23df32
3 changed files with 79 additions and 0 deletions

View file

@ -1,5 +1,8 @@
using HarmonyLib; using HarmonyLib;
using System; using System;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
namespace CustomCosmeticLoader namespace CustomCosmeticLoader
@ -8,6 +11,8 @@ namespace CustomCosmeticLoader
{ {
private static bool IsPatched = false; private static bool IsPatched = false;
public static Texture2D skin;
public static void Start() public static void Start()
{ {
SceneManager.sceneLoaded += beginPatch; SceneManager.sceneLoaded += beginPatch;
@ -20,7 +25,16 @@ namespace CustomCosmeticLoader
new Harmony("com.thearst3rd.customcosmeticloader").PatchAll(); new Harmony("com.thearst3rd.customcosmeticloader").PatchAll();
IsPatched = true; IsPatched = true;
SceneManager.sceneLoaded -= beginPatch; SceneManager.sceneLoaded -= beginPatch;
LoadCosmetics();
} }
} }
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));
}
} }
} }

View file

@ -0,0 +1,38 @@
using HarmonyLib;
using System;
namespace CustomCosmeticLoader.Patches
{
[HarmonyPatch(typeof(MarbleController))]
[HarmonyPatch("ApplyMyCosmetics")]
internal class MarbleControllerApplyMyCosmeticsPatch
{
private static Cosmetic mirageSkin = null;
[HarmonyPostfix]
static void Postfix(MarbleController __instance)
{
if (mirageSkin == 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")
{
mirageSkin = skin;
break;
}
}
if (mirageSkin == null) //whut
return;
}
MarbleHolder mHolder = __instance.MHolder;
string actualSkinId = mHolder.CosmeticSet.skin;
mHolder.SetMarble(mirageSkin);
// Setting the skin id here allows others in multiplayer/replays to see your normal skin
mHolder.CosmeticSet.skin = actualSkinId;
}
}
}

View file

@ -0,0 +1,27 @@
using HarmonyLib;
using System;
using UnityEngine;
namespace CustomCosmeticLoader.Patches
{
[HarmonyPatch(typeof(MarbleHolder))]
[HarmonyPatch("SetMarble")]
internal class MarbleHolderSetMarblePatch
{
static void Postfix(MarbleHolder __instance, Cosmetic marbleObject)
{
if (marbleObject.Id != "Swirl_M")
return; // I only hijack this one skin, "Mirage"
MeshRenderer[] componentsInChildren = __instance.currentMarble.GetComponentsInChildren<MeshRenderer>();
for (int i = 0; i < componentsInChildren.Length; i++)
{
Material[] materials = componentsInChildren[i].materials;
foreach (Material material in materials)
{
material.mainTexture = PatchEntryPoint.skin;
}
}
}
}
}