Decompiled source of WorldEventsMultiplier v1.2.0

BepInEx\plugins\WorldEventsMultiplier.dll

Decompiled 26 minutes ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using Eremite;
using Eremite.Model;
using Eremite.WorldMap.Controllers;
using Eremite.WorldMap.Controllers.Generator;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace WorldEventsMultiplier;

[BepInPlugin("com.worldevents.multiplier", "World Events Multiplier", "1.2.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "com.worldevents.multiplier";

	public const string Name = "World Events Multiplier";

	public const string Version = "1.2.0";

	public static ConfigEntry<float> WorldEventsMultiplier;

	public static ConfigEntry<float> ModifiersMultiplier;

	public static ConfigEntry<bool> AllowModifiersNextToEvents;

	public static ConfigEntry<bool> AllowWorldEventNearStart;

	public static ConfigEntry<bool> KeepEventsUntilTriggered;

	private void Awake()
	{
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Expected O, but got Unknown
		WorldEventsMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "WorldEventsMultiplier", 1.5f, "Multiplier for the number of world events generated on the map.");
		ModifiersMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "ModifiersMultiplier", 1.5f, "Multiplier for the number of modifiers generated on the map.");
		AllowModifiersNextToEvents = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowModifiersNextToEvents", false, "If true, modifiers may be generated on fields adjacent to world events.");
		AllowWorldEventNearStart = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowWorldEventNearStart", false, "If true, world events may be generated near the starting point (capital).");
		KeepEventsUntilTriggered = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "KeepEventsUntilTriggered", false, "If true, world events are not auto-removed over time; they persist until triggered/completed.");
		Harmony val = new Harmony("com.worldevents.multiplier");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)("World Events Multiplier loaded. events=x" + WorldEventsMultiplier.Value + ", modifiers=x" + ModifiersMultiplier.Value + ", allowModifiersNextToEvents=" + AllowModifiersNextToEvents.Value + ", allowWorldEventNearStart=" + AllowWorldEventNearStart.Value + ", keepEventsUntilTriggered=" + KeepEventsUntilTriggered.Value));
	}
}
internal static class State
{
	public static bool GeneratingModifiers;

	public static bool GeneratingWorldEvents;
}
[HarmonyPatch(typeof(WorldGenerator), "GenerateWorldEvents")]
internal static class PatchEventsCount
{
	[HarmonyTranspiler]
	public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Expected O, but got Unknown
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		MethodInfo methodInfo = AccessTools.Method(typeof(VectorExtensions), "Random", new Type[2]
		{
			typeof(Vector2Int),
			typeof(Random)
		}, (Type[])null);
		MethodInfo operandAndAdvance = AccessTools.Method(typeof(PatchEventsCount), "MultiplyEvents", (Type[])null, (Type[])null);
		CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
		val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
		{
			new CodeMatch((OpCode?)OpCodes.Call, (object)methodInfo, (string)null)
		});
		val.SetOperandAndAdvance((object)operandAndAdvance);
		return val.InstructionEnumeration();
	}

	public static int MultiplyEvents(Vector2Int range, Random rng)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		return Round((float)VectorExtensions.Random(range, rng) * Plugin.WorldEventsMultiplier.Value);
	}

	private static int Round(float value)
	{
		return (int)Math.Round(value, MidpointRounding.AwayFromZero);
	}
}
[HarmonyPatch(typeof(WorldGenerator), "GenerateWorldEvents")]
internal static class PatchEventsFlag
{
	[HarmonyPrefix]
	public static void Prefix()
	{
		State.GeneratingWorldEvents = true;
	}

