Decompiled source of MissionRework v1.0.2

MissionRework.dll

Decompiled 7 months 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 SailwindModdingHelper;
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("MissionRework")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MissionRework")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1378a4e9-1f10-4206-862b-1ce097b407ee")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MissionRework;

public enum MissionType
{
	Available,
	Active,
	Last
}
internal class GPMissionTypeButton : GoPointerButton
{
	public TextMesh text;

	public static Port currentPort;

	public static PortDude portDude;

	public static MissionType missionType = MissionType.Available;

	private static string[] sortingStrings = new string[2] { "Available", "Active" };

	public override void OnActivate()
	{
		if (missionType == MissionType.Available)
		{
			missionType = MissionType.Active;
		}
		else
		{
			missionType = MissionType.Available;
		}
		UpdateMissions();
		UpdateText();
	}

	public static void UpdateMissions()
	{
		if ((Object)(object)currentPort != (Object)null)
		{
			switch (missionType)
			{
			case MissionType.Active:
				MissionListUI.instance.EnablePortMissionUI(PlayerMissions.missions, Extensions.GetPrivateField<Transform>((object)portDude, "missionTable"), portDude);
				Extensions.GetPrivateField<GameObject>((object)MissionListUI.instance, "pageButtons").SetActive(false);
				Extensions.SetPrivateField((object)MissionListUI.instance, "closeCooldown", (object)0f);
				break;
			case MissionType.Available:
				MissionListUI.instance.EnablePortMissionUI(Extensions.GetPrivateField<Mission[]>((object)currentPort, "missions"), Extensions.GetPrivateField<Transform>((object)portDude, "missionTable"), portDude);
				MissionListUI.instance.ChangePage(0);
				Extensions.SetPrivateField((object)MissionListUI.instance, "closeCooldown", (object)0f);
				Extensions.InvokePrivateMethod((object)MissionListUI.instance, "UpdatePageCountText", Array.Empty<object>());
				break;
			}
		}
	}

	public void UpdateText()
	{
		string text = missionType.ToString();
		if ((int)missionType < sortingStrings.Length)
		{
			text = sortingStrings[(int)missionType];
		}
		this.text.text = "Showing: " + text;
	}
}
[BepInPlugin("com.nandbrew.missionrework", "Mission Rework", "1.0.2")]
[BepInDependency("com.app24.sailwindmoddinghelper", "2.0.0")]
internal class Main : BaseUnityPlugin
{
	public const string GUID = "com.nandbrew.missionrework";

	public const string NAME = "Mission Rework";

	public const string VERSION = "1.0.2";

	internal static Main instance;

	internal static ManualLogSource logSource;

	internal static ConfigEntry<bool> safety;

