using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using SandWormNerf.Patches;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("SandWormNerf")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SandWormNerf")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("46855733-3096-4797-b9a7-49f6ead28c55")]
[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 SandWormNerf
{
[BepInPlugin("LongParsnip.SandWormNerf", "SandWormNerf", "1.0.0.0")]
public class SandWormNerfBase : BaseUnityPlugin
{
private const string modGUID = "LongParsnip.SandWormNerf";
private const string modName = "SandWormNerf";
private const string modVersion = "1.0.0.0";
public const bool LogDebugMessages = false;
private readonly Harmony harmony = new Harmony("LongParsnip.SandWormNerf");
private static SandWormNerfBase Instance;
private ManualLogSource mls;
public static string NotSpotPlayerDistancekey = "Not Spot Player Distance";
public static string EmergenceChanceKey = "Emergence Chance";
public static ConfigEntry<float> NotSpotPlayerDistance;
public static ConfigEntry<float> EmergenceChance;
private void Awake()
{
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Expected O, but got Unknown
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Expected O, but got Unknown
NotSpotPlayerDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", NotSpotPlayerDistancekey, 13f, new ConfigDescription("Distance from player a sandworm will not spot you (game default = 19.0). A lower setting will decrease the worms target range.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
EmergenceChance = ((BaseUnityPlugin)this).Config.Bind<float>("General", EmergenceChanceKey, 13f, new ConfigDescription("Chance that the worm will emerge (game default = 17.0). A setting will make the worm more likely to attack when in range.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 101f), Array.Empty<object>()));
if ((Object)(object)Instance == (Object)null)
{
Instance = this;
}
mls = Logger.CreateLogSource("LongParsnip.SandWormNerf");
mls.LogInfo((object)"SandWormNerf is awake and needs coffee.");
harmony.PatchAll(typeof(SandWormAIPatch));
}
}
}
namespace SandWormNerf.Patches
{
[HarmonyPatch(typeof(SandWormAI))]
[HarmonyPatch("DoAIInterval")]
public static class SandWormAIPatch
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> list = new List<CodeInstruction>(instructions);
for (int i = 0; i < list.Count; i++)
{
CodeInstruction instruction = list[i];
bool flag = false;
if (list[i].opcode == OpCodes.Ldfld && list[i].operand is FieldInfo fieldInfo && fieldInfo.Name == "mostOptimalDistance")
{
int num = i + 1;
if (num >= 0 && list[num].opcode == OpCodes.Ldc_R4 && (float)list[num].operand == 19f)
{
Debug.Log((object)$"SandWormNerf Modified instruction at index {num}. Current value: {(float)list[num].operand}");
list[num].operand = SandWormNerfBase.NotSpotPlayerDistance.Value;
Debug.Log((object)$"SandWormNerf Modified instruction at index {num}. New value: {(float)list[num].operand}");
}
}
if (list[i].opcode == OpCodes.Call && IsRandomRangeCall(instruction))
{
int num2 = i + 1;
if (((num2 >= 0) & (list[num2].opcode == OpCodes.Ldc_I4_S)) && list[num2].operand is sbyte b && b == 17)
{
Debug.Log((object)$"SandWormNerf Modified instruction for emergence chance at index {num2}. Current value: {(sbyte)list[num2].operand}");
list[num2].operand = (sbyte)SandWormNerfBase.EmergenceChance.Value;
Debug.Log((object)$"SandWormNerf Modified instruction for emergence chance at index {num2}. New value: {(sbyte)list[num2].operand}");
}
}
}
return list.AsEnumerable();
}
private static bool IsRandomRangeCall(CodeInstruction instruction)
{
return instruction.opcode == OpCodes.Call && instruction.operand is MethodInfo methodInfo && methodInfo.DeclaringType == typeof(Random) && methodInfo.Name == "Range";
}
private static void LogDebuggingMessages(CodeInstruction instruction, int i)
{
Debug.Log((object)$"Opcode at index {i}: {instruction.opcode}");
if (instruction.operand is FieldInfo fieldInfo)
{
Debug.Log((object)$"Field Name: {fieldInfo.Name}, Declaring Type: {fieldInfo.DeclaringType}");
}
if (IsRandomRangeCall(instruction))
{
Debug.Log((object)$"Found the call to UnityEngine.Random.Range at index {i}");
}
}
}
}