Decompiled source of SaveRested v0.2.0

SaveRested.dll

Decompiled 3 weeks 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 HarmonyLib;
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("Save Rested")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("RALIV")]
[assembly: AssemblyProduct("RALIV.Valheim.SaveRested")]
[assembly: AssemblyCopyright("Copyright © RALIV 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e6ad1a3f-36f6-4be1-9605-50adcd1610c4")]
[assembly: AssemblyFileVersion("1.26.0926.0333")]
[assembly: AssemblyInformationalVersion("0.26.0926.0333")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyVersion("1.26.926.333")]
namespace RALIV.Valheim.SaveRested;

public static class Patch_Player
{
	public const string RESTED_KEY = "Save Rested";

	private static bool spawned;

	private static int updated;

	[HarmonyPatch(typeof(Player), "Save")]
	[HarmonyPrefix]
	public static void Save_Prefix(Player __instance)
	{
		if (!SaveRestedPlugin.Enabled.Value || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return;
		}
		SaveRestedPlugin.UnityDebugLog("Save Rested");
		SEMan sEMan = ((Character)__instance).GetSEMan();
		StatusEffect statusEffect = sEMan.GetStatusEffect(SEMan.s_statusEffectRested);
		SE_Rested val = (SE_Rested)(object)((statusEffect is SE_Rested) ? statusEffect : null);
		if ((Object)(object)val == (Object)null || ((StatusEffect)val).m_ttl <= 0f)
		{
			if (__instance.m_customData.Remove("Save Rested"))
			{
				SaveRestedPlugin.UnityDebugLog("Save Rested: Clear");
			}
		}
		else
		{
			string text = string.Concat(((StatusEffect)val).m_ttl, ',', ((StatusEffect)val).GetDuration());
			SaveRestedPlugin.UnityDebugLog("Save Rested: " + text);
			__instance.m_customData["Save Rested"] = text;
		}
	}

	[HarmonyPatch(typeof(Player), "Load")]
	[HarmonyPostfix]
	public static void Load_Postfix(Player __instance)
	{
		SaveRestedPlugin.UnityDebugLog("Rested Restoring: Load");
		spawned = false;
		updated = 0;
		if (__instance.m_customData.TryGetValue("Save Rested", out var value))
		{
			SaveRestedPlugin.UnityDebugLog("Rested Restoring: Loaded " + value);
		}
	}

	[HarmonyPatch(typeof(Player), "OnSpawned")]
	[HarmonyPostfix]
	public static void OnSpawned_Postfix(Player __instance)
	{
		SaveRestedPlugin.UnityDebugLog("Rested Restoring: Spawn");
		spawned = true;
		updated = 0;
	}

	[HarmonyPatch(typeof(Player), "Update")]
	[HarmonyPrefix]
	public static void Update_Prefix(Player __instance)
	{
		if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || 1 < updated || !spawned)
		{
			return;
		}
		updated++;
		if (!SaveRestedPlugin.Enabled.Value)
		{
			return;
		}
		SaveRestedPlugin.UnityDebugLog("Rested Restoring");
		if (!__instance.m_customData.TryGetValue("Save Rested", out var value) || string.IsNullOrEmpty(value))
		{
			return;
		}
		SaveRestedPlugin.UnityDebugLog("Rested Restoring: Loaded " + value);
		string[] array = value.Split(new char[1] { ',' });
		float result = 0f;
		float result2 = 0f;
		bool flag = false;
		if (array.Length == 2)
		{
			if (!float.TryParse(array[0], out result))
			{
				flag = true;
			}
			if (!float.TryParse(array[1], out result2))
			{
				flag = true;
			}
		}
		else
		{
			flag = true;
		}
		if (flag)
		{
			SaveRestedPlugin.UnityDebugLog("Rested Restoring: Bad Data");
			return;
		}
		SaveRestedPlugin.UnityDebugLog("Rested Restoring: TTL " + result);
		SaveRestedPlugin.UnityDebugLog("Rested Restoring: Time " + result2);
		if (!(result <= 0f) && !(result2 <= 0f) && !(result <= result2))
		{
			SEMan sEMan = ((Character)__instance).GetSEMan();
			StatusEffect statusEffect = sEMan.GetStatusEffect(SEMan.s_statusEffectRested);
			SE_Rested val = (SE_Rested)(object)((statusEffect is SE_Rested) ? statusEffect : null);
			bool flag2 = (Object)(object)val == (Object)null;
			if (flag2)
			{
				StatusEffect statusEffect2 = ObjectDB.instance.GetStatusEffect(SEMan.s_statusEffectRested);
				val = (SE_Rested)(object)((statusEffect2 is SE_Rested) ? statusEffect2 : null);
			}
			((StatusEffect)val).m_ttl = result;
			Traverse.Create((object)val).Field("m_time").SetValue((object)result2);
			if (flag2)
			{
				sEMan.AddStatusEffect((StatusEffect)(object)val, false, 0, 0f, (short)(-1));
				SaveRestedPlugin.UnityDebugLog("Rested Restoring: Added");
			}
			if (updated == 2 && __instance.m_customData.Remove("Save Rested"))
			{
				SaveRestedPlugin.UnityDebugLog("Save Rested: Clear");
			}
		}
	}
}
[BepInPlugin("SaveRested.RussLeeIV", "Save Rested", "0.2.0")]
public class SaveRestedPlugin : BaseUnityPlugin
{
	public const string logColor = "blue";

	private static Harmony _harmony;

	public static ConfigEntry<bool> Enabled;

	public static void UnityDebugLog(string message, Object context = null)
	{
		if (context == (Object)null)
		{
			Debug.Log((object)(_harmony.Id + " " + message));
			return;
		}
		Debug.LogFormat(_harmony.Id + " " + message, new object[1] { context });
	}

	public SaveRestedPlugin()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		_harmony = new Harmony("RussLeeIV.Valheim.SaveRested");
	}

	public void Awake()
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		UnityDebugLog("Save Rested, Awake!");
		((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Rested", "Enabled", true, new ConfigDescription("Is the mod enabled?", (AcceptableValueBase)null, Array.Empty<object>()));
		_harmony.PatchAll(typeof(Patch_Player));
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		_harmony = null;
	}
}