using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RestoreMapper")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Restore the unused mapper item in the shop")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1+994f7f030c62aca836edfde8d1317e0b0b2f626a")]
[assembly: AssemblyProduct("RestoreMapper")]
[assembly: AssemblyTitle("RestoreMapper")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace RestoreMapper
{
[BepInPlugin("butterystancakes.lethalcompany.restoremapper", "Restore Mapper", "1.1.1")]
public class Plugin : BaseUnityPlugin
{
private const string PLUGIN_GUID = "butterystancakes.lethalcompany.restoremapper";
private const string PLUGIN_NAME = "Restore Mapper";
private const string PLUGIN_VERSION = "1.1.1";
internal static ManualLogSource Logger;
private void Awake()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
Logger = ((BaseUnityPlugin)this).Logger;
new Harmony("butterystancakes.lethalcompany.restoremapper").PatchAll();
Logger.LogInfo((object)"Restore Mapper v1.1.1 loaded");
}
}
[HarmonyPatch]
internal class RestoreMapperPatches
{
[HarmonyPatch(typeof(Terminal), "Awake")]
[HarmonyPostfix]
private static void TerminalPostAwake(Terminal __instance)
{
TerminalKeyword? obj = ((IEnumerable<TerminalKeyword>)__instance.terminalNodes?.allKeywords).FirstOrDefault((Func<TerminalKeyword, bool>)((TerminalKeyword keyword) => ((Object)keyword).name == "Buy"));
TerminalNode val = ((IEnumerable<CompatibleNoun>)obj?.compatibleNouns).FirstOrDefault((Func<CompatibleNoun, bool>)delegate(CompatibleNoun compatibleNoun)
{
TerminalKeyword noun2 = compatibleNoun.noun;
return ((noun2 != null) ? ((Object)noun2).name : null) == "Mapper";
}).result;
CompatibleNoun val2 = ((IEnumerable<CompatibleNoun>)val?.terminalOptions).FirstOrDefault((Func<CompatibleNoun, bool>)delegate(CompatibleNoun compatibleNoun)
{
TerminalKeyword noun = compatibleNoun.noun;
return ((noun != null) ? ((Object)noun).name : null) == "Confirm";
});
List<Item> list = StartOfRound.Instance?.allItemsList?.itemsList;
Item val3 = list?.FirstOrDefault((Func<Item, bool>)((Item item) => ((Object)item).name == "MapDevice"));
if ((Object)(object)obj == (Object)null || (Object)(object)val == (Object)null || val2 == null || list == null || (Object)(object)val3 == (Object)null)
{
Plugin.Logger.LogError((object)"Encountered an error while caching essential references. Loading will be skipped.");
return;
}
__instance.buyableItemsList = CollectionExtensions.AddItem<Item>((IEnumerable<Item>)__instance.buyableItemsList, val3).ToArray();
val.buyItemIndex = __instance.buyableItemsList.Length - 1;
val2.result.buyItemIndex = val.buyItemIndex;
Plugin.Logger.LogDebug((object)"Assigned IDs");
val3.creditsWorth = 150;
Plugin.Logger.LogDebug((object)"Assigned price");
Item val4 = ((IEnumerable<Item>)list).FirstOrDefault((Func<Item, bool>)((Item item) => ((Object)item).name == "ProFlashlight"));
if ((Object)(object)val4 != (Object)null)
{
val3.grabSFX = val4.grabSFX;
val3.dropSFX = val4.dropSFX;
val3.pocketSFX = val4.pocketSFX;
Plugin.Logger.LogDebug((object)"Assigned SFX");
}
else
{
Plugin.Logger.LogWarning((object)"Couldn't find pro-flashlight item. Sound effects will be missing.");
}
try
{
AssetBundle val5 = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "restoremapper"));
val3.itemIcon = val5.LoadAsset<Sprite>("MapperIcon");
val5.Unload(false);
Plugin.Logger.LogDebug((object)"Assigned special icon");
}
catch
{
Plugin.Logger.LogWarning((object)"Encountered some error loading asset bundle. Inventory icon will be incorrect.");
Item val6 = list?.FirstOrDefault((Func<Item, bool>)delegate(Item item)
{
Sprite itemIcon = item.itemIcon;
return ((itemIcon != null) ? ((Object)itemIcon).name : null) == "caticontest";
});
if ((Object)(object)val6 != (Object)null)
{
val3.itemIcon = val6.itemIcon;
}
}
}
[HarmonyPatch(typeof(MapDevice), "Start")]
[HarmonyPrefix]
private static bool MapDevicePreStart(MapDevice __instance)
{
if ((Object)(object)__instance.mapCamera != (Object)null)
{
Plugin.Logger.LogWarning((object)$"Mapper #{((Object)__instance).GetInstanceID()} tried to call Start() more than once, this is dangerous and would've caused a memory leak");
return false;
}
return true;
}
[HarmonyPatch(typeof(MapDevice), "Start")]
[HarmonyPostfix]
private static void MapDevicePostStart(MapDevice __instance)
{
//IL_0073: Unknown result type (might be due to invalid IL or missing references)
//IL_0078: Unknown result type (might be due to invalid IL or missing references)
//IL_0082: Expected O, but got Unknown
//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
if (((Component)__instance.mapCamera).CompareTag("MapCamera"))
{
RenderTexture targetTexture = __instance.mapCamera.targetTexture;
__instance.mapCamera = Object.Instantiate<GameObject>(((Component)__instance.mapCamera).gameObject, ((Component)__instance.mapCamera).transform.parent).GetComponent<Camera>();
((Component)__instance.mapCamera).tag = "Untagged";
__instance.mapCamera.targetTexture = new RenderTexture(((Texture)targetTexture).width, ((Texture)targetTexture).height, targetTexture.depth, targetTexture.format);
Plugin.Logger.LogDebug((object)$"Mapper #{((Object)__instance).GetInstanceID()} cam&tex cloned");
__instance.mapAnimatorTransition = ((Component)__instance.mapCamera).GetComponentInChildren<Animator>();
((Component)__instance.mapAnimatorTransition).transform.localPosition = new Vector3(0f, 0f, -0.95f);
__instance.mapLight = ((Component)__instance.mapCamera).GetComponentInChildren<Light>();
((Component)__instance.mapCamera).gameObject.SetActive(false);
((Behaviour)__instance.mapCamera).enabled = true;
((Behaviour)__instance.mapLight).enabled = false;
Plugin.Logger.LogDebug((object)$"Mapper #{((Object)__instance).GetInstanceID()} light setup");
MeshRenderer componentInChildren = ((Component)__instance).GetComponentInChildren<MeshRenderer>();
Material[] materials = ((Renderer)componentInChildren).materials;
((IEnumerable<Material>)materials).FirstOrDefault((Func<Material, bool>)((Material mat) => ((Object)mat).name.StartsWith("MapScreen"))).mainTexture = (Texture)(object)__instance.mapCamera.targetTexture;
((Renderer)componentInChildren).materials = materials;
Plugin.Logger.LogDebug((object)$"Mapper #{((Object)__instance).GetInstanceID()} screen retex'd");
}
}
[HarmonyPatch(typeof(MapDevice), "ItemActivate")]
[HarmonyPrefix]
private static bool MapDeviceItemActivate(MapDevice __instance, Coroutine ___pingMapCoroutine)
{
if ((Object)(object)((GrabbableObject)__instance).playerHeldBy == (Object)null || ((Component)__instance.mapCamera).CompareTag("MapCamera"))
{
return true;
}
if (___pingMapCoroutine != null)
{
((MonoBehaviour)__instance).StopCoroutine(___pingMapCoroutine);
}
((MonoBehaviour)__instance).StartCoroutine(pingMapSystem(__instance, ((GrabbableObject)__instance).playerHeldBy));
return false;
}
[HarmonyPatch(typeof(NetworkBehaviour), "OnDestroy")]
[HarmonyPostfix]
private static void NetworkBehaviourPostOnDestroy(NetworkBehaviour __instance)
{
MapDevice val = (MapDevice)(object)((__instance is MapDevice) ? __instance : null);
if (val != null && (Object)(object)val.mapCamera != (Object)null && !((Component)val.mapCamera).CompareTag("MapCamera"))
{
if (val.mapCamera.targetTexture.IsCreated())
{
val.mapCamera.targetTexture.Release();
}
Object.Destroy((Object)(object)val.mapCamera.targetTexture);
Object.Destroy((Object)(object)((Component)val.mapCamera).gameObject);
Plugin.Logger.LogDebug((object)$"Mapper #{((Object)__instance).GetInstanceID()} cleaned up");
}
}
[HarmonyPatch(typeof(GrabbableObject), "UseItemBatteries")]
[HarmonyPrefix]
private static bool GrabbableObjectPreUseItemBatteries(GrabbableObject __instance)
{
if (__instance is MapDevice)
{
if (!StartOfRound.Instance.mapScreen.overrideCameraForOtherUse)
{
if (!((Object)(object)__instance.playerHeldBy == (Object)null))
{
if (__instance.playerHeldBy.isInHangarShipRoom || __instance.playerHeldBy.isInElevator)
{
return !((Behaviour)StartOfRound.Instance.mapScreen.cam).enabled;
}
return true;
}
return true;
}
return false;
}
return true;
}
private static IEnumerator pingMapSystem(MapDevice mapDevice, PlayerControllerB playerHeldBy)
{
((Component)mapDevice.mapCamera).gameObject.SetActive(true);
if ((Object)(object)playerHeldBy == (Object)(object)GameNetworkManager.Instance.localPlayerController && ((!GameNetworkManager.Instance.localPlayerController.isInHangarShipRoom && !GameNetworkManager.Instance.localPlayerController.isInElevator) || !((Behaviour)StartOfRound.Instance.mapScreen.cam).enabled))
{
mapDevice.mapAnimatorTransition.SetTrigger("Transition");
((Component)StartOfRound.Instance.mapScreenPlayerName).gameObject.SetActive(false);
}
yield return (object)new WaitForSeconds(0.035f);
Vector3 position = ((Component)playerHeldBy).transform.position;
position.y += 3.636f;
((Component)mapDevice.mapCamera).transform.position = position;
yield return (object)new WaitForSeconds(0.2f);
((Behaviour)mapDevice.mapLight).enabled = playerHeldBy.isInsideFactory || ((Component)playerHeldBy).transform.position.y < -80f;
mapDevice.mapCamera.Render();
((Behaviour)mapDevice.mapLight).enabled = false;
((Component)mapDevice.mapCamera).gameObject.SetActive(false);
((Component)StartOfRound.Instance.mapScreenPlayerName).gameObject.SetActive(true);
}
[HarmonyPatch(typeof(StartOfRound), "SwitchMapMonitorPurpose")]
[HarmonyPostfix]
private static void PostSwitchMapMonitorPurpose(bool displayInfo)
{
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
if (!displayInfo)
{
return;
}
MapDevice[] array = Object.FindObjectsOfType<MapDevice>();
foreach (MapDevice val in array)
{
if ((Object)(object)val.mapCamera != (Object)null && !((Component)val.mapCamera).CompareTag("MapCamera"))
{
RenderTexture active = RenderTexture.active;
RenderTexture.active = val.mapCamera.targetTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = active;
}
}
}
[HarmonyPatch(typeof(MapDevice), "EquipItem")]
[HarmonyPostfix]
private static void MapDevicePostEquipItem(MapDevice __instance)
{
((GrabbableObject)__instance).playerHeldBy.equippedUsableItemQE = false;
}
}
public static class PluginInfo
{
public const string PLUGIN_GUID = "RestoreMapper";
public const string PLUGIN_NAME = "RestoreMapper";
public const string PLUGIN_VERSION = "1.1.1";
}
}