Decompiled source of LaSiesta v1.0.0

LaSiesta.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LaSiesta")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LaSiesta")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("796fcd37-3fc3-407e-8337-af1a6a32a074")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LaSiesta;

[HarmonyPatch(typeof(Bed), "Interact")]
public class BedInteractPatch
{
	private static bool runningSiesta;

	private static bool Prefix(Humanoid human, bool repeat, bool alt, ref bool __result)
	{
		if (repeat)
		{
			__result = false;
			return false;
		}
		Player val = (Player)(object)((human is Player) ? human : null);
		if ((Object)(object)val == (Object)null)
		{
			__result = false;
			return false;
		}
		if (!EnvMan.IsNight())
		{
			skipToBeforeNight();
			__result = true;
			return false;
		}
		return true;
	}

	private static void skipToBeforeNight()
	{
		if (!runningSiesta)
		{
			float value = ConfigurationFile.oneDayLength.Value;
			float value2 = ConfigurationFile.oneHourLength.Value;
			float value3 = ConfigurationFile.siestaHours.Value;
			float num = value3 * value2;
			Logger.Log($"One day: {value}, one hour: {value2}, hours: {value3}, Seconds to skip: {num}");
			float num2 = (float)typeof(EnvMan).GetField("m_smoothDayFraction", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(EnvMan.instance);
			float num3 = num2 + num / value;
			Logger.Log($"old fraction: {num2}, new fraction: {num3}");
			if (num3 < 0.75f)
			{
				turnCheatMode(cheat: true);
				((Terminal)Console.instance).TryRunCommand($"skiptime {num}", true, false);
				turnCheatMode(cheat: false);
			}
			else
			{
				turnCheatMode(cheat: false);
				MessageHud.instance.ShowMessage((MessageType)2, ConfigurationFile.tooCloseForSiesta.Value, 0, (Sprite)null);
			}
		}
	}

	private static async Task WaitForSecondsAsyncOnly(float seconds)
	{
		await Task.Delay((int)(Math.Max(0f, seconds) * 1000f));
		((Character)Player.m_localPlayer).AttachStop();
	}

	private static void turnCheatMode(bool cheat)
	{
		Type typeFromHandle = typeof(Terminal);
		FieldInfo field = typeFromHandle.GetField("m_cheat", BindingFlags.Static | BindingFlags.NonPublic);
		field.SetValue(Console.instance, cheat);
		Logger.Log($"** CheatMode {cheat}");
	}
}
internal class ConfigurationFile
{
	public static ConfigEntry<bool> debug;

	public static ConfigEntry<float> oneDayLength;

	public static ConfigEntry<float> oneHourLength;

	public static ConfigEntry<string> tooCloseForSiesta;

	public static ConfigEntry<bool> siestaOnlyAfternoon;

	public static ConfigEntry<float> siestaHours;

	public static ConfigEntry<bool> showDayInfoOnScreen;

	public static ConfigEntry<int> xPosClock;

	public static ConfigEntry<int> yPosClock;

	public static ConfigEntry<int> fontSize;

	private static ConfigFile config;

	internal static void LoadConfig(BaseUnityPlugin plugin)
	{
		config = plugin.Config;
		debug = config.Bind<bool>("1 - Config", "DebugMode", false, "Enabling/Disabling the debugging in the console (default = false)");
		oneDayLength = config.Bind<float>("1 - Config", "OneDayLength", 1800f, "Length of one game day in ticks. Careful when changing this value (default = 1800)");
		oneHourLength = config.Bind<float>("1 - Config", "OneHourLength", 90f, "Length of one game hour in ticks. Careful when changing this value (default = 90)");
		tooCloseForSiesta = config.Bind<string>("1 - Config", "TooCloseForSiestaText", "Can't take siesta yet, night is too close", "Text to show when night is too close for a siesta (override to translate if you need)");
		siestaHours = config.Bind<float>("2 - General", "SiestaHours", 2f, "Number of hours of each siesta (default = 2)");
		showDayInfoOnScreen = config.Bind<bool>("3 - Clock", "ShowDayInfoOnScreen", false, "Shows day attributes on the screen (default = false)");
		xPosClock = config.Bind<int>("3 - Clock", "xPosClock", 70, "Horizontal Position of the clock from the minimap (default = 70)");
		yPosClock = config.Bind<int>("3 - Clock", "yPosClock", -150, "Vertical Position of the clock from the minimap (default = -150)");
		fontSize = config.Bind<int>("3 - Clock", "FontSize", 18, "Clock font size (default = 18)");
		xPosClock.SettingChanged += OnClockChanged;
		yPosClock.SettingChanged += OnClockChanged;
		fontSize.SettingChanged += OnClockChanged;
	}

