Decompiled source of DeathSoundMetalPipe v1.0.0

DeathSoundMetalPipe.dll

Decompiled 19 hours ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("DeathSoundMetalPipe")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DeathSoundMetalPipe")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("433e47bd-f13e-4c39-848a-4c6c98da5ce6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DeathSoundMetalPipe;

[BepInPlugin("Pan_kapkan.DeathSoundMetalPipe", "Death Sound Metal Pipe", "1.0.0")]
[BepInProcess("valheim.exe")]
public class DeathSoundPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Game), "Start")]
	private class Game_Start_Patch
	{
		private static void Postfix()
		{
			if (ZRoutedRpc.instance != null)
			{
				ZRoutedRpc.instance.Register("PanKapkan_DeathSoundMetalPipe", (Action<long>)RPC_DeathSound);
			}
		}
	}

	[HarmonyPatch(typeof(Player), "OnDeath")]
	private class Player_OnDeath_Patch
	{
		private static void Postfix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && ZRoutedRpc.instance != null)
			{
				ZRoutedRpc.instance.InvokeRoutedRPC(0L, "PanKapkan_DeathSoundMetalPipe", Array.Empty<object>());
			}
		}
	}

	public const string PluginGuid = "Pan_kapkan.DeathSoundMetalPipe";

	private const string RpcName = "PanKapkan_DeathSoundMetalPipe";

	private const string SoundFileName = "death_metal_pipe.wav";

	private const float PlayDuration = 5f;

	private const float MaxVolumeSetting = 20f;

	private readonly Harmony harmony = new Harmony("Pan_kapkan.DeathSoundMetalPipe");

	internal static DeathSoundPlugin Instance;

	private ConfigEntry<float> volumeSetting;

	private AudioClip deathClip;

	private AudioSource source;

	private Coroutine stopRoutine;

	private void Awake()
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		Instance = this;
		volumeSetting = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Volume", 15f, new ConfigDescription("Death sound volume (0 = silent, 20 = full volume of the file).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 20f), Array.Empty<object>()));
		source = ((Component)this).gameObject.AddComponent<AudioSource>();
		source.spatialBlend = 0f;
		source.playOnAwake = false;
		source.loop = false;
		((MonoBehaviour)this).StartCoroutine(LoadClip());
		harmony.PatchAll();
	}

	private void OnDestroy()
	{
		harmony.UnpatchSelf();
	}

	private IEnumerator LoadClip()
	{
		string path = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "death_metal_pipe.wav");
		if (!File.Exists(path))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Sound file not found: " + path));
			yield break;
		}
		UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(new Uri(path).AbsoluteUri, (AudioType)20);
		try
		{
			yield return req.SendWebRequest();
			if ((int)req.result != 1)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failed to load sound: " + req.error));
				yield break;
			}
			deathClip = DownloadHandlerAudioClip.GetContent(req);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Death sound loaded: death_metal_pipe.wav");
		}
		finally
		{
			((IDisposable)req)?.Dispose();
		}
	}

	internal void PlayDeathSound()
	{
		if (!((Object)(object)deathClip == (Object)null))
		{
			if (stopRoutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(stopRoutine);
			}
			source.Stop();
			source.clip = deathClip;
			source.volume = Mathf.Clamp01(volumeSetting.Value / 20f);
			source.Play();
			stopRoutine = ((MonoBehaviour)this).StartCoroutine(StopAfter(5f));
		}
	}

	private IEnumerator StopAfter(float seconds)
	{
		yield return (object)new WaitForSecondsRealtime(seconds);
		source.Stop();
		stopRoutine = null;
	}

	private static void RPC_DeathSound(long sender)
	{
		if ((Object)(object)Instance != (Object)null)
		{
			Instance.PlayDeathSound();
		}
	}
}