Decompiled source of IndianRingtone v0.2.0

plugins/SwyfIndianRingtone.dll

Decompiled 7 hours ago
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace SwyfIndianRingtone;

[BepInPlugin("be.mahyass.swyf.indianringtone", "Indian Ringtone", "0.2.0")]
public sealed class Plugin : BaseUnityPlugin
{
	private static ManualLogSource log;

	private static Object clip;

	private static Type clipType;

	private static Type sourceType;

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		log = ((BaseUnityPlugin)this).Logger;
		Harmony harmony = new Harmony("be.mahyass.swyf.indianringtone");
		Hook(harmony, "ScammerWorkspaceAudio, Assembly-CSharp", "SetPhoneRinging", "BeforeDeskRing", "AfterDeskRing");
		Hook(harmony, "MonitorInteractable, ScriptsAssDef", "PlayPhoneRing", "BeforeMonitorRing", null);
	}

	private static void Hook(Harmony harmony, string typeName, string method, string prefix, string postfix)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Type type = Type.GetType(typeName, throwOnError: true);
			harmony.Patch((MethodBase)AccessTools.Method(type, method, (Type[])null, (Type[])null), (prefix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(typeof(Plugin), prefix, (Type[])null), (postfix == null) ? ((HarmonyMethod)null) : new HarmonyMethod(typeof(Plugin), postfix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			log.LogInfo((object)("Hooked " + method));
		}
		catch (Exception ex)
		{
			log.LogError((object)("Could not hook " + method + " (game updated?): " + ex));
		}
	}

	private static Object Clip()
	{
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Expected O, but got Unknown
		if (clip != (Object)null)
		{
			return clip;
		}
		clipType = Type.GetType("UnityEngine.AudioClip, UnityEngine.AudioModule", throwOnError: true);
		sourceType = Type.GetType("UnityEngine.AudioSource, UnityEngine.AudioModule", throwOnError: true);
		byte[] array;
		using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ringtone.pcm"))
		{
			using MemoryStream memoryStream = new MemoryStream();
			stream.CopyTo(memoryStream);
			array = memoryStream.ToArray();
		}
		float[] array2 = new float[array.Length / 2];
		for (int i = 0; i < array2.Length; i++)
		{
			array2[i] = (float)(short)(array[i * 2] | (array[i * 2 + 1] << 8)) / 32768f;
		}
		MethodInfo method = clipType.GetMethod("Create", new Type[5]
		{
			typeof(string),
			typeof(int),
			typeof(int),
			typeof(int),
			typeof(bool)
		});
		clip = (Object)method.Invoke(null, new object[5]
		{
			"IndianRingtone",
			array2.Length / 2,
			2,
			44100,
			false
		});
		clipType.GetMethod("SetData", new Type[2]
		{
			typeof(float[]),
			typeof(int)
		}).Invoke(clip, new object[2] { array2, 0 });
		clip.hideFlags = (HideFlags)61;
		return clip;
	}

	private static void BeforeDeskRing(object __instance)
	{
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Expected O, but got Unknown
		try
		{
			Object val = Clip();
			GameObject[] array = (GameObject[])AccessTools.Field(__instance.GetType(), "phoneRingAudio").GetValue(__instance);
			if (array == null)
			{
				return;
			}
			GameObject[] array2 = array;
			foreach (GameObject val2 in array2)
			{
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				Component[] componentsInChildren = val2.GetComponentsInChildren(sourceType, true);
				foreach (Component obj in componentsInChildren)
				{
					PropertyInfo property = sourceType.GetProperty("clip");
					if ((Object)property.GetValue(obj, null) != val)
					{
						property.SetValue(obj, val, null);
					}
					sourceType.GetProperty("loop").SetValue(obj, true, null);
					sourceType.GetProperty("volume").SetValue(obj, 1f, null);
				}
			}
		}
		catch (Exception ex)
		{
			log.LogError((object)ex);
		}
	}

	private static void AfterDeskRing(object __instance, bool __0)
	{
		if (!__0 || sourceType == null)
		{
			return;
		}
		try
		{
			GameObject[] array = (GameObject[])AccessTools.Field(__instance.GetType(), "phoneRingAudio").GetValue(__instance);
			if (array == null)
			{
				return;
			}
			GameObject[] array2 = array;
			foreach (GameObject val in array2)
			{
				if ((Object)(object)val == (Object)null || !val.activeInHierarchy)
				{
					continue;
				}
				Component[] componentsInChildren = val.GetComponentsInChildren(sourceType, false);
				foreach (Component obj in componentsInChildren)
				{
					if (!(bool)sourceType.GetProperty("isPlaying").GetValue(obj, null))
					{
						sourceType.GetMethod("Play", Type.EmptyTypes).Invoke(obj, null);
					}
				}
			}
		}
		catch (Exception ex)
		{
			log.LogError((object)ex);
		}
	}

	private static void BeforeMonitorRing(object __instance)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Expected O, but got Unknown
		try
		{
			FieldInfo fieldInfo = AccessTools.Field(__instance.GetType(), "phoneRingClip");
			if ((Object)fieldInfo.GetValue(__instance) != (Object)null)
			{
				fieldInfo.SetValue(__instance, Clip());
			}
			AccessTools.Field(__instance.GetType(), "phoneRingVolume").SetValue(__instance, 1f);
		}
		catch (Exception ex)
		{
			log.LogError((object)ex);
		}
	}
}