Decompiled source of BuildableEventBuildings v1.0.4

BuildableEventBuildings.dll

Decompiled 3 days 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.Buildings;
using Eremite.Controller;
using Eremite.Model;
using Eremite.Model.Configs;
using Eremite.Services;
using HarmonyLib;
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("BuildableEventBuildings")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Make buildings that are from events essential")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+de126099c6e12e89214b31c3ca42017d63576620")]
[assembly: AssemblyProduct("BuildableEventBuildings")]
[assembly: AssemblyTitle("BuildableEventBuildings")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 BuildableEventBuildings
{
	public enum BuildingMode
	{
		Disabled,
		Unlocked,
		Pickable
	}
	[HarmonyPatch]
	[BepInPlugin("BuildableEventBuildings", "BuildableEventBuildings", "1.0.4")]
	public class Plugin : BaseUnityPlugin
	{
		public static Plugin Instance;

		private Harmony harmony;

		public static ManualLogSource Log;

		private static readonly string[] BuildingNames = new string[15]
		{
			"Feast Hall", "Finesmith", "Flawless Cellar", "Flawless Cooperage", "Flawless Druid", "Flawless Leatherworks", "Flawless Rain Mill", "Flawless Smelter", "Hallowed Herb Garden", "Hallowed SmallFarm",
			"Holy Guild House", "Holy Market", "Holy Temple", "Homestead", "Rainpunk Foundry"
		};

		private static readonly Dictionary<string, ConfigEntry<BuildingMode>> BuildingConfigs = new Dictionary<string, ConfigEntry<BuildingMode>>();

		private static readonly Dictionary<string, ConfigEntry<float>> CostMultipliers = new Dictionary<string, ConfigEntry<float>>();

		private static readonly Dictionary<string, string> RegularVersions = new Dictionary<string, string>
		{
			{ "Flawless Cellar", "Cellar" },
			{ "Flawless Cooperage", "Cooperage" },
			{ "Flawless Druid", "Druid" },
			{ "Flawless Leatherworks", "Leatherworks" },
			{ "Flawless Rain Mill", "Rain Mill" },
			{ "Flawless Smelter", "Smelter" },
			{ "Hallowed Herb Garden", "Herb Garden" },
			{ "Hallowed SmallFarm", "SmallFarm" },
			{ "Holy Guild House", "Guild House" },
			{ "Holy Market", "Market" },
			{ "Holy Temple", "Temple" }
		};

		private static readonly Dictionary<string, ConfigEntry<bool>> RemoveRegular = new Dictionary<string, ConfigEntry<bool>>();

		private static ConfigEntry<bool> RemoveRegularOnPick;

		private void Awake()
		{
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			RemoveRegularOnPick = ((BaseUnityPlugin)this).Config.Bind<bool>("RemoveRegularVersionOnPick", "RemoveRegularVersionOnPick", true, "If true, picking an advanced building (e.g. Flawless Cellar) removes its regular version (e.g. Cellar) from the draft pool.");
			string[] buildingNames = BuildingNames;
			foreach (string text in buildingNames)
			{
				BuildingConfigs[text] = ((BaseUnityPlugin)this).Config.Bind<BuildingMode>("Buildings", text, BuildingMode.Pickable, "Disabled = do nothing, Unlocked = unlock at the start of every new game, Pickable = add to the pool of buildings that can be picked.");
				CostMultipliers[text] = ((BaseUnityPlugin)this).Config.Bind<float>("CostMultipliers", text, 2f, new ConfigDescription("Multiplier applied to this building's construction cost. 1 = vanilla cost.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 20f), Array.Empty<object>()));
				if (RegularVersions.TryGetValue(text, out var value))
				{
					RemoveRegular[text] = ((BaseUnityPlugin)this).Config.Bind<bool>("RemoveRegularVersion", text, false, "If true, the regular '" + value + "' is removed from the draft pool at the start of a new game.");
				}
			}
			harmony = Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
			Log.LogInfo((object)"BuildableEventBuildings log v1.0.4 Plugin loaded");
		}

		private static BuildingMode GetMode(string name)
		{
			ConfigEntry<BuildingMode> value;
			return BuildingConfigs.TryGetValue(name, out value) ? value.Value : BuildingMode.Disabled;
		}

		[HarmonyPatch(typeof(MainController), "OnServicesReady")]
		[HarmonyPostfix]
		private static void HookMainControllerSetup()
		{
			Log.LogInfo((object)"Performing game initialization on behalf of BuildableEventBuildings.");
			string[] buildingNames = BuildingNames;
			foreach (string text in buildingNames)
			{
				if (GetMode(text) == BuildingMode.Disabled)
				{
					continue;
				}
				BuildingModel building = MB.Settings.GetBuilding(text);
				if ((Object)(object)building == (Object)null)
				{
					Log.LogWarning((object)("Building '" + text + "' was not found; skipping."));
					continue;
				}
				if (!MB.MetaStateService.Content.buildings.Contains(((SO)building).Name))
				{
					Log.LogInfo((object)("Adding " + ((SO)building).Name + " to the list of available buildings."));
					MB.MetaStateService.Content.buildings.Add(((SO)building).Name);
				}
				float multiplier = CostMultipliers[text].Value;
				if (building.requiredGoods != null && !building.requiredGoods.Any())
				{
					Log.LogInfo((object)(((SO)building).Name + " has no construction cost to scale."));
				}
				else if (building.requiredGoods != null && Math.Abs(multiplier - 1f) > 0.001f)
				{
					building.requiredGoods = building.requiredGoods.Select((GoodRef g) => g.ScaleToNew(multiplier)).ToArray();
					Log.LogInfo((object)$"{((SO)building).Name} construction cost scaled by x{multiplier}.");
				}
			}
		}

		private static void RemoveFromPool(BuildingModel regular)
		{
			BiomeBlueprintsConfig blueprints = SO.BiomeService.Blueprints;
			blueprints.wildcards = blueprints.wildcards.Where((BuildingWeightedChance w) => (Object)(object)w.building != (Object)(object)regular).ToArray();
			BlueprintsRange[] blueprints2 = blueprints.blueprints;
			foreach (BlueprintsRange val in blueprints2)
			{
				BuildingsWeightedContainer[] sets = val.sets;
				foreach (BuildingsWeightedContainer val2 in sets)
				{
					val2.buildings = val2.buildings.Where((BuildingWeightedChance b) => (Object)(object)b.building != (Object)(object)regular).ToArray();
				}
			}
			Log.LogInfo((object)("Removed regular version " + ((SO)regular).Name + " from the draft pool."));
		}

		[HarmonyPatch(typeof(ReputationRewardsService), "OnLoading")]
		[HarmonyPrefix]
		private static void PrepareDraftPool()
		{
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Expected O, but got Unknown
			bool flag = MB.GameSaveService.IsNewGame();
			Log.LogInfo((object)$"Preparing draft pool. Is this a new game: {flag}.");
			if (!flag)
			{
				return;
			}
			string[] buildingNames = BuildingNames;
			foreach (string text in buildingNames)
			{
				BuildingMode mode = GetMode(text);
				if (mode == BuildingMode.Disabled)
				{
					continue;
				}
				BuildingModel building = MB.Settings.GetBuilding(text);
				if ((Object)(object)building == (Object)null)
				{
					continue;
				}
				if (RemoveRegular.TryGetValue(text, out var value) && value.Value && RegularVersions.TryGetValue(text, out var value2))
				{
					BuildingModel building2 = MB.Settings.GetBuilding(value2);
					if ((Object)(object)building2 != (Object)null)
					{
						RemoveFromPool(building2);
					}
					else
					{
						Log.LogWarning((object)(text + ": regular version '" + value2 + "' was not found; nothing removed."));
					}
				}
				if (mode == BuildingMode.Pickable && !SO.BiomeService.Blueprints.wildcards.Any((BuildingWeightedChance w) => (Object)(object)w.building == (Object)(object)building))
				{
					SO.BiomeService.Blueprints.wildcards = SO.BiomeService.Blueprints.wildcards.Append(new BuildingWeightedChance
					{
						building = building
					}).ToArray();
					Log.LogInfo((object)("Added " + ((SO)building).Name + " to the pickable buildings."));
				}
			}
		}

		[HarmonyPatch(typeof(GameContentService), "Unlock")]
		[HarmonyPostfix]
		private static void RemoveRegularWhenAdvancedUnlocked(BuildingModel building)
		{
			if (!RemoveRegularOnPick.Value)
			{
				return;
			}
			string name = ((SO)building).Name;
			if (RegularVersions.TryGetValue(name, out var value))
			{
				BuildingModel building2 = MB.Settings.GetBuilding(value);
				if ((Object)(object)building2 != (Object)null)
				{
					Log.LogInfo((object)(name + " was picked; removing regular version '" + value + "' from the draft pool."));
					RemoveFromPool(building2);
				}
				else
				{
					Log.LogWarning((object)(name + ": regular version '" + value + "' was not found; nothing removed."));
				}
			}
		}

		[HarmonyPatch(typeof(GameController), "StartGame")]
		[HarmonyPostfix]
		private static void HookEveryGameStart()
		{
			bool flag = MB.GameSaveService.IsNewGame();
			Log.LogInfo((object)$"Entered a game. Is this a new game: {flag}.");
			if (!flag)
			{
				return;
			}
			string[] buildingNames = BuildingNames;
			foreach (string text in buildingNames)
			{
				if (GetMode(text) == BuildingMode.Unlocked)
				{
					BuildingModel building = MB.Settings.GetBuilding(text);
					if (!((Object)(object)building == (Object)null))
					{
						SO.GameContentService.Unlock(building);
						Log.LogInfo((object)("Unlocked " + ((SO)building).Name + "."));
					}
				}
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "BuildableEventBuildings";

		public const string PLUGIN_NAME = "BuildableEventBuildings";

		public const string PLUGIN_VERSION = "1.0.4";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}