Decompiled source of OperatorDroneTransparency v1.0.0

OpDroneTrans.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using On.RoR2;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
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("OpDroneTrans")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("OpDroneTrans")]
[assembly: AssemblyTitle("OpDroneTrans")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 OpDroneTrans
{
	[BepInPlugin("com.operatordronetransparency", "Operator Drone Transparency", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class OpDroneTransPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.operatordronetransparency";

		public const string PluginName = "Operator Drone Transparency";

		public const string PluginVersion = "1.0.0";

		private static ConfigEntry<float> opacityConfig;

		private static ConfigEntry<bool> showGlowConfig;

		private static ConfigEntry<bool> enableLogging;

		private static readonly HashSet<CharacterModel> trackedDrones = new HashSet<CharacterModel>();

		private static CharacterModel currentQueuedDrone = null;

		private static Highlight currentDroneHighlight = null;

		private static readonly string[] DroneBodyNames = new string[20]
		{
			"DTHealingDroneBody", "DTHaulerDroneBody", "DTGunnerDroneBody", "RechargeDroneBody", "BombardmentDroneBody", "CleanupDroneBody", "EmergencyDroneBody", "EquipmentDroneBody", "CopycatDroneBody", "Drone1Body",
			"Drone2Body", "FlameDroneBody", "JailerDroneBody", "JunkDroneBody", "MissileDroneBody", "MegaDroneBody", "HaulerDroneBody", "BackupDroneBody", "DroneBomberBody", "DroneCommanderBody"
		};

		public void Awake()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Expected O, but got Unknown
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Expected O, but got Unknown
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Expected O, but got Unknown
			opacityConfig = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Drone Opacity", 20f, new ConfigDescription("Opacity of the Operator's companion drone model (0 = invisible, 100 = fully opaque)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
			showGlowConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Show Drone Glow", false, "Show the highlight glow overlay on the companion drone.");
			enableLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "Enable Body Name Logging", false, "Log all CharacterModel body names to help identify the correct drone names.");
			InitRiskOfOptions();
			CharacterModel.Start += new hook_Start(CharacterModel_Start);
			CharacterModel.UpdateMaterials += new hook_UpdateMaterials(CharacterModel_UpdateMaterials);
			CharacterModel.OnDestroy += new hook_OnDestroy(CharacterModel_OnDestroy);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Operator Drone Transparency v1.0.0 loaded.");
		}

		public void OnDestroy()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Expected O, but got Unknown
			CharacterModel.Start -= new hook_Start(CharacterModel_Start);
			CharacterModel.UpdateMaterials -= new hook_UpdateMaterials(CharacterModel_UpdateMaterials);
			CharacterModel.OnDestroy -= new hook_OnDestroy(CharacterModel_OnDestroy);
			trackedDrones.Clear();
			currentQueuedDrone = null;
			currentDroneHighlight = null;
		}

		private void CharacterModel_Start(orig_Start orig, CharacterModel self)
		{
			orig.Invoke(self);
			if (enableLogging.Value)
			{
				string text = (Object.op_Implicit((Object)(object)self.body) ? ((Object)self.body).name : "null");
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[OpDroneTrans] CharacterModel body name: " + text));
			}
			if (IsOperatorDrone(self))
			{
				trackedDrones.Add(self);
				if (enableLogging.Value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)("[OpDroneTrans] Tracking drone: " + ((Object)self.body).name));
				}
			}
		}

		private void CharacterModel_OnDestroy(orig_OnDestroy orig, CharacterModel self)
		{
			if (trackedDrones.Remove(self) && (Object)(object)currentQueuedDrone == (Object)(object)self)
			{
				currentQueuedDrone = null;
				currentDroneHighlight = null;
			}
			orig.Invoke(self);
		}

		private void CharacterModel_UpdateMaterials(orig_UpdateMaterials orig, CharacterModel self)
		{
			if (trackedDrones.Contains(self))
			{
				if ((self.invisibilityCount > 0 || self.fade < 1f) && (Object)(object)currentQueuedDrone != (Object)(object)self)
				{
					if ((Object)(object)currentQueuedDrone != (Object)null)
					{
						currentQueuedDrone.invisibilityCount = 0;
						currentQueuedDrone.fade = 1f;
					}
					currentQueuedDrone = self;
					currentDroneHighlight = ((Component)self).GetComponent<Highlight>();
				}
				if ((Object)(object)self == (Object)(object)currentQueuedDrone)
				{
					float num = opacityConfig.Value / 100f;
					if (enableLogging.Value)
					{
						((BaseUnityPlugin)this).Logger.LogInfo((object)$"[OpDroneTrans] Queued drone: {((Object)self.body).name} fade={self.fade} invisCount={self.invisibilityCount} -> targetFade={num}");
					}
					if (num <= 0.001f)
					{
						self.invisibilityCount = 1;
						orig.Invoke(self);
					}
					else
					{
						self.invisibilityCount = 0;
						self.fade = num;
						orig.Invoke(self);
					}
					if ((Object)(object)currentDroneHighlight == (Object)null)
					{
						currentDroneHighlight = ((Component)self).GetComponent<Highlight>();
					}
					if ((Object)(object)currentDroneHighlight != (Object)null)
					{
						if (showGlowConfig.Value && num > 0.001f)
						{
							currentDroneHighlight.isOn = true;
						}
						else
						{
							currentDroneHighlight.isOn = false;
						}
					}
					return;
				}
			}
			orig.Invoke(self);
		}

		private static bool IsOperatorDrone(CharacterModel model)
		{
			if ((Object)(object)model == (Object)null || (Object)(object)model.body == (Object)null)
			{
				return false;
			}
			string name = ((Object)model.body).name;
			bool flag = false;
			string[] droneBodyNames = DroneBodyNames;
			foreach (string value in droneBodyNames)
			{
				if (name.Contains(value))
				{
					flag = true;
					break;
				}
			}
			if (!flag && name.StartsWith("DT") && name.Contains("Drone") && !name.Contains("DroneTechBody"))
			{
				flag = true;
			}
			if (!flag)
			{
				return false;
			}
			if (!IsOwnedByLocalPlayer(model.body))
			{
				return false;
			}
			return true;
		}

		private static bool IsOwnedByLocalPlayer(CharacterBody body)
		{
			if ((Object)(object)body == (Object)null || (Object)(object)body.master == (Object)null)
			{
				return false;
			}
			MinionOwnership minionOwnership = body.master.minionOwnership;
			if ((Object)(object)minionOwnership == (Object)null || (Object)(object)minionOwnership.ownerMaster == (Object)null)
			{
				return false;
			}
			CharacterMaster ownerMaster = minionOwnership.ownerMaster;
			LocalUser firstLocalUser = LocalUserManager.GetFirstLocalUser();
			if (firstLocalUser == null || (Object)(object)firstLocalUser.cachedMasterController == (Object)null)
			{
				return false;
			}
			return (Object)(object)ownerMaster == (Object)(object)firstLocalUser.cachedMasterController.master;
		}

		private void InitRiskOfOptions()
		{
			try
			{
				RiskOfOptionsHelper.Init(opacityConfig, showGlowConfig);
			}
			catch (Exception)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"RiskOfOptions not installed — using config file only.");
			}
		}
	}
	internal static class RiskOfOptionsHelper
	{
		internal static void Init(ConfigEntry<float> opacityConfig, ConfigEntry<bool> showGlowConfig)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			ModSettingsManager.AddOption((BaseOption)new StepSliderOption(opacityConfig, new StepSliderConfig
			{
				min = 0f,
				max = 100f,
				increment = 1f,
				FormatString = "{0:0}%"
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(showGlowConfig));
			ModSettingsManager.SetModDescription("Adjust the transparency and glow of the Operator's companion drone.");
		}
	}
}