Decompiled source of PickPocket Police MONO v1.0.1

Mods/PickPocketPoliceMONO.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using FishNet.Object;
using HarmonyLib;
using MelonLoader;
using PickPocketPoliceMONO;
using ScheduleOne.GameTime;
using ScheduleOne.ItemFramework;
using ScheduleOne.NPCs;
using ScheduleOne.PlayerScripts;
using ScheduleOne.Police;
using ScheduleOne.UI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(PickPocketCopsMONO), "PickPocketCops", "1.0.1", "SadPoty", null)]
[assembly: MelonColor]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("PickPocketPoliceMONO")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PickPocketPoliceMONO")]
[assembly: AssemblyTitle("PickPocketPoliceMONO")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PickPocketPoliceMONO;

public static class BuildInfo
{
	public const string Name = "PickPocketCops";

	public const string Description = "Make cops pickpockable";

	public const string Author = "SadPoty";

	public const string Company = null;

	public const string Version = "1.0.1";

	public const string DownloadLink = null;
}
public class PickPocketCopsMONO : MelonMod
{
	public override void OnSceneWasLoaded(int buildIndex, string sceneName)
	{
		if (sceneName == "Main")
		{
			EnablePickpocketOnOfficers();
		}
	}

	public static List<PoliceOfficer> GetAllOfficers()
	{
		PoliceOfficer[] array = Object.FindObjectsOfType<PoliceOfficer>();
		List<PoliceOfficer> list = new List<PoliceOfficer>(array.Length);
		PoliceOfficer[] array2 = array;
		foreach (PoliceOfficer item in array2)
		{
			list.Add(item);
		}
		return list;
	}

	private static void EnablePickpocketOnOfficers()
	{
		PoliceOfficer[] array = Object.FindObjectsOfType<PoliceOfficer>();
		List<PoliceOfficer> allOfficers = GetAllOfficers();
		PoliceOfficer[] array2 = array;
		foreach (PoliceOfficer item in array2)
		{
			allOfficers.Add(item);
		}
		PoliceOfficer[] array3 = array;
		foreach (PoliceOfficer val in array3)
		{
			if ((Object)(object)val != (Object)null && (Object)(object)((NPC)val).Inventory != (Object)null)
			{
				((NPC)val).Inventory.CanBePickpocketed = true;
				((NPC)val).Inventory.RandomCash = true;
				SetupCustomLootPool(val);
			}
		}
		DistributeRandomLootToOfficers(allOfficers);
		MelonLogger.Msg("PickPocketCops Loaded !");
	}

	private static void SetupCustomLootPool(PoliceOfficer officer)
	{
		ItemDefinition[] itemDefs = Resources.FindObjectsOfTypeAll<ItemDefinition>();
		List<StorableItemDefinition> lootList = new List<StorableItemDefinition>();
		StorableItemDefinition val = GetItem("Donut");
		if ((Object)(object)val != (Object)null)
		{
			AddItemMultiple(val, 6);
		}
		StorableItemDefinition val2 = GetItem("M1911");
		if ((Object)(object)val2 != (Object)null)
		{
			AddItemMultiple(val2, 1);
		}
		StorableItemDefinition val3 = GetItem("Revolver");
		if ((Object)(object)val3 != (Object)null)
		{
			AddItemMultiple(val3, 1);
		}
		StorableItemDefinition val4 = GetItem("RevolverCylinder");
		if ((Object)(object)val4 != (Object)null)
		{
			AddItemMultiple(val4, 3);
		}
		StorableItemDefinition val5 = GetItem("M1911_Magazine");
		if ((Object)(object)val5 != (Object)null)
		{
			AddItemMultiple(val5, 3);
		}
		StorableItemDefinition[] array = (StorableItemDefinition[])(object)new StorableItemDefinition[lootList.Count];
		for (int i = 0; i < lootList.Count; i++)
		{
			array[i] = lootList[i];
		}
		((NPC)officer).Inventory.RandomItemDefinitions = array;
		void AddItemMultiple(StorableItemDefinition item, int count)
		{
			for (int j = 0; j < count; j++)
			{
				lootList.Add(item);
			}
		}
		StorableItemDefinition GetItem(string name)
		{
			ItemDefinition val6 = ((IEnumerable<ItemDefinition>)itemDefs).FirstOrDefault((Func<ItemDefinition, bool>)((ItemDefinition d) => ((Object)d).name == name));
			return (StorableItemDefinition)(object)((val6 is StorableItemDefinition) ? val6 : null);
		}
	}

	public static void DistributeRandomLootToOfficers(List<PoliceOfficer> officers)
	{
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Expected O, but got Unknown
		if (officers == null || officers.Count == 0)
		{
			return;
		}
		int num = Mathf.Min(Random.Range(7, officers.Count + 1), officers.Count);
		List<int> list = new List<int>();
		while (list.Count < num)
		{
			int item = Random.Range(0, officers.Count);
			if (!list.Contains(item))
			{
				list.Add(item);
			}
		}
		foreach (int item2 in list)
		{
			PoliceOfficer val = officers[item2];
			StorableItemDefinition[] randomItemDefinitions = ((NPC)val).Inventory.RandomItemDefinitions;
			if (randomItemDefinitions == null || randomItemDefinitions.Length == 0)
			{
				continue;
			}
			StorableItemDefinition val2 = randomItemDefinitions[Random.Range(0, randomItemDefinitions.Length)];
			if (!((Object)(object)val2 == (Object)null))
			{
				int num2 = 1;
				int num3;
				switch (((Object)val2).name)
				{
				case "Revolver":
				case "RevolverCylinder":
					num3 = 6;
					break;
				case "M1911":
				case "M1911_Magazine":
					num3 = 7;
					break;
				default:
					num3 = 1;
					break;
				}
				IntegerItemInstance val3 = new IntegerItemInstance((ItemDefinition)(object)val2, num2, num3);
				((NPC)val).Inventory.InsertItem((ItemInstance)(object)val3, true);
			}
		}
	}
}
[HarmonyPatch(typeof(PickpocketScreen), "Update")]
public static class PickpocketScreenUpdatePatch
{
	private static bool PickFailed = false;

	private static Vector3 oldDestination = default(Vector3);

	private static void Postfix(PickpocketScreen __instance)
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Expected O, but got Unknown
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		//IL_016e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		FieldInfo fieldInfo = AccessTools.Field(typeof(PickpocketScreen), "npc");
		NPC val = (NPC)fieldInfo.GetValue(__instance);
		FieldInfo fieldInfo2 = AccessTools.Field(typeof(PickpocketScreen), "isFail");
		bool flag = (bool)fieldInfo2.GetValue(__instance);
		MethodInfo methodInfo = AccessTools.Method(typeof(PickpocketScreen), "OnDestroy", (Type[])null, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(PickpocketScreen), "Awake", (Type[])null, (Type[])null);
		if ((Object)(object)val == (Object)null || !((Object)val).name.StartsWith("Officer"))
		{
			return;
		}
		if (__instance.IsOpen && !PickFailed)
		{
			NPC val2 = val;
			val2.Movement.MoveSpeedMultiplier = 0f;
			oldDestination = val2.Movement.CurrentDestination;
			PickFailed = true;
		}
		else if (!__instance.IsOpen && PickFailed)
		{
			NPC val3 = val;
			val3.Movement.MoveSpeedMultiplier = 1f;
			if (oldDestination != Vector3.zero)
			{
				val3.Movement.SetDestination(oldDestination);
			}
			else
			{
				val3.Movement.SetDestination(((Component)val3).transform.position + new Vector3(1f, 0f, 1f));
			}
			PickFailed = false;
		}
		if (flag)
		{
			PlayerCrimeData val4 = Object.FindObjectOfType<PlayerCrimeData>();
			if ((Object)(object)val4 != (Object)null)
			{
				val4.SetPursuitLevel((EPursuitLevel)3);
				Player val5 = Object.FindObjectsOfType<Player>(true).FirstOrDefault();
				PoliceOfficer nearestOfficer = val4.NearestOfficer;
				nearestOfficer.BeginFootPursuit_Networked(((NetworkBehaviour)val5).NetworkObject, true);
			}
			fieldInfo2.SetValue(__instance, false);
			methodInfo.Invoke(__instance, null);
			methodInfo2.Invoke(__instance, null);
		}
	}
}
[HarmonyPatch(typeof(TimeManager), "MarkHostSleepDone")]
public static class RestockOfficerPatch
{
	private static void Postfix(TimeManager __instance)
	{
		List<PoliceOfficer> allOfficers = PickPocketCopsMONO.GetAllOfficers();
		PickPocketCopsMONO.DistributeRandomLootToOfficers(allOfficers);
	}
}