Decompiled source of Venture Logout Tweaks v0.5.0

Plugins/VentureValheim.LogoutTweaks.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("VentureValheim.LogoutTweaks")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VentureValheim.LogoutTweaks")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("D19D40C6-9069-40E0-823D-ACEDD32A7821")]
[assembly: AssemblyFileVersion("0.5.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.5.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace VentureValheim.LogoutTweaks
{
	[BepInPlugin("com.orianaventure.mod.LogoutTweaks", "LogoutTweaks", "0.5.0")]
	public class LogoutTweaksPlugin : BaseUnityPlugin
	{
		private const string ModName = "LogoutTweaks";

		private const string ModVersion = "0.5.0";

		private const string Author = "com.orianaventure.mod";

		private const string ModGUID = "com.orianaventure.mod.LogoutTweaks";

		private readonly Harmony HarmonyInstance = new Harmony("com.orianaventure.mod.LogoutTweaks");

		public static readonly ManualLogSource LogoutTweaksLogger = Logger.CreateLogSource("LogoutTweaks");

		public void Awake()
		{
			LogoutTweaksLogger.LogInfo((object)"The cat fell in the toilet, BRB!");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
		}
	}
	public class LogoutTweaks
	{
		private readonly struct FileData
		{
			public List<StatusEffectManager.StatusEffectData> StatusEffects { get; }

			public FileData(List<StatusEffectManager.StatusEffectData> effects)
			{
				StatusEffects = effects;
			}

			public FileData(string saveString)
			{
				StatusEffects = new List<StatusEffectManager.StatusEffectData>();
				if (saveString == null)
				{
					return;
				}
				string[] array = saveString.Split(new char[1] { ';' });
				if (array != null)
				{
					for (int i = 0; i < array.Length; i++)
					{
						StatusEffectManager.StatusEffectData item = new StatusEffectManager.StatusEffectData(array[i]);
						StatusEffects.Add(item);
					}
				}
			}

			public override string ToString()
			{
				string text = "";
				foreach (StatusEffectManager.StatusEffectData statusEffect in StatusEffects)
				{
					text = text + statusEffect.ToString() + ";";
				}
				return text;
			}
		}

		[HarmonyPatch(typeof(Player), "Save")]
		public static class Patch_Player_Save
		{
			private static void Prefix(Player __instance)
			{
				if (!IsInTheMainScene())
				{
					return;
				}
				try
				{
					List<StatusEffectManager.StatusEffectData> list = new List<StatusEffectManager.StatusEffectData>();
					List<StatusEffect> statusEffects = ((Character)__instance).m_seman.GetStatusEffects();
					if (statusEffects == null)
					{
						LogoutTweaksPlugin.LogoutTweaksLogger.LogInfo((object)"Unable to determine status effects, this can indicate a mod conflict.");
					}
					else
					{
						for (int i = 0; i < statusEffects.Count; i++)
						{
							if (StatusEffectManager.SupportedStatusEffect(statusEffects[i].m_nameHash))
							{
								StatusEffectManager.StatusEffectData item = new StatusEffectManager.StatusEffectData(statusEffects[i]);
								list.Add(item);
							}
						}
					}
					FileData data = new FileData(list);
					Instance.SaveData(ref __instance, data);
				}
				catch (Exception ex)
				{
					LogoutTweaksPlugin.LogoutTweaksLogger.LogError((object)"Error trying to parse and save extra data.");
					LogoutTweaksPlugin.LogoutTweaksLogger.LogError((object)ex);
				}
			}
		}

		[HarmonyPatch(typeof(Player), "Load")]
		public static class Patch_Player_Load
		{
			private static void Postfix(Player __instance)
			{
				if (!IsInTheMainScene())
				{
					return;
				}
				FileData? fileData = Instance.LoadData(ref __instance);
				if (!fileData.HasValue)
				{
					return;
				}
				FileData value = fileData.Value;
				if (value.StatusEffects != null)
				{
					List<StatusEffectManager.StatusEffectData> statusEffects = value.StatusEffects;
					for (int i = 0; i < statusEffects.Count; i++)
					{
						try
						{
							StatusEffect val = StatusEffectManager.BuildStatusEffect(statusEffects[i]);
							if ((Object)(object)val != (Object)null)
							{
								((Character)__instance).m_seman.AddStatusEffect(val, false, 0, 0f);
							}
						}
						catch (Exception ex)
						{
							LogoutTweaksPlugin.LogoutTweaksLogger.LogWarning((object)$"Status Effect {statusEffects[i].Name} could not be restored.");
							LogoutTweaksPlugin.LogoutTweaksLogger.LogDebug((object)ex);
						}
					}
					Hud.instance.UpdateStatusEffects(((Character)__instance).m_seman.m_statusEffects);
				}
				Instance.ClearData(ref __instance);
			}
		}

		private static readonly LogoutTweaks _instance = new LogoutTweaks();

		private const string PLAYER_SAVE_KEY = "VV_LogoutData";

		public static LogoutTweaks Instance => _instance;

		private LogoutTweaks()
		{
		}

		private void SaveData(ref Player player, FileData data)
		{
			if (!((Object)(object)player == (Object)null))
			{
				if (player.m_customData.ContainsKey("VV_LogoutData"))
				{
					player.m_customData["VV_LogoutData"] = data.ToString();
				}
				else
				{
					player.m_customData.Add("VV_LogoutData", data.ToString());
				}
			}
		}

		private void ClearData(ref Player player)
		{
			if ((Object)(object)player != (Object)null && player.m_customData.ContainsKey("VV_LogoutData"))
			{
				player.m_customData["VV_LogoutData"] = "";
			}
		}

		private FileData? LoadData(ref Player player)
		{
			if ((Object)(object)player != (Object)null && player.m_customData.ContainsKey("VV_LogoutData"))
			{
				return new FileData(player.m_customData["VV_LogoutData"]);
			}
			return null;
		}

		public static bool IsInTheMainScene()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			return ((Scene)(ref activeScene)).name.Equals("main");
		}
	}
	public class StatusEffectManager
	{
		public readonly struct StatusEffectData
		{
			public int Name { get; }

			public float Ttl { get; }

			public float Time { get; }

			public float Value1 { get; }

			public float Value2 { get; }

			public float Value3 { get; }

			public float Value4 { get; }

			public StatusEffectData(StatusEffect se)
			{
				//IL_005e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0064: Expected O, but got Unknown
				//IL_008b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0091: Expected O, but got Unknown
				Name = se.NameHash();
				Ttl = se.m_ttl;
				Time = se.m_time;
				Value1 = 0f;
				Value2 = 0f;
				Value3 = 0f;
				Value4 = 0f;
				if (Name == Poison)
				{
					SE_Poison val = (SE_Poison)se;
					Value1 = val.m_damageLeft;
					Value2 = val.m_damagePerHit;
				}
				else if (Name == Burning)
				{
					SE_Burning val2 = (SE_Burning)se;
					Value1 = val2.m_fireDamageLeft;
					Value2 = val2.m_fireDamagePerHit;
					Value3 = val2.m_spiritDamageLeft;
					Value4 = val2.m_spiritDamagePerHit;
				}
			}

			public StatusEffectData(string data)
			{
				Name = 0;
				Ttl = 0f;
				Time = 0f;
				Value1 = 0f;
				Value2 = 0f;
				Value3 = 0f;
				Value4 = 0f;
				string[] array = data.Split(new char[1] { ':' });
				if (array.Length >= 3 && int.TryParse(array[0], out var result) && float.TryParse(array[1], out var result2) && float.TryParse(array[2], out var result3))
				{
					Name = result;
					Ttl = result2;
					Time = result3;
					if (array.Length >= 5 && float.TryParse(array[3], out var result4) && float.TryParse(array[4], out var result5))
					{
						Value1 = result4;
						Value2 = result5;
					}
					if (array.Length >= 7 && float.TryParse(array[5], out var result6) && float.TryParse(array[6], out var result7))
					{
						Value3 = result6;
						Value4 = result7;
					}
				}
			}

			public override string ToString()
			{
				if (Name == Poison)
				{
					return $"{Name}:{Ttl}:{Time}:{Value1}:{Value2}";
				}
				if (Name == Burning)
				{
					return $"{Name}:{Ttl}:{Time}:{Value1}:{Value2}:{Value3}:{Value4}";
				}
				return $"{Name}:{Ttl}:{Time}";
			}
		}

		private static readonly int Poison = StringExtensionMethods.GetStableHashCode("Poison");

		private static readonly int Burning = StringExtensionMethods.GetStableHashCode("Burning");

		private static HashSet<int> BasicStatusEffects = new HashSet<int>
		{
			StringExtensionMethods.GetStableHashCode("Rested"),
			StringExtensionMethods.GetStableHashCode("Wet"),
			StringExtensionMethods.GetStableHashCode("GP_Eikthyr"),
			StringExtensionMethods.GetStableHashCode("GP_TheElder"),
			StringExtensionMethods.GetStableHashCode("GP_Bonemass"),
			StringExtensionMethods.GetStableHashCode("GP_Moder"),
			StringExtensionMethods.GetStableHashCode("GP_Yagluth"),
			StringExtensionMethods.GetStableHashCode("GP_Queen"),
			StringExtensionMethods.GetStableHashCode("Potion_tasty"),
			StringExtensionMethods.GetStableHashCode("Potion_barleywine"),
			StringExtensionMethods.GetStableHashCode("Potion_frostresist"),
			StringExtensionMethods.GetStableHashCode("Potion_poisonresist"),
			StringExtensionMethods.GetStableHashCode("potion_health_major"),
			StringExtensionMethods.GetStableHashCode("Potion_health_medium"),
			StringExtensionMethods.GetStableHashCode("Potion_health_minor"),
			StringExtensionMethods.GetStableHashCode("potion_stamina_lingering"),
			StringExtensionMethods.GetStableHashCode("Potion_stamina_medium"),
			StringExtensionMethods.GetStableHashCode("Potion_stamina_minor"),
			StringExtensionMethods.GetStableHashCode("potion_eitr_minor"),
			StringExtensionMethods.GetStableHashCode("CorpseRun"),
			StringExtensionMethods.GetStableHashCode("SoftDeath"),
			StringExtensionMethods.GetStableHashCode("Slimed"),
			StringExtensionMethods.GetStableHashCode("Tared")
		};

		public static bool SupportedStatusEffect(int name)
		{
			if (!BasicStatusEffects.Contains(name) && name != Poison)
			{
				return name == Burning;
			}
			return true;
		}

		public static StatusEffect BuildStatusEffect(StatusEffectData data)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			StatusEffect statusEffect = ObjectDB.instance.GetStatusEffect(data.Name);
			if ((Object)(object)statusEffect == (Object)null)
			{
				return null;
			}
			if (BasicStatusEffects.Contains(data.Name))
			{
				statusEffect.m_ttl = data.Ttl;
				statusEffect.m_time = data.Time;
				return statusEffect;
			}
			if (data.Name == Poison)
			{
				SE_Poison val = (SE_Poison)statusEffect;
				((StatusEffect)val).m_ttl = data.Ttl;
				((StatusEffect)val).m_time = data.Time;
				val.m_damageLeft = data.Value1;
				val.m_damagePerHit = data.Value2;
				return (StatusEffect)val;
			}
			if (data.Name == Burning)
			{
				SE_Burning val2 = (SE_Burning)statusEffect;
				((StatusEffect)val2).m_ttl = data.Ttl;
				((StatusEffect)val2).m_time = data.Time;
				val2.m_fireDamageLeft = data.Value1;
				val2.m_fireDamagePerHit = data.Value2;
				val2.m_spiritDamageLeft = data.Value3;
				val2.m_spiritDamagePerHit = data.Value4;
				return (StatusEffect)val2;
			}
			return null;
		}
	}
}