Decompiled source of HardheimTorchWarmth v1.0.0

plugins/HardheimTorchWarmt.dll

Decompiled 8 hours ago
using System;
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 Jotunn.Managers;
using Jotunn.Utils;
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("HardheimTorchWarmt")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HardheimTorchWarmt")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("0e15bd9a-1096-4c46-bfa0-fe5561f50cff")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace HardheimTorchWarmth;

[BepInPlugin("hardheim.torchwarmth", "Hardheim Torch Warmth", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[SynchronizationMode(/*Could not decode attribute arguments.*/)]
public class HardheimTorchWarmthPlugin : BaseUnityPlugin
{
	public const string ModGuid = "hardheim.torchwarmth";

	public const string ModName = "Hardheim Torch Warmth";

	public const string ModVersion = "1.0.0";

	internal static HardheimTorchWarmthPlugin Instance;

	internal static ManualLogSource Log;

	private Harmony _harmony;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<float> CheckIntervalSeconds;

	internal static ConfigEntry<float> StaminaPerTick;

	internal static ConfigEntry<float> HealthPerTick;

	internal static ConfigEntry<bool> ShowMessageOnStateChange;

	internal static ConfigEntry<string> StatusEffectName;

	internal static ConfigEntry<string> StatusEffectTooltip;

	internal static ConfigEntry<string> TorchKeywords;

	internal static ConfigEntry<bool> LoggingEnabled;

	private void Awake()
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		CreateConfigEntries();
		SynchronizationManager.OnConfigurationSynchronized += OnConfigurationSynchronized;
		_harmony = new Harmony("hardheim.torchwarmth");
		_harmony.PatchAll();
		LogAlways("Hardheim Torch Warmth betöltve. Verzió: 1.0.0");
	}

	private void OnDestroy()
	{
		try
		{
			SynchronizationManager.OnConfigurationSynchronized -= OnConfigurationSynchronized;
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch (Exception arg)
		{
			LogError($"Hiba az unpatch közben: {arg}");
		}
	}

	private void OnConfigurationSynchronized(object sender, ConfigurationSynchronizationEventArgs args)
	{
		try
		{
			LogAlways($"Config sync esemény. Initial={args.InitialSynchronization}");
			LogAlways($"Synced Enabled={Enabled.Value}");
			LogAlways($"Synced CheckIntervalSeconds={CheckIntervalSeconds.Value}");
			LogAlways($"Synced StaminaPerTick={StaminaPerTick.Value}");
			LogAlways($"Synced HealthPerTick={HealthPerTick.Value}");
			LogAlways($"Synced ShowMessageOnStateChange={ShowMessageOnStateChange.Value}");
			LogAlways("Synced StatusEffectName=" + StatusEffectName.Value);
			LogAlways("Synced StatusEffectTooltip=" + StatusEffectTooltip.Value);
			LogAlways("Synced TorchKeywords=" + TorchKeywords.Value);
		}
		catch (Exception arg)
		{
			LogError($"Hiba a config sync esemény kezelése közben: {arg}");
		}
	}

	private void CreateConfigEntries()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Expected O, but got Unknown
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Expected O, but got Unknown
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Expected O, but got Unknown
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Expected O, but got Unknown
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Expected O, but got Unknown
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Expected O, but got Unknown
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cc: Expected O, but got Unknown
		ConfigurationManagerAttributes val = new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, new ConfigDescription("Mod engedélyezése.", (AcceptableValueBase)null, new object[1] { val }));
		CheckIntervalSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("1 - General", "CheckIntervalSeconds", 3f, new ConfigDescription("Milyen gyakran ellenőrizze a játékos kezében lévő tárgyat és alkalmazza a hatást.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), new object[1] { val }));
		StaminaPerTick = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Warmth", "StaminaPerTick", 3f, new ConfigDescription("Ennyi staminát ad minden egyes ellenőrzési ticknél, ha a játékos kézben tartott fáklyával van.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), new object[1] { val }));
		HealthPerTick = ((BaseUnityPlugin)this).Config.Bind<float>("2 - Warmth", "HealthPerTick", 2f, new ConfigDescription("Ennyi életet ad minden egyes ellenőrzési ticknél, ha a játékos kézben tartott fáklyával van.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), new object[1] { val }));
		ShowMessageOnStateChange = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Warmth", "ShowMessageOnStateChange", true, new ConfigDescription("Jelenjen meg játékbeli üzenet, amikor a fáklya melege aktiválódik vagy megszűnik.", (AcceptableValueBase)null, new object[1] { val }));
		StatusEffectName = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Warmth", "StatusEffectName", "Fáklya melege", new ConfigDescription("Az egyedi buff megjelenített neve.", (AcceptableValueBase)null, new object[1] { val }));
		StatusEffectTooltip = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Warmth", "StatusEffectTooltip", "A kézben tartott fáklya kellemes meleget ad.", new ConfigDescription("Az egyedi buff leírása.", (AcceptableValueBase)null, new object[1] { val }));
		TorchKeywords = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Warmth", "TorchKeywords", "torch,faklya,fáklya", new ConfigDescription("Vesszővel elválasztott kulcsszavak. Ha a kézben tartott tárgy neve vagy prefab neve tartalmazza valamelyiket, a mod fáklyának tekinti.", (AcceptableValueBase)null, new object[1] { val }));
		LoggingEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Debug", "LoggingEnabled", false, "Részletes logolás engedélyezése. Ez csak helyi debug célra szolgál.");
	}

	internal static void LogAlways(string message)
	{
		Log.LogInfo((object)("[Hardheim Torch Warmth] " + message));
	}

	internal static void LogInfo(string message)
	{
		if (LoggingEnabled != null && LoggingEnabled.Value)
		{
			Log.LogInfo((object)("[Hardheim Torch Warmth] " + message));
		}
	}

	internal static void LogWarning(string message)
	{
		Log.LogWarning((object)("[Hardheim Torch Warmth] " + message));
	}

	internal static void LogError(string message)
	{
		Log.LogError((object)("[Hardheim Torch Warmth] " + message));
	}
}
internal static class TorchWarmthStatusEffectManager
{
	internal const string StatusEffectInternalName = "SE_HardheimTorchWarmth";

	private static SE_Stats _statusEffect;

	private static bool _initialized;

	internal static void EnsureInitialized()
	{
		try
		{
			if (_initialized && (Object)(object)_statusEffect != (Object)null)
			{
				UpdateDynamicFields();
				return;
			}
			_statusEffect = ScriptableObject.CreateInstance<SE_Stats>();
			((Object)_statusEffect).name = "SE_HardheimTorchWarmth";
			TrySetTorchIcon(initialLoad: true);
			((StatusEffect)_statusEffect).m_name = HardheimTorchWarmthPlugin.StatusEffectName.Value;
			((StatusEffect)_statusEffect).m_tooltip = HardheimTorchWarmthPlugin.StatusEffectTooltip.Value;
			((StatusEffect)_statusEffect).m_ttl = Mathf.Max(1.5f, HardheimTorchWarmthPlugin.CheckIntervalSeconds.Value + 1f);
			((StatusEffect)_statusEffect).m_startMessage = "";
			((StatusEffect)_statusEffect).m_stopMessage = "";
			((StatusEffect)_statusEffect).m_flashIcon = false;
			((StatusEffect)_statusEffect).m_cooldownIcon = false;
			_initialized = true;
			HardheimTorchWarmthPlugin.LogInfo("Az egyedi status effect inicializálva.");
		}
		catch (Exception arg)
		{
			HardheimTorchWarmthPlugin.LogError($"Hiba az egyedi status effect létrehozásakor: {arg}");
		}
	}

	internal static void UpdateDynamicFields()
	{
		if (!((Object)(object)_statusEffect == (Object)null))
		{
			((StatusEffect)_statusEffect).m_name = HardheimTorchWarmthPlugin.StatusEffectName.Value;
			((StatusEffect)_statusEffect).m_tooltip = HardheimTorchWarmthPlugin.StatusEffectTooltip.Value;
			((StatusEffect)_statusEffect).m_ttl = Mathf.Max(1.5f, HardheimTorchWarmthPlugin.CheckIntervalSeconds.Value + 1f);
			if ((Object)(object)((StatusEffect)_statusEffect).m_icon == (Object)null)
			{
				TrySetTorchIcon(initialLoad: false);
			}
		}
	}

	private static void TrySetTorchIcon(bool initialLoad)
	{
		try
		{
			if ((Object)(object)ObjectDB.instance == (Object)null)
			{
				HardheimTorchWarmthPlugin.LogWarning("ObjectDB.instance még null, ezért az ikon most nem állítható be.");
				return;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Torch");
			if ((Object)(object)itemPrefab == (Object)null)
			{
				HardheimTorchWarmthPlugin.LogWarning("A Torch prefab nem található az ObjectDB-ben.");
				return;
			}
			ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
			if ((Object)(object)component == (Object)null || component.m_itemData == null || component.m_itemData.m_shared == null)
			{
				HardheimTorchWarmthPlugin.LogWarning("A Torch ItemDrop vagy m_itemData/m_shared hiányzik.");
				return;
			}
			Sprite[] icons = component.m_itemData.m_shared.m_icons;
			if (icons != null && icons.Length != 0 && (Object)(object)icons[0] != (Object)null)
			{
				((StatusEffect)_statusEffect).m_icon = icons[0];
				if (initialLoad)
				{
					HardheimTorchWarmthPlugin.LogInfo("Torch ikon sikeresen beállítva.");
				}
				else
				{
					HardheimTorchWarmthPlugin.LogInfo("Torch ikon utólag sikeresen beállítva.");
				}
			}
			else
			{
				HardheimTorchWarmthPlugin.LogWarning("A Torch itemhez nem találtam használható ikont.");
			}
		}
		catch (Exception arg)
		{
			if (initialLoad)
			{
				HardheimTorchWarmthPlugin.LogError($"Hiba az ikon betöltésekor: {arg}");
			}
			else
			{
				HardheimTorchWarmthPlugin.LogError($"Hiba az ikon utólagos betöltésekor: {arg}");
			}
		}
	}

	internal static SE_Stats GetStatusEffect()
	{
		EnsureInitialized();
		UpdateDynamicFields();
		return _statusEffect;
	}

	internal static int GetStableHash()
	{
		return "SE_HardheimTorchWarmth".GetStableHashCodeCompat();
	}
}
internal static class TorchWarmthController
{
	private static float _nextCheckTime;

	private static bool _warmthActive;

	internal static void Update(Player player)
	{
		if (!HardheimTorchWarmthPlugin.Enabled.Value || (Object)(object)player == (Object)null || (Object)(object)player != (Object)(object)Player.m_localPlayer || !((Character)player).IsOwner())
		{
			return;
		}
		TorchWarmthStatusEffectManager.EnsureInitialized();
		if (Time.time < _nextCheckTime)
		{
			return;
		}
		float num = Mathf.Max(0.1f, HardheimTorchWarmthPlugin.CheckIntervalSeconds.Value);
		_nextCheckTime = Time.time + num;
		if (HasHandTorchEquipped(player))
		{
			ApplyWarmth(player);
			EnsureStatusEffect(player);
			if (!_warmthActive)
			{
				_warmthActive = true;
				OnWarmthStarted(player);
			}
		}
		else
		{
			RemoveStatusEffect(player);
			if (_warmthActive)
			{
				_warmthActive = false;
				OnWarmthStopped(player);
			}
		}
	}

	private static void ApplyWarmth(Player player)
	{
		float num = Mathf.Max(0f, HardheimTorchWarmthPlugin.StaminaPerTick.Value);
		float num2 = Mathf.Max(0f, HardheimTorchWarmthPlugin.HealthPerTick.Value);
		float health = ((Character)player).GetHealth();
		float maxHealth = ((Character)player).GetMaxHealth();
		if (num > 0f)
		{
			((Character)player).AddStamina(num);
		}
		bool flag = false;
		if (num2 > 0f && health < maxHealth)
		{
			flag = true;
			((Character)player).Heal(num2, true);
		}
		float health2 = ((Character)player).GetHealth();
		HardheimTorchWarmthPlugin.LogInfo("Fáklya melege aktív | " + $"healthTick={num2:0.##} | " + $"staminaTick={num:0.##} | " + $"attemptedHeal={flag} | " + $"HP előtte={health:0.##}/{maxHealth:0.##} | " + $"HP utána={health2:0.##}/{maxHealth:0.##}");
	}

	private static void EnsureStatusEffect(Player player)
	{
		try
		{
			SEMan sEMan = ((Character)player).GetSEMan();
			if (sEMan == null)
			{
				return;
			}
			int stableHash = TorchWarmthStatusEffectManager.GetStableHash();
			StatusEffect statusEffect = sEMan.GetStatusEffect(stableHash);
			if ((Object)(object)statusEffect == (Object)null)
			{
				SE_Stats statusEffect2 = TorchWarmthStatusEffectManager.GetStatusEffect();
				if ((Object)(object)statusEffect2 != (Object)null)
				{
					sEMan.AddStatusEffect((StatusEffect)(object)statusEffect2, false, 0, 0f);
					HardheimTorchWarmthPlugin.LogInfo("Egyedi status effect felhelyezve.");
				}
			}
			else
			{
				statusEffect.ResetTime();
			}
		}
		catch (Exception arg)
		{
			HardheimTorchWarmthPlugin.LogError($"Hiba a status effect felhelyezése közben: {arg}");
		}
	}

	private static void RemoveStatusEffect(Player player)
	{
		try
		{
			SEMan sEMan = ((Character)player).GetSEMan();
			if (sEMan != null)
			{
				int stableHash = TorchWarmthStatusEffectManager.GetStableHash();
				if ((Object)(object)sEMan.GetStatusEffect(stableHash) != (Object)null)
				{
					sEMan.RemoveStatusEffect(stableHash, false);
					HardheimTorchWarmthPlugin.LogInfo("Egyedi status effect eltávolítva.");
				}
			}
		}
		catch (Exception arg)
		{
			HardheimTorchWarmthPlugin.LogError($"Hiba a status effect eltávolítása közben: {arg}");
		}
	}

	private static void OnWarmthStarted(Player player)
	{
		HardheimTorchWarmthPlugin.LogInfo("A fáklya melege aktiválódott.");
		if (HardheimTorchWarmthPlugin.ShowMessageOnStateChange.Value)
		{
			((Character)player).Message((MessageType)1, HardheimTorchWarmthPlugin.StatusEffectName.Value, 0, (Sprite)null);
		}
	}

	private static void OnWarmthStopped(Player player)
	{
		HardheimTorchWarmthPlugin.LogInfo("A fáklya melege megszűnt.");
		if (HardheimTorchWarmthPlugin.ShowMessageOnStateChange.Value)
		{
			((Character)player).Message((MessageType)1, HardheimTorchWarmthPlugin.StatusEffectName.Value + " megszűnt", 0, (Sprite)null);
		}
	}

	private static bool HasHandTorchEquipped(Player player)
	{
		try
		{
			ItemData currentWeapon = ((Humanoid)player).GetCurrentWeapon();
			if (currentWeapon?.m_shared == null)
			{
				return false;
			}
			string text = currentWeapon.m_shared.m_name ?? string.Empty;
			string text2 = (((Object)(object)currentWeapon.m_dropPrefab != (Object)null) ? (((Object)currentWeapon.m_dropPrefab).name ?? string.Empty) : string.Empty);
			string text3 = (text + "|" + text2).ToLowerInvariant();
			string[] array = HardheimTorchWarmthPlugin.TorchKeywords.Value.ToLowerInvariant().Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = array;
			foreach (string text4 in array2)
			{
				string text5 = text4.Trim();
				if (!string.IsNullOrWhiteSpace(text5) && text3.Contains(text5))
				{
					HardheimTorchWarmthPlugin.LogInfo("Fáklyának felismert tárgy: név='" + text + "', prefab='" + text2 + "', kulcsszó='" + text5 + "'");
					return true;
				}
			}
		}
		catch (Exception arg)
		{
			HardheimTorchWarmthPlugin.LogError($"Hiba a kézben tartott tárgy ellenőrzése közben: {arg}");
		}
		return false;
	}
}
[HarmonyPatch(typeof(Player), "Update")]
internal static class PlayerUpdateTorchWarmthPatch
{
	private static void Postfix(Player __instance)
	{
		TorchWarmthController.Update(__instance);
	}
}
[HarmonyPatch(typeof(ObjectDB), "Awake")]
internal static class ObjectDBAwakePatch
{
	private static void Postfix()
	{
		TorchWarmthStatusEffectManager.EnsureInitialized();
	}
}
[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
internal static class ObjectDBCopyOtherDBPatch
{
	private static void Postfix()
	{
		TorchWarmthStatusEffectManager.EnsureInitialized();
	}
}
public static class StringExtensionMethodsCompat
{
	public static int GetStableHashCodeCompat(this string str)
	{
		int num = 5381;
		int num2 = num;
		for (int i = 0; i < str.Length && str[i] != 0; i += 2)
		{
			num = ((num << 5) + num) ^ str[i];
			if (i == str.Length - 1 || str[i + 1] == '\0')
			{
				break;
			}
			num2 = ((num2 << 5) + num2) ^ str[i + 1];
		}
		return num + num2 * 1566083941;
	}
}