using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Crafting.Construction;
using HarmonyLib;
using Level_Manager;
using Terrain.Platforms;
using Terrain.World;
using Utilities;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace WorldGenTweaks;
[BepInPlugin("worldgentweaks", "worldgentweaks", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
public static ConfigEntry<bool> configLogDebugILPatches;
public static ConfigEntry<bool> stompVanillaFirstRingClusterCount;
public static ConfigEntry<int> minClustersPerSector;
public static ConfigEntry<int> maxClustersPerSector;
public static ConfigEntry<int> minIslandsPerCluster;
public static ConfigEntry<int> maxIslandsPerCluster;
public static ConfigEntry<bool> claimEveryIsland;
public static ManualLogSource _logger;
public static Harmony _Harmony;
public Plugin()
{
//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
//IL_0104: Expected O, but got Unknown
//IL_0143: Unknown result type (might be due to invalid IL or missing references)
//IL_014f: Expected O, but got Unknown
//IL_0191: Unknown result type (might be due to invalid IL or missing references)
//IL_019e: Expected O, but got Unknown
//IL_01d1: Unknown result type (might be due to invalid IL or missing references)
//IL_01de: Expected O, but got Unknown
//IL_0211: Unknown result type (might be due to invalid IL or missing references)
//IL_021e: Expected O, but got Unknown
_logger = ((BaseUnityPlugin)this).Logger;
configLogDebugILPatches = ((BaseUnityPlugin)this).Config.Bind<bool>("Logging", "LogILPatchInstructions", false, "Log (at debug) the IL Patch instructions after the patch has been applied.");
stompVanillaFirstRingClusterCount = ((BaseUnityPlugin)this).Config.Bind<bool>("Clusters", "DisableSpawnSectorClusterCap", true, "The minimum number of clusters to generate per sector");
minClustersPerSector = ((BaseUnityPlugin)this).Config.Bind<int>("Clusters", "MinPerSector", 20, "The minimum number of clusters to generate per sector");
maxClustersPerSector = ((BaseUnityPlugin)this).Config.Bind<int>("Clusters", "MaxPerSector", 30, "The maximum number of clusters to generate per sector");
minIslandsPerCluster = ((BaseUnityPlugin)this).Config.Bind<int>("Islands", "MinPerCluster", 20, "The minimum number of islands to generate per cluster");
maxIslandsPerCluster = ((BaseUnityPlugin)this).Config.Bind<int>("Islands", "MaxPerCluster", 30, "The maximum number of islands to generate per cluster");
claimEveryIsland = ((BaseUnityPlugin)this).Config.Bind<bool>("Islands", "ClaimEveryIsland", false, "Allow claiming every island. No more `too big` and `fresco` and `titan` errors.");
_Harmony = new Harmony("worldgentweaks");
if (stompVanillaFirstRingClusterCount.Value)
{
_Harmony.Patch((MethodBase)AccessTools.Method(typeof(WorldGenerator), "GenerateSectors", (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "StompSpawnSectorClusterCap", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null);
}
if (claimEveryIsland.Value)
{
_Harmony.Patch((MethodBase)AccessTools.Method(typeof(Ghost_CanConstructPlatformHomeIsland), "CheckIsland", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "CheckIsland", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
_Harmony.Patch((MethodBase)AccessTools.Method(typeof(PlatformAdditionalData), "get_IsTooHeavyToMove", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "TooHeavy", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
_Harmony.Patch((MethodBase)AccessTools.Method(typeof(Ghost_CanConstructPlatformTooHeavy), "CanConstruct", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(AccessTools.Method(typeof(Plugin), "PlatformToHeavyConstruct", (Type[])null, (Type[])null)), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
}
public void Awake()
{
Level.LoadSceneCallBack = (Action)Delegate.Combine(Level.LoadSceneCallBack, new Action(LoadSceneCallBack));
((BaseUnityPlugin)this).Logger.LogWarning((object)"Awake!");
}
private void LoadSceneCallBack()
{
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_006b: Expected O, but got Unknown
//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Expected O, but got Unknown
((BaseUnityPlugin)this).Logger.LogDebug((object)"Load callback from world generator!");
RingData[] ringDatas = Level.ConstantManager.ConstantManagers.WorldManager.RingGenerator.RingDatas;
for (int i = 0; i < ringDatas.Length; i++)
{
Level.ConstantManager.ConstantManagers.WorldManager.RingGenerator.RingDatas[i].ClusterPerSectors = new MinMaxInt(minClustersPerSector.Value, maxClustersPerSector.Value);
}
ClusterPerBiome[] clusterPerBiomes = Level.ConstantManager.ConstantManagers.WorldManager.ClusterGenerator.ClusterPerBiomes;
for (int j = 0; j < clusterPerBiomes.Length; j++)
{
Level.ConstantManager.ConstantManagers.WorldManager.ClusterGenerator.ClusterPerBiomes[j].IslandCountPerCluster = new MinMaxInt(minIslandsPerCluster.Value, maxIslandsPerCluster.Value);
}
}
public static IEnumerable<CodeInstruction> StompSpawnSectorClusterCap(IEnumerable<CodeInstruction> instructions, ILGenerator il)
{
//IL_0066: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Expected O, but got Unknown
FieldInfo fieldInfo = AccessTools.Field(typeof(GenerationSector), "ClusterAmount");
List<CodeInstruction> list = new List<CodeInstruction>(instructions);
int num = -1;
for (int i = 0; i < list.Count; i++)
{
if (CodeInstructionExtensions.StoresField(list[i], fieldInfo) && list[i - 1].opcode == OpCodes.Ldc_I4_4)
{
num = i;
}
}
if (num != -1)
{
list[num] = new CodeInstruction(OpCodes.Pop, (object)null);
}
if (configLogDebugILPatches.Value)
{
_logger.LogDebug((object)("New code: " + string.Join("\n", list)));
}
return list;
}
public static bool CheckIsland(bool __result, PlatformGlobalData globalData)
{
_logger.LogDebug((object)"Allowing claiming of island.");
return true;
}
public static bool TooHeavy(bool __result)
{
_logger.LogDebug((object)"Skipping too heavy check.");
return false;
}
public static bool PlatformToHeavyConstruct(bool __result)
{
_logger.LogDebug((object)"Skipping paltform can construct check.");
return true;
}
public static IEnumerable<CodeInstruction> HomeKiteInteract(IEnumerable<CodeInstruction> instructions, ILGenerator il)
{
//IL_0066: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Expected O, but got Unknown
FieldInfo fieldInfo = AccessTools.Field(typeof(GenerationSector), "ClusterAmount");
List<CodeInstruction> list = new List<CodeInstruction>(instructions);
int num = -1;
for (int i = 0; i < list.Count; i++)
{
if (CodeInstructionExtensions.StoresField(list[i], fieldInfo) && list[i - 1].opcode == OpCodes.Ldc_I4_4)
{
num = i;
}
}
if (num != -1)
{
list[num] = new CodeInstruction(OpCodes.Pop, (object)null);
}
if (configLogDebugILPatches.Value)
{
_logger.LogDebug((object)("New code: " + string.Join("\n", list)));
}
return list;
}
}