Decompiled source of PortalFreight v1.0.0

plugins/PortalFreight/PortalFreight.dll

Decompiled 4 days ago
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PortalFreight;

[BepInPlugin("felih.PortalFreight", "Portal Freight", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "felih.PortalFreight";

	public const string NAME = "Portal Freight";

	public const string VERSION = "1.0.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<bool> HideMarks;

	internal static ConfigEntry<bool> DetailedLog;

	internal static ConfigEntry<bool> FastArrival;

	internal static ConfigEntry<float> MinLoadSeconds;

	internal static ConfigEntry<float> GlowRange;

	private void Awake()
	{
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Expected O, but got Unknown
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e2: Expected O, but got Unknown
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Carry anything through portals: ores, metal bars, eggs and every other item the game holds back, whatever the world settings say. Only your own game is changed: it works on any server.");
		HideMarks = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "HideNoPortalMarks", true, "Hide the \"can't go through portals\" mark on items in your bag and the line saying so in their tooltip.");
		FastArrival = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Teleport", "FastArrival", true, "End the loading screen as soon as the place you go to is loaded. The game always waits at least 8 seconds for a far portal, even when everything is ready long before.");
		MinLoadSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Teleport", "MinLoadSeconds", 3f, new ConfigDescription("Shortest loading screen with FastArrival, in seconds (the game: 8).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(2.5f, 8f), new object[0]));
		GlowRange = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Portals", "GlowRange", 0.7f, new ConfigDescription("How close you must be for a portal to light up and hum, as a share of the game's distance (1 = the game's own, 0.7 = a bit closer, so portals you walk past stay quiet).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.2f, 1f), new object[0]));
		DetailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "DetailedLog", false, "Writes details to the BepInEx log.");
		new Harmony("felih.PortalFreight").PatchAll(typeof(Plugin).Assembly);
		Log.LogInfo((object)"Portal Freight 1.0.0 loaded");
	}

	internal static void Debug(string msg)
	{
		if (DetailedLog.Value)
		{
			Log.LogInfo((object)msg);
		}
	}
}
[HarmonyPatch(typeof(Inventory), "IsTeleportable")]
internal static class AnythingGoes
{
	private static bool _said;

	private static void Prefix(ref bool allowAllItems)
	{
		if (Plugin.Enabled.Value && !allowAllItems)
		{
			allowAllItems = true;
			if (!_said)
			{
				_said = true;
				Plugin.Debug("portals let everything through");
			}
		}
	}
}
[HarmonyPatch(typeof(InventoryGrid), "UpdateGui")]
internal static class NoMarks
{
	private static readonly FieldInfo Elements = AccessTools.Field(typeof(InventoryGrid), "m_elements");

	private static FieldInfo _mark;

	private static void Postfix(InventoryGrid __instance)
	{
		if (!Plugin.Enabled.Value || !Plugin.HideMarks.Value || Elements == null || !(Elements.GetValue(__instance) is IList list))
		{
			return;
		}
		foreach (object item in list)
		{
			if (item != null)
			{
				if (_mark == null)
				{
					_mark = AccessTools.Field(item.GetType(), "m_noteleport");
				}
				if (_mark == null)
				{
					break;
				}
				object? value = _mark.GetValue(item);
				Image val = (Image)((value is Image) ? value : null);
				if ((Object)(object)val != (Object)null && ((Behaviour)val).enabled)
				{
					((Behaviour)val).enabled = false;
				}
			}
		}
	}
}
[HarmonyPatch]
internal static class NoLine
{
	private const string Line = "<color=orange>$item_noteleport</color>";

	private static IEnumerable<MethodBase> TargetMethods()
	{
		try
		{
			MethodInfo[] methods = typeof(ItemData).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (MethodInfo m in methods)
			{
				if (m.Name == "GetTooltip" && m.ReturnType == typeof(string))
				{
					yield return m;
				}
			}
		}
		finally
		{
		}
	}

	private static void Postfix(ref string __result)
	{
		if (Plugin.Enabled.Value && Plugin.HideMarks.Value && !string.IsNullOrEmpty(__result) && __result.IndexOf("<color=orange>$item_noteleport</color>") >= 0)
		{
			__result = __result.Replace("\n<color=orange>$item_noteleport</color>", "").Replace("<color=orange>$item_noteleport</color>", "");
		}
	}
}
[HarmonyPatch(typeof(Player), "UpdateTeleport")]
internal static class FastArrival
{
	private const float GameWait = 8f;

	private const float Settle = 0.5f;

	private static float _readySince = -1f;

	private static void Prefix(Player __instance, bool ___m_teleporting, bool ___m_distantTeleport, ref float ___m_teleportTimer, Vector3 ___m_teleportTargetPos)
	{
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		if (!___m_teleporting)
		{
			_readySince = -1f;
		}
		else
		{
			if (!Plugin.Enabled.Value || !Plugin.FastArrival.Value || !___m_distantTeleport || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || ___m_teleportTimer <= 2.2f || ___m_teleportTimer >= 8f || (Object)(object)ZNetScene.instance == (Object)null)
			{
				return;
			}
			if (!ZNetScene.instance.IsAreaReady(___m_teleportTargetPos))
			{
				_readySince = -1f;
				return;
			}
			if (_readySince < 0f)
			{
				_readySince = ___m_teleportTimer;
			}
			if (!(___m_teleportTimer - _readySince < 0.5f) && !(___m_teleportTimer < Plugin.MinLoadSeconds.Value))
			{
				Plugin.Debug("teleport ends after " + ___m_teleportTimer.ToString("0.0") + " s instead of " + 8f + " s");
				___m_teleportTimer = 8f;
			}
		}
	}
}
[HarmonyPatch(typeof(TeleportWorld), "Awake")]
internal static class GlowRange
{
	private static void Postfix(TeleportWorld __instance)
	{
		if (Plugin.Enabled.Value)
		{
			__instance.m_activationRange *= Mathf.Clamp(Plugin.GlowRange.Value, 0.2f, 1f);
		}
	}
}