Decompiled source of EventTweaks v0.1.0

plugins/EventTweaks.dll

Decompiled 21 hours 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.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using EventTweaks.patches;
using HarmonyLib;
using Ink.Runtime;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using TrainworksReloaded.Core;
using TrainworksReloaded.Core.Extensions;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("EventTweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+9367dde38de226bea99d75090c9f1b97988d980d")]
[assembly: AssemblyProduct("EventTweaks")]
[assembly: AssemblyTitle("EventTweaks")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
	[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 EventTweaks
{
	[BepInPlugin("EventTweaks", "EventTweaks", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger = new ManualLogSource("EventTweaks");

		public void Awake()
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin EventTweaks is loaded!");
			Railhead.GetBuilder().Configure("EventTweaks", (Action<IConfigurationBuilder>)delegate(IConfigurationBuilder c)
			{
				ConfigurationExtensions.AddMergedJsonFile(c, new List<string> { "json/plugin.json" });
			});
			HephsBlueprints.Init(((BaseUnityPlugin)this).Config);
			ArchusWing.Init(((BaseUnityPlugin)this).Config);
			new Harmony("EventTweaks").PatchAll();
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "EventTweaks";

		public const string PLUGIN_NAME = "EventTweaks";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}
namespace EventTweaks.patches
{
	internal static class ArchusWing
	{
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static class RewriteKnot
		{
			private static void Prefix(ref string __0)
			{
				if (__0.Contains("\"CorruptedAngels\":"))
				{
					if (!__0.Contains("\"^Purify\",\"/str\",{\"x()\":\"GetTrainHealth\"},10,\">\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":5},") || !__0.Contains("\"g-0\":[\"end\",{\"#f\":7}]}],{\"round2\":") || !__0.Contains("\"^<style=\\\"Body\\\"><darkangel>\\\"Now, Seraph will learn to kneel before me. Heaven's throne will be mine...\\\"</darkangel>\",\"\\n\",\"^With an eruption of shadow, <darkangel><shake>ARCHUS</darkangel></shake> disappears into the darkness. </style>\",\"\\n\",") || !__0.Contains("\"^<style=\\\"Body\\\">You decline the artifact. Before you have a chance to look up, you see <darkangel><shake>ARCHUS</darkangel></shake> trudging down the path alone.</style>\",\"\\n\","))
					{
						Plugin.Logger.LogWarning((object)"CorruptedAngels changed in the Ink story; Archus' Wing left as is.");
					}
					else
					{
						__0 = __0.Replace("\"^Purify\",\"/str\",{\"x()\":\"GetTrainHealth\"},10,\">\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":5},", "\"^Purify\",\"/str\",{\"x()\":\"GetTrainHealth\"},10,\">\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":5},\"ev\",\"str\",\"^EventTweaksGiveWing\",\"/str\",\"/ev\",{\"*\":\".^.c-2\",\"flg\":4},").Replace("\"g-0\":[\"end\",{\"#f\":7}]}],{\"round2\":", "\"c-2\":[\"\\n\",\"^>>>EVENTTWEAKS_GIVE_ARCHUS_WING\",\"\\n\",{\"->\":\"CorruptedAngels.EventTweaksWing1\"},{\"#f\":7}],\"g-0\":[\"end\",{\"#f\":7}]}],{" + Rolls + "\"EventTweaksRestored\":[\"^EventTweaks_ArchusRestored\",\"\\n\",\"ev\",\"str\",\"^GainArtifact\",\"/str\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":4},\"ev\",\"str\",\"^Leave\",\"/str\",\"/ev\",{\"*\":\".^.c-1\",\"flg\":4},{\"c-0\":[\"\\n\",\"^>>>GIVE_REWARD: CorruptedAngelRelicReward\",\"\\n\",\"^<style=\\\"Body\\\"><darkangel>\\\"Now, Seraph will learn to kneel before me. Heaven's throne will be mine...\\\"</darkangel>\",\"\\n\",\"^With an eruption of shadow, <darkangel><shake>ARCHUS</darkangel></shake> disappears into the darkness. </style>\",\"\\n\",\"end\",{\"#f\":7}],\"c-1\":[\"\\n\",\"^<style=\\\"Body\\\">You decline the artifact. Before you have a chance to look up, you see <darkangel><shake>ARCHUS</darkangel></shake> trudging down the path alone.</style>\",\"\\n\",\"end\",{\"#f\":7}],\"#f\":3}],\"round2\":");
					}
				}
			}
		}

		[HarmonyPatch(typeof(InkWriter), "HandleCommand")]
		private static class Commands
		{
			private static bool Prefix(string command, SaveManager ____saveManager, ref bool __result)
			{
				if (command == "EVENTTWEAKS_GIVE_ARCHUS_WING")
				{
					CardState val = WingBearer(____saveManager);
					CardUpgradeState val2 = ((val != null) ? val.GetCardStateModifiers().GetCardUpgrades().Find((CardUpgradeState u) => u.GetAssetName() == "ClippedWingsWing2RewardUpgrade") : null);
					if (val2 == null || !val.RemoveUpgrade(val2, val.GetCardStateModifiers(), (string)null))
					{
						Plugin.Logger.LogWarning((object)"Archus' Wing: no deck unit lost the wing.");
					}
					if (val != null)
					{
						val.UpdateCardBodyText(____saveManager);
					}
					__result = true;
					return false;
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(InkWriter), "GetStoryChoiceData")]
		private static class Choices
		{
			private static void Prefix(List<Choice> choices, StoryEventData ____currentStoryEventData, SaveManager ____saveManager)
			{
				if (((____currentStoryEventData != null) ? ____currentStoryEventData.KnotName : null) == "CorruptedAngels" && WingBearer(____saveManager) == null)
				{
					choices.RemoveAll((Choice c) => c.text == "EventTweaksGiveWing");
				}
			}

			private static void Postfix(List<StoryChoiceData> __result, StoryEventData ____currentStoryEventData, SaveManager ____saveManager)
			{
				if (((____currentStoryEventData != null) ? ____currentStoryEventData.KnotName : null) != "CorruptedAngels")
				{
					return;
				}
				foreach (Choice item in from d in __result
					select d.choice into c
					where c.choiceId == "EventTweaksGiveWing"
					select c)
				{
					string optionalText = item.optionalText;
					object optionalText2;
					if (optionalText == null)
					{
						optionalText2 = null;
					}
					else
					{
						CardState? obj = WingBearer(____saveManager);
						optionalText2 = optionalText.Replace("{unit}", (obj != null) ? obj.GetTitle() : null);
					}
					item.optionalText = (string)optionalText2;
				}
			}
		}

		[HarmonyPatch(typeof(SaveManager), "GenerateStoryEventsAtDistance")]
		private static class ArchusTickets
		{
			private static readonly FieldRef<StoryEventData, int> TicketsRef = AccessTools.FieldRefAccess<StoryEventData, int>("priorityTicketCount");

			private static readonly Func<SaveManager, StoryEventData, bool> IsEventQueued = AccessTools.MethodDelegate<Func<SaveManager, StoryEventData, bool>>(AccessTools.Method(typeof(SaveManager), "IsEventQueued", (Type[])null, (Type[])null), (object)null, true);

			private static int? originalTickets;

			private static void Prefix(SaveManager __instance)
			{
				AllGameData allGameData = __instance.GetAllGameData();
				StoryEventData val = allGameData.FindStoryEventDataByName("CorruptedAngels");
				StoryEventData val2 = allGameData.FindStoryEventDataByName("ClippedWings");
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
				{
					int valueOrDefault = originalTickets.GetValueOrDefault();
					if (!originalTickets.HasValue)
					{
						valueOrDefault = TicketsRef.Invoke(val);
						originalTickets = valueOrDefault;
					}
					TicketsRef.Invoke(val) = (IsEventQueued(__instance, val2) ? ArchusTicketCount.Value : originalTickets.Value);
				}
			}
		}

		private const string Knot = "CorruptedAngels";

		private const string WingUpgrade = "ClippedWingsWing2RewardUpgrade";

		private const string GiveWingCommand = "EVENTTWEAKS_GIVE_ARCHUS_WING";

		private const string PurifyChoice = "\"^Purify\",\"/str\",{\"x()\":\"GetTrainHealth\"},10,\">\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":5},";

		private const string GiveWingChoice = "\"ev\",\"str\",\"^EventTweaksGiveWing\",\"/str\",\"/ev\",{\"*\":\".^.c-2\",\"flg\":4},";

		private const string GiveWingBranch = "\"c-2\":[\"\\n\",\"^>>>EVENTTWEAKS_GIVE_ARCHUS_WING\",\"\\n\",{\"->\":\"CorruptedAngels.EventTweaksWing1\"},{\"#f\":7}],";

		private const string BranchEnd = "\"g-0\":[\"end\",{\"#f\":7}]}],{";

		private const string StitchAnchor = "\"g-0\":[\"end\",{\"#f\":7}]}],{\"round2\":";

		private static readonly string Rolls = string.Concat(Enumerable.Range(1, 4).Select(delegate(int k)
		{
			string text = "CorruptedAngels." + ((k < 4) ? $"EventTweaksWing{k + 1}" : "EventTweaksRestored");
			return string.Format("\"EventTweaksWing{0}\":[\"ev\",1,2,\"rnd\",1,\"==\",\"/ev\",{{\"->\":\"{1}.EventTweaksWing{2}.win\",\"c\":true}},", k, "CorruptedAngels", k) + $"{{\"->\":\"{text}\"}},{{\"win\":[\"^>>>GIVE_REWARD: CorruptedAngelRareRelicReward{k}\",\"\\n\"," + "{\"->\":\"" + text + "\"},null],\"#f\":3}],";
		}));

		private const string FelFarewell = "\"^<style=\\\"Body\\\"><darkangel>\\\"Now, Seraph will learn to kneel before me. Heaven's throne will be mine...\\\"</darkangel>\",\"\\n\",\"^With an eruption of shadow, <darkangel><shake>ARCHUS</darkangel></shake> disappears into the darkness. </style>\",\"\\n\",";

		private const string Declined = "\"^<style=\\\"Body\\\">You decline the artifact. Before you have a chance to look up, you see <darkangel><shake>ARCHUS</darkangel></shake> trudging down the path alone.</style>\",\"\\n\",";

		private const string Restored = "\"EventTweaksRestored\":[\"^EventTweaks_ArchusRestored\",\"\\n\",\"ev\",\"str\",\"^GainArtifact\",\"/str\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":4},\"ev\",\"str\",\"^Leave\",\"/str\",\"/ev\",{\"*\":\".^.c-1\",\"flg\":4},{\"c-0\":[\"\\n\",\"^>>>GIVE_REWARD: CorruptedAngelRelicReward\",\"\\n\",\"^<style=\\\"Body\\\"><darkangel>\\\"Now, Seraph will learn to kneel before me. Heaven's throne will be mine...\\\"</darkangel>\",\"\\n\",\"^With an eruption of shadow, <darkangel><shake>ARCHUS</darkangel></shake> disappears into the darkness. </style>\",\"\\n\",\"end\",{\"#f\":7}],\"c-1\":[\"\\n\",\"^<style=\\\"Body\\\">You decline the artifact. Before you have a chance to look up, you see <darkangel><shake>ARCHUS</darkangel></shake> trudging down the path alone.</style>\",\"\\n\",\"end\",{\"#f\":7}],\"#f\":3}],";

		internal static ConfigEntry<int> ArchusTicketCount = null;

		public static void Init(ConfigFile config)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			ArchusTicketCount = config.Bind<int>("Archus Wing", "ArchusTicketCount", 20, new ConfigDescription("Lottery tickets of the Archus event once Clipped Wings is on an earlier ring of the map; otherwise it keeps the game's 10. Events at a distance are drawn in proportion to their tickets.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1000), Array.Empty<object>()));
		}

		private static CardState? WingBearer(SaveManager saveManager)
		{
			return saveManager.GetDeckState().Find((CardState card) => card.GetCardStateModifiers().GetCardUpgrades().Exists((CardUpgradeState u) => u.GetAssetName() == "ClippedWingsWing2RewardUpgrade"));
		}
	}
	internal static class HephsBlueprints
	{
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static class RewriteKnot
		{
			private static void Prefix(ref string __0)
			{
				if (__0.Contains("\"TrainRoomInstall\":"))
				{
					if (!__0.Contains("\"TrainRoomInstall\":[[\"ev\",2,{\"f()\":\"GetTrainRoomInstallList\"}") || !__0.Contains("{\"HephOffer\":") || FloorDivert.Matches(__0).Count != 3)
					{
						Plugin.Logger.LogWarning((object)"TrainRoomInstall changed in the Ink story; Heph's Blueprints left as is.");
					}
					else
					{
						__0 = FloorDivert.Replace(__0.Replace("\"TrainRoomInstall\":[[\"ev\",2,{\"f()\":\"GetTrainRoomInstallList\"}", "\"TrainRoomInstall\":[[\"ev\",{\"x()\":\"EventTweaks_TrainRoomCount\"},{\"f()\":\"GetTrainRoomInstallList\"}").Replace("{\"HephOffer\":", "{\"EventTweaksHammer\":[[\"ev\",{\"x()\":\"EventTweaks_IsHeph\"},\"/ev\",{\"->\":\".^.b\",\"c\":true},{\"b\":[{\"->\":\"TrainRoomInstall.EventTweaksHammerOffer\"},null]}],{\"->\":\"TrainRoomInstall.ending\"},{\"#f\":3}],\"EventTweaksHammerOffer\":[\"^EventTweaks_HephHammerOffer\",\"\\n\",\"ev\",\"str\",\"^EventTweaksTakeHammer\",\"/str\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":4},\"ev\",\"str\",\"^EventTweaksNoHammer\",\"/str\",\"/ev\",{\"*\":\".^.c-1\",\"flg\":4},{\"c-0\":[\"\\n\",{\"->\":\"TrainRoomInstall.ending\"},{\"#f\":7}],\"c-1\":[\"\\n\",{\"->\":\"TrainRoomInstall.ending\"},{\"#f\":7}],\"#f\":3}],\"HephOffer\":"), "$1{\"->\":\"TrainRoomInstall.EventTweaksHammer\"}");
					}
				}
			}
		}

		[HarmonyPatch(typeof(InkWriter), "BeginStory")]
		private static class BindFunctions
		{
			private static void Prefix(Story ____currentStory, SaveManager ____saveManager)
			{
				if (!((Object)(object)____currentStory == (Object)null))
				{
					____currentStory.TryUnbindExternalFunction("EventTweaks_TrainRoomCount");
					____currentStory.BindExternalFunction("EventTweaks_TrainRoomCount", (Func<object>)(() => IsHeph(____saveManager) ? 3 : 2));
					____currentStory.TryUnbindExternalFunction("EventTweaks_IsHeph");
					____currentStory.BindExternalFunction("EventTweaks_IsHeph", (Func<object>)(() => IsHeph(____saveManager)));
				}
			}
		}

		[HarmonyPatch(typeof(InkWriter), "GetStoryChoiceData")]
		private static class DropHammerChoice
		{
			private static void Prefix(List<Choice> choices, StoryEventData ____currentStoryEventData, SaveManager ____saveManager)
			{
				if (((____currentStoryEventData != null) ? ____currentStoryEventData.KnotName : null) == "TrainRoomInstall" && IsHeph(____saveManager) && choices.Exists((Choice c) => c.text.EndsWith("Floor")))
				{
					choices.RemoveAll((Choice c) => c.text == "TakeHammer");
				}
			}
		}

		[HarmonyPatch(typeof(SaveManager), "GenerateStoryEventsAtDistance")]
		private static class HephTickets
		{
			private static readonly FieldRef<StoryEventData, int> TicketsRef = AccessTools.FieldRefAccess<StoryEventData, int>("priorityTicketCount");

			private static int? originalTickets;

			private static void Prefix(SaveManager __instance)
			{
				StoryEventData val = __instance.GetAllGameData().FindStoryEventDataByName("TrainRoomInstall");
				if (!((Object)(object)val == (Object)null))
				{
					int valueOrDefault = originalTickets.GetValueOrDefault();
					if (!originalTickets.HasValue)
					{
						valueOrDefault = TicketsRef.Invoke(val);
						originalTickets = valueOrDefault;
					}
					TicketsRef.Invoke(val) = (IsHeph(__instance) ? HephTicketCount.Value : originalTickets.Value);
				}
			}
		}

		[HarmonyPatch(typeof(LocalizationExtensions), "LocalizeInk")]
		private static class TermLines
		{
			private static bool Prefix(string englishText, ILocalizationParameterContext context, ref string __result)
			{
				if (englishText == null || !englishText.StartsWith("EventTweaks_"))
				{
					return true;
				}
				__result = LocalizationExtensions.Localize(englishText, context);
				return false;
			}
		}

		private const string Knot = "TrainRoomInstall";

		private const string CountFunction = "EventTweaks_TrainRoomCount";

		private const string IsHephFunction = "EventTweaks_IsHeph";

		private const string TermPrefix = "EventTweaks_";

		private const string InkOffer = "\"TrainRoomInstall\":[[\"ev\",2,{\"f()\":\"GetTrainRoomInstallList\"}";

		private const string PatchedOffer = "\"TrainRoomInstall\":[[\"ev\",{\"x()\":\"EventTweaks_TrainRoomCount\"},{\"f()\":\"GetTrainRoomInstallList\"}";

		private static readonly Regex FloorDivert = new Regex("(>>>GIVE_REWARD: TrainRoomInstallFloor\\dRelicReward\",\"\\\\n\",)\\{\"->\":\"\\.\\^\\.\\^\\.\\^\\.\\^\\.\\^\\.ending\"\\}");

		private const string HammerDivert = "{\"->\":\"TrainRoomInstall.EventTweaksHammer\"}";

		private const string StitchAnchor = "{\"HephOffer\":";

		private const string HammerStitches = "{\"EventTweaksHammer\":[[\"ev\",{\"x()\":\"EventTweaks_IsHeph\"},\"/ev\",{\"->\":\".^.b\",\"c\":true},{\"b\":[{\"->\":\"TrainRoomInstall.EventTweaksHammerOffer\"},null]}],{\"->\":\"TrainRoomInstall.ending\"},{\"#f\":3}],\"EventTweaksHammerOffer\":[\"^EventTweaks_HephHammerOffer\",\"\\n\",\"ev\",\"str\",\"^EventTweaksTakeHammer\",\"/str\",\"/ev\",{\"*\":\".^.c-0\",\"flg\":4},\"ev\",\"str\",\"^EventTweaksNoHammer\",\"/str\",\"/ev\",{\"*\":\".^.c-1\",\"flg\":4},{\"c-0\":[\"\\n\",{\"->\":\"TrainRoomInstall.ending\"},{\"#f\":7}],\"c-1\":[\"\\n\",{\"->\":\"TrainRoomInstall.ending\"},{\"#f\":7}],\"#f\":3}],\"HephOffer\":";

		internal static ConfigEntry<int> HephTicketCount = null;

		public static void Init(ConfigFile config)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			HephTicketCount = config.Bind<int>("Hephs Blueprints", "HephTicketCount", 25, new ConfigDescription("Lottery tickets of the Trainyard event in Heph runs; other runs keep the game's 10. Events at a distance are drawn in proportion to their tickets.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1000), Array.Empty<object>()));
		}

		private static bool IsHeph(SaveManager saveManager)
		{
			ChampionData mainChampionData = saveManager.GetMainChampionData();
			object obj;
			if (mainChampionData == null)
			{
				obj = null;
			}
			else
			{
				CardData championCardData = mainChampionData.championCardData;
				obj = ((championCardData != null) ? championCardData.GetName() : null);
			}
			return (string?)obj == "Heph";
		}
	}
}