using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("BugleReplace", "BugleReplace", "1.1.0")]
public class BugleReplace : BaseUnityPlugin
{
private static AudioClip customBugle;
private static BugleReplace instance;
private void Awake()
{
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0023: Expected O, but got Unknown
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: Expected O, but got Unknown
//IL_0097: Unknown result type (might be due to invalid IL or missing references)
//IL_00a4: Expected O, but got Unknown
instance = this;
((BaseUnityPlugin)this).Logger.LogInfo((object)"[BugleReplace] Awake called.");
Harmony val = new Harmony("BugleReplace");
MethodInfo methodInfo = AccessTools.Method(typeof(BugleSFX), "Start", (Type[])null, (Type[])null);
MethodInfo method = typeof(BugleReplace).GetMethod("ReplaceBugleClips", BindingFlags.Static | BindingFlags.NonPublic);
val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(method), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
MethodInfo methodInfo2 = AccessTools.Method(typeof(BugleSFX), "Update", (Type[])null, (Type[])null);
MethodInfo method2 = typeof(BugleReplace).GetMethod("ForceBugleSound", BindingFlags.Static | BindingFlags.NonPublic);
val.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
((BaseUnityPlugin)this).Logger.LogInfo((object)"[BugleReplace] Patches applied.");
LoadBugle();
}
private void LoadBugle()
{
//IL_007d: Unknown result type (might be due to invalid IL or missing references)
//IL_0083: Expected O, but got Unknown
string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
((BaseUnityPlugin)this).Logger.LogInfo((object)$"[BugleReplace] Looking for .ogg in: {directoryName}");
string[] files = Directory.GetFiles(directoryName, "*.ogg");
if (files.Length == 0)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"[BugleReplace] No .ogg file found.");
return;
}
string text = files[0];
((BaseUnityPlugin)this).Logger.LogInfo((object)$"[BugleReplace] Found .ogg: {text}");
WWW www = new WWW("file://" + text);
((MonoBehaviour)this).StartCoroutine(WaitLoad(www, text));
}
private IEnumerator WaitLoad(WWW www, string fileName)
{
yield return www;
if (www.error == null)
{
customBugle = www.GetAudioClip(false, false, (AudioType)14);
((BaseUnityPlugin)this).Logger.LogInfo((object)$"[BugleReplace] Loaded bugle: {fileName}");
}
else
{
((BaseUnityPlugin)this).Logger.LogError((object)("[BugleReplace] Failed to load .ogg: " + www.error));
}
}
private static void ReplaceBugleClips(BugleSFX __instance, ref AudioClip[] ___bugle)
{
if ((Object)(object)customBugle != (Object)null)
{
___bugle = (AudioClip[])(object)new AudioClip[1] { customBugle };
((BaseUnityPlugin)instance).Logger.LogInfo((object)"[BugleReplace] Replaced bugle list.");
}
}
private static void ForceBugleSound(BugleSFX __instance)
{
if (!((Object)(object)customBugle == (Object)null))
{
bool value = Traverse.Create((object)__instance).Field("hold").GetValue<bool>();
AudioSource value2 = Traverse.Create((object)__instance).Field("buglePlayer").GetValue<AudioSource>();
if (value && (Object)(object)value2 != (Object)null && (Object)(object)value2.clip != (Object)(object)customBugle)
{
value2.clip = customBugle;
value2.Play();
((BaseUnityPlugin)instance).Logger.LogInfo((object)"[BugleReplace] Forced custom bugle playback.");
}
}
}
}