	private void Awake()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		logSource = ((BaseUnityPlugin)this).Logger;
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.nandbrew.missionrework");
		safety = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "Safety", false, new ConfigDescription("\"Abandon\" button will read \"(wrong port)\" or \"(no goods in area)\" and must be clicked twice", (AcceptableValueBase)null, Array.Empty<object>()));
	}
}
internal class MissionPatches
{
	[HarmonyPatch(typeof(MissionListUI))]
	private class MissionListUIPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("EnablePortMissionUI")]
		public static void EnablePortMissionUIPatch()
		{
			Refs.SetPlayerControl(false);
		}

		[HarmonyPostfix]
		[HarmonyPatch("DisablePortMissionUI")]
		public static void DisablePortMissionUIPatch()
		{
			Refs.SetPlayerControl(true);
		}

		[HarmonyPrefix]
		[HarmonyPatch("ToggleMenu")]
		public static bool ToggleMenuPatch(MissionListUI __instance, bool ___UIActive)
		{
			sortButton.SetActive(false);
			backButton.SetActive(false);
			acceptCancelButton.SetActive(false);
			return true;
		}
	}

	[HarmonyPatch(typeof(IslandMarketWarehouseArea))]
	private static class MissionGoodRegistrationPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		public static void Register(IslandMarketWarehouseArea __instance, IslandMarket ___market)
		{
			currentPortIndex = ___market.GetPortIndex();
			if (!portLists.ContainsKey(currentPortIndex))
			{
				portLists.Add(currentPortIndex, new List<Good>());
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("OnTriggerEnter")]
		public static void OnTriggerEnterPatch(IslandMarketWarehouseArea __instance, Collider other, IslandMarket ___market)
		{
			currentPortIndex = ___market.GetPortIndex();
			currentMarket = ___market;
			if (!portLists.ContainsKey(currentPortIndex))
			{
				portLists.Add(currentPortIndex, new List<Good>());
			}
			Good component = ((Component)other).GetComponent<Good>();
			List<Good> list = portLists[currentPortIndex];
			if (Object.op_Implicit((Object)(object)component) && !list.Contains(component) && component.GetMissionIndex() != -1)
			{
				list.Add(component);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("OnTriggerExit")]
		public static void OnTriggerExitPatch(IslandMarketWarehouseArea __instance, Collider other, IslandMarket ___market)
		{
			currentPortIndex = ___market.GetPortIndex();
			currentMarket = ___market;
			List<Good> list = portLists[currentPortIndex];
			Good component = ((Component)other).GetComponent<Good>();
			if (Object.op_Implicit((Object)(object)component) && list.Contains(component))
			{
				list.Remove(component);
			}
		}
	}

	[HarmonyPatch(typeof(MissionDetailsUI))]
	private static class MissionDetailsUiPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("Start")]
		public static void StartPatch(MissionDetailsUI __instance, GameObject ___UI)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Expected O, but got Unknown
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Expected O, but got Unknown
			foreach (Transform item in ___UI.transform.parent)
			{
				Transform val = item;
				if (!(((Object)val).name == "current mission buttons"))
				{
					continue;
				}
				foreach (Transform item2 in val)
				{
					Transform val2 = item2;
					if (((Object)val2).name == "page buttons")
					{
						GameObject gameObject = ((Component)val2.Find("mission button (back)")).gameObject;
						GameObject val3 = Object.Instantiate<GameObject>(gameObject);
						val3.transform.parent = val;
						gameObject.transform.parent = val;
						Transform transform = val3.transform;
						Vector3 localPosition = gameObject.transform.localPosition;
						transform.localPosition = new Vector3(((Vector3)(ref localPosition))[0], 0.22f, 0.03f);
						val3.transform.localRotation = gameObject.transform.localRotation;
						val3.transform.localScale = gameObject.transform.localScale;
						((Object)val3).name = "sort button";
						GameObject gameObject2 = ((Component)val3.GetComponentInChildren<GoPointerButton>()).gameObject;
						Object.Destroy((Object)(object)gameObject2.GetComponent<GoPointerButton>());
						GPMissionTypeButton gPMissionTypeButton = gameObject2.AddComponent<GPMissionTypeButton>();
						gPMissionTypeButton.text = val3.GetComponentInChildren<TextMesh>();
						gPMissionTypeButton.UpdateText();
						Extensions.SetPrivateField((object)MissionListUI.instance, "closeCooldown", (object)0f);
						sortButton = val3;
						backButton = gameObject;
						val3.SetActive(false);
					}
				}
			}
			foreach (Transform item3 in ___UI.transform)
			{
				Transform val4 = item3;
				foreach (Transform item4 in val4)
				{
					Transform val5 = item4;
					if (((Object)val5).name == "accept/cancel button")
					{
						acceptCancelButton = ((Component)val5).gameObject;
					}
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("ClickButton")]
		public static bool ClickButton(MissionDetailsUI __instance, ref bool ___clickable, bool ___mapZoomedIn, ref Mission ___currentMission, ref GameObject ___UI, ref TextMesh ___buttonText)
		{
			int portIndex = ___currentMission.destinationPort.portIndex;
			List<Good> list = portLists[portIndex];
			if (GPMissionTypeButton.missionType == MissionType.Available)
			{
				return true;
			}
			if (!___mapZoomedIn && ___currentMission.missionIndex != -1)
			{
				if (___clickable)
				{
					if (currentPortIndex == portIndex && list.Count > 0)
					{
						int num = 0;
						while (num < list.Count)
						{
							if (___currentMission == list[num].GetAssignedMission())
							{
								list[num].Deliver();
								list.Remove(list[num]);
							}
							else
							{
								num++;
							}
						}
						int num2 = ___currentMission.goodCount - ___currentMission.GetDeliveredCount();
						if (num2 > 0)
						{
							int num3 = num2 * 100;
							___currentMission.AbandonMission();
							NotificationUi.instance.ShowNotification("Ended mission:\n" + ___currentMission.missionName + "\nReputation penalty: " + num3);
							UISoundPlayer.instance.PlayWritingSound();
							PlayerMissions.missions[___currentMission.missionIndex] = null;
							LoggedMission obj = MissionLog.instance.loggedMissions[0];
							obj.totalRep -= num3;
							MissionLog.instance.UpdateTexts();
						}
						MissionListUI.instance.RefreshList();
						Extensions.InvokePrivateMethod((object)__instance, "UpdateTexts", Array.Empty<object>());
						Extensions.GetPrivateField<GameObject>((object)MissionListUI.instance, "book").SetActive(false);
						Extensions.GetPrivateField<PortDude>((object)MissionListUI.instance, "currentPortDude").ActivateMissionListUI(false);
						List<Good> list2 = (List<Good>)Extensions.GetPrivateField(Extensions.GetPrivateField((object)currentMarket, "warehouseArea"), "goodsInArea");
						foreach (Good item in list2)
						{
							((Component)item).GetComponent<Rigidbody>().WakeUp();
						}
					}
					else
					{
						PlayerMissions.AbandonMission(___currentMission.missionIndex);
						MissionListUI.instance.RefreshList();
						Extensions.GetPrivateField<GameObject>((object)MissionListUI.instance, "book").SetActive(false);
						Extensions.GetPrivateField<PortDude>((object)MissionListUI.instance, "currentPortDude").ActivateMissionListUI(false);
						Debug.Log((object)"Button Clicked!");
					}
				}
				else
				{
					___buttonText.text = "Abandon";
					___clickable = true;
				}
			}
			return false;
		}

		[HarmonyPostfix]
		[HarmonyPatch("UpdateTexts")]
		private static void UpdateTextsPatch(ref bool ___clickable, ref TextMesh ___buttonText, Mission ___currentMission, ref TextMesh ___amount)
		{
			int portIndex = ___currentMission.destinationPort.portIndex;
			List<Good> list = portLists[currentPortIndex];
			if (___currentMission.missionIndex == -1 || !((Object)(object)Extensions.GetPrivateField<PortDude>((object)MissionListUI.instance, "currentPortDude") != (Object)null))
			{
				return;
			}
			___amount.text = ___currentMission.GetDeliveredCount() + " / " + ___amount.text;
			int num = 0;
			foreach (Good item in list)
			{
				if (___currentMission == item.GetAssignedMission())
				{
					num++;
				}
			}
			if (currentPortIndex == portIndex)
			{
				if (num > 0)
				{
					if (num < ___currentMission.goodCount)
					{
						___buttonText.text = "Deliver\npartial (" + num + ")";
					}
					if (num >= ___currentMission.goodCount)
					{
						___buttonText.text = "Deliver all";
					}
					___clickable = true;
				}
				else if (Main.safety.Value)
				{
					___buttonText.text = "(no goods\nin area)";
					___clickable = false;
				}
			}
			else if (Main.safety.Value)
			{
				___buttonText.text = "(wrong port)";
				___clickable = false;
			}
		}
	}

	[HarmonyPatch(typeof(PortDude))]
	private static class ActivateMissionListUIPatch
	{
		[HarmonyPrefix]
		[HarmonyPatch("ActivateMissionListUI")]
		public static bool ActivateMissionListUI(PortDude __instance, bool openEconomyUI)
		{
			if (!openEconomyUI)
			{
				GPMissionTypeButton.currentPort = __instance.GetPort();
				GPMissionTypeButton.portDude = __instance;
				sortButton.SetActive(true);
				backButton.SetActive(true);
				acceptCancelButton.SetActive(true);
				GPMissionTypeButton.UpdateMissions();
				return false;
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch("DeactivateMissionListUI")]
		public static void DeactivateMissionListUI(PortDude __instance)
		{
			GPMissionTypeButton.currentPort = null;
			GPMissionTypeButton.portDude = null;
			sortButton.SetActive(false);
			backButton.SetActive(false);
		}

		[HarmonyPrefix]
		[HarmonyPatch("OnTriggerEnter")]
		public static bool OnTriggerEnter()
		{
			return false;
		}
	}

	[HarmonyPatch(typeof(PlayerMissions))]
	private static class PlayerMissionsPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("AbandonMission")]
		public static bool AbandonMissionPatch(int missionIndex)
		{
			int num = -100 * (PlayerMissions.missions[missionIndex].goodCount - PlayerMissions.missions[missionIndex].GetDeliveredCount());
			PlayerMissions.missions[missionIndex].destinationPort.IncreaseDemand(PlayerMissions.missions[missionIndex]);
			PlayerMissions.missions[missionIndex].AbandonMission();
			NotificationUi.instance.ShowNotification("Abandoning mission:\n" + PlayerMissions.missions[missionIndex].missionName + "\n" + num + " reputation");
			PlayerMissions.missions[missionIndex] = null;
			return false;
		}
	}

	private static GameObject sortButton;

	private static GameObject backButton;

	private static GameObject acceptCancelButton;

	public static int currentPortIndex;

	public static Dictionary<int, List<Good>> portLists = new Dictionary<int, List<Good>>();

	private static IslandMarket currentMarket;
}