Decompiled source of FranticGrubs v0.9.1

FranticGrubs.dll

Decompiled an hour ago
using System;
using System.Collections;
using System.Collections.Generic;
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 Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("FranticGrubs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FranticGrubs")]
[assembly: AssemblyTitle("FranticGrubs")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace FranticGrubs
{
	[BepInPlugin("frantic.grubretexture", "Frantic Grub Retexture", "1.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource Log;

		public static string PluginDir;

		public static readonly Dictionary<string, byte[]> TexReplacements = new Dictionary<string, byte[]>();

		public static readonly Dictionary<string, AudioClip> AudioReplacements = new Dictionary<string, AudioClip>();

		private void Awake()
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			PluginDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			LoadTextures(Path.Combine(PluginDir, "Textures"));
			LoadTextures(PluginDir);
			Log.LogInfo((object)("Loaded " + TexReplacements.Count + " replacement texture(s)."));
			new Harmony("frantic.grubretexture").PatchAll();
			GameObject val = new GameObject("FranticGrubs Worker");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<GrubWorker>();
			Log.LogInfo((object)"FranticGrubs 1.2.0 (standalone) worker started.");
		}

		private void LoadTextures(string dir)
		{
			if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
			{
				return;
			}
			string[] files = Directory.GetFiles(dir, "*.png");
			foreach (string text in files)
			{
				string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
				if (!TexReplacements.ContainsKey(fileNameWithoutExtension))
				{
					try
					{
						TexReplacements[fileNameWithoutExtension] = File.ReadAllBytes(text);
					}
					catch (Exception ex)
					{
						Log.LogWarning((object)("Could not read " + text + ": " + ex.Message));
					}
				}
			}
		}
	}
	public class GrubWorker : MonoBehaviour
	{
		private readonly HashSet<int> _handled = new HashSet<int>();

		private float _timer;

		private void Awake()
		{
			((MonoBehaviour)this).StartCoroutine(LoadSounds(new string[2]
			{
				Path.Combine(Plugin.PluginDir, "Sounds"),
				Plugin.PluginDir
			}));
		}

		private IEnumerator LoadSounds(string[] dirs)
		{
			foreach (string text in dirs)
			{
				if (string.IsNullOrEmpty(text) || !Directory.Exists(text))
				{
					continue;
				}
				string[] files = Directory.GetFiles(text);
				foreach (string text2 in files)
				{
					AudioType val = (AudioType)(Path.GetExtension(text2).ToLowerInvariant() switch
					{
						".mp3" => 13, 
						".ogg" => 14, 
						".wav" => 20, 
						_ => 0, 
					});
					if ((int)val == 0)
					{
						continue;
					}
					string name = Path.GetFileNameWithoutExtension(text2);
					if (Plugin.AudioReplacements.ContainsKey(name))
					{
						continue;
					}
					string absoluteUri = new Uri(text2).AbsoluteUri;
					UnityWebRequest req = UnityWebRequestMultimedia.GetAudioClip(absoluteUri, val);
					try
					{
						yield return req.SendWebRequest();
						if ((int)req.result != 1)
						{
							Plugin.Log.LogWarning((object)("Failed to load sound " + name + ": " + req.error));
							continue;
						}
						AudioClip content = DownloadHandlerAudioClip.GetContent(req);
						if ((Object)(object)content == (Object)null)
						{
							Plugin.Log.LogWarning((object)("Null clip for " + name));
							continue;
						}
						((Object)content).name = name;
						Plugin.AudioReplacements[name] = content;
						Plugin.Log.LogInfo((object)("Loaded sound: " + name));
					}
					finally
					{
						((IDisposable)req)?.Dispose();
					}
				}
			}
			Plugin.Log.LogInfo((object)("Loaded " + Plugin.AudioReplacements.Count + " replacement sound(s)."));
		}

		private void Update()
		{
			if (Plugin.TexReplacements.Count == 0)
			{
				return;
			}
			_timer += Time.unscaledDeltaTime;
			if (_timer < 1f)
			{
				return;
			}
			_timer = 0f;
			Texture2D[] array = Resources.FindObjectsOfTypeAll<Texture2D>();
			foreach (Texture2D val in array)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				int instanceID = ((Object)val).GetInstanceID();
				if (_handled.Contains(instanceID))
				{
					continue;
				}
				string name = ((Object)val).name;
				if (!string.IsNullOrEmpty(name) && Plugin.TexReplacements.TryGetValue(name, out var value))
				{
					try
					{
						ImageConversion.LoadImage(val, value);
						_handled.Add(instanceID);
						Plugin.Log.LogInfo((object)("Replaced texture: " + name));
					}
					catch (Exception ex)
					{
						Plugin.Log.LogWarning((object)("Failed to replace " + name + ": " + ex.Message));
						_handled.Add(instanceID);
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(AudioSource), "PlayOneShot", new Type[]
	{
		typeof(AudioClip),
		typeof(float)
	})]
	public static class PlayOneShotVol_Patch
	{
		private static void Prefix(ref AudioClip clip)
		{
			if ((Object)(object)clip != (Object)null && Plugin.AudioReplacements.TryGetValue(((Object)clip).name, out var value) && (Object)(object)value != (Object)null)
			{
				clip = value;
			}
		}
	}
	[HarmonyPatch(typeof(AudioSource), "PlayOneShot", new Type[] { typeof(AudioClip) })]
	public static class PlayOneShot_Patch
	{
		private static void Prefix(ref AudioClip clip)
		{
			if ((Object)(object)clip != (Object)null && Plugin.AudioReplacements.TryGetValue(((Object)clip).name, out var value) && (Object)(object)value != (Object)null)
			{
				clip = value;
			}
		}
	}
	[HarmonyPatch(typeof(AudioSource), "Play", new Type[] { })]
	public static class Play_Patch
	{
		private static void Prefix(AudioSource __instance)
		{
			AudioClip clip = __instance.clip;
			if ((Object)(object)clip != (Object)null && Plugin.AudioReplacements.TryGetValue(((Object)clip).name, out var value) && (Object)(object)value != (Object)null)
			{
				__instance.clip = value;
			}
		}
	}
}