Decompiled source of OdinStorage ModernFix v1.0.5

OdinStorageFix.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using OdinStorage;
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: AssemblyCompany("Ntxfloy")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Modern Valheim compatibility patch for OdinStorage by Koehlerworks. Fixes terminal interaction, restores hover prompts, and enables direct UI hotkey opening.")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+18c7abf26918fc53141468904e4f2facb5b27509")]
[assembly: AssemblyProduct("OdinStorageFix")]
[assembly: AssemblyTitle("OdinStorageFix")]
[assembly: AssemblyVersion("1.0.3.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 OdinStorageFix
{
	public class FixedStorageTerminal : MonoBehaviour, Interactable, Hoverable
	{
		public string GetHoverName()
		{
			if (Localization.instance != null)
			{
				string text = Localization.instance.Localize("$piece_odinterminal_name");
				if (!string.IsNullOrEmpty(text) && !text.StartsWith("$"))
				{
					return text;
				}
			}
			return "Odin Terminal";
		}

		public string GetHoverText()
		{
			if (Localization.instance != null)
			{
				return Localization.instance.Localize("[<color=yellow><b>$KEY_Use</b></color>] Open Storage");
			}
			return "[E] Open Storage";
		}

		public float GetHoverOffset()
		{
			return 0f;
		}

		public bool Interact(Humanoid user, bool hold, bool alt)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if (hold)
			{
				return false;
			}
			StorageTerminalUI.Open(((Component)this).transform.position);
			return true;
		}

		public bool UseItem(Humanoid user, ItemData item)
		{
			return false;
		}
	}
	[BepInPlugin("com.ntxfloy.odinstoragefix", "OdinStorage Fix", "1.0.5")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class OdinStorageFixPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.ntxfloy.odinstoragefix";

		public const string PluginName = "OdinStorage Fix";

		public const string PluginVersion = "1.0.5";

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("com.ntxfloy.odinstoragefix");
			SafePatch(typeof(Patches.PlayerInteractPatch));
			SafePatch(typeof(Patches.PieceAwakePatch));
			SafePatch(typeof(Patches.ZNetSceneAwakePatch));
			SafePatch(typeof(Patches.PluginUpdatePatch));
			Log.LogInfo((object)"OdinStorage Fix v1.0.5 initialized.");
		}

		private void SafePatch(Type patchClass)
		{
			try
			{
				_harmony.CreateClassProcessor(patchClass).Patch();
				Log.LogInfo((object)("[OdinStorageFix] Patched: " + patchClass.Name));
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("[OdinStorageFix] Patch skipped (" + patchClass.Name + "): " + ex.Message));
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	public static class Patches
	{
		[HarmonyPatch(typeof(Player), "Interact")]
		public static class PlayerInteractPatch
		{
			public static bool Prefix(Player __instance, GameObject go, bool hold, bool alt)
			{
				//IL_004c: Unknown result type (might be due to invalid IL or missing references)
				if (hold)
				{
					return true;
				}
				if ((Object)(object)go == (Object)null)
				{
					return true;
				}
				Piece componentInParent = go.GetComponentInParent<Piece>();
				if ((Object)(object)componentInParent == (Object)null)
				{
					return true;
				}
				if (!(((Object)componentInParent).name ?? "").StartsWith("OdinTerminal", StringComparison.OrdinalIgnoreCase))
				{
					return true;
				}
				try
				{
					StorageTerminalUI.Create();
					StorageTerminalUI.Open(((Component)__instance).transform.position);
					return false;
				}
				catch (Exception ex)
				{
					OdinStorageFixPlugin.Log.LogWarning((object)("[OdinStorageFix] PlayerInteract error: " + ex.Message));
					return true;
				}
			}
		}

		[HarmonyPatch(typeof(Piece), "Awake")]
		public static class PieceAwakePatch
		{
			public static void Postfix(Piece __instance)
			{
				if (!((Object)(object)__instance == (Object)null))
				{
					string name = ((Object)__instance).name;
					if (!string.IsNullOrEmpty(name) && name.StartsWith("OdinTerminal", StringComparison.OrdinalIgnoreCase) && (Object)(object)((Component)__instance).GetComponent<FixedStorageTerminal>() == (Object)null)
					{
						((Component)__instance).gameObject.AddComponent<FixedStorageTerminal>();
						OdinStorageFixPlugin.Log.LogInfo((object)("[OdinStorageFix] Attached FixedStorageTerminal to world piece: " + name));
					}
				}
			}
		}

		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		public static class ZNetSceneAwakePatch
		{
			public static void Postfix(ZNetScene __instance)
			{
				if (!((Object)(object)__instance == (Object)null))
				{
					GameObject prefab = __instance.GetPrefab("OdinTerminal");
					if ((Object)(object)prefab != (Object)null && (Object)(object)prefab.GetComponent<FixedStorageTerminal>() == (Object)null)
					{
						prefab.AddComponent<FixedStorageTerminal>();
						OdinStorageFixPlugin.Log.LogInfo((object)"[OdinStorageFix] Attached FixedStorageTerminal to prefab 'OdinTerminal'.");
					}
				}
			}
		}

		[HarmonyPatch(typeof(Plugin), "Update")]
		public static class PluginUpdatePatch
		{
			public static bool Prefix(Plugin __instance)
			{
				//IL_0032: Unknown result type (might be due to invalid IL or missing references)
				//IL_0037: Unknown result type (might be due to invalid IL or missing references)
				//IL_005a: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
				//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)Player.m_localPlayer == (Object)null)
				{
					return false;
				}
				try
				{
					bool isOpen = StorageTerminalUI.IsOpen;
					KeyboardShortcut value;
					if (_hotkeyField?.GetValue(null) is ConfigEntry<KeyboardShortcut> val)
					{
						value = val.Value;
						if (((KeyboardShortcut)(ref value)).IsDown())
						{
							if (isOpen)
							{
								StorageTerminalUI.Close();
							}
							else
							{
								StorageTerminalUI.Create();
								StorageTerminalUI.Open(((Component)Player.m_localPlayer).transform.position);
							}
							return false;
						}
					}
					if (isOpen && Input.GetKeyDown((KeyCode)27))
					{
						StorageTerminalUI.Close();
						return false;
					}
					if (_devToolsField?.GetValue(null) is ConfigEntry<bool> val2 && val2.Value)
					{
						if (_spawnKeyField?.GetValue(null) is ConfigEntry<KeyboardShortcut> val3)
						{
							value = val3.Value;
							if (((KeyboardShortcut)(ref value)).IsDown())
							{
								DevTools.SpawnTestStorage();
							}
						}
						if (_clearKeyField?.GetValue(null) is ConfigEntry<KeyboardShortcut> val4)
						{
							value = val4.Value;
							if (((KeyboardShortcut)(ref value)).IsDown())
							{
								DevTools.ClearTestStorage();
							}
						}
					}
				}
				catch (Exception ex)
				{
					OdinStorageFixPlugin.Log.LogWarning((object)("[OdinStorageFix] PluginUpdatePatch error: " + ex.Message));
				}
				return false;
			}
		}

		private static readonly FieldInfo _hotkeyField = AccessTools.Field(typeof(Plugin), "Hotkey");

		private static readonly FieldInfo _devToolsField = AccessTools.Field(typeof(Plugin), "DevTools");

		private static readonly FieldInfo _spawnKeyField = AccessTools.Field(typeof(Plugin), "SpawnKey");

		private static readonly FieldInfo _clearKeyField = AccessTools.Field(typeof(Plugin), "ClearKey");
	}
}