Decompiled source of NuclearCruiser v1.0.0

Tomatobird.NuclearCruiser.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using NuclearCruiser.Network;
using NuclearCruiser.Utils;
using Tomatobird.NuclearCruiser.NetcodePatcher;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Tomatobird.NuclearCruiser")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+38354a771f8dffce5abec0d64374de6d9d51204f")]
[assembly: AssemblyProduct("NuclearCruiser")]
[assembly: AssemblyTitle("Tomatobird.NuclearCruiser")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 NuclearCruiser
{
	[BepInPlugin("Tomatobird.NuclearCruiser", "NuclearCruiser", "1.0.0")]
	public class NuclearCruiser : BaseUnityPlugin
	{
		internal static Texture2D? cruiserTexture;

		internal static Texture2D? destroyedCruiserTexture;

		internal static GameObject? nukeObject;

		internal static float nukeScale = 0.5f;

		internal static float nuclearCruiserChance = 1f;

		internal static bool infiniteBoosts = true;

		internal static bool nuclearCruiserWarning = true;

		internal static bool nuclearCruiserRadiationWarning = true;

		public static NuclearCruiser Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal static Harmony? Harmony { get; set; }

		private void Awake()
		{
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Expected O, but got Unknown
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			cruiserTexture = GetTexture(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "cruiser.png"));
			destroyedCruiserTexture = GetTexture(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "cruiser_blown.png"));
			AssetBundle val = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "cruisernuke"));
			nukeObject = val.LoadAsset<GameObject>("Assets/LethalCompany/Mods/TomatoScrap/Prefabs/Miscallaneous/NuclearBombCruiser.prefab");
			if ((Object)(object)cruiserTexture == (Object)null || (Object)(object)destroyedCruiserTexture == (Object)null || (Object)(object)nukeObject == (Object)null)
			{
				Logger.LogError((object)"Failed to load textures. Plugin loading failed.");
				return;
			}
			nuclearCruiserChance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "NuclearCruiserChance", 1f, new ConfigDescription("Chance of cruiser being a Nuclear Cruiser. 1 is always, 0 is never.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>())).Value;
			nukeScale = ((BaseUnityPlugin)this).Config.Bind<float>("General", "NukeScale", 0.5f, "How large should the explosion be? 0.5 can already cover most of a moon surface.").Value;
			infiniteBoosts = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "InfiniteBoosts", true, "Should nuclear cruiser have infinite boosts?").Value;
			nuclearCruiserWarning = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "NuclearCruiserWarning", true, "Should a warning pop up when a Nuclear Cruiser is spawned?").Value;
			nuclearCruiserRadiationWarning = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "NuclearCruiserRadiationWarning", true, "Should a radiation warning pop up when a Nuclear Cruiser is spawned?").Value;
			Transform transform = nukeObject.transform;
			transform.localScale *= nukeScale;
			((Object)cruiserTexture).name = "nukeCruiserTexture";
			((Object)destroyedCruiserTexture).name = "blownNukeCruiserTexture";
			Patch();
			PatchNetwork();
			Logger.LogInfo((object)"Tomatobird.NuclearCruiser v1.0.0 has loaded!");
		}

		internal static Texture2D? GetTexture(string path)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected O, but got Unknown
			if (File.Exists(path))
			{
				Texture2D val = new Texture2D(2, 2);
				ImageConversion.LoadImage(val, File.ReadAllBytes(path));
				return val;
			}
			return null;
		}

		internal static void PatchNetwork()
		{
			Type[] types = Assembly.GetExecutingAssembly().GetTypes();
			Type[] array = types;
			foreach (Type type in array)
			{
				MethodInfo[] methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				MethodInfo[] array2 = methods;
				foreach (MethodInfo methodInfo in array2)
				{
					object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), inherit: false);
					if (customAttributes.Length != 0)
					{
						methodInfo.Invoke(null, null);
					}
				}
			}
		}

		internal static void Patch()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony = new Harmony("Tomatobird.NuclearCruiser");
			}
			Logger.LogDebug((object)"Patching...");
			Harmony.PatchAll();
			Logger.LogDebug((object)"Finished patching!");
		}

		internal static void Unpatch()
		{
			Logger.LogDebug((object)"Unpatching...");
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Logger.LogDebug((object)"Finished unpatching!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Tomatobird.NuclearCruiser";

		public const string PLUGIN_NAME = "NuclearCruiser";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace NuclearCruiser.Utils
{
	internal class CruiserNuker : MonoBehaviour
	{
		internal void Explode()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = Object.Instantiate<GameObject>(NuclearCruiser.nukeObject);
			if ((Object)(object)val != (Object)null)
			{
				val.transform.position = ((Component)this).gameObject.transform.position;
			}
		}

		private void Start()
		{
			MeshRenderer[] componentsInChildren = ((Component)this).GetComponentsInChildren<MeshRenderer>();
			MeshRenderer[] array = componentsInChildren;
			foreach (MeshRenderer val in array)
			{
				if (((Object)((Component)val).transform).name == "MainBody" || ((Object)((Component)val).transform).name == "CarHoodMesh" || ((Object)((Component)val).transform).name == "Door" || ((Object)((Component)val).transform).name == "MainBody (1)" || ((Object)((Component)val).transform).name == "MainBody (2)")
				{
					((Renderer)val).materials[0].mainTexture = (Texture)(object)NuclearCruiser.cruiserTexture;
				}
			}
			if (NuclearCruiser.nuclearCruiserRadiationWarning)
			{
				HUDManager.Instance.RadiationWarningHUD();
			}
			if (NuclearCruiser.nuclearCruiserWarning)
			{
				if (NuclearCruiser.infiniteBoosts)
				{
					HUDManager.Instance.DisplayTip("NUCLEAR CRUISER SPAWNED", "Using nuclear power the cruiser can boost infinitely. Handle with extreme care.", true, false, "LC_Tip1");
				}
				else
				{
					HUDManager.Instance.DisplayTip("NUCLEAR CRUISER SPAWNED", "Handle with extreme care.", true, false, "LC_Tip1");
				}
			}
			if (NuclearCruiser.infiniteBoosts)
			{
				((Component)this).GetComponent<VehicleController>().turboBoosts = 5;
				((Component)this).GetComponent<VehicleController>().AddTurboBoost();
			}
		}
	}
}
namespace NuclearCruiser.Patches
{
	[HarmonyPatch(typeof(GameNetworkManager))]
	internal class GameNetworkManagerPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void StartPostFix()
		{
			NetworkHandler.CreateAndRegisterPrefab();
		}

		[HarmonyPatch("Disconnect")]
		[HarmonyPrefix]
		private static void DisconnectPostfix()
		{
			NetworkHandler.DespawnNetworkHandler();
		}
	}
	[HarmonyPatch(typeof(StartOfRound))]
	internal class StartOfRoundPatch
	{
		[HarmonyPatch("Awake")]
		[HarmonyPrefix]
		private static void AwakePrefix()
		{
			NetworkHandler.SpawnNetworkHandler();
		}
	}
	[HarmonyPatch(typeof(VehicleController))]
	internal class VehicleControllerPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		internal static void StartPatch(VehicleController __instance)
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			if (((NetworkBehaviour)__instance).NetworkManager.IsHost || ((NetworkBehaviour)__instance).NetworkManager.IsServer)
			{
				Random random = new Random();
				float num = (float)random.NextDouble();
				if (num < NuclearCruiser.nuclearCruiserChance)
				{
					NetworkHandler.Instance.AddCruiserNukerClientRpc(NetworkObjectReference.op_Implicit(((NetworkBehaviour)__instance).NetworkObject));
				}
			}
		}

		[HarmonyPatch("UseTurboBoostLocalClient")]
		[HarmonyPostfix]
		internal static void UseTurboBoostLocalClientPatch(VehicleController __instance)
		{
			if (!((Object)(object)((Component)__instance).GetComponent<CruiserNuker>() == (Object)null) && NuclearCruiser.infiniteBoosts)
			{
				__instance.turboBoosts = 5;
			}
		}

		[HarmonyPatch("DestroyCar")]
		[HarmonyPostfix]
		internal static void DestroyCarPatch(VehicleController __instance)
		{
			CruiserNuker component = ((Component)__instance).GetComponent<CruiserNuker>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			MeshRenderer[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<MeshRenderer>();
			MeshRenderer[] array = componentsInChildren;
			foreach (MeshRenderer val in array)
			{
				if (((Object)((Component)val).transform).name == "MainBodyDestroyed" || ((Object)((Component)val).transform).name == "CarHoodMesh" || ((Object)((Component)val).transform).name == "Door")
				{
					((Renderer)val).materials[0].mainTexture = (Texture)(object)NuclearCruiser.destroyedCruiserTexture;
				}
			}
			component.Explode();
		}
	}
}
namespace NuclearCruiser.Network
{
	internal class NetworkHandler : NetworkBehaviour
	{
		private static GameObject? prefab;