	private static void OnClockChanged(object sender, EventArgs e)
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		Transform transform = UIStatsPatch.timeTextObject.transform;
		RectTransform val = (RectTransform)(object)((transform is RectTransform) ? transform : null);
		val.anchoredPosition = new Vector2((float)xPosClock.Value, (float)yPosClock.Value);
		((Component)val).GetComponent<Text>().fontSize = fontSize.Value;
	}
}
[BepInPlugin("Turbero.LaSiesta", "La Siesta", "1.0.0")]
public class LaSiesta : BaseUnityPlugin
{
	public const string GUID = "Turbero.LaSiesta";

	public const string NAME = "La Siesta";

	public const string VERSION = "1.0.0";

	private readonly Harmony harmony = new Harmony("Turbero.LaSiesta");

	private void Awake()
	{
		ConfigurationFile.LoadConfig((BaseUnityPlugin)(object)this);
		harmony.PatchAll();
	}

	private void onDestroy()
	{
		harmony.UnpatchSelf();
	}
}
[HarmonyPatch(typeof(EnvMan), "CalculateCanSleep")]
public class CalculateCanSleepPatch
{
	private static bool Prefix(ref bool __result)
	{
		__result = true;
		return false;
	}
}
public static class Logger
{
	internal static void Log(object s)
	{
		if (ConfigurationFile.debug.Value)
		{
			string text = "La Siesta 1.0.0: " + ((s != null) ? s.ToString() : "null");
			Debug.Log((object)text);
		}
	}

	internal static void LogWarning(object s)
	{
		string text = "La Siesta 1.0.0: " + ((s != null) ? s.ToString() : "null");
		Debug.LogWarning((object)text);
	}

	internal static void LogError(object s)
	{
		string text = "La Siesta 1.0.0: " + ((s != null) ? s.ToString() : "null");
		Debug.LogError((object)text);
	}
}
[HarmonyPatch(typeof(Hud), "Awake")]
public class UIStatsPatch
{
	[HarmonyPatch(typeof(Hud), "Update")]
	public static class Hud_Update_Patch
	{
		private static void Postfix()
		{
			timeTextObject.SetActive(ConfigurationFile.showDayInfoOnScreen.Value);
			if (ConfigurationFile.showDayInfoOnScreen.Value && (Object)(object)timeText != (Object)null)
			{
				timeText.text = GetCurrentGameTime();
			}
		}

		private static string GetCurrentGameTime()
		{
			EnvMan instance = EnvMan.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return "00:00";
			}
			FieldInfo field = typeof(EnvMan).GetField("m_smoothDayFraction", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field == null)
			{
				Debug.LogError((object)"No se pudo acceder a m_smoothDayFraction.");
				return "00:00";
			}
			float num = (float)field.GetValue(instance);
			int num2 = (int)(num * 24f);
			int num3 = (int)(num * 1440f % 60f);
			return $"{num2:D2}:{num3:D2}";
		}
	}

	private static Text timeText;

	public static GameObject timeTextObject;

	private static void Postfix(Hud __instance)
	{
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Expected O, but got Unknown
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		Hud instance = Hud.instance;
		object obj;
		if (instance == null)
		{
			obj = null;
		}
		else
		{
			Transform obj2 = ((Component)instance).transform.Find("hudroot");
			if (obj2 == null)
			{
				obj = null;
			}
			else
			{
				Transform obj3 = ((Component)obj2).transform.Find("MiniMap");
				if (obj3 == null)
				{
					obj = null;
				}
				else
				{
					Transform obj4 = ((Component)obj3).transform.Find("small");
					obj = ((obj4 != null) ? ((Component)obj4).gameObject : null);
				}
			}
		}
		GameObject val = (GameObject)obj;
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogError("Minimap not found.");
			return;
		}
		timeTextObject = new GameObject("LaSiestaTimeText");
		timeTextObject.transform.SetParent(val.transform);
		timeText = timeTextObject.AddComponent<Text>();
		timeText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
		timeText.fontSize = 18;
		((Graphic)timeText).color = Color.white;
		timeText.alignment = (TextAnchor)1;
		RectTransform component = ((Component)timeText).GetComponent<RectTransform>();
		component.anchoredPosition = new Vector2(70f, -150f);
	}
}