using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CustomBoomboxTracks.Configuration;
using CustomBoomboxTracks.Managers;
using CustomBoomboxTracks.Utilities;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CustomBoomboxMusicFixed")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CustomBoomboxMusicFixed")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("040e4eaf-8b77-41da-8aa0-35ff36e55b81")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CustomBoomboxTracks
{
[BepInPlugin("rafl.CustomBoomboxMusicFixed", "Custom Boombox Music (Fixed)", "1.0.2")]
public class BoomboxPlugin : BaseUnityPlugin
{
private const string GUID = "rafl.CustomBoomboxMusicFixed";
private const string NAME = "Custom Boombox Music (Fixed)";
private const string VERSION = "1.0.2";
private static BoomboxPlugin Instance;
private void Awake()
{
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
Instance = this;
LogInfo("Loading...");
AudioManager.GenerateFolders();
Config.Init();
new Harmony("rafl.CustomBoomboxMusicFixed").PatchAll();
LogInfo("Loading Complete! Yoinked by rafl :)");
}
internal static void LogDebug(string message)
{
Instance.Log(message, (LogLevel)32);
}
internal static void LogInfo(string message)
{
Instance.Log(message, (LogLevel)16);
}
internal static void LogWarning(string message)
{
Instance.Log(message, (LogLevel)4);
}
internal static void LogError(string message)
{
Instance.Log(message, (LogLevel)2);
}
internal static void LogError(Exception ex)
{
Instance.Log(ex.Message + "\n" + ex.StackTrace, (LogLevel)2);
}
private void Log(string message, LogLevel logLevel)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
((BaseUnityPlugin)this).Logger.Log(logLevel, (object)message);
}
}
}
namespace CustomBoomboxTracks.Utilities
{
public class SharedCoroutineStarter : MonoBehaviour
{
private static SharedCoroutineStarter _instance;
public static Coroutine StartCoroutine(IEnumerator routine)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)_instance == (Object)null)
{
_instance = new GameObject("Shared Coroutine Starter").AddComponent<SharedCoroutineStarter>();
Object.DontDestroyOnLoad((Object)(object)_instance);
}
return ((MonoBehaviour)_instance).StartCoroutine(routine);
}
}
}
namespace CustomBoomboxTracks.Patches
{
[HarmonyPatch(typeof(BoomboxItem), "PocketItem")]
internal class BoomboxItem_PocketItem
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> list = instructions.ToList();
bool flag = false;
for (int i = 0; i < list.Count; i++)
{
if (!flag)
{
if (list[i].opcode == OpCodes.Call)
{
flag = true;
}
continue;
}
if (list[i].opcode == OpCodes.Ret)
{
break;
}
list[i].opcode = OpCodes.Nop;
}
return list;
}
}
[HarmonyPatch(typeof(BoomboxItem), "Start")]
internal class BoomboxItem_Start
{
private static bool Prefix(BoomboxItem __instance)
{
if (AudioManager.FinishedLoading)
{
AudioManager.ApplyClips(__instance);
}
else
{
AudioManager.OnAllSongsLoaded += delegate
{
AudioManager.ApplyClips(__instance);
};
}
return true;
}
}
[HarmonyPatch(typeof(BoomboxItem), "StartMusic")]
internal class BoomboxItem_StartMusic
{
private static void Postfix(BoomboxItem __instance, bool startMusic)
{
if (startMusic)
{
BoomboxPlugin.LogInfo("Playing " + ((Object)__instance.boomboxAudio.clip).name);
}
}
}
[HarmonyPatch(typeof(StartOfRound), "Awake")]
internal class StartOfRound_Awake
{
private static void Prefix()
{
AudioManager.Load();
}
}
}
namespace CustomBoomboxTracks.Managers
{
internal static class AudioManager
{
private static string[] allSongPaths;
private static List<AudioClip> clips = new List<AudioClip>();
private static bool firstRun = true;
private static bool finishedLoading = false;
private static readonly string directory = Path.Combine(Paths.BepInExRootPath, "Custom Songs", "Boombox Music");
public static bool FinishedLoading => finishedLoading;
public static bool HasNoSongs => allSongPaths.Length == 0;
public static event Action OnAllSongsLoaded;
public static void GenerateFolders()
{
Directory.CreateDirectory(directory);
}
public static void Load()
{
if (!firstRun)
{
return;
}
firstRun = false;
allSongPaths = Directory.GetFiles(directory);
if (allSongPaths.Length == 0)
{
BoomboxPlugin.LogWarning("No songs found!");
return;
}
BoomboxPlugin.LogInfo("Preparing to load AudioClips...");
List<Coroutine> list = new List<Coroutine>();
string[] array = allSongPaths;
for (int i = 0; i < array.Length; i++)
{
Coroutine item = SharedCoroutineStarter.StartCoroutine(LoadAudioClip(array[i]));
list.Add(item);
}
SharedCoroutineStarter.StartCoroutine(WaitForAllClips(list));
}
private static IEnumerator LoadAudioClip(string filePath)
{
BoomboxPlugin.LogInfo("Loading " + filePath + "!");
if ((int)GetAudioType(filePath) == 0)
{
BoomboxPlugin.LogError("Failed to load AudioClip from " + filePath + "\nUnsupported file extension!");
yield break;
}
UnityWebRequest loader = UnityWebRequestMultimedia.GetAudioClip(filePath, GetAudioType(filePath));
if (Config.StreamFromDisk)
{
DownloadHandler downloadHandler = loader.downloadHandler;
((DownloadHandlerAudioClip)((downloadHandler is DownloadHandlerAudioClip) ? downloadHandler : null)).streamAudio = true;
}
loader.SendWebRequest();
while (!loader.isDone)
{
yield return null;
}
if (loader.error != null)
{
BoomboxPlugin.LogError("Error loading clip from path: " + filePath + "\n" + loader.error);
BoomboxPlugin.LogError(loader.error);
yield break;
}
AudioClip content = DownloadHandlerAudioClip.GetContent(loader);
if (Object.op_Implicit((Object)(object)content) && (int)content.loadState == 2)
{
BoomboxPlugin.LogInfo("Loaded " + filePath);
((Object)content).name = Path.GetFileName(filePath);
clips.Add(content);
}
else
{
BoomboxPlugin.LogError("Failed to load clip at: " + filePath + "\nThis might be due to an mismatch between the audio codec and the file extension!");
}
}
private static IEnumerator WaitForAllClips(List<Coroutine> coroutines)
{
foreach (Coroutine coroutine in coroutines)
{
yield return coroutine;
}
finishedLoading = true;
AudioManager.OnAllSongsLoaded?.Invoke();
AudioManager.OnAllSongsLoaded = null;
}
public static void ApplyClips(BoomboxItem __instance)
{
BoomboxPlugin.LogInfo("Applying clips!");
AudioClip[] array = clips.ToArray();
Array.Sort(array, (AudioClip clip1, AudioClip clip2) => ((Object)clip1).name.CompareTo(((Object)clip2).name));
if (Config.UseDefaultSongs)
{
__instance.musicAudios = __instance.musicAudios.Concat(clips).ToArray();
}
else
{
__instance.musicAudios = array;
}
__instance.musicRandomizer = new Random(12345);
BoomboxPlugin.LogInfo($"Total Clip Count: {__instance.musicAudios.Length}");
int num = __instance.musicAudios.Length;
BoomboxPlugin.LogError($"len: {num}");
__instance.musicRandomizer = new Random(12321);
}
private static AudioType GetAudioType(string path)
{
string text = Path.GetExtension(path).ToLower();
switch (text)
{
case ".wav":
return (AudioType)20;
case ".ogg":
return (AudioType)14;
case ".mp3":
return (AudioType)13;
default:
BoomboxPlugin.LogError("Unsupported extension type: " + text);
return (AudioType)0;
}
}
}
}
namespace CustomBoomboxTracks.Configuration
{
internal static class Config
{
private const string CONFIG_FILE_NAME = "custom-boombox-music-fixed.cfg";
private static ConfigFile _config;
private static ConfigEntry<bool> _useDefaultSongs;
private static ConfigEntry<bool> _streamAudioFromDisk;
public static bool UseDefaultSongs
{
get
{
if (!_useDefaultSongs.Value)
{
return AudioManager.HasNoSongs;
}
return true;
}
}
public static bool StreamFromDisk => _streamAudioFromDisk.Value;
public static void Init()
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Expected O, but got Unknown
BoomboxPlugin.LogInfo("Initializing config...");
_config = new ConfigFile(Path.Combine(Paths.ConfigPath, "custom-boombox-music-fixed.cfg"), true);
_useDefaultSongs = _config.Bind<bool>("Config", "Use Default Songs", false, "Include the default songs in the rotation.");
_streamAudioFromDisk = _config.Bind<bool>("Config", "Stream Audio From Disk", false, "Requires less memory and takes less time to load, but prevents playing the same song twice at once.");
BoomboxPlugin.LogInfo("Config initialized!");
}
private static void PrintConfig()
{
BoomboxPlugin.LogInfo($"Use Default Songs: {_useDefaultSongs.Value}");
BoomboxPlugin.LogInfo($"Stream From Disk: {_streamAudioFromDisk}");
}
}
}