using System; 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.Configuration; using HarmonyLib; using TMPro; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyConfiguration("Debug")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0+84ecdf67601019596bca6ee59e7c1b3bb50936f4")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.0.0.0")] [module: UnverifiableCode] public class AedenthornUtils { public static bool IgnoreKeyPresses(bool extra = false) { if (!extra) { if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog()) { Chat instance = Chat.instance; if (instance == null || !instance.HasFocus()) { return Menu.IsVisible(); } } return true; } if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog()) { Chat instance2 = Chat.instance; if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible()) { TextViewer instance3 = TextViewer.instance; if (instance3 == null) { return false; } return instance3.IsVisible(); } } return true; } public static bool CheckKeyDown(string value) { try { return Input.GetKeyDown(value.ToLower()); } catch { return false; } } public static bool CheckKeyHeld(string value, bool req = true) { try { return Input.GetKey(value.ToLower()); } catch { return !req; } } } namespace LockableDoors; [BepInPlugin("aedenthorn.LockableDoors", "Lockable Doors", "0.6.0")] public class BepInExPlugin : BaseUnityPlugin { [HarmonyPatch(typeof(Player), "PlacePiece")] private static class Player_PlacePiece_Patch { private static void Postfix(bool __result, Piece piece, GameObject ___m_placementGhost) { //IL_003f: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_00c0: Unknown result type (might be due to invalid IL or missing references) //IL_00c5: Unknown result type (might be due to invalid IL or missing references) //IL_00ca: Unknown result type (might be due to invalid IL or missing references) //IL_00cf: Unknown result type (might be due to invalid IL or missing references) //IL_0110: Unknown result type (might be due to invalid IL or missing references) if (!modEnabled.Value || !__result || !AedenthornUtils.CheckKeyHeld(modKey.Value)) { return; } Door component = ((Component)piece).gameObject.GetComponent(); if (Object.op_Implicit((Object)(object)component)) { Vector3 position = ___m_placementGhost.transform.position; Guid guid = Guid.NewGuid(); newDoors.Add(position, guid); if (promptNameOnCreate.Value) { Guid guid2 = guid; RenameDoor(guid2.ToString() ?? ""); } GameObject prefab = ZNetScene.instance.GetPrefab("DoorKey"); Dbgl($"Spawning door key(s) amount: {duplicateKeysOnCreate.Value}"); for (int i = 0; i < duplicateKeysOnCreate.Value; i++) { GameObject val = Object.Instantiate(prefab, ((Component)Player.m_localPlayer).transform.position + Vector3.up, Quaternion.identity); ItemData itemData = val.GetComponent().m_itemData; Guid guid2 = guid; itemData.m_crafterName = guid2.ToString() ?? ""; Dbgl($"Spawned door key for door {guid} at {position}"); } } } } [HarmonyPatch(typeof(ZNetScene), "Awake")] public static class ZNetScene_Awake_Patch { public static bool Prefix(ZNetScene __instance) { TryRegisterFabs(__instance); Debug.Log((object)"Loading the stuff"); return true; } } [HarmonyPatch(typeof(ObjectDB), "Awake")] public static class ObjectDB_Awake_Patch { public static void Postfix() { Debug.Log((object)"Trying to register Items"); RegisterItems(); } } [HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")] public static class ObjectDB_CopyOtherDB_Patch { public static void Postfix() { Debug.Log((object)"Trying to register Items"); RegisterItems(); } } [HarmonyPatch(typeof(Door), "UpdateState")] private static class Door_UpdateState_Patch { private static void Postfix(Door __instance, ZNetView ___m_nview) { //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_00a6: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_00db: Unknown result type (might be due to invalid IL or missing references) if (modEnabled.Value && ___m_nview.IsValid() && newDoors.Any() && newDoors.ContainsKey(((Component)__instance).transform.position)) { ___m_nview.GetZDO().Set("LockedDoor", true); ___m_nview.GetZDO().Set("DoorLocked", true); ___m_nview.GetZDO().Set("DoorGUID", newDoors[((Component)__instance).transform.position].ToString() ?? ""); Dbgl($"Set door {newDoors[((Component)__instance).transform.position]} as lockable at {((Component)__instance).transform.position}"); newDoors.Remove(((Component)__instance).transform.position); } } } [HarmonyPatch(typeof(Door), "Interact")] private static class Door_Interact_Patch { private static bool Prefix(Door __instance, ZNetView ___m_nview, bool __result, Humanoid character) { //IL_0314: Unknown result type (might be due to invalid IL or missing references) //IL_029b: Unknown result type (might be due to invalid IL or missing references) //IL_02a6: Unknown result type (might be due to invalid IL or missing references) //IL_02f2: Unknown result type (might be due to invalid IL or missing references) if (!modEnabled.Value || ___m_nview.GetZDO() == null || !(character is Player) || !___m_nview.GetZDO().GetBool("LockedDoor", false)) { return true; } string guid = ___m_nview.GetZDO().GetString("DoorGUID", ""); Dbgl("trying to open door " + ___m_nview.GetZDO().GetString("DoorGUID", "")); if (AedenthornUtils.CheckKeyHeld(modKey.Value) && ((character is Player) ? character : null).GetInventory().GetAllItems().Exists((ItemData i) => i.m_crafterName == guid)) { ___m_nview.GetZDO().Set("DoorLocked", !___m_nview.GetZDO().GetBool("DoorLocked", false)); __result = true; ((Character)character).Message((MessageType)2, string.Format(lockedMessage.Value, ___m_nview.GetZDO().GetBool("DoorLocked", false)), 0, (Sprite)null); Dbgl(string.Format("Door locked: {0}", ___m_nview.GetZDO().GetBool("DoorLocked", false))); return false; } if (AedenthornUtils.CheckKeyHeld(renameModKey.Value) && ((character is Player) ? character : null).GetInventory().GetAllItems().Exists((ItemData i) => i.m_crafterName == guid)) { __result = true; RenameDoor(guid); Dbgl(string.Format("Door name set to : {0}", ___m_nview.GetZDO().GetBool("DoorLocked", false))); return false; } if (___m_nview.GetZDO().GetBool("DoorLocked", false) && (needKeyToClose.Value || ___m_nview.GetZDO().GetInt("state", 0) == 0)) { Dbgl("Trying to open door " + ___m_nview.GetZDO().GetString("DoorGUID", "") + " " + ((character is Player) ? character : null).GetInventory().GetAllItems().Find((ItemData i) => i.m_crafterName == ___m_nview.GetZDO().GetString("DoorGUID", ""))?.m_crafterName); if (!((character is Player) ? character : null).GetInventory().GetAllItems().Exists((ItemData i) => i.m_crafterName == ___m_nview.GetZDO().GetString("DoorGUID", ""))) { __instance.m_lockedEffects.Create(((Component)__instance).transform.position, ((Component)__instance).transform.rotation, (Transform)null, 1f, -1); ((Character)character).Message((MessageType)2, Localization.instance.Localize("$msg_door_needkey", new string[1] { GetDoorName(guid) }), 0, (Sprite)null); __result = true; Dbgl($"player doesn't have key for {((Component)__instance).transform.position}"); return false; } Dbgl($"player has key for {((Component)__instance).transform.position}"); } return true; } } [HarmonyPatch(typeof(Door), "HaveKey")] private static class HaveKey_Patch { private static void Postfix(Door __instance, ZNetView ___m_nview, ref bool __result, Humanoid player) { if (__result && ___m_nview.GetZDO() != null && player is Player && !((player is Player) ? player : null).GetInventory().GetAllItems().Exists((ItemData i) => i.m_shared.m_name == __instance.m_keyItem.m_itemData.m_shared.m_name && (i.m_crafterID != 0L || i.m_crafterName.Length != 36))) { __result = false; Dbgl("Tried to open crypt with door key"); } } } [HarmonyPatch(typeof(Door), "GetHoverText")] private static class Door_GetHoverText_Patch { private static void Postfix(Door __instance, ref string __result, ZNetView ___m_nview) { if (modEnabled.Value && ___m_nview.GetZDO() != null && ___m_nview.GetZDO().GetBool("LockedDoor", false)) { __result = __result.Replace(Localization.instance.Localize(__instance.m_name), string.Format(doorName.Value, GetDoorName(___m_nview.GetZDO().GetString("DoorGUID", "")), ___m_nview.GetZDO().GetBool("DoorLocked", false))); } } } [HarmonyPatch(typeof(ItemData), "GetTooltip", new Type[] { typeof(ItemData), typeof(int), typeof(bool), typeof(float) })] private static class GetTooltip_Patch { private static void Postfix(ItemData item, ref string __result) { if (modEnabled.Value && IsDoorKey(item)) { __result = __result.Replace(item.m_shared.m_description, string.Format(keyDescription.Value, GetDoorName(item.m_crafterName))); } } } [HarmonyPatch(typeof(Character), "ShowPickupMessage")] private static class ShowPickupMessage_Patch { private static bool Prefix(Character __instance, ItemData item, int amount) { if (!modEnabled.Value || !IsDoorKey(item)) { return true; } __instance.Message((MessageType)1, "$msg_added " + string.Format(keyName.Value, GetDoorName(item.m_crafterName), amount, item.GetIcon()), 0, (Sprite)null); return false; } } [HarmonyPatch(typeof(ItemDrop), "GetHoverText")] private static class ItemDrop_GetHoverText_Patch { private static void Postfix(ItemDrop __instance, ref string __result) { if (modEnabled.Value && IsDoorKey(__instance.m_itemData)) { __result = __result.Replace(Localization.instance.Localize(__instance.m_itemData.m_shared.m_name), string.Format(keyName.Value, GetDoorName(__instance.m_itemData.m_crafterName))); } } } [HarmonyPatch(typeof(InventoryGrid), "CreateItemTooltip")] private static class CreateItemTooltip_Patch { private static void Postfix(ItemData item, UITooltip tooltip) { //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_003f: Unknown result type (might be due to invalid IL or missing references) if (modEnabled.Value && IsDoorKey(item)) { tooltip.Set(string.Format(keyName.Value, GetDoorName(item.m_crafterName)), tooltip.m_text, (RectTransform)null, default(Vector2)); } } } [HarmonyPatch(typeof(ItemData), "GetIcon")] private static class GetIcon_Patch { private static void Postfix(ItemData __instance, ref Sprite __result) { //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_005b: Expected O, but got Unknown //IL_0083: Unknown result type (might be due to invalid IL or missing references) //IL_0088: Unknown result type (might be due to invalid IL or missing references) string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), typeof(BepInExPlugin).Namespace, "icon.png"); if (modEnabled.Value && IsDoorKey(__instance) && File.Exists(path)) { if ((Object)(object)icon == (Object)null) { Texture2D val = new Texture2D(2, 2); byte[] array = File.ReadAllBytes(path); ImageConversion.LoadImage(val, array); icon = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), Vector2.zero); } __result = icon; } } } [HarmonyPatch(typeof(Terminal), "InputText")] private static class InputText_Patch { private static bool Prefix(Terminal __instance) { if (!modEnabled.Value) { return true; } string text = ((TMP_InputField)__instance.m_input).text; if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset")) { ((BaseUnityPlugin)context).Config.Reload(); ((BaseUnityPlugin)context).Config.Save(); icon = null; LoadDoorNames(); Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue(); Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue(); return false; } return true; } } private static BepInExPlugin context; public static ConfigEntry modEnabled; public static ConfigEntry isDebug; public static ConfigEntry nexusID; public static ConfigEntry needKeyToClose; public static ConfigEntry customKeyIconFile; public static ConfigEntry modKey; public static ConfigEntry renameModKey; public static ConfigEntry duplicateKeysOnCreate; public static ConfigEntry doorName; public static ConfigEntry keyName; public static ConfigEntry keyDescription; public static ConfigEntry lockedMessage; public static ConfigEntry namePrompt; public static ConfigEntry doorNames; public static ConfigEntry defaultName; public static ConfigEntry promptNameOnCreate; public static ConfigEntry maxDoorNames; public static GameObject aedenkey; private static Dictionary newDoors = new Dictionary(); private static Dictionary doorNameDict = new Dictionary(); private static Sprite icon = null; public static void Dbgl(string str = "", bool pref = true) { if (isDebug.Value) { Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str)); } } private void Awake() { context = this; modEnabled = ((BaseUnityPlugin)this).Config.Bind("General", "Enabled", true, "Enable this mod"); isDebug = ((BaseUnityPlugin)this).Config.Bind("General", "IsDebug", false, "Enable debug logs"); nexusID = ((BaseUnityPlugin)this).Config.Bind("General", "NexusID", 1346, "Nexus mod ID for updates"); modKey = ((BaseUnityPlugin)this).Config.Bind("Options", "LockModKey", "left ctrl", "Modifier key used to create and lock/unlock a lockable door when interacting."); renameModKey = ((BaseUnityPlugin)this).Config.Bind("Options", "RenameModKey", "left alt", "Modifier key used to rename a lockable door when interacting."); duplicateKeysOnCreate = ((BaseUnityPlugin)this).Config.Bind("Options", "DuplicateKeysOnCreate", 1, "Amount of duplicate keys to be created per door."); doorName = ((BaseUnityPlugin)this).Config.Bind("Strings", "DoorName", "{0} Door [Locked:{1}]", "Name of door - replaces {0} with the door coordinates or name and {1} with locked status."); keyName = ((BaseUnityPlugin)this).Config.Bind("Strings", "KeyName", "{0} Door Key", "Name of key - replaces {0} with the door coordinates."); keyDescription = ((BaseUnityPlugin)this).Config.Bind("Strings", "KeyDescription", "Opens {0} Door.", "Description of key in tooltip - replaces {0} with the door name."); lockedMessage = ((BaseUnityPlugin)this).Config.Bind("Strings", "LockedMessage", "Door locked: {0}", "Message to show when locking/unlocking the door. Replaces {0} with true or false."); namePrompt = ((BaseUnityPlugin)this).Config.Bind("Strings", "NamePrompt", "Enter Name for Door:", "Prompt to show when naming a door / key pair."); defaultName = ((BaseUnityPlugin)this).Config.Bind("Strings", "DefaultName", "Locked", "Default name for a door if left blank."); needKeyToClose = ((BaseUnityPlugin)this).Config.Bind("Toggles", "NeedKeyToClose", false, "Require key in order to close a door as well as open it."); promptNameOnCreate = ((BaseUnityPlugin)this).Config.Bind("Toggles", "PromptNameOnCreate", true, "Prompt to enter a name for the door / key pair on creation."); maxDoorNames = ((BaseUnityPlugin)this).Config.Bind("Variables", "maxDoorNames", 20, "Max door names before removing old names."); doorNames = ((BaseUnityPlugin)this).Config.Bind("ZAuto", "DoorNames", "", "List of doorName:coord pairs, populated when renaming your doors."); LoadAssets(); LoadDoorNames(); Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null); } public static void TryRegisterFabs(ZNetScene zNetScene) { if (!((Object)(object)zNetScene == (Object)null) && zNetScene.m_prefabs != null && zNetScene.m_prefabs.Count > 0) { zNetScene.m_prefabs.Add(aedenkey); } } private static AssetBundle GetAssetBundleFromResources(string filename) { Assembly executingAssembly = Assembly.GetExecutingAssembly(); string name = executingAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith(filename)); using Stream stream = executingAssembly.GetManifestResourceStream(name); return AssetBundle.LoadFromStream(stream); } public static void LoadAssets() { AssetBundle assetBundleFromResources = GetAssetBundleFromResources("aedenkey"); aedenkey = assetBundleFromResources.LoadAsset("DoorKey"); if (assetBundleFromResources != null) { assetBundleFromResources.Unload(false); } } public static void RegisterItems() { if (ObjectDB.instance.m_items.Count == 0 || (Object)(object)ObjectDB.instance.GetItemPrefab("Amber") == (Object)null) { Debug.Log((object)"Waiting for game to initialize before adding prefabs."); return; } ItemDrop component = aedenkey.GetComponent(); if ((Object)(object)component != (Object)null && (Object)(object)ObjectDB.instance.GetItemPrefab(StringExtensionMethods.GetStableHashCode(((Object)aedenkey).name)) == (Object)null) { Debug.Log((object)"Loading ItemDrops"); ObjectDB.instance.m_items.Add(aedenkey); } if ((Object)(object)component == (Object)null) { Debug.Log((object)"There is no object with ItemDrop attempted to be insterted to objectDB this is bad..."); } } private static void LoadDoorNames() { if (doorNames.Value.Length != 0) { string[] array = doorNames.Value.Split(new char[1] { ';' }); foreach (string text in array) { string[] array2 = text.Split(new char[1] { ':' }); doorNameDict.Add(array2[0], array2[1]); } } } public static void SetDoorName(string guid, string text) { doorNameDict[guid] = text; List list = new List(); foreach (KeyValuePair item in doorNameDict) { list.Add(item.Key + ":" + item.Value); } if (list.Count > maxDoorNames.Value) { list.RemoveAt(0); } doorNames.Value = string.Join(";", list); } private static string GetDoorName(string guid) { if (!doorNameDict.ContainsKey(guid) || doorNameDict[guid].Length <= 0) { return defaultName.Value; } return doorNameDict[guid]; } public static void RenameDoor(string guid) { MyTextReceiver myTextReceiver = new MyTextReceiver(guid); if (doorNameDict.ContainsKey(guid)) { myTextReceiver.text = doorNameDict[guid]; } TextInput.instance.RequestText((TextReceiver)(object)myTextReceiver, namePrompt.Value, 255); } private static bool IsDoorKey(ItemData i) { if (i.m_shared.m_name == "$item_aeden_doorkey" && i.m_crafterID == 0L) { return i.m_crafterName.Length == 36; } return false; } } public class MyTextReceiver : TextReceiver { public string text; public string guid; public MyTextReceiver(string guid) { this.guid = guid; } public string GetText() { return text; } public void SetText(string text) { this.text = text; BepInExPlugin.SetDoorName(guid, text); } }