Decompiled source of LifetimeCruiserWarranty v1.3.0

plugins/LifetimeCruiserWarranty/LifetimeCruiserWarranty.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Unity.Netcode;
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("LifetimeCruiserWarranty")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LifetimeCruiserWarranty")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ea55591a-2a00-4d63-9d8a-f37f0abf8888")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LifetimeCruiserWarranty
{
	internal class Config
	{
		public static ConfigEntry<bool> applyPenalty;

		public static ConfigEntry<bool> isPercentagePenalty;

		public static ConfigEntry<float> percentagePenaltyAmount;

		public static ConfigEntry<int> flatPenaltyAmount;

		public static void Load()
		{
			applyPenalty = Plugin.config.Bind<bool>("Apply Penalty", "ApplyPenalty", true, "Determines if leaving behind or destroying the Company Cruiser will incur a credit penalty.");
			isPercentagePenalty = Plugin.config.Bind<bool>("Is Penalty Percentage", "IsPercentagePenalty", true, "Determines if the credit penalty is issued as a percentage of your total credits or as a flat rate. Apply Penalty must be true for this to have any effect.");
			percentagePenaltyAmount = Plugin.config.Bind<float>("Percentage Penalty Amount", "PercentagePenaltyAmount", 0.2f, "The percentage penalty which will be applied. Is Penalty Percentage must be true for this to have any effect.");
			flatPenaltyAmount = Plugin.config.Bind<int>("Flat Penalty Amount", "FlatPenaltyAmount", 150, "The flat rate penalty which will be applied. Is Penalty Percentage must be false for this to have any effect.");
		}
	}
	[BepInPlugin("33mamaster.LifetimeCruiserWarranty", "Lifetime Cruiser Warranty", "1.2.0")]
	public class Plugin : BaseUnityPlugin
	{
		private const string modGUID = "33mamaster.LifetimeCruiserWarranty";

		private const string modName = "Lifetime Cruiser Warranty";

		private const string modVersion = "1.2.0";

		public static ConfigFile config;

		internal static ManualLogSource Logger;

		private readonly Harmony harmony = new Harmony("33mamaster.LifetimeCruiserWarranty");

		private static Plugin Instance;

		internal static bool isVehicleLeftBehind;

		internal static int due;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Mod Lifetime Cruiser Warranty is loaded!");
			config = ((BaseUnityPlugin)this).Config;
			Config.Load();
			harmony.PatchAll();
		}
	}
}
namespace LifetimeCruiserWarranty.Patches
{
	[HarmonyPatch]
	public static class StartOfRoundTranspiler
	{
		public static MethodBase TargetMethod()
		{
			Type nestedType = typeof(StartOfRound).GetNestedType("<EndOfGame>d__278", BindingFlags.NonPublic);
			return AccessTools.Method(nestedType, "MoveNext", (Type[])null, (Type[])null);
		}

		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> TranspileEndOfGame(IEnumerable<CodeInstruction> instructions)
		{
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			for (int i = 0; i < list.Count - 3; i++)
			{
				if (list[i].opcode == OpCodes.Ldloc_1 && list[i + 1].opcode == OpCodes.Ldarg_0 && list[i + 2].opcode == OpCodes.Ldfld && list[i + 2].operand.ToString().Contains("connectedPlayersOnServer") && list[i + 3].opcode == OpCodes.Call && list[i + 3].operand.ToString().Contains("PassTimeToNextDay"))
				{
					List<CodeInstruction> list2 = new List<CodeInstruction>();
					list2.Add(new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(StartOfRoundTranspiler), "DisplayPenaltyWrapper", (Type[])null, (Type[])null)));
					List<CodeInstruction> collection = list2;
					list.InsertRange(i + 4, collection);
					break;
				}
			}
			return list.AsEnumerable();
		}

		private static void DisplayPenaltyWrapper()
		{
			Plugin.Logger.LogInfo((object)"Display Penalty Function");
			if (Plugin.isVehicleLeftBehind)
			{
				((MonoBehaviour)HUDManager.Instance).StartCoroutine(DisplayAlert());
			}
		}

		private static IEnumerator DisplayAlert()
		{
			yield return (object)new WaitForSeconds(4f);
			Plugin.Logger.LogInfo((object)"Displaying Fine");
			HUDManager.Instance.DisplayTip("Cruiser Lost", $"The Company Cruiser was lost.\nFINES DUE: ${Plugin.due}", true, false, "LC_Warning1");
			yield return (object)new WaitForSeconds(3f);
		}
	}
	[HarmonyPatch(typeof(RoundManager))]
	internal class RoundManagerPatch
	{
		[HarmonyPatch("DespawnPropsAtEndOfRound")]
		[HarmonyPrefix]
		private static void DespawnPropsAtEndOfRoundPatch()
		{
			Plugin.isVehicleLeftBehind = false;
			if (!Config.applyPenalty.Value)
			{
				return;
			}
			Terminal val = Object.FindObjectOfType<Terminal>();
			int groupCredits = val.groupCredits;
			Plugin.isVehicleLeftBehind = false;
			Plugin.due = 0;
			Plugin.Logger.LogInfo((object)"Attempting Lookup of Vehicles");
			try
			{
				VehicleController[] array = Object.FindObjectsByType<VehicleController>((FindObjectsSortMode)0);
				for (int i = 0; i < array.Length; i++)
				{
					if (!array[i].magnetedToShip && (Object)(object)((NetworkBehaviour)array[i]).NetworkObject != (Object)null)
					{
						Plugin.Logger.LogInfo((object)("Found Left Behind Vehicle: " + ((Object)array[i]).name));
						Plugin.isVehicleLeftBehind = true;
						Plugin.Logger.LogInfo((object)$"Credits: {val.groupCredits}");
						if (Config.isPercentagePenalty.Value)
						{
							val.groupCredits -= (int)((float)groupCredits * Config.percentagePenaltyAmount.Value);
						}
						else
						{
							val.groupCredits -= Config.flatPenaltyAmount.Value;
						}
						Plugin.Logger.LogInfo((object)$"Credits after: {val.groupCredits}");
						Plugin.due = groupCredits - val.groupCredits;
					}
				}
			}
			catch (Exception arg)
			{
				Plugin.Logger.LogError((object)$"Error Searching for vehicle: {arg}");
			}
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	internal class TerminalPatch
	{
		[HarmonyPatch("BuyVehicleServerRpc")]
		[HarmonyPostfix]
		private static void BuyVehicleServerRpcPatch(ref bool ___hasWarrantyTicket)
		{
			___hasWarrantyTicket = true;
		}

		[HarmonyPatch("BuyVehicleClientRpc")]
		[HarmonyPostfix]
		private static void BuyVehicleClientRpcPatch(ref bool ___hasWarrantyTicket)
		{
			___hasWarrantyTicket = true;
		}
	}
}