miku.dll

Decompiled 2 weeks ago
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("miku")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("miku")]
[assembly: AssemblyTitle("miku")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Miku;

[BepInPlugin("Ginja.Miku", "Miku", "1.0.0")]
public class MikuLoader : BaseUnityPlugin
{
	private readonly Harmony harmony = new Harmony("Ginja.Miku");

	internal static MikuLoader instance;

	internal static GameObject MikuModel;

	internal static Texture2D bodyTex;

	internal static Texture2D faceTex;

	internal static Texture2D clothesTex;

	internal static Texture2D hairTex;

	private void Awake()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Loading Miku Model");
		if ((Object)(object)instance == (Object)null)
		{
			instance = this;
			harmony.PatchAll();
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			string text = Path.Combine(directoryName, "miku");
			AssetBundle val = AssetBundle.LoadFromFile(text);
			if ((Object)(object)val == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Failed to load asset bundle!");
				return;
			}
			MikuModel = val.LoadAsset<GameObject>("Miku");
			bodyTex = val.LoadAsset<Texture2D>("Body.png");
			faceTex = val.LoadAsset<Texture2D>("Face.png");
			clothesTex = val.LoadAsset<Texture2D>("Clothes.png");
			hairTex = val.LoadAsset<Texture2D>("Hair.png");
			if ((Object)(object)MikuModel == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Failed to load replacement models!");
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Successfully loaded Miku Model");
			}
		}
		else
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
	}
}
[HarmonyPatch(typeof(BingBong))]
internal class MikuPatch
{
	private static readonly ManualLogSource Logger = Logger.CreateLogSource("MikuLoader");

	[HarmonyPatch("Start")]
	[HarmonyPostfix]
	private static void ReplaceMiku(BingBong __instance)
	{
		Logger.LogInfo((object)"Starting Loading Proccess");
		if ((Object)(object)MikuLoader.MikuModel == (Object)null)
		{
			Logger.LogError((object)"Miku Model Missing. Using Original Model");
			return;
		}
		Transform val = FindDeepChild(((Component)__instance).transform, "Bing Bong Plush");
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)"Bing Bong Model Not Found");
			return;
		}
		ReplaceBingBong(val, MikuLoader.MikuModel, __instance);
		Logger.LogInfo((object)"Model Replaced");
	}

	private static Transform FindDeepChild(Transform parent, string name)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		foreach (Transform item in parent)
		{
			Transform val = item;
			if (((Object)val).name == name)
			{
				return val;
			}
			Transform val2 = FindDeepChild(val, name);
			if ((Object)(object)val2 != (Object)null)
			{
				return val2;
			}
		}
		return null;
	}

	private static void ReplaceBingBong(Transform oldmodel, GameObject newModelPrefab, BingBong visuals)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = Object.Instantiate<GameObject>(newModelPrefab, oldmodel.parent);
		val.transform.SetPositionAndRotation(oldmodel.position, oldmodel.rotation);
		val.layer = ((Component)oldmodel).gameObject.layer;
		Shader val2 = Shader.Find("W/Peak_Standard");
		if ((Object)(object)val2 == (Object)null)
		{
			Logger.LogError((object)"Shader 'W/Peak_Standard' not found!");
			return;
		}
		Renderer[] componentsInChildren = val.GetComponentsInChildren<Renderer>();
		foreach (Renderer val3 in componentsInChildren)
		{
			Material[] materials = val3.materials;
			foreach (Material val4 in materials)
			{
				val4.shader = val2;
			}
		}
		ApplyMikuTextures(val);
		val.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
		Transform transform = val.transform;
		transform.rotation *= Quaternion.Euler(0f, -90f, 0f);
		Transform transform2 = val.transform;
		transform2.position += new Vector3(0f, -0.5f, 0f);
		Object.Destroy((Object)(object)((Component)oldmodel).gameObject);
	}

	private static void ApplyMikuTextures(GameObject miku)
	{
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Expected O, but got Unknown
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Expected O, but got Unknown
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		Shader shader = Shader.Find("W/Peak_Standard");
		string[] array = new string[8] { "Top", "Tie", "Skirt", "HairTies", "Gloves", "Cube", "Boots", "Belt" };
		string[] array2 = array;
		foreach (string name2 in array2)
		{
			ApplyToAll(name2, MikuLoader.clothesTex);
		}
		ApplyToAll("Hair", MikuLoader.hairTex);
		Transform obj = FindDeepChild(miku.transform, "Body");
		SkinnedMeshRenderer val = ((obj != null) ? ((Component)obj).GetComponent<SkinnedMeshRenderer>() : null);
		if ((Object)(object)val != (Object)null)
		{
			Material[] sharedMaterials = ((Renderer)val).sharedMaterials;
			if (sharedMaterials.Length != 0)
			{
				Material val2 = new Material(sharedMaterials[0]);
				val2.shader = shader;
				val2.SetTexture("_BaseTexture", (Texture)(object)MikuLoader.bodyTex);
				val2.SetColor("_BaseColor", Color.white);
				val2.SetFloat("_VertexColorAmount", 1f);
				sharedMaterials[0] = val2;
			}
			if (sharedMaterials.Length > 1)
			{
				Material val3 = new Material(sharedMaterials[1]);
				val3.shader = shader;
				val3.SetTexture("_BaseTexture", (Texture)(object)MikuLoader.faceTex);
				val3.SetColor("_BaseColor", Color.white);
				val3.SetFloat("_VertexColorAmount", 1f);
				sharedMaterials[1] = val3;
			}
			((Renderer)val).sharedMaterials = sharedMaterials;
		}
		void ApplyToAll(string name, Texture2D tex)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			Transform obj2 = FindDeepChild(miku.transform, name);
			Renderer val4 = ((obj2 != null) ? ((Component)obj2).GetComponent<Renderer>() : null);
			if (!((Object)(object)val4 == (Object)null))
			{
				Material[] sharedMaterials2 = val4.sharedMaterials;
				for (int j = 0; j < sharedMaterials2.Length; j++)
				{
					Material val5 = new Material(sharedMaterials2[j]);
					val5.shader = shader;
					val5.SetTexture("_BaseTexture", (Texture)(object)tex);
					val5.SetColor("_BaseColor", Color.white);
					val5.SetFloat("_VertexColorAmount", 1f);
					sharedMaterials2[j] = val5;
				}
				val4.sharedMaterials = sharedMaterials2;
			}
		}
	}
}