Decompiled source of Unlimited Seeds v1.0.1

unlimitedSeeds.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Extensions;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("unlimitedSeeds")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("unlimitedSeeds")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("87A972CE-47B2-488A-B4D5-C54B58656BF7")]
[assembly: AssemblyFileVersion("0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 unlimitedSeeds
{
	[BepInPlugin("warpalicious.unlimitedSeeds", "unlimitedSeeds", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class unlimitedSeedsPlugin : BaseUnityPlugin
	{
		private const string ModName = "unlimitedSeeds";

		private const string ModVersion = "1.0.1";

		private const string Author = "warpalicious";

		private const string ModGUID = "warpalicious.unlimitedSeeds";

		private static string ConfigFileName = "warpalicious.unlimitedSeeds.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony HarmonyInstance = new Harmony("warpalicious.unlimitedSeeds");

		public static readonly ManualLogSource Log;

		public static ConfigEntry<int> OffsetMin;

		public static ConfigEntry<int> OffsetMax;

		private DateTime _lastReloadTime;

		private const long RELOAD_DELAY = 10000000L;

		public void Awake()
		{
			ConfigFile config = ((BaseUnityPlugin)this).Config;
			AcceptableValueBase val = (AcceptableValueBase)(object)new AcceptableValueRange<int>(-1000000, -10000);
			OffsetMin = ConfigFileExtensions.BindConfig<int>(config, "General", "OffsetMin", -100000, "Minimum offset for world positioning in noise space. Vanilla: -10000. Lower values expand the accessible terrain region.", true, (int?)null, val, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null);
			ConfigFile config2 = ((BaseUnityPlugin)this).Config;
			val = (AcceptableValueBase)(object)new AcceptableValueRange<int>(10000, 1000000);
			OffsetMax = ConfigFileExtensions.BindConfig<int>(config2, "General", "OffsetMax", 100000, "Maximum offset for world positioning in noise space. Vanilla: 10000. Higher values expand the accessible terrain region.", true, (int?)null, val, (Action<ConfigEntryBase>)null, (ConfigurationManagerAttributes)null);
			UpdatePatchSettings();
			OffsetMin.SettingChanged += delegate
			{
				UpdatePatchSettings();
			};
			OffsetMax.SettingChanged += delegate
			{
				UpdatePatchSettings();
			};
			SynchronizationManager.OnConfigurationSynchronized += delegate
			{
				UpdatePatchSettings();
			};
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
			SetupWatcher();
		}

		private static void UpdatePatchSettings()
		{
			WorldGeneratorOffsetPatch.OffsetMin = OffsetMin.Value;
			WorldGeneratorOffsetPatch.OffsetMax = OffsetMax.Value;
			Log.LogInfo((object)$"Offset range: [{OffsetMin.Value}, {OffsetMax.Value}]");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			_lastReloadTime = DateTime.Now;
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			DateTime now = DateTime.Now;
			long num = now.Ticks - _lastReloadTime.Ticks;
			if (File.Exists(ConfigFileFullPath) && num >= 10000000)
			{
				try
				{
					Log.LogInfo((object)"Attempting to reload configuration...");
					((BaseUnityPlugin)this).Config.Reload();
					Log.LogInfo((object)"Configuration reloaded successfully!");
				}
				catch
				{
					Log.LogError((object)("There was an issue loading " + ConfigFileName));
					return;
				}
				_lastReloadTime = now;
				UpdatePatchSettings();
			}
		}

		static unlimitedSeedsPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			Log = Logger.CreateLogSource("unlimitedSeeds");
			OffsetMin = null;
			OffsetMax = null;
		}
	}
	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	public static class WorldGeneratorOffsetPatch
	{
		public static int OffsetMin = -100000;

		public static int OffsetMax = 100000;

		[ThreadStatic]
		private static bool _isMenuWorld;

		[HarmonyPrefix]
		public static void Prefix(World world)
		{
			_isMenuWorld = world.m_menu;
		}

		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int num = 0;
			MethodInfo methodInfo = AccessTools.Method(typeof(Random), "Range", new Type[2]
			{
				typeof(int),
				typeof(int)
			}, (Type[])null);
			for (int i = 0; i < list.Count - 2; i++)
			{
				if (IsLoadConstant(list[i], -10000) && IsLoadConstant(list[i + 1], 10000) && !(list[i + 2].opcode != OpCodes.Call) && list[i + 2].operand is MethodInfo methodInfo2 && !(methodInfo2 != methodInfo))
				{
					list[i] = new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(WorldGeneratorOffsetPatch), "GetMinOffset", (Type[])null, (Type[])null));
					list[i + 1] = new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(WorldGeneratorOffsetPatch), "GetMaxOffset", (Type[])null, (Type[])null));
					num++;
				}
			}
			return list;
		}

		public static int GetMinOffset()
		{
			if (!_isMenuWorld)
			{
				return OffsetMin;
			}
			return -10000;
		}

		public static int GetMaxOffset()
		{
			if (!_isMenuWorld)
			{
				return OffsetMax;
			}
			return 10000;
		}

		[HarmonyPostfix]
		public static void Postfix(WorldGenerator __instance, World world)
		{
			if (!world.m_menu)
			{
				Type typeFromHandle = typeof(WorldGenerator);
				BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.NonPublic;
				float num = (float)typeFromHandle.GetField("m_offset0", bindingAttr).GetValue(__instance);
				float num2 = (float)typeFromHandle.GetField("m_offset1", bindingAttr).GetValue(__instance);
				float num3 = (float)typeFromHandle.GetField("m_offset2", bindingAttr).GetValue(__instance);
				float num4 = (float)typeFromHandle.GetField("m_offset3", bindingAttr).GetValue(__instance);
				float num5 = (float)typeFromHandle.GetField("m_offset4", bindingAttr).GetValue(__instance);
				unlimitedSeedsPlugin.Log.LogInfo((object)($"WorldGenerator offsets: [{num:F0}, {num2:F0}, {num3:F0}, {num4:F0}, {num5:F0}] " + $"(range: [{OffsetMin}, {OffsetMax}])"));
			}
		}

		private static bool IsLoadConstant(CodeInstruction instruction, int value)
		{
			if (instruction.opcode == OpCodes.Ldc_I4)
			{
				if (instruction.operand is int num)
				{
					return num == value;
				}
				return false;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_S)
			{
				if (instruction.operand is sbyte b)
				{
					return b == value;
				}
				return false;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_M1 && value == -1)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_0 && value == 0)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_1 && value == 1)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_2 && value == 2)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_3 && value == 3)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_4 && value == 4)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_5 && value == 5)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_6 && value == 6)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_7 && value == 7)
			{
				return true;
			}
			if (instruction.opcode == OpCodes.Ldc_I4_8 && value == 8)
			{
				return true;
			}
			return false;
		}
	}
}