Decompiled source of BotanistFixIl2cpp v1.2.0

BotanistFix_Il2CPP.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BotanistFix;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppScheduleOne.DevUtilities;
using Il2CppScheduleOne.Employees;
using Il2CppScheduleOne.Management;
using Il2CppScheduleOne.NPCs;
using MelonLoader;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Core), "BotanistFix", "1.2.0", "Bars", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BotanistFix_Il2CPP")]
[assembly: AssemblyConfiguration("Il2cppRelease")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BotanistFix_Il2CPP")]
[assembly: AssemblyTitle("BotanistFix_Il2CPP")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 BotanistFix
{
	public class Core : MelonMod
	{
		[HarmonyPatch(typeof(NavMeshUtility), "GetAccessPoint")]
		private static class NavMeshUtilityGetAccessPointPatch
		{
			private struct CacheKey
			{
				public int NpcId;

				public int EntityId;

				public override bool Equals(object obj)
				{
					if (!(obj is CacheKey cacheKey))
					{
						return false;
					}
					return NpcId == cacheKey.NpcId && EntityId == cacheKey.EntityId;
				}

				public override int GetHashCode()
				{
					return NpcId.GetHashCode() ^ EntityId.GetHashCode();
				}
			}

			private class CacheEntry
			{
				public Transform BestAccessPoint;

				public bool Reachable;

				public Vector3 NpcPosition;

				public float Timestamp;
			}

			private static readonly Dictionary<CacheKey, CacheEntry> _cache = new Dictionary<CacheKey, CacheEntry>();

			private const float CACHE_TTL = 2.75f;

			private const float REVALIDATE_DISTANCE_SQR = 1.5f;

			private static bool Prefix(ITransitEntity entity, NPC npc, ref Transform __result)
			{
				//IL_006b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0071: Unknown result type (might be due to invalid IL or missing references)
				//IL_0076: Unknown result type (might be due to invalid IL or missing references)
				//IL_007b: Unknown result type (might be due to invalid IL or missing references)
				if (entity == null)
				{
					__result = null;
					return false;
				}
				CacheKey cacheKey = default(CacheKey);
				cacheKey.NpcId = ((Object)npc).GetInstanceID();
				cacheKey.EntityId = ((Object)((Il2CppObjectBase)entity).Cast<Component>()).GetInstanceID();
				CacheKey key = cacheKey;
				if (_cache.TryGetValue(key, out var value) && Time.time - value.Timestamp < 2.75f)
				{
					Vector3 val = ((Component)npc).transform.position - value.NpcPosition;
					if (((Vector3)(ref val)).sqrMagnitude < 1.5f)
					{
						__result = (value.Reachable ? value.BestAccessPoint : null);
						return false;
					}
				}
				return true;
			}

			private static void Postfix(ITransitEntity entity, NPC npc, Transform __result)
			{
				//IL_005a: Unknown result type (might be due to invalid IL or missing references)
				//IL_005f: Unknown result type (might be due to invalid IL or missing references)
				if (entity != null)
				{
					CacheKey cacheKey = default(CacheKey);
					cacheKey.NpcId = ((Object)npc).GetInstanceID();
					cacheKey.EntityId = ((Object)((Il2CppObjectBase)entity).Cast<Component>()).GetInstanceID();
					CacheKey key = cacheKey;
					_cache[key] = new CacheEntry
					{
						BestAccessPoint = __result,
						Reachable = ((Object)(object)__result != (Object)null),
						NpcPosition = ((Component)npc).transform.position,
						Timestamp = Time.time
					};
				}
			}
		}

		private static class NPCMovementCanGetToPatch
		{
			private static bool Prefix(NPCMovement __instance, Vector3 position, float proximityReq, ref NavMeshPath path, ref bool __result)
			{
				//IL_0004: Unknown result type (might be due to invalid IL or missing references)
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_004b: Unknown result type (might be due to invalid IL or missing references)
				//IL_007b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0082: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00da: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
				path = null;
				if (Vector3.Distance(position, ((Component)__instance).transform.position) <= proximityReq)
				{
					__result = true;
					return false;
				}
				if (!__instance.Agent.isOnNavMesh)
				{
					__result = false;
					return false;
				}
				NavMeshHit val = default(NavMeshHit);
				if (!NavMeshUtility.SamplePosition(position, ref val, 2f, -1, true))
				{
					__result = false;
					return false;
				}
				NavMeshPath path2 = __instance.PathCache.GetPath(((Component)__instance).transform.position, ((NavMeshHit)(ref val)).position, 1f);
				if (path2 != null)
				{
					path = path2;
					if (((Il2CppArrayBase<Vector3>)(object)path.corners).Length < 2)
					{
						__result = false;
						return false;
					}
					float num = Vector3.Distance(((Il2CppArrayBase<Vector3>)(object)path.corners)[((Il2CppArrayBase<Vector3>)(object)path.corners).Length - 1], ((NavMeshHit)(ref val)).position);
					float num2 = Vector3.Distance(((NavMeshHit)(ref val)).position, position);
					if (num <= proximityReq)
					{
						__result = num2 <= proximityReq;
						return false;
					}
					__result = false;
					return false;
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(Employee), "UpdateBehaviour")]
		private static class EmployeeUpdateBehaviourPatch
		{
			private static readonly Dictionary<int, float> _lastUpdateTimes = new Dictionary<int, float>();

			private const float UPDATE_INTERVAL = 1.5f;

			private static bool Prefix(Employee __instance)
			{
				int instanceID = ((Object)__instance).GetInstanceID();
				float time = Time.time;
				if (!_lastUpdateTimes.TryGetValue(instanceID, out var value) || time - value >= 1.5f)
				{
					_lastUpdateTimes[instanceID] = time;
					return true;
				}
				return false;
			}
		}

		public override void OnInitializeMelon()
		{
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Expected O, but got Unknown
			MelonLogger.Msg("BotanistFix loaded! Optimizing NPC navigation...");
			try
			{
				Type typeFromHandle = typeof(NPCMovement);
				MethodInfo method = typeFromHandle.GetMethod("CanGetTo", BindingFlags.Instance | BindingFlags.Public, null, new Type[3]
				{
					typeof(Vector3),
					typeof(float),
					typeof(NavMeshPath).MakeByRefType()
				}, null);
				if (method != null)
				{
					MethodInfo method2 = typeof(NPCMovementCanGetToPatch).GetMethod("Prefix", BindingFlags.Static | BindingFlags.NonPublic);
					((MelonBase)this).HarmonyInstance.Patch((MethodBase)method, new HarmonyMethod(method2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				else
				{
					MelonLogger.Error("Could not find NPCMovement.CanGetTo method with ref NavMeshPath parameter");
				}
			}
			catch (Exception ex)
			{
				MelonLogger.Error("Error while manually patching CanGetTo: " + ex.Message);
			}
		}
	}
}