Decompiled source of TimbnCampfireMeditation v1.4.0

Timbn.GraveyardKeeperTwo.CampfireMeditation/Timbn.GraveyardKeeperTwo.CampfireMeditation.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using LazyBearTechnology;
using Microsoft.CodeAnalysis;
using Timbn.GraveyardKeeperTwo.Core.Framework;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Timbn.GraveyardKeeperTwo.CampfireMeditation")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.4.0.0")]
[assembly: AssemblyInformationalVersion("1.4.0+fe25dbc0e499d8ef13d66532cd319ee531936bd9")]
[assembly: AssemblyProduct("Timbn Campfire Meditation")]
[assembly: AssemblyTitle("Timbn.GraveyardKeeperTwo.CampfireMeditation")]
[assembly: AssemblyVersion("1.4.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
	[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 Timbn.GraveyardKeeperTwo.CampfireMeditation
{
	internal sealed class Meditation
	{
		private const string _meditateKey = "timbn_campfire_meditate";

		private static readonly Vector3 _fire = new Vector3(-5.22f, 6f, 29.4f);

		private readonly TimbnFrameworkPlugin _plugin;

		private readonly TimbnInteractionSpot _spot;

		private IDisposable? _control;

		private IDisposable? _keyHints;

		private IDisposable? _fastTime;

		private bool _meditating;

		private bool _applied;

		private IDisposable? _fade;

		private float _lastTimeOfDay;

		private float _insanityRemoved;

		public Meditation(TimbnFrameworkPlugin plugin)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			Meditation meditation = this;
			_plugin = plugin;
			_spot = plugin.Interactions.Register(_fire, 0.3f).AddPrimary((Func<string>)(() => (!meditation.CanSitDown()) ? string.Empty : plugin.Text.Get("timbn_campfire_meditate")), (Action)SitDown);
		}

		public void Tick()
		{
			_spot.Radius = PluginConfig.Radius.Value;
			if (!TimbnGame.IsInGame || (Object)(object)MainGame.PlayerController == (Object)null)
			{
				Stop();
				return;
			}
			if (TimbnPlayer.GameHasControl((TakenControlType[])(object)new TakenControlType[1] { (TakenControlType)1 }))
			{
				Stop();
			}
			if (_meditating && TimbnPlayer.IsTired && CanGetUp())
			{
				GetUp();
			}
			if (_meditating)
			{
				KeepMeditating();
				if (CanGetUp() && TimbnInput.IsDownInGame(GameKey.Back))
				{
					GetUp();
				}
			}
		}

		public void Stop()
		{
			HideKeyHints();
			_fade?.Dispose();
			_fade = null;
			if (_meditating || _applied)
			{
				Restore();
			}
			_meditating = false;
		}

		private static bool CanGetUp()
		{
			if (!TimbnUI.IsScreenFading)
			{
				return TimbnInput.HotkeysEnabled;
			}
			return false;
		}

		private bool CanSitDown()
		{
			if (!_meditating)
			{
				return !TimbnPlayer.IsTired;
			}
			return false;
		}

		private void SitDown()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			_meditating = true;
			_keyHints = _plugin.UI.ShowKeyHints((TimbnKeyHint[])(object)new TimbnKeyHint[1]
			{
				new TimbnKeyHint(GameKey.Back, _plugin.Text.Key("hint_exit"))
			});
			_control?.Dispose();
			_control = _plugin.Player.TakeControl();
			_lastTimeOfDay = TimbnClock.TimeOfDay;
			_insanityRemoved = 0f;
			_fade?.Dispose();
			_fade = _plugin.UI.FadeThrough((Action)Begin);
		}

		private void GetUp()
		{
			_meditating = false;
			HideKeyHints();
			_fade?.Dispose();
			_fade = _plugin.UI.FadeThrough((Action)Restore);
		}

		private void Begin()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			if (TimbnPlayer.Face(_fire))
			{
				_applied = true;
				MeditationLighting.Begin();
				_fastTime?.Dispose();
				_fastTime = _plugin.Clock.MultiplyGameSpeed(PluginConfig.TimeSpeed.Value);
				TimbnFrameworkPlugin<Plugin>.Logger.LogInfo((object)string.Format("Meditating by the fire at {0} in {1}, time at x{2:0.#}.", _fire, TimbnPlayer.ZoneId ?? "no zone", PluginConfig.TimeSpeed.Value));
			}
		}

		private void Restore()
		{
			_control?.Dispose();
			_control = null;
			_fastTime?.Dispose();
			_fastTime = null;
			if (_applied)
			{
				_applied = false;
				MeditationLighting.End();
				TimbnFrameworkPlugin<Plugin>.Logger.LogInfo((object)"Stopped meditating.");
			}
		}

		private void KeepMeditating()
		{
			float timeOfDay = TimbnClock.TimeOfDay;
			float num = TimbnClock.DayFractionBetween(_lastTimeOfDay, timeOfDay);
			_lastTimeOfDay = timeOfDay;
			if (_applied && !(num <= 0f))
			{
				TimbnPlayer.AddEnergy(num * PluginConfig.EnergyPerDay.Value);
				CalmDown(num);
			}
		}

		private void CalmDown(float passed)
		{
			float num = PluginConfig.MaxInsanity.Value - _insanityRemoved;
			float num2 = Mathf.Min(new float[3]
			{
				passed * PluginConfig.InsanityPerDay.Value,
				num,
				TimbnPlayer.Insanity
			});
			if (num2 > 0f && TimbnPlayer.AddInsanity(0f - num2))
			{
				_insanityRemoved += num2;
			}
		}

		private void HideKeyHints()
		{
			_keyHints?.Dispose();
			_keyHints = null;
		}
	}
	internal static class MeditationLighting
	{
		private static readonly FieldRef<EnvironmentEngine, LightEnvironmentPreset?> _presetOverride = AccessTools.FieldRefAccess<EnvironmentEngine, LightEnvironmentPreset>("presetOverride");

		private static LightEnvironmentPreset? _midnight;

		private static LightEnvironmentPreset? _previousOverride;

		private static float _previousIntensity;

		public static void Begin()
		{
			End();
			EnvironmentEngine instance = EnvironmentEngine.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				LightEnvironmentPreset val = FindMidnight(Traverse.Create((object)instance).Field<TimeOfDayPresets>("timesOfDay").Value);
				if ((Object)(object)val == (Object)null)
				{
					TimbnFrameworkPlugin<Plugin>.Logger.LogWarning((object)"No midnight lighting preset found, so the lighting stays as it is.");
					return;
				}
				_midnight = val;
				_previousOverride = _presetOverride.Invoke(instance);
				_previousIntensity = instance.presetOverrideIntensity;
				instance.ApplyOverridePreset(val, 1f);
				TimbnFrameworkPlugin<Plugin>.Logger.LogInfo((object)("Lighting like midnight (" + ((Object)val).name + ") with the sun, moon and ambient light off."));
			}
		}

		public static void End()
		{
			if ((Object)(object)_midnight == (Object)null)
			{
				return;
			}
			LightEnvironmentPreset midnight = _midnight;
			LightEnvironmentPreset previousOverride = _previousOverride;
			_midnight = null;
			_previousOverride = null;
			EnvironmentEngine instance = EnvironmentEngine.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				if ((Object)(object)_presetOverride.Invoke(instance) == (Object)(object)midnight)
				{
					instance.ApplyOverridePreset(previousOverride, _previousIntensity);
				}
				else
				{
					TimbnFrameworkPlugin<Plugin>.Logger.LogInfo((object)"The game changed the lighting while meditating, so it stays as the game set it.");
				}
			}
		}

		public static void Darken()
		{
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_midnight == (Object)null)
			{
				return;
			}
			EnvironmentEngine instance = EnvironmentEngine.Instance;
			if (!((Object)(object)instance == (Object)null) && !((Object)(object)_presetOverride.Invoke(instance) != (Object)(object)_midnight))
			{
				LightsSystem instance2 = LightsSystem.Instance;
				if ((Object)(object)instance2 != (Object)null && (Object)(object)instance2.SunLight != (Object)null)
				{
					instance2.SunLight.intensity = 0f;
				}
				GlobalShaderParameters instance3 = LazySingleton<GlobalShaderParameters>.Instance;
				if ((Object)(object)instance3 != (Object)null)
				{
					instance3.sunLight = 0f;
					instance3.ApplyShaderParameters();
				}
				RenderSettings.ambientLight = Color.black;
			}
		}

		private static LightEnvironmentPreset? FindMidnight(TimeOfDayPresets? timesOfDay)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: 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)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)timesOfDay == (Object)null || timesOfDay.presets == null)
			{
				return null;
			}
			LightEnvironmentPreset result = null;
			float num = float.MaxValue;
			foreach (TimeAndPreset preset in timesOfDay.presets)
			{
				if (!((Object)(object)preset.preset == (Object)null))
				{
					float num2 = Mathf.Min(preset.time, 1f - preset.time);
					if (!(num2 >= num))
					{
						result = preset.preset;
						num = num2;
					}
				}
			}
			return result;
		}
	}
	[BepInPlugin("Timbn.GraveyardKeeperTwo.CampfireMeditation", "Timbn Campfire Meditation", "1.4.0")]
	[BepInDependency("Timbn.GraveyardKeeperTwo.Core", "2.2.0")]
	public class Plugin : TimbnFrameworkPlugin<Plugin>
	{
		protected override TimbnModInfo ModInfo => new TimbnModInfo
		{
			ModGroupName = "Timbn Mods",
			ModGroupId = "338834e9-f5a5-4d7b-9773-17dfe3d5a954",
			Description = "Sit by the campfire at home to pass the time while you slowly get energy back.",
			Author = "Timbn",
			NexusLink = "https://www.nexusmods.com/graveyardkeeper2/mods/236",
			SteamWorkshopLink = "https://steamcommunity.com/sharedfiles/filedetails/?id=3810071804"
		};

		protected override void BindConfig(ConfigFile config)
		{
			PluginConfig.Bind(config);
		}

		protected override void OnAwake()
		{
			Meditation meditation = new Meditation((TimbnFrameworkPlugin)(object)this);
			((TimbnFrameworkPlugin)this).Events.Update((Action)meditation.Tick);
			((TimbnFrameworkPlugin)this).Events.SaveClosed((Action)meditation.Stop);
		}
	}
	internal static class PluginConfig
	{
		public static ConfigEntry<float> TimeSpeed { get; private set; }

		public static ConfigEntry<float> EnergyPerDay { get; private set; }

		public static ConfigEntry<float> InsanityPerDay { get; private set; }

		public static ConfigEntry<float> MaxInsanity { get; private set; }

		public static ConfigEntry<float> Radius { get; private set; }

		public static void Bind(ConfigFile config)
		{
			TimeSpeed = TimbnConfig.BindRange<float>(config, "Meditation", "TimeSpeed", 7f, "How many times faster time runs while you meditate. At 7 a day takes about 43 seconds, sleep runs at 50.", 1f, 50f);
			EnergyPerDay = TimbnConfig.BindRange<float>(config, "Meditation", "EnergyPerDay", 100f, "Energy regained per in game day of meditating. Sleep gives 400.", 0f, 400f);
			InsanityPerDay = TimbnConfig.BindRange<float>(config, "Meditation", "InsanityPerDay", 40f, "Insanity removed per in game day of meditating, until MaxInsanity is reached.", 0f, 400f);
			MaxInsanity = TimbnConfig.BindRange<float>(config, "Meditation", "MaxInsanity", 20f, "The most insanity one meditation can remove. Sleep removes 20.", 0f, 100f);
			Radius = config.Bind<float>("Meditation", "Radius", 2f, "How far from the campfire you can be and still meditate.");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Timbn.GraveyardKeeperTwo.CampfireMeditation";

		public const string PLUGIN_NAME = "Timbn Campfire Meditation";

		public const string PLUGIN_VERSION = "1.4.0";
	}
}
namespace Timbn.GraveyardKeeperTwo.CampfireMeditation.Patches
{
	[HarmonyPatch(typeof(EnvironmentEngine))]
	internal static class EnvironmentEnginePatch
	{
		[HarmonyPatch("ApplyPreset")]
		[HarmonyPostfix]
		private static void ApplyPresetPostFix()
		{
			MeditationLighting.Darken();
		}
	}
}