		public static NetworkHandler Instance { get; private set; }

		public static void CreateAndRegisterPrefab()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_002a: 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)
			if (!((Object)(object)prefab != (Object)null))
			{
				prefab = new GameObject("Tomatobird.NuclearCruiser Prefab");
				GameObject? obj = prefab;
				((Object)obj).hideFlags = (HideFlags)(((Object)obj).hideFlags | 0x3D);
				NetworkObject obj2 = prefab.AddComponent<NetworkObject>();
				FieldInfo field = typeof(NetworkObject).GetField("GlobalObjectIdHash", BindingFlags.Instance | BindingFlags.NonPublic);
				field.SetValue(obj2, GetHash("Tomatobird.NuclearCruiser Prefab"));
				prefab.AddComponent<NetworkHandler>();
				NetworkManager.Singleton.AddNetworkPrefab(prefab);
				NuclearCruiser.Logger.LogInfo((object)"Nuclear Cruiser network prefab done.");
			}
		}

		public static void SpawnNetworkHandler()
		{
			if (NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)
			{
				GameObject obj = Object.Instantiate<GameObject>(prefab);
				if (obj != null)
				{
					obj.GetComponent<NetworkObject>().Spawn(false);
				}
				NuclearCruiser.Logger.LogInfo((object)"Spawned network handler.");
			}
		}

		public static void DespawnNetworkHandler()
		{
			if ((Object)(object)Instance != (Object)null && ((Component)Instance).gameObject.GetComponent<NetworkObject>().IsSpawned && (NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost))
			{
				((Component)Instance).gameObject.GetComponent<NetworkObject>().Despawn(true);
				NuclearCruiser.Logger.LogInfo((object)"Despawned network handler.");
			}
		}

		protected internal static uint GetHash(string value)
		{
			return value?.Aggregate(17u, (uint current, char c) => (current * 31) ^ c) ?? 0;
		}

		private void Awake()
		{
			Instance = this;
		}

		[ClientRpc]
		public void AddCruiserNukerClientRpc(NetworkObjectReference target)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager == null || !networkManager.IsListening)
			{
				return;
			}
			if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsServer || networkManager.IsHost))
			{
				ClientRpcParams val = default(ClientRpcParams);
				FastBufferWriter val2 = ((NetworkBehaviour)this).__beginSendClientRpc(1743576381u, val, (RpcDelivery)0);
				((FastBufferWriter)(ref val2)).WriteValueSafe<NetworkObjectReference>(ref target, default(ForNetworkSerializable));
				((NetworkBehaviour)this).__endSendClientRpc(ref val2, 1743576381u, val, (RpcDelivery)0);
			}
			if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsClient || networkManager.IsHost))
			{
				base.__rpc_exec_stage = (__RpcExecStage)0;
				NetworkObject val3 = default(NetworkObject);
				if (((NetworkObjectReference)(ref target)).TryGet(ref val3, (NetworkManager)null))
				{
					((Component)val3).gameObject.AddComponent<CruiserNuker>();
				}
				else
				{
					NuclearCruiser.Logger.LogError((object)"NetworkObject couldn't be found for AddCruiserNukerClientRpc");
				}
			}
		}

		protected override void __initializeVariables()
		{
			((NetworkBehaviour)this).__initializeVariables();
		}

		protected override void __initializeRpcs()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			((NetworkBehaviour)this).__registerRpc(1743576381u, new RpcReceiveHandler(__rpc_handler_1743576381), "AddCruiserNukerClientRpc");
			((NetworkBehaviour)this).__initializeRpcs();
		}

		private static void __rpc_handler_1743576381(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_002f: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				NetworkObjectReference target2 = default(NetworkObjectReference);
				((FastBufferReader)(ref reader)).ReadValueSafe<NetworkObjectReference>(ref target2, default(ForNetworkSerializable));
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((NetworkHandler)(object)target).AddCruiserNukerClientRpc(target2);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		protected internal override string __getTypeName()
		{
			return "NetworkHandler";
		}
	}
}
namespace __GEN
{
	internal class NetworkVariableSerializationHelper
	{
		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeSerialization()
		{
		}
	}
}
namespace Tomatobird.NuclearCruiser.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}