Decompiled source of WalkieTalkieTimeMod v1.0.0

BepInEx/plugins/WalkieTalkieTimeMod.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("WalkieTalkieTimeMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WalkieTalkieTimeMod")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("fc9ef871-d49d-49a2-a757-cce3d5de07ce")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace WalkieTalkieTimeMod;

[Serializable]
public class SyncedInstance<T>
{
	[NonSerialized]
	protected static int IntSize = 4;

	internal static CustomMessagingManager MessageManager => NetworkManager.Singleton.CustomMessagingManager;

	internal static bool IsClient => NetworkManager.Singleton.IsClient;

	internal static bool IsHost => NetworkManager.Singleton.IsHost;

	public static T Default { get; private set; }

	public static T Instance { get; private set; }

	public static bool Synced { get; internal set; }

	protected void InitInstance(T instance)
	{
		Default = instance;
		Instance = instance;
		IntSize = 4;
	}

	internal static void SyncInstance(byte[] data)
	{
		Instance = DeserializeFromBytes(data);
		Synced = true;
	}

	internal static void RevertSync()
	{
		Instance = Default;
		Synced = false;
	}

	public static byte[] SerializeToBytes(T val)
	{
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		MemoryStream memoryStream = new MemoryStream();
		try
		{
			binaryFormatter.Serialize(memoryStream, val);
			return memoryStream.ToArray();
		}
		catch (Exception arg)
		{
			PluginBase.Instance.mls.LogError((object)$"Error serializing instance: {arg}");
			return null;
		}
	}

	public static T DeserializeFromBytes(byte[] data)
	{
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		MemoryStream serializationStream = new MemoryStream(data);
		try
		{
			return (T)binaryFormatter.Deserialize(serializationStream);
		}
		catch (Exception arg)
		{
			PluginBase.Instance.mls.LogError((object)$"Error deserializing instance: {arg}");
			return default(T);
		}
	}
}
[Serializable]
internal class WTTConfig : SyncedInstance<WTTConfig>
{
	public ConfigEntry<bool> ShowTimeOnlyWithWalkieTalkie;

