Decompiled source of Thunderstruck lightning v1.3.1

ThunderstruckLightning.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
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: AssemblyVersion("0.0.0.0")]
[BepInPlugin("com.glowers.thunderstrucklightning", "Thunderstruck Lightning", "1.3.1")]
public class ThunderstruckLightning : BaseUnityPlugin
{
	public static ThunderstruckLightning Instance;

	public static ManualLogSource Log;

	[HideInInspector]
	public AudioSource musicSource;

	[HideInInspector]
	public AudioClip thunderClip;

	private void Awake()
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		SetupAudioSource();
		PreloadAudio();
		try
		{
			Harmony val = new Harmony("com.glowers.thunderstrucklightning");
			val.PatchAll();
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Harmony patching failed: " + ex));
		}
		Log.LogInfo((object)"Thunderstruck Lightning loaded successfully");
	}

	private void SetupAudioSource()
	{
		musicSource = ((Component)this).gameObject.AddComponent<AudioSource>();
		musicSource.spatialize = false;
		musicSource.spatializePostEffects = false;
		musicSource.spatialBlend = 0f;
		musicSource.volume = 1f;
	}

	private void PreloadAudio()
	{
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string text = Path.Combine(directoryName, "Thunder.ogg");
		Log.LogInfo((object)("Preloading audio from: " + text));
		if (!File.Exists(text))
		{
			Log.LogError((object)"Thunder.ogg not found in mod folder");
			return;
		}
		string text2 = "file:///" + text.Replace("\\", "/");
		WWW val = new WWW(text2);
		while (!val.isDone)
		{
		}
		if (!string.IsNullOrEmpty(val.error))
		{
			Log.LogError((object)("Audio load failed: " + val.error));
			return;
		}
		thunderClip = val.GetAudioClip(false, false, (AudioType)14);
		Log.LogInfo((object)"Thunder.ogg preloaded successfully");
	}

	public void PlayThunderSound()
	{
		if ((Object)(object)thunderClip != (Object)null && (Object)(object)musicSource != (Object)null)
		{
			musicSource.PlayOneShot(thunderClip, 1f);
			Log.LogInfo((object)"Thunder sound played");
		}
		else
		{
			Log.LogWarning((object)"Thunder clip or AudioSource missing");
		}
	}
}
[HarmonyPatch]
internal class MetalTargetPatch
{
	private static Type TargetType()
	{
		Type type = AccessTools.TypeByName("StormyWeather+TargetingMetalObject");
		if (type == null)
		{
			ThunderstruckLightning.Log.LogWarning((object)"StormyWeather+TargetingMetalObject not found. Skipping patch.");
		}
		return type;
	}

	private static MethodBase TargetMethod()
	{
		Type type = TargetType();
		if (type == null)
		{
			return null;
		}
		MethodInfo methodInfo = AccessTools.Method(type, "OnMetalTargeted", (Type[])null, (Type[])null);
		if (methodInfo == null)
		{
			ThunderstruckLightning.Log.LogWarning((object)"OnMetalTargeted method not found. Patch skipped.");
		}
		return methodInfo;
	}

	private static bool Prefix()
	{
		if ((Object)(object)ThunderstruckLightning.Instance != (Object)null)
		{
			ThunderstruckLightning.Instance.PlayThunderSound();
		}
		return false;
	}
}