Decompiled source of DiscountMoonsMod v1.0.6

BepInEx/plugins/DiscountMoonsMod.dll

Decompiled 8 months 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 BepInEx;
using DiscountPlanetsMod.Patches;
using HarmonyLib;
using LethalLevelLoader;
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("DiscountPlanetsMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DiscountPlanetsMod")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("f0086914-f753-49bf-b223-381b8de9bfa6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DiscountPlanetsMod
{
	[BepInPlugin("VewaveCorp.DiscountMoonsMod", "Discount Moons Mod 1.0.0.1", "1.0.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class DiscountMoonModBase : BaseUnityPlugin
	{
		private const string modGUID = "VewaveCorp.DiscountMoonsMod";

		private const string modName = "Discount Moons Mod 1.0.0.1";

		private const string modVersion = "1.0.0.1";

		private readonly Harmony harmony = new Harmony("VewaveCorp.DiscountMoonsMod");

		private static DiscountMoonModBase instance;

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			harmony.PatchAll(typeof(DiscountMoonModBase));
			harmony.PatchAll(typeof(TerminalPatch));
		}
	}
}
namespace DiscountPlanetsMod.Patches
{
	[HarmonyPatch(typeof(TerminalManager))]
	internal class TerminalPatch
	{
		public class MoonPrice
		{
			public int moonCostPercentage = 100;

			public int baseCost;

			public ExtendedLevel extendedLevel;

			public TerminalNode routeNode;

			public TerminalNode confirmNode;

			public MoonPrice(ExtendedLevel extendedLevel)
			{
				this.extendedLevel = extendedLevel;
				baseCost = extendedLevel.RoutePrice;
				routeNode = extendedLevel.routeNode;
				confirmNode = extendedLevel.routeConfirmNode;
			}

			public void ResetPrice()
			{
				moonCostPercentage = 100;
				extendedLevel.RoutePrice = baseCost;
				routeNode.itemCost = baseCost;
				confirmNode.itemCost = baseCost;
			}

			public void SetRouteNode(TerminalNode routeNode)
			{
				this.routeNode = routeNode;
				confirmNode = routeNode.terminalOptions[1].result;
				this.routeNode.itemCost = GetCost();
				confirmNode.itemCost = GetCost();
			}

			public bool ShouldHaveMoonSale()
			{
				return baseCost > 0;
			}

			public int GetCost()
			{
				return moonCostPercentage * baseCost / 100;
			}

			public void RandomizeMoonCostPercentage(Random random)
			{
				if (ShouldHaveMoonSale())
				{
					moonCostPercentage = (10 - random.Next(5, 11)) * 10;
					int cost = GetCost();
					extendedLevel.RoutePrice = cost;
					routeNode.itemCost = cost;
					confirmNode.itemCost = cost;
				}
			}
		}

		private const int probabilityOfSale = 50;

		private const int minSalePercentageTens = 5;

		private const int maxSalePercentageTens = 10;

		private const int maxNumberOfSales = 3;

		private const string planetTimePlaceholder = "[planetTime]";

		private const string discountPlaceholder = "[discount]";

		private const string companyBuyingPercentPlaceholder = "[companyBuyingPercent]";

		private static Dictionary<int, MoonPrice> mayHaveSaleMoons;

		[HarmonyPatch("CreateExtendedLevelGroups")]
		[HarmonyPostfix]
		private static void InitializeMoonSales()
		{
			bool flag = mayHaveSaleMoons == null;
			if (!flag)
			{
				CollectionExtensions.Do<MoonPrice>((IEnumerable<MoonPrice>)mayHaveSaleMoons.Values, (Action<MoonPrice>)delegate(MoonPrice moon)
				{
					moon.ResetPrice();
				});
			}
			mayHaveSaleMoons = new Dictionary<int, MoonPrice>();
			foreach (ExtendedLevel extendedLevel in PatchedContent.ExtendedLevels)
			{
				if (extendedLevel.RoutePrice > 0)
				{
					Debug.Log((object)$"   Adding {((Object)extendedLevel).name}, {extendedLevel.selectableLevel.levelID}, {((Object)extendedLevel.routeNode).name}, {extendedLevel.RoutePrice}");
					mayHaveSaleMoons[extendedLevel.selectableLevel.levelID] = new MoonPrice(extendedLevel);
				}
			}
			if (!flag)
			{
				SetTerminalNodes();
			}
			SetMoonSales();
		}

		private static void SetTerminalNodes()
		{
			Terminal val = Object.FindObjectOfType<Terminal>();
			TerminalKeyword val2 = ((IEnumerable<TerminalKeyword>)val.terminalNodes.allKeywords).FirstOrDefault((Func<TerminalKeyword, bool>)((TerminalKeyword word) => word.word == "route"));
			CompatibleNoun[] compatibleNouns = val2.compatibleNouns;
			CompatibleNoun[] array = compatibleNouns;
			foreach (CompatibleNoun val3 in array)
			{
				TerminalNode result = val3.result;
				if (mayHaveSaleMoons.ContainsKey(result.displayPlanetInfo))
				{
					mayHaveSaleMoons[result.displayPlanetInfo].SetRouteNode(result);
				}
			}
		}

		[HarmonyPatch(typeof(Terminal), "SetItemSales")]
		[HarmonyPostfix]
		private static void SetMoonSales()
		{
			if (mayHaveSaleMoons == null)
			{
				return;
			}
			Debug.Log((object)$"    Resetting sales with {50}% of sale...");
			CollectionExtensions.Do<MoonPrice>((IEnumerable<MoonPrice>)mayHaveSaleMoons.Values, (Action<MoonPrice>)delegate(MoonPrice moon)
			{
				moon.ResetPrice();
			});
			MoonPrice[] array = mayHaveSaleMoons.Values.ToArray();
			Random random = new Random(StartOfRound.Instance.randomMapSeed + 91);
			array[random.Next(0, array.Length)].RandomizeMoonCostPercentage(random);
			for (int i = 0; i < 3; i++)
			{
				if (random.Next(0, 100) >= 50)
				{
					break;
				}
				int num = random.Next(0, array.Length);
				array[num].RandomizeMoonCostPercentage(random);
				Debug.Log((object)$"    Putting {((Object)array[num].extendedLevel.routeNode).name} on {100 - array[num].moonCostPercentage} sale!");
			}
		}

		[HarmonyPatch("GetExtendedLevelPreviewInfo")]
		[HarmonyPostfix]
		private static void GetExtendedLevelPreviewInfo(ref string __result, object[] __args)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			if (__args.Length < 1 || __args[0] == null)
			{
				return;
			}
			ExtendedLevel val = (ExtendedLevel)__args[0];
			int levelID = val.selectableLevel.levelID;
			if (mayHaveSaleMoons.ContainsKey(levelID))
			{
				MoonPrice moonPrice = mayHaveSaleMoons[levelID];
				if (moonPrice.moonCostPercentage < 100)
				{
					Debug.Log((object)$"  {((Object)val).name} has a sale: {moonPrice.baseCost}, {100 - moonPrice.moonCostPercentage}% OFF = {val.RoutePrice}");
					__result += $"   ({100 - moonPrice.moonCostPercentage}% OFF!)";
				}
			}
		}
	}
}