	public WTTConfig(ConfigFile configFile)
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Expected O, but got Unknown
		InitInstance(this);
		configFile.SaveOnConfigSet = false;
		ShowTimeOnlyWithWalkieTalkie = configFile.Bind<bool>("General", "ShowTimeOnlyWithWalkieTalkie", false, new ConfigDescription("Time is always shown outside by default. Make game harder by only showing time by holding an active walkie talkie.", (AcceptableValueBase)null, Array.Empty<object>()));
		ClearOrphanedEntries(configFile);
		configFile.Save();
		configFile.SaveOnConfigSet = true;
	}

	private static void ClearOrphanedEntries(ConfigFile cfg)
	{
		((Dictionary<ConfigDefinition, string>)AccessTools.Property(typeof(ConfigFile), "OrphanedEntries").GetValue(cfg)).Clear();
	}

	public static void RequestSync()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		if (SyncedInstance<WTTConfig>.IsClient)
		{
			FastBufferWriter val = default(FastBufferWriter);
			((FastBufferWriter)(ref val))..ctor(SyncedInstance<WTTConfig>.IntSize, (Allocator)2, -1);
			SyncedInstance<WTTConfig>.MessageManager.SendNamedMessage("WTT_OnRequestConfigSync", 0uL, val, (NetworkDelivery)3);
		}
	}

	public static void OnRequestSync(ulong clientId, FastBufferReader _)
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		if (!SyncedInstance<WTTConfig>.IsHost)
		{
			return;
		}
		PluginBase.Instance.mls.LogInfo((object)$"Config sync request received from client: {clientId}");
		byte[] array = SyncedInstance<WTTConfig>.SerializeToBytes(SyncedInstance<WTTConfig>.Instance);
		int num = array.Length;
		int num2 = FastBufferWriter.GetWriteSize<byte>(array, -1, 0) + SyncedInstance<WTTConfig>.IntSize;
		FastBufferWriter val = default(FastBufferWriter);
		((FastBufferWriter)(ref val))..ctor(num2, (Allocator)2, -1);
		try
		{
			((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num, default(ForPrimitives));
			((FastBufferWriter)(ref val)).WriteBytesSafe(array, -1, 0);
			SyncedInstance<WTTConfig>.MessageManager.SendNamedMessage("WTT_OnReceiveConfigSync", clientId, val, (NetworkDelivery)3);
		}
		catch (Exception arg)
		{
			PluginBase.Instance.mls.LogInfo((object)$"Error occurred syncing config with client: {clientId}\n{arg}");
		}
	}

	public static void OnReceiveSync(ulong _, FastBufferReader reader)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		if (!((FastBufferReader)(ref reader)).TryBeginRead(SyncedInstance<WTTConfig>.IntSize))
		{
			PluginBase.Instance.mls.LogError((object)"Config sync error: Could not begin reading buffer.");
			return;
		}
		int num = default(int);
		((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
		if (!((FastBufferReader)(ref reader)).TryBeginRead(num))
		{
			PluginBase.Instance.mls.LogError((object)"Config sync error: Host could not sync.");
			return;
		}
		byte[] data = new byte[num];
		((FastBufferReader)(ref reader)).ReadBytesSafe(ref data, num, 0);
		SyncedInstance<WTTConfig>.SyncInstance(data);
		PluginBase.Instance.mls.LogInfo((object)"Successfully synced config with host.");
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
	public static void InitializeLocalPlayer()
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		if (SyncedInstance<WTTConfig>.IsHost)
		{
			SyncedInstance<WTTConfig>.MessageManager.RegisterNamedMessageHandler("WTT_OnRequestConfigSync", new HandleNamedMessageDelegate(OnRequestSync));
			SyncedInstance<WTTConfig>.Synced = true;
		}
		else
		{
			SyncedInstance<WTTConfig>.Synced = false;
			SyncedInstance<WTTConfig>.MessageManager.RegisterNamedMessageHandler("WTT_OnReceiveConfigSync", new HandleNamedMessageDelegate(OnReceiveSync));
			RequestSync();
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
	public static void PlayerLeave()
	{
		SyncedInstance<WTTConfig>.RevertSync();
	}
}
[BepInPlugin("belea.WalkieTalkieTimeMod", "Walkie Talkie Time Mod", "1.0.0")]
public class PluginBase : BaseUnityPlugin
{
	private const string modGUID = "belea.WalkieTalkieTimeMod";

	private const string modName = "Walkie Talkie Time Mod";

	private const string modVersion = "1.0.0";

	private readonly Harmony harmony = new Harmony("belea.WalkieTalkieTimeMod");

	public static PluginBase Instance;

	internal ManualLogSource mls;

	internal static WTTConfig Config { get; private set; }

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		mls = Logger.CreateLogSource("belea.WalkieTalkieTimeMod");
		harmony.PatchAll(typeof(PluginBase));
		harmony.PatchAll(typeof(WTTConfig));
		Config = new WTTConfig(((BaseUnityPlugin)this).Config);
		mls.LogInfo((object)"Walkie Talkie Time app has been installed...");
	}

	public bool ClockShouldBeVisible()
	{
		PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
		if ((Object)(object)localPlayerController == (Object)null)
		{
			return false;
		}
		if (!SyncedInstance<WTTConfig>.Instance.ShowTimeOnlyWithWalkieTalkie.Value && !localPlayerController.isInsideFactory && !localPlayerController.isInHangarShipRoom)
		{
			return true;
		}
		GrabbableObject val = localPlayerController.ItemSlots[localPlayerController.currentItemSlot];
		if ((Object)(object)val == (Object)null)
		{
			return false;
		}
		WalkieTalkie val2 = default(WalkieTalkie);
		if (!((Component)val).TryGetComponent<WalkieTalkie>(ref val2))
		{
			return false;
		}
		return ((GrabbableObject)val2).isBeingUsed;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(TimeOfDay), "Update")]
	public static void UpdatePatch(TimeOfDay __instance)
	{
		if (!((Object)(object)__instance.sunDirect == (Object)null) && !((Object)(object)__instance.sunIndirect == (Object)null) && __instance.currentDayTimeStarted && !(bool)typeof(TimeOfDay).GetField("timeStartedThisFrame", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(__instance) && (Object)(object)__instance.sunAnimator != (Object)null)
		{
			HUDManager.Instance.SetClockVisible(Instance.ClockShouldBeVisible());
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(TimeOfDay), "PlayerSeesNewTimeOfDay")]
	public static void PlayerSeesNewTimeOfDayPatch(TimeOfDay __instance)
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		if (Instance.ClockShouldBeVisible() && __instance.playersManager.shipHasLanded)
		{
			typeof(TimeOfDay).GetField("dayModeLastTimePlayerWasOutside", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(__instance, __instance.dayMode);
			HUDManager.Instance.SetClockIcon(__instance.dayMode);
			if (__instance.currentLevel.planetHasTime)
			{
				__instance.PlayTimeMusicDelayed(__instance.timeOfDayCues[__instance.dayMode], 0.5f, true);
			}
		}
	}
}