Decompiled source of TrueNight v1.0.7

TrueNight.dll

Decompiled 6 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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 UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("TrueNight")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TrueNight")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6cfa05ec-6c9d-4420-b328-984c6e5fb3f2")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace TrueNight;

[BepInPlugin("com.t13.truenight", "TrueNight", "1.0.7")]
public class TrueNight : BaseUnityPlugin
{
	private const string PluginGuid = "com.t13.truenight";

	private const string PluginName = "TrueNight";

	private const string PluginVersion = "1.0.7";

	public static ConfigEntry<bool> OverrideEnabled;

	public static ConfigEntry<string> CustomGradientDefinition;

	public static ConfigEntry<bool> LightsEnabled;

	public static ConfigEntry<float> LightsIntensity;

	public static ConfigEntry<float> LightsRange;

	public static ConfigEntry<float> LightsAngle;

	private static Gradient _customGradient;

	private static bool _useGamesDefault;

	public static Gradient CustomGradient => _customGradient;

	public static bool UseGamesDefault => _useGamesDefault;

	private void Awake()
	{
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogMessage((object)"Greetings from TrueNight plugin!");
		OverrideEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Sky", "Enable Sky Override", true, "If true, the sky/fog color will be overridden, using a custom gradient defined below.\nIn other words it enables custom sky.");
		string text = "#[email protected], #[email protected], #[email protected], #[email protected]";
		CustomGradientDefinition = ((BaseUnityPlugin)this).Config.Bind<string>("Sky", "Custom Gradient Definition", text, "Defines new gradient keys. Format: HexColor@TimeValue, HexColor@TimeValue, ... \nTimeValue must be a float between 0.0 (day) and 1.0 (night).");
		LightsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Car", "Enable Car Headlights Change", true, (ConfigDescription)null);
		LightsIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Car", "Car Headlights Intensity", 160f, "Changes car's headlights intensity. In-game default - 80.");
		LightsRange = ((BaseUnityPlugin)this).Config.Bind<float>("Car", "Car Headlights Range", 200f, "Changes car's headlights range. In-game default - 100.");
		LightsAngle = ((BaseUnityPlugin)this).Config.Bind<float>("Car", "Car Headlights Angle", 69.625f, "Changes car's headlights angle. In-game default - 69.625.");
		CustomGradientDefinition.SettingChanged += delegate
		{
			RebuildCustomGradient(((BaseUnityPlugin)this).Logger);
		};
		RebuildCustomGradient(((BaseUnityPlugin)this).Logger);
		Harmony val = new Harmony("com.t13.truenight");
		val.PatchAll(typeof(TrueNightPatch));
		val.PatchAll(typeof(CarHeadlightsPatch));
		((BaseUnityPlugin)this).Logger.LogMessage((object)"Plugin TrueNight loaded and patches applied!");
	}

	private static void RebuildCustomGradient(ManualLogSource Logger)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bf: 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)
		_customGradient = new Gradient();
		string value = CustomGradientDefinition.Value;
		List<GradientColorKey> list = new List<GradientColorKey>();
		string[] array = value.Split(new char[1] { ',' });
		string[] array2 = array;
		Color val = default(Color);
		foreach (string text in array2)
		{
			string text2 = text.Trim();
			try
			{
				string[] array3 = text2.Split(new char[1] { '@' });
				if (array3.Length != 2)
				{
					continue;
				}
				string text3 = array3[0].Trim();
				string text4 = array3[1].Trim();
				bool flag = ColorUtility.TryParseHtmlString(text3, ref val);
				float result;
				bool flag2 = float.TryParse(text4, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
				if (flag && flag2)
				{
					result = Mathf.Clamp01(result);
					list.Add(new GradientColorKey(val, result));
					continue;
				}
				if (!flag2)
				{
					Logger.LogWarning((object)("Failed to parse time '" + text4 + "'. Ensure format is N.NNNN (period decimal)."));
				}
				if (!flag)
				{
					Logger.LogWarning((object)("Failed to parse hex color: '" + text3 + "'."));
				}
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Error parsing gradient key '" + text2 + "': " + ex.Message));
			}
		}
		if (list.Count > 0)
		{
			_customGradient.SetKeys(list.ToArray(), (GradientAlphaKey[])(object)new GradientAlphaKey[2]
			{
				new GradientAlphaKey(1f, 0f),
				new GradientAlphaKey(1f, 1f)
			});
			Logger.LogMessage((object)$"Successfully loaded {list.Count} custom gradient keys.");
		}
		else
		{
			Logger.LogError((object)"Failed to parse any gradient keys. Running game's default.");
			_useGamesDefault = true;
		}
	}
}
[HarmonyPatch(typeof(sDayNightCycle))]
[HarmonyPatch("Start")]
public static class TrueNightPatch
{
	private static FieldInfo originalSkyColorField = AccessTools.Field(typeof(sDayNightCycle), "skyColor");

	public static bool Prefix(sDayNightCycle __instance)
	{
		if (TrueNight.OverrideEnabled.Value && originalSkyColorField != null && TrueNight.CustomGradient != null && !TrueNight.UseGamesDefault)
		{
			originalSkyColorField.SetValue(__instance, TrueNight.CustomGradient);
		}
		return true;
	}
}
[HarmonyPatch(typeof(Headlights))]
[HarmonyPatch("Toggle", new Type[] { })]
public static class CarHeadlightsPatch
{
	public static void Postfix(Headlights __instance)
	{
		if (TrueNight.LightsEnabled.Value && __instance.headlightsOn)
		{
			Light[] componentsInChildren = ((Component)((Component)__instance).transform.GetChild(0)).GetComponentsInChildren<Light>();
			componentsInChildren[0].intensity = TrueNight.LightsIntensity.Value;
			componentsInChildren[1].intensity = TrueNight.LightsIntensity.Value;
			componentsInChildren[0].range = TrueNight.LightsRange.Value;
			componentsInChildren[1].range = TrueNight.LightsRange.Value;
			componentsInChildren[0].spotAngle = TrueNight.LightsAngle.Value;
			componentsInChildren[1].spotAngle = TrueNight.LightsAngle.Value;
		}
	}
}