Decompiled source of UseAllRaces v1.1.0

UseAllRaces.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Eremite;
using Eremite.Controller;
using Eremite.Model;
using Eremite.Services;
using Eremite.Services.Meta;
using Eremite.View.HUD;
using Eremite.View.HUD.Monitors;
using HarmonyLib;
using JLPlugin;
using JamesGames;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: AssemblyCompany("UseAllRaces")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Use all races in every run instead of just 3")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+5e7c589539143c090f7511560f5c08eeab6ea700")]
[assembly: AssemblyProduct("UseAllRaces")]
[assembly: AssemblyTitle("UseAllRaces")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ATS_UseAllRaces
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "UseAllRaces";

		public const string PLUGIN_NAME = "UseAllRaces";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace JamesGames
{
	[HarmonyPatch]
	[BepInPlugin("ATS_UseAllRaces", "ATS_UseAllRaces", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string GUID = "ATS_UseAllRaces";

		private const string NAME = "ATS_UseAllRaces";

		private const string VERSION = "1.0.0";

		public static Plugin Instance;

		public static ManualLogSource Log;

		private static Action OnRealignAlerts;

		private static int DefaultMaxRaces;

		private static Vector2? DefaultAlertsPosition;

		private static bool InitializedConfigs;

		private static MonitorsHUD m_MonitorsHUD;

		public static int MaxRaces()
		{
			int maxRaces = Configs.MaxRaces;
			return maxRaces switch
			{
				-1 => Serviceable.Settings.gameplayRaces, 
				0 => MB.Settings.Races.Length, 
				_ => maxRaces, 
			};
		}

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "ATS_UseAllRaces");
			Log.LogInfo((object)"ATS_UseAllRaces v1.0.0 Plugin loaded");
		}

		[HarmonyPatch(typeof(MainController), "OnServicesReady")]
		[HarmonyPostfix]
		private static void HookMainControllerSetup()
		{
			InitializedConfigs = true;
			DefaultMaxRaces = Serviceable.Settings.gameplayRaces;
			Configs.Initialize(((BaseUnityPlugin)Instance).Config);
			SetRaces();
		}

		public static void SetRaces()
		{
			int maxRaces = Configs.MaxRaces;
			if (maxRaces == -1)
			{
				Serviceable.Settings.gameplayRaces = MB.Settings.Races.Length;
			}
			else
			{
				Serviceable.Settings.gameplayRaces = Mathf.Clamp(maxRaces, 1, MB.Settings.Races.Length);
			}
		}

		[HarmonyPatch(typeof(RacesHUD), "SetUpSlots")]
		[HarmonyPostfix]
		private static void RacesHUD_SetUpSlots(RacesHUD __instance)
		{
			RealignAlerts(__instance);
			OnRealignAlerts = delegate
			{
				RealignAlerts(__instance);
			};
		}

		[HarmonyPatch(typeof(RacesHUDSlot), "Enable")]
		[HarmonyPostfix]
		private static void RacesHUDSlot_Enable(RacesHUD __instance)
		{
			OnRealignAlerts?.Invoke();
		}

		private static void RealignAlerts(RacesHUD hud)
		{
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			RacesHUDSlot val = null;
			for (int i = 0; i < hud.slots.Length; i++)
			{
				RacesHUDSlot val2 = hud.slots[i];
				if (((Component)val2).gameObject.activeSelf)
				{
					val = val2;
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			MonitorsHUD monitorsHUD = GetMonitorsHUD();
			if (!((Object)(object)monitorsHUD == (Object)null))
			{
				if (!DefaultAlertsPosition.HasValue)
				{
					DefaultAlertsPosition = Vector2.op_Implicit(((Component)monitorsHUD).transform.localPosition);
				}
				Vector3 localPosition = ((Component)monitorsHUD).transform.localPosition;
				localPosition.y = ((Component)val).transform.localPosition.y + ((MB)val).GetRectSize().y - 20f;
				if (!(localPosition.y > DefaultAlertsPosition.Value.y))
				{
					((Component)monitorsHUD).transform.localPosition = localPosition;
				}
			}
		}

		private static MonitorsHUD GetMonitorsHUD()
		{
			if ((Object)(object)m_MonitorsHUD == (Object)null)
			{
				m_MonitorsHUD = Object.FindObjectOfType<MonitorsHUD>(true);
			}
			return m_MonitorsHUD;
		}

		[HarmonyPatch(typeof(RacesHUD), "SetUp")]
		[HarmonyPrefix]
		private static bool RacesHUD_SetUp(RacesHUD __instance)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			while (__instance.slots.Length < GameMB.RacesService.Races.Length)
			{
				int num = __instance.slots.Length - 1;
				RacesHUDSlot val = __instance.slots[num];
				RacesHUDSlot val2 = Object.Instantiate<RacesHUDSlot>(val, ((Component)val).transform.parent);
				Transform transform = ((Component)val2).transform;
				transform.localPosition += ((Component)val).transform.localPosition - ((Component)__instance.slots[num - 1]).transform.localPosition;
				((Object)((Component)val2).gameObject).name = $"RaceHUDSlot ({num + 1})";
				__instance.slots = ArrayExtension.ForceAdd<RacesHUDSlot>(__instance.slots, val2);
			}
			return true;
		}

		[HarmonyPatch(typeof(CaravanGenerator), "GetRandomRaces")]
		[HarmonyPrefix]
		private static bool CaravanGenerator_DefineNewSettlementRaces(CaravanGenerator __instance, ref List<string> __result)
		{
			if (!InitializedConfigs)
			{
				return true;
			}
			List<RaceModel> list = ((IEnumerable<RaceModel>)ArrayShuffler.ShuffleToNew<RaceModel>(Serviceable.Settings.Races, __instance.rng)).Where((Func<RaceModel, bool>)Serviceable.MetaConditionsService.IsUnlocked).ToList();
			list.RemoveAll((RaceModel a) => Configs.RaceState(((Object)a).name) == Configs.State.Never);
			List<RaceModel> list2 = list.Where((RaceModel a) => Configs.RaceState(((Object)a).name) == Configs.State.Always).ToList();
			int num = MaxRaces() - list2.Count;
			if (num > 0)
			{
				List<RaceModel> source = list.Where((RaceModel a) => Configs.RaceState(((Object)a).name) == Configs.State.Optional).ToList();
				list2.AddRange(source.Take(num));
			}
			__result = list2.Select((RaceModel a) => ((Object)a).name).ToList();
			return false;
		}
	}
}
namespace JLPlugin
{
	internal static class Configs
	{
		public enum State
		{
			Never,
			Optional,
			Always
		}

		private static ConfigEntry<int> maxRaces;

		private static List<ConfigEntry<State>> raceConfigs = new List<ConfigEntry<State>>();

		public static int MaxRaces => maxRaces.Value;

		public static void Initialize(ConfigFile config)
		{
			maxRaces = config.Bind<int>("General", "Max Races", -1, "Maximum amount of races settlements can have. -1 is use all the races.");
			raceConfigs.Clear();
			RaceModel[] races = SO.Settings.Races;
			foreach (RaceModel race in races)
			{
				GetConfig(config, race, State.Always);
			}
			config.ConfigReloaded += delegate(object sender, EventArgs args)
			{
				Plugin.Log.LogInfo((object)("Config reloaded " + sender?.ToString() + " " + args));
			};
			config.SettingChanged += delegate(object sender, SettingChangedEventArgs args)
			{
				if (((object)maxRaces).Equals(sender))
				{
					Plugin.Log.LogInfo((object)("Max Race Setting changed to " + maxRaces.Value));
					Plugin.SetRaces();
				}
				else if (!raceConfigs.Contains(sender as ConfigEntry<State>))
				{
				}
			};
		}

		private static void GetConfig(ConfigFile config, RaceModel race, State defaultState)
		{
			ConfigEntry<State> item = config.Bind<State>("Races", ((Object)race).name, defaultState, "How frequent should this race be chosen in new races.");
			raceConfigs.Add(item);
		}

		public static State RaceState(string raceName)
		{
			foreach (ConfigEntry<State> raceConfig in raceConfigs)
			{
				if (((ConfigEntryBase)raceConfig).Definition.Key == raceName)
				{
					return raceConfig.Value;
				}
			}
			return State.Optional;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}