Decompiled source of MoonRoutePriceMod v1.1.5

MoonRoutePriceMod.dll

Decompiled a year ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using MoonRoutePriceMod.Patches;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("MoonRoutePriceMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MoonRoutePriceMod")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7d247e85-ff55-474f-8220-b0f8f5d29692")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.1", FrameworkDisplayName = ".NET Framework 4.7.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MoonRoutePriceMod
{
	[BepInPlugin("ImoutoSama.MoonRoutePriceMod", "Moon Route Price Mod", "1.1.5")]
	[BepInProcess("Lethal Company.exe")]
	public class MoonRoutePricePlugin : BaseUnityPlugin
	{
		public class MoonData
		{
			public int moonCataIndex;

			public TerminalNode priceA;

			public TerminalNode priceB;

			public int basePrice;

			public int finalMultiplier;

			public bool displayMultiplier;
		}

		private const string modGUID = "ImoutoSama.MoonRoutePriceMod";

		private const string modName = "Moon Route Price Mod";

		private const string modVersion = "1.1.5";

		private readonly Harmony harmony = new Harmony("ImoutoSama.MoonRoutePriceMod");

		internal ManualLogSource logger;

		public ConfigEntry<int> configMulti;

		public ConfigEntry<int> configMinPre;

		public ConfigEntry<int> configMinPost;

		public ConfigEntry<int> configRound;

		public ConfigEntry<bool> configMultiType;

		public ConfigEntry<int> configFoggy;

		public ConfigEntry<int> configRainy;

		public ConfigEntry<int> configStormy;

		public ConfigEntry<int> configFlooded;

		public ConfigEntry<int> configEclipsed;

		public Terminal terminal;

		public MoonData[] moonData;

		public Dictionary<TerminalNode, MoonData> nodeToMoonData;

		public static MoonRoutePricePlugin Instance { get; private set; }

		private void Awake()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			ConfigDefinition val = new ConfigDefinition("General", "Discount");
			ConfigEntry<int> val2 = default(ConfigEntry<int>);
			int defaultValue = (((BaseUnityPlugin)this).Config.TryGetEntry<int>(val, ref val2) ? (100 - val2.Value) : 50);
			((BaseUnityPlugin)this).Config.Remove(val);
			configMulti = ConfigBindClamp("General", "Multiplier", defaultValue, "The price modifier % for all moon routes. Clamped from 0% to 400%. 0% = Free. 100% = No effect", 0, 400);
			configMinPre = ConfigBindClamp("General", "Minimum Cost (Pre)", 0, "The lowest price for all moon routes (before calcuations). Clamped from 0 to 1000. 0 = No effect", 0, 1000);
			configMinPost = ConfigBindClamp("General", "Minimum Cost (Post)", 0, "The lowest price for all moon routes (after calcuations). Clamped from 0 to 1000. 0 = No effect", 0, 1000);
			configRound = ConfigBindClamp("General", "Round Figure", 10, "The modified price is rounded to a multiple of this number. Clamped from 1 to 100. 1 = No rounding", 1, 100);
			configFoggy = ConfigBindClamp("Weather", "Foggy Multiplier", 100, "The price modifier % for all moon routes with foggy weather. Clamped from 0% to 400%. 0% = Free. 100% = No effect", 0, 400);
			configRainy = ConfigBindClamp("Weather", "Rainy Multiplier", 100, "The price modifier % for all moon routes with rainy weather. Clamped from 0% to 400%. 0% = Free. 100% = No effect", 0, 400);
			configStormy = ConfigBindClamp("Weather", "Stormy Multiplier", 100, "The price modifier % for all moon routes with stormy weather. Clamped from 0% to 400%. 0% = Free. 100% = No effect", 0, 400);
			configFlooded = ConfigBindClamp("Weather", "Flooded Multiplier", 100, "The price modifier % for all moon routes with flooded weather. Clamped from 0% to 400%. 0% = Free. 100% = No effect", 0, 400);
			configEclipsed = ConfigBindClamp("Weather", "Eclipsed Multiplier", 100, "The price modifier % for all moon routes with eclipsed weather. Clamped from 0% to 400%. 0% = Free. 100% = No effect", 0, 400);
			logger = Logger.CreateLogSource("ImoutoSama.MoonRoutePriceMod");
			logger.LogInfo((object)"Plugin Moon Route Price Mod has been added!");
			harmony.PatchAll(typeof(MoonRoutePricePlugin));
			harmony.PatchAll(typeof(TerminalPatch));
			harmony.PatchAll(typeof(StartOfRoundPatch));
		}

		public ConfigEntry<int> ConfigBindClamp(string section, string key, int defaultValue, string description, int min, int max)
		{
			ConfigEntry<int> val = ((BaseUnityPlugin)this).Config.Bind<int>(section, key, defaultValue, description);
			val.Value = Mathf.Clamp(val.Value, min, max);
			return val;
		}

		public int GetWeatherMulti(LevelWeatherType weather)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected I4, but got Unknown
			return (weather - -1) switch
			{
				4 => configFoggy.Value, 
				2 => configRainy.Value, 
				3 => configStormy.Value, 
				5 => configFlooded.Value, 
				6 => configEclipsed.Value, 
				_ => 100, 
			};
		}

		public static string GetFullMultiString(int multiplier)
		{
			if (multiplier == 100)
			{
				return string.Empty;
			}
			if (multiplier < 100)
			{
				return $"{100 - multiplier}% discount";
			}
			return $"{multiplier - 100}% markup";
		}

		public static string GetShortMultiString(int multiplier)
		{
			if (multiplier == 100)
			{
				return string.Empty;
			}
			if (multiplier < 100)
			{
				return $" ({multiplier - 100}%)";
			}
			return $" (+{multiplier - 100}%)";
		}
	}
}
namespace MoonRoutePriceMod.Patches
{
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("SetPlanetsWeather")]
		[HarmonyPostfix]
		private static void SetPlanetsWeatherPatch()
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			MoonRoutePricePlugin.MoonData[] moonData = MoonRoutePricePlugin.Instance.moonData;
			if (moonData == null)
			{
				MoonRoutePricePlugin.Instance.logger.LogError((object)"Start of round started before the Terminal was found. BIG BUG");
				return;
			}
			SelectableLevel[] moonsCatalogueList = MoonRoutePricePlugin.Instance.terminal.moonsCatalogueList;
			for (int i = 0; i < moonData.Length; i++)
			{
				MoonRoutePricePlugin.MoonData moonData2 = moonData[i];
				SelectableLevel val = moonsCatalogueList[i];
				int value = MoonRoutePricePlugin.Instance.configMinPre.Value;
				float num = Mathf.Max(moonData2.basePrice, value);
				float num2 = (float)MoonRoutePricePlugin.Instance.configMulti.Value * 0.01f * ((float)MoonRoutePricePlugin.Instance.GetWeatherMulti(val.currentWeather) * 0.01f);
				num *= num2;
				num = Mathf.Max(num, 0f);
				int value2 = MoonRoutePricePlugin.Instance.configRound.Value;
				int num3 = Mathf.RoundToInt(num * (1f / (float)value2)) * value2;
				int value3 = MoonRoutePricePlugin.Instance.configMinPost.Value;
				num3 = Mathf.Max(num3, value3);
				moonData2.priceA.itemCost = num3;
				moonData2.priceB.itemCost = num3;
				moonData2.finalMultiplier = Mathf.RoundToInt(num2 * 100f);
				MoonRoutePricePlugin.Instance.logger.LogInfo((object)$"{val.PlanetName}: {moonData2.finalMultiplier}%");
			}
			MoonRoutePricePlugin.Instance.logger.LogInfo((object)"New route prices have been set");
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	internal class TerminalPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		private static void AwakePatch(ref Terminal __instance)
		{
			MoonRoutePricePlugin.Instance.logger.LogInfo((object)"Recording newest terminal instance");
			MoonRoutePricePlugin.Instance.terminal = __instance;
			if (MoonRoutePricePlugin.Instance.moonData != null)
			{
				return;
			}
			TerminalKeyword[] allKeywords = __instance.terminalNodes.allKeywords;
			TerminalKeyword val = ((IEnumerable<TerminalKeyword>)allKeywords).FirstOrDefault((Func<TerminalKeyword, bool>)((TerminalKeyword a) => a.word == "route"));
			TerminalKeyword val2 = ((IEnumerable<TerminalKeyword>)allKeywords).FirstOrDefault((Func<TerminalKeyword, bool>)((TerminalKeyword a) => a.word == "moons"));
			if ((Object)(object)val == (Object)null)
			{
				MoonRoutePricePlugin.Instance.logger.LogError((object)"Could not find route terminal entry. New patch?");
				return;
			}
			if ((Object)(object)val2 == (Object)null)
			{
				MoonRoutePricePlugin.Instance.logger.LogError((object)"Could not find moons terminal entry. New patch?");
				return;
			}
			CompatibleNoun[] compatibleNouns = val.compatibleNouns;
			if (compatibleNouns == null)
			{
				MoonRoutePricePlugin.Instance.logger.LogError((object)"Route terminal entry has no nouns. New patch?");
				return;
			}
			bool flag = MoonRoutePricePlugin.Instance.configMinPre.Value > 0;
			SelectableLevel[] moonsCatalogueList = __instance.moonsCatalogueList;
			MoonRoutePricePlugin.MoonData[] array = new MoonRoutePricePlugin.MoonData[moonsCatalogueList.Length];
			Dictionary<TerminalNode, MoonRoutePricePlugin.MoonData> dictionary = new Dictionary<TerminalNode, MoonRoutePricePlugin.MoonData>();
			CompatibleNoun[] array2 = compatibleNouns;
			foreach (CompatibleNoun val3 in array2)
			{
				MoonRoutePricePlugin.MoonData moonData = new MoonRoutePricePlugin.MoonData();
				TerminalNode val4 = (moonData.priceA = val3.result);
				moonData.priceB = val4.terminalOptions[1].result;
				moonData.basePrice = val4.itemCost;
				moonData.finalMultiplier = 100;
				moonData.displayMultiplier = moonData.basePrice > 0 || flag;
				moonData.moonCataIndex = -1;
				string text = val3.noun.word.ToLowerInvariant();
				for (int j = 0; j < moonsCatalogueList.Length; j++)
				{
					string text2 = moonsCatalogueList[j].PlanetName.ToLowerInvariant();
					if (text2.Contains(text))
					{
						moonData.moonCataIndex = j;
						break;
					}
				}
				if (moonData.moonCataIndex == -1)
				{
					if (text == "company")
					{
						MoonRoutePricePlugin.Instance.logger.LogInfo((object)("Skipping " + text));
					}
					else
					{
						MoonRoutePricePlugin.Instance.logger.LogError((object)(text + " has no moon matching in the catalogue. New patch?"));
					}
				}
				else if (array[moonData.moonCataIndex] != null)
				{
					MoonRoutePricePlugin.Instance.logger.LogWarning((object)(text + " is a duplicate in the route keywords. This is a current bug for dine in the base game"));
				}
				else
				{
					string displayText = moonData.priceA.displayText;
					displayText = displayText.Replace("[totalCost]", "[totalCost][finalMulti]");
					moonData.priceA.displayText = displayText;
					array[moonData.moonCataIndex] = moonData;
					dictionary.Add(moonData.priceA, moonData);
				}
			}
			MoonRoutePricePlugin.Instance.moonData = array;
			MoonRoutePricePlugin.Instance.nodeToMoonData = dictionary;
			MoonRoutePricePlugin.Instance.logger.LogInfo((object)"Attempting to add text to Moons keyword text");
			TerminalNode specialKeywordResult = val2.specialKeywordResult;
			string displayText2 = specialKeywordResult.displayText;
			int startIndex = displayText2.IndexOf("_______");
			displayText2 = displayText2.Insert(startIndex, "[routeMulti]");
			displayText2 = displayText2.Replace("[planetTime]", "[planetTime][weatherMulti]");
			specialKeywordResult.displayText = displayText2;
			MoonRoutePricePlugin.Instance.logger.LogInfo((object)"Finished the Terminal patch");
		}

		[HarmonyPatch("TextPostProcess")]
		[HarmonyPostfix]
		private static void TextPostProgressPostPatch(ref Terminal __instance, ref string __result, TerminalNode node)
		{
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: 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)
			int value = MoonRoutePricePlugin.Instance.configMulti.Value;
			__result = __result.Replace("[routeMulti]", (value != 100) ? ("All moon routes at " + MoonRoutePricePlugin.GetFullMultiString(value) + "!\n") : string.Empty);
			int num = __result.Split(new string[1] { "[weatherMulti]" }, StringSplitOptions.None).Length - 1;
			if (num > 0)
			{
				Regex regex = new Regex(Regex.Escape("[weatherMulti]"));
				MoonRoutePricePlugin.MoonData[] moonData = MoonRoutePricePlugin.Instance.moonData;
				SelectableLevel[] moonsCatalogueList = __instance.moonsCatalogueList;
				for (int i = 0; i < num && moonsCatalogueList.Length > i; i++)
				{
					MoonRoutePricePlugin.MoonData moonData2 = moonData[i];
					string replacement;
					if (moonData2.displayMultiplier)
					{
						LevelWeatherType currentWeather = moonsCatalogueList[i].currentWeather;
						int weatherMulti = MoonRoutePricePlugin.Instance.GetWeatherMulti(currentWeather);
						replacement = MoonRoutePricePlugin.GetShortMultiString(weatherMulti);
					}
					else
					{
						replacement = string.Empty;
					}
					__result = regex.Replace(__result, replacement, 1);
				}
			}
			if (MoonRoutePricePlugin.Instance.nodeToMoonData.TryGetValue(node, out var value2))
			{
				int finalMultiplier = value2.finalMultiplier;
				string newValue = ((finalMultiplier == 100 || !value2.displayMultiplier) ? string.Empty : (" (" + MoonRoutePricePlugin.GetFullMultiString(finalMultiplier) + ")"));
				__result = __result.Replace("[finalMulti]", newValue);
			}
		}
	}
}