using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
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("SphereLegs")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SphereLegs")]
[assembly: AssemblyTitle("SphereLegs")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SphereLegs;
[HarmonyPatch(typeof(PlayerAvatarVisuals))]
internal class LegReplacementPatch
{
private static readonly ManualLogSource Logger = Logger.CreateLogSource("LegReplacer");
[HarmonyPatch("Start")]
[HarmonyPostfix]
private static void ReplaceLegs(PlayerAvatarVisuals __instance)
{
bool flag = Chainloader.PluginInfos.ContainsKey("Mhz.REPOMoreHead");
Logger.LogInfo((object)"[Sphere Legs] Starting leg replacement process...");
if (flag)
{
Logger.LogInfo((object)"[Sphere Legs] MoreHead is loaded. Stopping leg replacement process.");
return;
}
if ((Object)(object)LegReplacer.ReplacementLegR == (Object)null || (Object)(object)LegReplacer.ReplacementLegL == (Object)null)
{
Logger.LogError((object)"[Sphere Legs] Replacement leg models are missing! Aborting replacement.");
return;
}
Transform val = FindDeepChild(((Component)__instance).transform, "mesh_leg_r");
Transform val2 = FindDeepChild(((Component)__instance).transform, "mesh_leg_l");
if ((Object)(object)val == (Object)null)
{
Logger.LogWarning((object)"[Sphere Legs] Right leg not found in character model.");
}
else
{
ReplaceLeg(val, LegReplacer.ReplacementLegR, __instance);
Logger.LogInfo((object)"[Sphere Legs] Successfully replaced right leg.");
}
if ((Object)(object)val2 == (Object)null)
{
Logger.LogWarning((object)"[Sphere Legs] Left leg not found in character model.");
}
else
{
ReplaceLeg(val2, LegReplacer.ReplacementLegL, __instance);
Logger.LogInfo((object)"[Sphere Legs] Successfully replaced left leg.");
}
Logger.LogInfo((object)"[Sphere Legs] Leg replacement process completed.");
}
public 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 ReplaceLeg(Transform oldLeg, GameObject newLegPrefab, PlayerAvatarVisuals 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_003b: Unknown result type (might be due to invalid IL or missing references)
GameObject val = Object.Instantiate<GameObject>(newLegPrefab, oldLeg.parent);
val.transform.SetPositionAndRotation(oldLeg.position, oldLeg.rotation);
val.transform.localScale = new Vector3(100f, 100f, 100f);
val.layer = ((Component)oldLeg).gameObject.layer;
Renderer component = val.GetComponent<Renderer>();
Renderer component2 = ((Component)oldLeg).GetComponent<Renderer>();
}
public static void fixMaterialColour(GameObject oldLeg, GameObject newLeg, PlayerAvatarVisuals visuals)
{
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
Renderer component = newLeg.GetComponent<Renderer>();
Renderer component2 = oldLeg.GetComponent<Renderer>();
component.material.SetTexture("_MainTex", component2.material.GetTexture("_AlbedoTexture"));
FieldInfo fieldInfo = AccessTools.Field(((object)visuals).GetType(), "color");
component.material.color = (Color)fieldInfo.GetValue(visuals);
}
}
[HarmonyPatch(typeof(PlayerAvatarVisuals), "Update")]
internal class PlayerAvatarVisualsUpdatePatch
{
private static readonly ManualLogSource Logger = Logger.CreateLogSource("Test");
private static void Postfix(PlayerAvatarVisuals __instance)
{
Transform val = LegReplacementPatch.FindDeepChild(((Component)__instance).transform, "leg_R(Clone)");
Transform val2 = LegReplacementPatch.FindDeepChild(((Component)__instance).transform, "leg_L(Clone)");
Transform val3 = LegReplacementPatch.FindDeepChild(((Component)__instance).transform, "mesh_leg_r");
Transform val4 = LegReplacementPatch.FindDeepChild(((Component)__instance).transform, "mesh_leg_l");
if ((Object)(object)val != (Object)null)
{
LegReplacementPatch.fixMaterialColour(((Component)val3).gameObject, ((Component)val).gameObject, __instance);
}
if ((Object)(object)val2 != (Object)null)
{
LegReplacementPatch.fixMaterialColour(((Component)val4).gameObject, ((Component)val2).gameObject, __instance);
}
if (Chainloader.PluginInfos.ContainsKey("Mhz.REPOMoreHead"))
{
Transform val5 = LegReplacementPatch.FindDeepChild(((Component)__instance).transform, "SphereR");
Transform val6 = LegReplacementPatch.FindDeepChild(((Component)__instance).transform, "SphereL");
LegReplacementPatch.fixMaterialColour(((Component)val3).gameObject, ((Component)val5).gameObject, __instance);
LegReplacementPatch.fixMaterialColour(((Component)val4).gameObject, ((Component)val6).gameObject, __instance);
}
}
}
[BepInPlugin("Ginja.LegSphere", "Sphere Legs", "1.1.0")]
public class LegReplacer : BaseUnityPlugin
{
private readonly Harmony harmony = new Harmony("Ginja.LegSphere");
internal static LegReplacer instance;
internal static GameObject ReplacementLegR;
internal static GameObject ReplacementLegL;
private void Awake()
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"[Sphere Legs] Loading Sphere Legs mod...");
if ((Object)(object)instance == (Object)null)
{
instance = this;
harmony.PatchAll();
string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
string text = Path.Combine(directoryName, "legmodels");
AssetBundle val = AssetBundle.LoadFromFile(text);
if ((Object)(object)val == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogError((object)"[Sphere Legs] Failed to load asset bundle!");
return;
}
ReplacementLegR = val.LoadAsset<GameObject>("leg_R");
ReplacementLegL = val.LoadAsset<GameObject>("leg_L");
if ((Object)(object)ReplacementLegR == (Object)null || (Object)(object)ReplacementLegL == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogError((object)"[Sphere Legs] Failed to load replacement models!");
}
else
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"[Sphere Legs] Successfully loaded replacement models for leg_R and leg_L!");
}
}
else
{
Object.Destroy((Object)(object)((Component)this).gameObject);
}
}
}