Decompiled source of CustomDropshipMusic v1.0.0

CustomDropshipMusic.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace CustomDropshipMusic;

[BepInPlugin("custom.dropshipmusic", "CustomDropshipMusic", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "custom.dropshipmusic";

	public const string NAME = "CustomDropshipMusic";

	public const string VERSION = "1.0.0";

	internal static ManualLogSource Log;

	internal static AudioClip CustomClip;

	private void Awake()
	{
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string text = FindAudioFile(directoryName);
		if (text == null)
		{
			Log.LogError((object)("Nie znaleziono pliku audio (.ogg/.wav/.mp3) w folderze: " + directoryName));
			return;
		}
		CustomClip = LoadClip(text);
		if ((Object)(object)CustomClip == (Object)null)
		{
			Log.LogError((object)("Nie udalo sie wczytac: " + text));
			return;
		}
		((Object)CustomClip).name = "CustomDropshipMusic";
		Log.LogInfo((object)("Wczytano muzyke dropshipa: " + Path.GetFileName(text) + " (" + CustomClip.length.ToString("0.0") + " s)"));
		new Harmony("custom.dropshipmusic").PatchAll(typeof(DropshipPatches));
	}

	private static string FindAudioFile(string folder)
	{
		string[] array = new string[3] { ".ogg", ".wav", ".mp3" };
		string[] array2 = array;
		foreach (string text in array2)
		{
			string text2 = Path.Combine(folder, "dropship" + text);
			if (File.Exists(text2))
			{
				return text2;
			}
		}
		string[] files = Directory.GetFiles(folder);
		foreach (string text3 in files)
		{
			string value = Path.GetExtension(text3).ToLowerInvariant();
			if (Array.IndexOf(array, value) >= 0)
			{
				return text3;
			}
		}
		return null;
	}

	private static AudioClip LoadClip(string path)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Invalid comparison between Unknown and I4
		AudioType val = (AudioType)14;
		string text = Path.GetExtension(path).ToLowerInvariant();
		if (text == ".wav")
		{
			val = (AudioType)20;
		}
		else if (text == ".mp3")
		{
			val = (AudioType)13;
		}
		string absoluteUri = new Uri(path).AbsoluteUri;
		UnityWebRequest audioClip = UnityWebRequestMultimedia.GetAudioClip(absoluteUri, val);
		try
		{
			((DownloadHandlerAudioClip)audioClip.downloadHandler).streamAudio = false;
			audioClip.SendWebRequest();
			while (!audioClip.isDone)
			{
			}
			if ((int)audioClip.result != 1)
			{
				Log.LogError((object)audioClip.error);
				return null;
			}
			return DownloadHandlerAudioClip.GetContent(audioClip);
		}
		finally
		{
			((IDisposable)audioClip)?.Dispose();
		}
	}
}
internal static class DropshipPatches
{
	private static readonly Dictionary<ItemDropship, AudioSource[]> Sources = new Dictionary<ItemDropship, AudioSource[]>();

	[HarmonyPostfix]
	[HarmonyPatch(typeof(ItemDropship), "Start")]
	private static void StartPostfix(ItemDropship __instance)
	{
		AudioSource[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<AudioSource>(true);
		List<AudioSource> list = new List<AudioSource>();
		AudioSource[] array = componentsInChildren;
		foreach (AudioSource val in array)
		{
			Plugin.Log.LogDebug((object)("Dropship AudioSource: " + ((Object)((Component)val).gameObject).name + " clip=" + (((Object)(object)val.clip != (Object)null) ? ((Object)val.clip).name : "null")));
			list.Add(val);
		}
		Sources[__instance] = list.ToArray();
		Swap(__instance);
	}

	[HarmonyPatch(typeof(ItemDropship), "Update")]
	[HarmonyPostfix]
	private static void UpdatePostfix(ItemDropship __instance)
	{
		Swap(__instance);
	}

	private static void Swap(ItemDropship ship)
	{
		if ((Object)(object)Plugin.CustomClip == (Object)null || !Sources.TryGetValue(ship, out var value))
		{
			return;
		}
		AudioSource[] array = value;
		foreach (AudioSource val in array)
		{
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val.clip == (Object)null) && !((Object)(object)val.clip == (Object)(object)Plugin.CustomClip) && ((Object)val.clip).name.StartsWith("IcecreamTruck", StringComparison.OrdinalIgnoreCase))
			{
				bool isPlaying = val.isPlaying;
				val.clip = Plugin.CustomClip;
				if (isPlaying)
				{
					val.Play();
				}
			}
		}
	}
}