using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using Agents;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Enemies;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.AI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace EnemySpacingAdjuster
{
internal static class Dbg
{
private static ManualLogSource _source;
private static readonly HashSet<string> SeenOnce = new HashSet<string>();
internal static void Init(ManualLogSource source)
{
_source = source;
}
internal static void Info(string message)
{
ManualLogSource source = _source;
if (source != null)
{
source.LogInfo((object)message);
}
}
internal static void Once(string message)
{
if (SeenOnce.Add(message))
{
ManualLogSource source = _source;
if (source != null)
{
source.LogWarning((object)message);
}
}
}
}
internal static class EnemyNavMeshPatches
{
internal static int Applied;
internal static int AlreadySet;
internal static int NonVanilla;
internal static int Unresolved;
internal static void Post_CommonEnter(ES_PathMove __instance)
{
Apply(__instance);
}
internal static void Post_Enter(ES_PathMove __instance)
{
Apply(__instance);
}
private static void Apply(ES_PathMove state)
{
try
{
if (state == null || !Plugin.Enabled.Value)
{
return;
}
NavMeshAgent val = ResolveAgent(state);
if ((Object)(object)val == (Object)null)
{
Unresolved++;
Dbg.Once("could not resolve a NavMeshAgent for an ES_PathMove instance");
return;
}
float value = Plugin.Radius.Value;
float radius = val.radius;
if (Math.Abs(radius - value) < 0.0001f)
{
AlreadySet++;
return;
}
if (Plugin.OnlyAtVanillaRadius.Value && Math.Abs(radius - Plugin.VanillaRadius.Value) > 0.001f)
{
NonVanilla++;
return;
}
val.radius = value;
Applied++;
if (Plugin.Verbose.Value && (Applied <= 5 || Applied % 50 == 0))
{
Dbg.Info($"radius {radius:0.###} -> {value:0.###} (applied={Applied}, alreadySet={AlreadySet}, nonVanilla={NonVanilla}, unresolved={Unresolved})");
}
}
catch (Exception ex)
{
Dbg.Once("apply failed: " + ex.GetType().Name + ": " + ex.Message);
}
}
private static NavMeshAgent ResolveAgent(ES_PathMove state)
{
EnemyAI ai = ((ES_Base)state).m_ai;
if ((Object)(object)ai == (Object)null)
{
return null;
}
INavigation navMeshAgent = ((AgentAI)ai).m_navMeshAgent;
if (navMeshAgent == null)
{
return null;
}
try
{
NavMeshAgentProxy val = ((Il2CppObjectBase)navMeshAgent).TryCast<NavMeshAgentProxy>();
if (val != null && (Object)(object)val.m_agent != (Object)null)
{
return val.m_agent;
}
}
catch (Exception ex)
{
Dbg.Once("TryCast<NavMeshAgentProxy> threw: " + ex.GetType().Name + ": " + ex.Message);
}
try
{
EnemyAgent enemyAgent = ((ES_Base)state).m_enemyAgent;
if ((Object)(object)enemyAgent != (Object)null)
{
return ((Component)enemyAgent).GetComponentInChildren<NavMeshAgent>(true);
}
}
catch (Exception ex2)
{
Dbg.Once("GetComponentInChildren<NavMeshAgent> threw: " + ex2.GetType().Name + ": " + ex2.Message);
}
return null;
}
}
[BepInPlugin("YG.EnemySpacingAdjuster", "EnemySpacingAdjuster", "1.0.0")]
public class Plugin : BasePlugin
{
public const string GUID = "YG.EnemySpacingAdjuster";
public const string NAME = "EnemySpacingAdjuster";
public const string VERSION = "1.0.0";
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<float> Radius;
internal static ConfigEntry<bool> OnlyAtVanillaRadius;
internal static ConfigEntry<float> VanillaRadius;
internal static ConfigEntry<bool> Verbose;
private Harmony _harmony;
public override void Load()
{
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Expected O, but got Unknown
//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
//IL_00dc: Expected O, but got Unknown
//IL_0185: Unknown result type (might be due to invalid IL or missing references)
//IL_018b: Expected O, but got Unknown
//IL_013a: Unknown result type (might be due to invalid IL or missing references)
//IL_0140: Expected O, but got Unknown
Enabled = ((BasePlugin)this).Config.Bind<bool>("General Settings", "Enabled", true, "Master switch. When off, the patches do nothing at all.");
Radius = ((BasePlugin)this).Config.Bind<float>("General Settings", "EnemyNavMeshRadius", 0.25f, new ConfigDescription("NavMeshAgent radius claimed by ground enemies (walkers). High values make enemies push each other apart, low values let them pass through each other. The unmodified game runs at 0.5 (the prefabs serialise 0.2, but Unity overwrites it from the navmesh agent type when the agent binds to the navmesh).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.02f, 1f), Array.Empty<object>()));
OnlyAtVanillaRadius = ((BasePlugin)this).Config.Bind<bool>("Compatibility", "OnlyIfAtVanillaRadius", false, "When true, only enemies whose current radius still equals VanillaRadius are touched. Custom enemies that deliberately set their own radius are then left alone.");
VanillaRadius = ((BasePlugin)this).Config.Bind<float>("Compatibility", "VanillaRadius", 0.5f, "Baseline radius used by the OnlyIfAtVanillaRadius check. 0.5 is the observed runtime value.");
Verbose = ((BasePlugin)this).Config.Bind<bool>("Diagnostics", "Verbose", false, "Log the first few adjustments and a periodic counter. Leave off in normal play.");
Dbg.Init(((BasePlugin)this).Log);
_harmony = new Harmony("YG.EnemySpacingAdjuster");
int num = 0;
if (TryPatch("CommonEnter", AccessTools.Method(typeof(EnemyNavMeshPatches), "Post_CommonEnter", (Type[])null, (Type[])null)))
{
num++;
}
if (TryPatch("Enter", AccessTools.Method(typeof(EnemyNavMeshPatches), "Post_Enter", (Type[])null, (Type[])null)))
{
num++;
}
bool flag = default(bool);
if (num == 0)
{
ManualLogSource log = ((BasePlugin)this).Log;
BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(47, 2, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("EnemySpacingAdjuster");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.0.0");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": no patch could be applied, the mod is inert.");
}
log.LogError(val);
}
else
{
ManualLogSource log2 = ((BasePlugin)this).Log;
BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(72, 7, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("EnemySpacingAdjuster");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("1.0.0");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" loaded (");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(num);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" patch(es), enabled=");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<bool>(Enabled.Value);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", ");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("radius=");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(Radius.Value, "0.###");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", onlyVanilla=");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<bool>(OnlyAtVanillaRadius.Value);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", ");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("vanillaBaseline=");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(VanillaRadius.Value, "0.###");
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(")");
}
log2.LogInfo(val2);
}
Radius.SettingChanged += delegate
{
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Expected O, but got Unknown
ManualLogSource log3 = ((BasePlugin)this).Log;
bool flag2 = default(bool);
BepInExInfoLogInterpolatedStringHandler val3 = new BepInExInfoLogInterpolatedStringHandler(76, 1, ref flag2);
if (flag2)
{
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("EnemyNavMeshRadius -> ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(Radius.Value, "0.###");
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" (takes effect the next time an enemy enters PathMove)");
}
log3.LogInfo(val3);
};
}
public override bool Unload()
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
return true;
}
private bool TryPatch(string methodName, MethodInfo postfix)
{
//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
//IL_0100: Expected O, but got Unknown
//IL_0027: Unknown result type (might be due to invalid IL or missing references)
//IL_002d: Expected O, but got Unknown
//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
//IL_00b7: Expected O, but got Unknown
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00ca: Expected O, but got Unknown
//IL_006e: Unknown result type (might be due to invalid IL or missing references)
//IL_0074: Expected O, but got Unknown
bool flag2 = default(bool);
try
{
MethodInfo methodInfo = AccessTools.Method(typeof(ES_PathMove), methodName, (Type[])null, (Type[])null);
if (methodInfo == null)
{
ManualLogSource log = ((BasePlugin)this).Log;
bool flag = default(bool);
BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(38, 1, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("ES_PathMove.");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(methodName);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" not found - hook skipped.");
}
log.LogWarning(val);
return false;
}
if (postfix == null)
{
ManualLogSource log2 = ((BasePlugin)this).Log;
BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(66, 1, ref flag2);
if (flag2)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("postfix for ES_PathMove.");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(methodName);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" not found in own assembly - hook skipped.");
}
log2.LogWarning(val);
return false;
}
_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(postfix), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
ManualLogSource log3 = ((BasePlugin)this).Log;
BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(20, 1, ref flag2);
if (flag2)
{
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("patched ES_PathMove.");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(methodName);
}
log3.LogInfo(val2);
return true;
}
catch (Exception ex)
{
ManualLogSource log4 = ((BasePlugin)this).Log;
BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(32, 3, ref flag2);
if (flag2)
{
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("patching ES_PathMove.");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(methodName);
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" failed: ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.GetType().Name);
((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(": ");
((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(ex.Message);
}
log4.LogError(val3);
return false;
}
}
}
}