Decompiled source of SigmaMaleTV v1.0.0

TVLoader.dll

Decompiled 11 months ago
using System.Collections.Generic;
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.Logging;
using HarmonyLib;
using TVLoader.Utils;
using UnityEngine.Video;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("TVLoader")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TVLoader")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e59845a7-f2f7-4416-9a61-ca1939ce6e2d")]
[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 TVLoader
{
	[BepInPlugin("rattenbonkers.TVLoader", "TVLoader", "1.0.3")]
	public class TVLoaderPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "rattenbonkers.TVLoader";

		private const string PluginName = "TVLoader";

		private const string VersionString = "1.0.3";

		private static readonly Harmony Harmony = new Harmony("rattenbonkers.TVLoader");

		public static ManualLogSource Log = new ManualLogSource("TVLoader");

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.PatchAll();
			VideoManager.Load();
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("PluginName: {0}, VersionString: {1} is loaded. Video Count: ${2}", "TVLoader", "1.0.3", VideoManager.Videos.Count));
		}
	}
}
namespace TVLoader.Utils
{
	internal static class VideoManager
	{
		public static List<string> Videos = new List<string>();

		public static void Load()
		{
			string[] directories = Directory.GetDirectories(Paths.PluginPath);
			foreach (string text in directories)
			{
				string path = Path.Combine(Paths.PluginPath, text, "Television Videos");
				if (Directory.Exists(path))
				{
					string[] files = Directory.GetFiles(path, "*.mp4");
					Videos.AddRange(files);
					TVLoaderPlugin.Log.LogInfo((object)$"{text} has {files.Length} videos.");
				}
			}
			string path2 = Path.Combine(Paths.PluginPath, "Television Videos");
			if (!Directory.Exists(path2))
			{
				Directory.CreateDirectory(path2);
			}
			string[] files2 = Directory.GetFiles(path2, "*.mp4");
			Videos.AddRange(files2);
			TVLoaderPlugin.Log.LogInfo((object)$"Global has {files2.Length} videos.");
			TVLoaderPlugin.Log.LogInfo((object)$"Loaded {Videos.Count} total.");
		}
	}
}
namespace TVLoader.Patches
{
	[HarmonyPatch(typeof(TVScript))]
	internal class TVScriptPatches
	{
		private static FieldInfo currentClipProperty = typeof(TVScript).GetField("currentClip", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo currentTimeProperty = typeof(TVScript).GetField("currentClipTime", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo wasLastFrameProp = typeof(TVScript).GetField("wasTvOnLastFrame", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo timeSinceTurningOffTVProp = typeof(TVScript).GetField("timeSinceTurningOffTV", BindingFlags.Instance | BindingFlags.NonPublic);

		private static MethodInfo setMatProperty = typeof(TVScript).GetMethod("SetTVScreenMaterial", BindingFlags.Instance | BindingFlags.NonPublic);

		[HarmonyPatch(typeof(TVScript), "Update")]
		[HarmonyPrefix]
		public static bool Update(TVScript __instance)
		{
			return false;
		}

		[HarmonyPatch(typeof(TVScript), "TurnTVOnOff")]
		[HarmonyPrefix]
		public static bool TurnTVOnOff(TVScript __instance, bool on)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Invalid comparison between Unknown and I4
			if (VideoManager.Videos.Count == 0)
			{
				return false;
			}
			if ((int)__instance.video.source != 1)
			{
				__instance.video.clip = null;
				__instance.tvSFX.clip = null;
			}
			__instance.tvOn = on;
			if (on)
			{
				PlayNextVideo(__instance);
				__instance.tvSFX.PlayOneShot(__instance.switchTVOn);
				WalkieTalkie.TransmitOneShotAudio(__instance.tvSFX, __instance.switchTVOn, 1f);
			}
			else
			{
				__instance.video.Stop();
				__instance.tvSFX.PlayOneShot(__instance.switchTVOff);
				WalkieTalkie.TransmitOneShotAudio(__instance.tvSFX, __instance.switchTVOff, 1f);
			}
			setMatProperty.Invoke(__instance, new object[1] { on });
			return false;
		}

		[HarmonyPatch(typeof(TVScript), "TVFinishedClip")]
		[HarmonyPrefix]
		public static bool TVFinishedClip(TVScript __instance, VideoPlayer source)
		{
			PlayNextVideo(__instance);
			return false;
		}

		private static void PlayNextVideo(TVScript instance)
		{
			if (VideoManager.Videos.Count != 0)
			{
				TVLoaderPlugin.Log.LogInfo((object)"Playing next video...");
				int num = (int)currentClipProperty.GetValue(instance);
				num = (num + 1) % VideoManager.Videos.Count;
				TVLoaderPlugin.Log.LogInfo((object)$"currentClip: {num} - {VideoManager.Videos[num]}");
				currentTimeProperty.SetValue(instance, 0f);
				currentClipProperty.SetValue(instance, num);
				instance.tvSFX.time = 0f;
				instance.video.url = "file://" + VideoManager.Videos[num];
				instance.video.source = (VideoSource)1;
				instance.video.controlledAudioTrackCount = 1;
				instance.video.audioOutputMode = (VideoAudioOutputMode)1;
				instance.video.SetTargetAudioSource((ushort)0, instance.tvSFX);
				instance.video.Play();
				instance.SyncTVServerRpc();
			}
		}
	}
}