Decompiled source of MoreCameras v1.0.0

MoreCameras.dll

Decompiled 7 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using EPOOutline;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using UnityEngine;
using Zorro.Core;
using Zorro.Recorder;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("MoreCameras")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MoreCameras")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("5d72c4f3-ff7f-4f12-8773-eb7534a584d1")]
[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 MoreCameras;

[HarmonyPatch(typeof(ExtractMachineCheckItemState))]
public class ExtractMachineItemPatch
{
	private static float m_timeBeenSearchingForItem;

	[HarmonyPatch("Enter")]
	[HarmonyPostfix]
	private static void EnterPatch()
	{
		m_timeBeenSearchingForItem = 0f;
	}

	[HarmonyPatch("Update")]
	[HarmonyPrefix]
	private static bool UpdatePatch(ExtractMachineCheckItemState __instance)
	{
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Invalid comparison between Unknown and I4
		if (((VideoExtractMachineState)__instance).Machine.Hatch.m_opened)
		{
			((VideoExtractMachineState)__instance).Machine.Hatch.Close();
		}
		if (!((VideoExtractMachineState)__instance).Machine.Hatch.IsFullyClosed())
		{
			return false;
		}
		m_timeBeenSearchingForItem += Time.deltaTime;
		if (m_timeBeenSearchingForItem > 0.3f && m_timeBeenSearchingForItem < 1.5f)
		{
			List<(Item, Pickup)> list = ((VideoExtractMachineState)__instance).Machine.Detector.CheckForItems();
			bool flag = true;
			foreach (var item in list)
			{
				if ((int)item.Item1.itemType > 0)
				{
					flag = false;
					break;
				}
			}
			if (list.Count > 0 && flag)
			{
				Debug.Log((object)"FOUND CAMERAS");
				((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineExtractingState>();
			}
			else
			{
				Debug.Log((object)"FOUND SOMETHING ELSE");
				((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			}
		}
		Debug.Log((object)"Hatch is fully closed");
		return false;
	}
}
[HarmonyPatch(typeof(ExtractMachineExtractingState))]
public class ExtractMachineExtractingPatch
{
	[HarmonyPatch("Enter")]
	[HarmonyPrefix]
	private static bool Enter(ExtractMachineExtractingState __instance)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Invalid comparison between Unknown and I4
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
		Base.Log("Ran Extract Machine Patch");
		if (((VideoExtractMachineState)__instance).Machine.m_hasCDInRom.IsSome)
		{
			Debug.LogError((object)"Already has CD in rom");
			((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			return false;
		}
		List<(Item, Pickup)> list = ((VideoExtractMachineState)__instance).Machine.Detector.CheckForItems();
		bool flag = true;
		foreach (var item2 in list)
		{
			if ((int)item2.Item1.itemType > 0)
			{
				flag = false;
				break;
			}
		}
		if (list.Count == 0 || !flag)
		{
			Debug.LogError((object)"Not only cameras in extract machine");
			((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			return false;
		}
		ItemInstanceData val = default(ItemInstanceData);
		VideoInfoEntry val2 = default(VideoInfoEntry);
		for (int i = 0; i < list.Count; i++)
		{
			Pickup item = list[i].Item2;
			if (!item.itemInstance.m_guid.IsSome)
			{
				Debug.LogError((object)"Camera Pickup has no guid");
				((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			}
			else if (!ItemInstanceDataHandler.TryGetInstanceData(item.itemInstance.m_guid.Value, ref val))
			{
				Debug.LogError((object)"No data found for camera");
				((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			}
			else if (!val.TryGetEntry<VideoInfoEntry>(ref val2))
			{
				Debug.LogError((object)"No VideoInfoEntry found for camera");
				((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			}
			else if (((VideoHandle)(ref val2.videoID)).Equals(VideoHandle.Invalid))
			{
				Debug.LogError((object)"VideoID is invalid");
				((StateMachine<VideoExtractMachineState>)(object)((VideoExtractMachineState)__instance).Machine.StateMachine).SwitchState<ExtractMachineEjectState>();
			}
			else
			{
				item.RPC_Remove();
				((VideoExtractMachineState)__instance).Machine.StartExtract(val2.videoID);
			}
		}
		return false;
	}
}
[HarmonyPatch(typeof(RecordingsHandler))]
public class RecordingsHandlerPatch
{
	[HarmonyPatch(typeof(ExtractVideoMachine))]
	public class ExtractVideoMachinePatch
	{
		public static ExtractVideoMachine machine;

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPatch(ExtractVideoMachine __instance)
		{
			((MonoBehaviour)__instance).StartCoroutine(WaitOneFrame(__instance));
		}

		private static IEnumerator WaitOneFrame(ExtractVideoMachine __instance)
		{
			yield return null;
			machine = __instance;
			Transform camConverter = ((Component)__instance).transform.GetChild(0);
			Transform button = ((Component)__instance).transform.GetChild(0).GetChild(0);
			((Component)button).gameObject.SetActive(true);
			if (!SurfaceNetworkHandler.ReturningFromLostWorld || SurfaceNetworkHandler.Instance.firstDay)
			{
				Base.LogError("Disabling button");
				yield break;
			}
			Pickup pickup = PhotonNetwork.InstantiateRoomObject("PickupHolder", Vector3.zero, Quaternion.identity, (byte)0, (object[])null).GetComponent<Pickup>();
			((Component)pickup).transform.parent = camConverter;
			button.parent = ((Component)pickup).transform;
			((Component)button).gameObject.layer = HelperFunctions.GetLayer((LayerType)4);
			((Component)((Component)pickup).transform).GetComponent<Outlinable>().TryAddTarget(new OutlineTarget(((Component)button).GetComponent<Renderer>(), 0));
			pickup.ConfigurePickup((byte)0, new ItemInstanceData(Guid.NewGuid()));
			((Interactable)pickup).hoverText = "Begin Extract";
			Base.LogError("Enabled button!");
		}
	}

	[HarmonyPatch(typeof(SurfaceNetworkHandler))]
	public class SurfaceNetworkHandlerPatch
	{
		[HarmonyPatch("CheckIfCameraIsPresent")]
		[HarmonyPrefix]
		private static bool CheckIfCameraIsPresentPatch(ref bool __result, SurfaceNetworkHandler __instance)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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)
			int num = 0;
			Player[] playerList = PhotonNetwork.PlayerList;
			Player[] array = playerList;
			GlobalPlayerData val2 = default(GlobalPlayerData);
			foreach (Player val in array)
			{
				if (GlobalPlayerData.TryGetPlayerData(val, ref val2))
				{
					foreach (ItemDescriptor item in val2.inventory.GetItems())
					{
						if (item.item.id == 1)
						{
							VerboseDebug.Log("Found a camera! " + val.NickName + " has one!");
							num++;
							__result = true;
						}
					}
				}
				else
				{
					Debug.LogError((object)("Cant find playerData for Player: " + val.NickName + " Bug!?"));
				}
			}
			foreach (Pickup item2 in Object.FindObjectOfType<DiveBellPickupDetector>().CheckForPickups())
			{
				if (item2.itemInstance.item.id == 1)
				{
					VerboseDebug.Log("Found Camera On Ground!");
					num++;
					__result = true;
				}
			}
			ShopHandlerPatch.cameras = num;
			if (num == 0)
			{
				__result = false;
			}
			return false;
		}
	}

	[HarmonyPatch(typeof(BigNumbers))]
	public class BigNumbersPatch
	{
		[HarmonyPatch("GetScoreToViews")]
		[HarmonyPrefix]
		private static bool GetScoreToViewsPatch(float score, int day, ref int __result)
		{
			int num = Mathf.FloorToInt(((float)day - 1f) / 3f);
			__result = Mathf.RoundToInt(score * SingletonAsset<BigNumbers>.Instance.scoreToViewConversionPerRun.Evaluate((float)num) * Convert(ShopHandlerPatch.cameras));
			return false;
		}

		private static float Convert(float num)
		{
			return 1.50464f * (1f / (num + 0.480554f)) - 0.0152392f;
		}
	}

	[HarmonyPatch(typeof(ExtractMachineIdleState))]
	public class ExtractMachineIdleStatePatch
	{
		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		private static bool UpdatePatch()
		{
			return false;
		}
	}

	[HarmonyPatch(typeof(Pickup))]
	public class PickupPatch
	{
		[HarmonyPatch("Interact")]
		[HarmonyPrefix]
		private static bool Interact(Interactable __instance)
		{
			Base.Log("Got Interact()");
			Base.Log(((Object)((Component)__instance).gameObject).name);
			if (((Object)((Component)__instance).transform.GetChild(0)).name == "Button")
			{
				((StateMachine<VideoExtractMachineState>)(object)ExtractVideoMachinePatch.machine.StateMachine).SwitchState<ExtractMachineCheckItemState>();
				Base.Log("Button pressed!");
				return false;
			}
			return true;
		}

		[HarmonyPatch("RPC_ConfigurePickup")]
		[HarmonyPostfix]
		private static void ConfigurePickupPatch(Rigidbody ___m_rigidbody)
		{
			if (((Object)((Component)((Component)___m_rigidbody).transform).gameObject).name == "Button")
			{
				___m_rigidbody.useGravity = false;
				___m_rigidbody.isKinematic = false;
			}
		}
	}

	[HarmonyPatch(typeof(ExtractVideoStationHatch))]
	public class ExtractVideoStationHatchPatch
	{
		[HarmonyPatch("HitByThrowable")]
		[HarmonyPrefix]
		private static bool HitByThrowablePatch()
		{
			return false;
		}
	}

	[HarmonyPatch(typeof(ShopHandler))]
	public class ShopHandlerPatch
	{
		public const byte cameraID = 1;

		public static int cameras = 1;

		private static int camerasInCart = 0;

		[HarmonyPatch("RPCA_AddItemToCart")]
		[HarmonyPrefix]
		private static bool RPCA_AddItemToCartPatch(byte itemID, TextMeshProUGUI ___m_ItemAddedToCartText, ShopHandler __instance)
		{
			if (itemID == 1 && cameras >= PhotonNetwork.PlayerList.Length)
			{
				Base.Log("Too many cameras!!");
				((MonoBehaviour)__instance).StartCoroutine(DisplayTooManyCameras());
				return false;
			}
			cameras++;
			camerasInCart++;
			return true;
			IEnumerator DisplayTooManyCameras()
			{
				((TMP_Text)___m_ItemAddedToCartText).text = "You have enough cameras!";
				((Component)___m_ItemAddedToCartText).gameObject.SetActive(true);
				yield return (object)new WaitForSecondsRealtime(2f);
				((Component)___m_ItemAddedToCartText).gameObject.SetActive(false);
				((TMP_Text)___m_ItemAddedToCartText).text = string.Empty;
			}
		}

		[HarmonyPatch("OnClearCartClicked")]
		[HarmonyPostfix]
		private static void OnClearCartClickedPatch()
		{
			cameras -= camerasInCart;
			camerasInCart = 0;
		}
	}

	private static List<CameraRecording> allRecordings = new List<CameraRecording>();

	private static bool startedTimer = false;

	[HarmonyPatch("ExtractRecording")]
	[HarmonyPrefix]
	private static bool ExtractRecordingPatch(RecordingsHandler __instance, CameraRecording recording, ref bool __result)
	{
		allRecordings.Add(recording);
		if (!startedTimer)
		{
			((MonoBehaviour)__instance).StartCoroutine(ExtractTimer());
			startedTimer = true;
		}
		__result = true;
		return false;
	}

	private static IEnumerator ExtractTimer()
	{
		yield return null;
		ExtractRecordings(allRecordings.ToArray());
		allRecordings.Clear();
	}

	private static bool ExtractRecordings(CameraRecording[] recordings)
	{
		bool flag = true;
		int num = 0;
		Clip[] array = (Clip[])(object)new Clip[100];
		foreach (CameraRecording val in recordings)
		{
			if (!val.ReadyToExtract)
			{
				flag = false;
				break;
			}
		}
		if (!flag)
		{
			Base.LogError("Not ready to extract all clips");
			return false;
		}
		foreach (CameraRecording val2 in recordings)
		{
			if (!val2.ReadyToExtract)
			{
				flag = false;
			}
			num += val2.ClipCount;
			array = array.Concat(from clip in val2.GetAllClips()
				where clip.Valid
				select clip).ToArray();
		}
		Clip[] source = OrderClips(array);
		Debug.Log((object)"All clips ready, starting stitching...");
		if (num == 1)
		{
			FileInfo fileInfo = new FileInfo(Path.Combine(recordings[0].GetClip(0).GetClipDirectory(), "output.webm"));
			if (!fileInfo.Exists)
			{
				Debug.LogError((object)("No file found to stitch: " + fileInfo.FullName));
				return false;
			}
			string text = Path.Combine(recordings[0].GetDirectory(), "fullRecording.webm");
			Debug.Log((object)("Copying file " + fileInfo.FullName + " to: " + text));
			File.Copy(fileInfo.FullName, text, overwrite: true);
		}
		else
		{
			FFmpegStitcher.StichVideos(source.Select((Clip clip) => Path.Combine(clip.GetClipDirectory(), "output.webm")).ToArray(), recordings[^1].GetDirectory());
		}
		return true;
	}

	private static Clip[] OrderClips(Clip[] source)
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		List<Clip> list = new List<Clip>();
		foreach (Clip val in source)
		{
			if (val == null || !val.Valid)
			{
				Base.Log("Null clip, discarding");
				continue;
			}
			Base.Log($"Got clip {val.clipID}");
			if (list.Count == 0)
			{
				list.Add(val);
				continue;
			}
			bool flag = false;
			for (int j = 0; j < list.Count; j++)
			{
				Clip val2 = list[j];
				if (val.m_timeStarted < val2.m_timeStarted)
				{
					list.Insert(j, val);
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				list.Add(val);
			}
		}
		Base.Log("Ordered clips: ");
		foreach (Clip item in list)
		{
			Base.Log(((object)(ClipID)(ref item.clipID)).ToString());
		}
		return list.ToArray();
	}
}
[BepInPlugin("AA24-MoreCameras", "MoreCameras", "1.0.0")]
public class Base : BaseUnityPlugin
{
	private const string modGUID = "AA24-MoreCameras";

	private const string modName = "MoreCameras";

	private const string modVersion = "1.0.0";

	private readonly Harmony harmony = new Harmony("AA24-MoreCameras");

	public static Base instance;

	internal ManualLogSource mls;

	private void Awake()
	{
		if ((Object)(object)instance == (Object)null)
		{
			instance = this;
		}
		mls = Logger.CreateLogSource("AA24-MoreCameras");
		mls.LogMessage((object)"Loaded More Cameras!!");
		harmony.PatchAll(typeof(Base));
		harmony.PatchAll(typeof(ExtractMachineItemPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.ExtractVideoMachinePatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.PickupPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.ExtractMachineIdleStatePatch));
		harmony.PatchAll(typeof(ExtractMachineExtractingPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.ExtractVideoStationHatchPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.ShopHandlerPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.SurfaceNetworkHandlerPatch));
		harmony.PatchAll(typeof(RecordingsHandlerPatch.BigNumbersPatch));
	}

	public static void Log(string msg)
	{
		instance.mls.LogInfo((object)msg);
	}

	public static void LogError(string msg)
	{
		instance.mls.LogError((object)msg);
	}
}