Decompiled source of ZetasMoonDiscounts v1.0.0

ZetasMoonDiscounts.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LethalLevelLoader;
using UnityEngine;
using WeatherRegistry;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ZetasMoonDiscounts")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ZetasMoonDiscounts")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("051aec3d-698c-48a7-b7ba-2445de4e8117")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ZetasMoonDiscounts;

[BepInPlugin("ZetaArcade.ZetasMoonDiscounts", "ZetasMoonDiscounts", "1.0.0")]
public class ZetasMoonDiscountsBase : BaseUnityPlugin
{
	internal class LevelPairedWithPrice
	{
		public ExtendedLevel Level;

		public int OriginalPrice;
	}

	[HarmonyPatch(typeof(Terminal), "Start")]
	internal class TerminalPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void TerminalStartPatch(ref Terminal __instance)
		{
			Logger.LogDebug((object)"Terminal patch detected");
			Instance.SaveDefaultMoonPrices();
			Instance.CalculateMoonPrices();
		}

		[HarmonyPatch("BeginUsingTerminal")]
		[HarmonyPostfix]
		private static void TerminalBeginUsePatch(ref Terminal __instance)
		{
			Logger.LogDebug((object)"Terminal begin use patch detected");
			Instance.CalculateMoonPrices();
		}
	}

	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("PassTimeToNextDay")]
		[HarmonyPostfix]
		private static void PassTimeToNextDay()
		{
			Logger.LogDebug((object)"PassTimeToNextDay patch detected");
			Instance.CalculateMoonPrices();
		}

		[HarmonyPatch("ArriveAtLevel")]
		[HarmonyPostfix]
		private static void ArriveAtLevelPatch()
		{
			Logger.LogDebug((object)"ArriveAtLevel patch detected");
			Instance.CalculateMoonPrices();
		}
	}

	[HarmonyPatch(typeof(GameNetworkManager))]
	internal class GameNetworkManagerPatch
	{
		[HarmonyPatch("SaveGame")]
		[HarmonyPostfix]
		private static void SaveGameValuesPatch()
		{
			Instance.CalculateMoonPrices();
		}

		[HarmonyPatch("ResetSavedGameValues")]
		[HarmonyPostfix]
		private static void ResetSavedGameValuesPatch()
		{
			Instance.CalculateMoonPrices();
		}
	}

	[HarmonyPatch(typeof(TimeOfDay))]
	internal class TimeOfDayPatch
	{
		[HarmonyPatch("SetNewProfitQuota")]
		[HarmonyPrefix]
		private static void SetNewProfitQuotaPatch()
		{
			Instance.CalculateMoonPrices();
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetBuyingRateForDay")]
		private static void setBuyingRateForDayPatch()
		{
			Instance.ModifyBuyingRate();
		}

		[HarmonyPostfix]
		[HarmonyPatch("SetNewProfitQuota")]
		private static void setNewProfitQuotaPatch()
		{
			Instance.ModifyBuyingRate();
		}
	}

	private const string modGUID = "ZetaArcade.ZetasMoonDiscounts";

	private const string modName = "ZetasMoonDiscounts";

	private const string modVersion = "1.0.0";

	private readonly Harmony harmony = new Harmony("ZetaArcade.ZetasMoonDiscounts");

	public static ManualLogSource Logger;

	public static ZetasMoonDiscountsBase Instance;

	public ConfigEntry<bool> ApplyBuyingChanges;

	public ConfigEntry<float> NormalSellRate;

	public ConfigEntry<float> ZeroDaysSelLRate;

	public ConfigEntry<int> FreeThreshold;

	public ConfigEntry<int> FreeThresholdCap;

	public ConfigEntry<int> DowngradeCost;

	public ConfigEntry<int> UpgradeThreshold;

	public ConfigEntry<string> IronmanMoons;

	public ConfigEntry<bool> IronmanMode;

	private List<LevelPairedWithPrice> MoonDefaultCosts = new List<LevelPairedWithPrice>();

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		ApplyBuyingChanges = ((BaseUnityPlugin)this).Config.Bind<bool>("Buying Rate Changes", "Toggle Buying Rate Changes", true, "If true, the below 2 configs will be applied to the Company Buying Rate (How many credits you get for selling things)");
		NormalSellRate = ((BaseUnityPlugin)this).Config.Bind<float>("Buying Rate Changes", "Normal Selling Rate", 1f, "The selling rate at the company on all days except the final day, where 1f = 100%, 0.5f = 50% etc.");
		ZeroDaysSelLRate = ((BaseUnityPlugin)this).Config.Bind<float>("Buying Rate Changes", "Zero Days Selling Rate", 1f, "The selling rate at the company on the final day, where 1f = 100%, 0.5f = 50% etc.");
		FreeThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Discount Settings", "Free Threshold", 300, "When at Moon A, if Moon B is this amount cheaper (or even cheaper!) it becomes free to route to. E.g. with the default value of 300, if Moon A costs 800 and you route to it, then any moons that cost 500 or less are free to route to.");
		FreeThresholdCap = ((BaseUnityPlugin)this).Config.Bind<int>("Discount Settings", "Free Threshold Cap", 1000, "A Cap for the above config, where if a moon costs this amount or more, then other moons cannot route down to it for free. Note moons that are $3500 cheaper than the current moon or more are set to free anyways, bypassing this config");
		DowngradeCost = ((BaseUnityPlugin)this).Config.Bind<int>("Discount Settings", "Downgrade Cost", 50, "When at Moon A, if Moon B is cheaper but isn't cheap enough to become free with the above config (or costs the same amount as Moon A), then it will be this route price instead.");
		UpgradeThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Discount Settings", "Upgrade Threshold", 500, "When at Moon A, if Moon B is more expensive, then if it's within the range of this config then you can route 'up' to it, only paying the difference in cost to route. E.g. (with default config at 500) if Moon A costs 400, Moon B costs 800, and Moon C costs 1200, then you can route from Moon A to B for 400 (Since its less than 500 in difference), but Moon C will cost the full 1200.");
		IronmanMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Ironman Settings", "Ironman Mode", false, "When true, will just use the Ironman section of the config instead for moon prices. See mod desc. for how Ironman mode works.");
		IronmanMoons = ((BaseUnityPlugin)this).Config.Bind<string>("Ironman Settings", "Ironman Moon List", "", "List of groups of moons, starting with a price for that group of moons, in the format of: Price@[email protected]. ; e.g. 0@Exp@Vow;300@Assurance@Solace; Only works if Ironman Mode is enabled.");
		Logger = ((BaseUnityPlugin)this).Logger;
		Logger.LogInfo((object)"Plugin is loaded!");
		harmony.PatchAll(typeof(ZetasMoonDiscountsBase));
		harmony.PatchAll(typeof(TerminalPatch));
		harmony.PatchAll(typeof(StartOfRoundPatch));
		harmony.PatchAll(typeof(TimeOfDayPatch));
		harmony.PatchAll(typeof(GameNetworkManagerPatch));
	}

	public void ModifyBuyingRate()
	{
		if (ApplyBuyingChanges.Value && Object.op_Implicit((Object)(object)TimeOfDay.Instance) && Object.op_Implicit((Object)(object)StartOfRound.Instance))
		{
			if (TimeOfDay.Instance.daysUntilDeadline != 0)
			{
				StartOfRound.Instance.companyBuyingRate = NormalSellRate.Value;
			}
			else
			{
				StartOfRound.Instance.companyBuyingRate = ZeroDaysSelLRate.Value;
			}
		}
	}

	public void CalculateMoonPrices()
	{
		Logger.LogDebug((object)"Attempting CalculateMoonPrices");
		if (MoonDefaultCosts.Count < 1 && !IronmanMode.Value)
		{
			Logger.LogDebug((object)"Default moon prices have not been set, skipping Calculation of moon prices");
			return;
		}
		Logger.LogDebug((object)"Default moon prices been set, continuing");
		if (!Object.op_Implicit((Object)(object)StartOfRound.Instance) || !Object.op_Implicit((Object)(object)StartOfRound.Instance.currentLevel))
		{
			return;
		}
		SelectableLevel currentLevel = StartOfRound.Instance.currentLevel;
		Logger.LogDebug((object)"Got current level ref");
		ExtendedLevel val = PatchedContent.ExtendedLevels.Find((ExtendedLevel x) => (Object)(object)x.SelectableLevel == (Object)(object)currentLevel);
		if (!IronmanMode.Value)
		{
			foreach (ExtendedLevel level2 in PatchedContent.ExtendedLevels)
			{
				level2.RoutePrice = MoonDefaultCosts.Find((LevelPairedWithPrice x) => (Object)(object)x.Level == (Object)(object)level2).OriginalPrice;
			}
			if (val.NumberlessPlanetName == "Gordion" || val.NumberlessPlanetName == "Galetry" || val.NumberlessPlanetName == "Oxyde" || val.RoutePrice == 0)
			{
				Logger.LogDebug((object)("Current moon " + val.NumberlessPlanetName + " is free/company/oxyde, so no discounts will be applied"));
				return;
			}
			Logger.LogDebug((object)("Current moon " + val.NumberlessPlanetName + " is not free/company/oxyde, so discounts will be applied"));
			{
				foreach (ExtendedLevel level in PatchedContent.ExtendedLevels)
				{
					if (level.NumberlessPlanetName == "Gordion" || level.NumberlessPlanetName == "Galetry" || level.NumberlessPlanetName == "Oxyde" || level.NumberlessPlanetName == val.NumberlessPlanetName || level.RoutePrice == 0)
					{
						level.RoutePrice = MoonDefaultCosts.Find((LevelPairedWithPrice x) => (Object)(object)x.Level == (Object)(object)level).OriginalPrice;
						continue;
					}
					int num = val.RoutePrice - level.RoutePrice;
					if ((num >= FreeThreshold.Value && level.RoutePrice < FreeThresholdCap.Value) || num >= 3500)
					{
						level.RoutePrice = 0;
						Logger.LogDebug((object)("Current moon " + val.NumberlessPlanetName + "(" + val.RoutePrice + ") --> " + level.NumberlessPlanetName + "(" + level.RoutePrice + ") Meets the free discount criteria"));
						continue;
					}
					if (level.RoutePrice <= val.RoutePrice)
					{
						level.RoutePrice = DowngradeCost.Value;
						Logger.LogDebug((object)("Current moon " + val.NumberlessPlanetName + "(" + val.RoutePrice + ") --> " + level.NumberlessPlanetName + "(" + level.RoutePrice + ") Meets the downgrade discount criteria"));
						continue;
					}
					num = level.RoutePrice - val.RoutePrice;
					if (num <= UpgradeThreshold.Value)
					{
						level.RoutePrice = num;
						Logger.LogDebug((object)("Current moon " + val.NumberlessPlanetName + "(" + val.RoutePrice + ") --> " + level.NumberlessPlanetName + "(" + level.RoutePrice + ") Meets the upgrade discount criteria"));
						continue;
					}
					level.RoutePrice = MoonDefaultCosts.Find((LevelPairedWithPrice x) => (Object)(object)x.Level == (Object)(object)level).OriginalPrice;
					Logger.LogDebug((object)("Current moon " + val.NumberlessPlanetName + "(" + val.RoutePrice + ") --> " + level.NumberlessPlanetName + "(" + level.RoutePrice + ") Does not meet any discount criteria"));
				}
				return;
			}
		}
		Logger.LogDebug((object)"Ironman Mode Detected");
		if (val.NumberlessPlanetName == "Gordion" || val.NumberlessPlanetName == "Galetry" || val.NumberlessPlanetName == "Oxyde")
		{
			Logger.LogDebug((object)"Ironman Is Company");
			string[] array = IronmanMoons.Value.ToString().Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = array;
			foreach (string text in array2)
			{
				Logger.LogDebug((object)("Ironman currently on entry " + text));
				string[] array3 = text.Split(new char[1] { '@' }, StringSplitOptions.RemoveEmptyEntries);
				int num2 = 0;
				string[] array4 = array3;
				foreach (string pair in array4)
				{
					Logger.LogDebug((object)("Ironman currently on pair " + pair));
					if (num2 != 0)
					{
						ExtendedLevel val2 = PatchedContent.ExtendedLevels.Find((ExtendedLevel x) => (Object)(object)x.SelectableLevel == (Object)(object)ConfigHelper.ConvertStringToLevels(pair)[0]);
						if (int.TryParse(array3[0].ToString(), out var result))
						{
							Logger.LogDebug((object)("Setting " + val2.NumberlessPlanetName + " route price to " + result));
							val2.RoutePrice = result;
						}
					}
					num2++;
				}
			}
			return;
		}
		Logger.LogDebug((object)"Ironman is a normal moon");
		bool flag = false;
		List<string> list = new List<string>();
		string[] array5 = IronmanMoons.Value.ToString().Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
		string[] array6 = array5;
		foreach (string text2 in array6)
		{
			string[] pairs = text2.Split(new char[1] { '@' }, StringSplitOptions.RemoveEmptyEntries);
			int index = 0;
			string[] array7 = pairs;
			foreach (string text3 in array7)
			{
				if (index != 0)
				{
					ExtendedLevel val3 = PatchedContent.ExtendedLevels.Find((ExtendedLevel x) => (Object)(object)x.SelectableLevel == (Object)(object)ConfigHelper.ConvertStringToLevels(pairs[index])[0]);
					Logger.LogDebug((object)("Current level is: " + val.NumberlessPlanetName + " and Current config level is " + val3.NumberlessPlanetName));
					if (val.NumberlessPlanetName == val3.NumberlessPlanetName)
					{
						flag = true;
						int index2 = 0;
						string[] array8 = pairs;
						foreach (string text4 in array8)
						{
							if (index2 != 0)
							{
								ExtendedLevel val4 = PatchedContent.ExtendedLevels.Find((ExtendedLevel x) => (Object)(object)x.SelectableLevel == (Object)(object)ConfigHelper.ConvertStringToLevels(pairs[index2])[0]);
								Logger.LogDebug((object)("Trying to add " + pairs[index2] + " passed as " + (object)val4));
								list.Add(val4.NumberlessPlanetName);
							}
							index2++;
						}
					}
				}
				index++;
			}
		}
		foreach (ExtendedLevel extendedLevel in PatchedContent.ExtendedLevels)
		{
			if (!(extendedLevel.NumberlessPlanetName != "Gordion") || !(extendedLevel.NumberlessPlanetName != "Galetry") || !(extendedLevel.NumberlessPlanetName != "Oxyde") || !(extendedLevel.NumberlessPlanetName != val.NumberlessPlanetName))
			{
				continue;
			}
			bool flag2 = false;
			foreach (string item in list)
			{
				Logger.LogDebug((object)("Ironman current level checked is " + extendedLevel.NumberlessPlanetName + " vs. " + item));
				if (extendedLevel.NumberlessPlanetName == item)
				{
					Logger.LogDebug((object)"Ironman they are a match");
					flag2 = true;
				}
				else
				{
					Logger.LogDebug((object)"Ironman they are not a match");
				}
			}
			if (!flag2)
			{
				extendedLevel.RoutePrice = 9999;
			}
			else
			{
				extendedLevel.RoutePrice = 0;
			}
		}
	}

	public void SaveDefaultMoonPrices()
	{
		Logger.LogDebug((object)"Attempting to save moon default prices");
		Logger.LogDebug((object)("ExtendedLevels Count is: " + PatchedContent.ExtendedLevels.Count));
		if (MoonDefaultCosts.Count > 1)
		{
			Logger.LogDebug((object)"Default prices already have been set, skipping");
			return;
		}
		int num = 0;
		foreach (ExtendedLevel extendedLevel in PatchedContent.ExtendedLevels)
		{
			Logger.LogDebug((object)"In loop");
			Logger.LogDebug((object)("Looping through level, current is: " + extendedLevel.NumberlessPlanetName));
			LevelPairedWithPrice levelPairedWithPrice = new LevelPairedWithPrice();
			levelPairedWithPrice.Level = extendedLevel;
			levelPairedWithPrice.OriginalPrice = extendedLevel.RoutePrice;
			MoonDefaultCosts.Add(levelPairedWithPrice);
			Logger.LogDebug((object)("Saved " + extendedLevel.NumberlessPlanetName + " default price as " + extendedLevel.RoutePrice));
			num++;
		}
		Logger.LogDebug((object)"Finished setting moon default prices");
	}
}