Config system, configurable texture and hijack skin

This commit is contained in:
Terry Hearst 2023-08-27 02:44:35 -04:00
parent 1c3e23df32
commit b3ad618392
4 changed files with 119 additions and 18 deletions

84
Config.cs Normal file
View file

@ -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<string, Texture2D> skins = new Dictionary<string, Texture2D>();
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());
}
}
}

View file

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

View file

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

View file

@ -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<MeshRenderer>();
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];
}
}
}