	[HarmonyPostfix]
	public static void Postfix()
	{
		State.GeneratingWorldEvents = false;
	}
}
[HarmonyPatch(typeof(WorldGenerator), "GenerateWorldEvents")]
internal static class PatchEventsNearStart
{
	[HarmonyTranspiler]
	public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Expected O, but got Unknown
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Expected O, but got Unknown
		MethodInfo methodInfo = AccessTools.Method(typeof(WorldEventModel), "GetMinDistance", new Type[0], (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(PatchEventsNearStart), "GetMinDistanceForEvents", (Type[])null, (Type[])null);
		CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
		val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
		{
			new CodeMatch((OpCode?)OpCodes.Callvirt, (object)methodInfo, (string)null)
		});
		val.SetInstructionAndAdvance(new CodeInstruction(OpCodes.Call, (object)methodInfo2));
		return val.InstructionEnumeration();
	}

	public static int GetMinDistanceForEvents(WorldEventModel model)
	{
		return Plugin.AllowWorldEventNearStart.Value ? 1 : model.GetMinDistance();
	}
}
[HarmonyPatch(typeof(WorldGenerator), "GenerateModifiers")]
internal static class PatchModifiersCount
{
	[HarmonyTranspiler]
	public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Expected O, but got Unknown
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		MethodInfo methodInfo = AccessTools.Method(typeof(VectorExtensions), "Random", new Type[2]
		{
			typeof(Vector2Int),
			typeof(Random)
		}, (Type[])null);
		MethodInfo operandAndAdvance = AccessTools.Method(typeof(PatchModifiersCount), "MultiplyModifiers", (Type[])null, (Type[])null);
		CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
		val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
		{
			new CodeMatch((OpCode?)OpCodes.Call, (object)methodInfo, (string)null)
		});
		val.SetOperandAndAdvance((object)operandAndAdvance);
		return val.InstructionEnumeration();
	}

	public static int MultiplyModifiers(Vector2Int range, Random rng)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		return Round((float)VectorExtensions.Random(range, rng) * Plugin.ModifiersMultiplier.Value);
	}

	private static int Round(float value)
	{
		return (int)Math.Round(value, MidpointRounding.AwayFromZero);
	}
}
[HarmonyPatch(typeof(WorldGenerator), "GenerateModifiers")]
internal static class PatchModifiersFlag
{
	[HarmonyPrefix]
	public static void Prefix()
	{
		State.GeneratingModifiers = true;
	}

	[HarmonyPostfix]
	public static void Postfix()
	{
		State.GeneratingModifiers = false;
	}
}
[HarmonyPatch(typeof(WorldGenerator), "IsCapital")]
internal static class PatchCapitalAllow
{
	[HarmonyPrefix]
	public static bool Prefix(ref bool __result)
	{
		if (!State.GeneratingWorldEvents || !Plugin.AllowWorldEventNearStart.Value)
		{
			return true;
		}
		__result = false;
		return false;
	}
}
[HarmonyPatch(typeof(WorldGenerator), "AdjacentFieldsHasAnyObject")]
internal static class PatchAdjacency
{
	private static FieldInfo fieldsField = AccessTools.Field(typeof(WorldGenerator), "fields");

	private static FieldInfo modifiersField = AccessTools.Field(typeof(WorldGenerator), "modifiers");

	private static FieldInfo sealsField = AccessTools.Field(typeof(WorldGenerator), "seals");

	[HarmonyPrefix]
	public static bool Prefix(object __instance, Vector3Int field, ref bool __result)
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		if (!State.GeneratingModifiers || !Plugin.AllowModifiersNextToEvents.Value)
		{
			return true;
		}
		IDictionary dictionary = (IDictionary)fieldsField.GetValue(__instance);
		IDictionary dictionary2 = (IDictionary)modifiersField.GetValue(__instance);
		IDictionary dictionary3 = (IDictionary)sealsField.GetValue(__instance);
		Vector3Int[] cubicDirections = Constants.CubicDirections;
		for (int i = 0; i < cubicDirections.Length; i++)
		{
			Vector3Int val = field + cubicDirections[i];
			if (dictionary.Contains(val) && (dictionary2.Contains(val) || dictionary3.Contains(val)))
			{
				__result = true;
				return false;
			}
		}
		__result = false;
		return false;
	}
}
[HarmonyPatch(typeof(WorldEventsController), "IsOldEvent")]
internal static class PatchEventPersistence
{
	[HarmonyPrefix]
	public static bool Prefix(ref bool __result)
	{
		if (!Plugin.KeepEventsUntilTriggered.Value)
		{
			return true;
		}
		__result = false;
		return false;
	}
}