Decompiled source of SpeakingIcon v1.0.0

RepoSpeakingIcon.dll

Decompiled 33 minutes ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[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("RepoSpeakingIcon")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RepoSpeakingIcon")]
[assembly: AssemblyTitle("RepoSpeakingIcon")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 RepoSpeakingIcon
{
	[BepInPlugin("com.sangbigye.repo.speakingicon", "REPO Speaking Icon", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.sangbigye.repo.speakingicon";

		public const string Name = "REPO Speaking Icon";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			GameObject val = new GameObject("RepoSpeakingIcon.Runner");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<SpeakingIconRunner>();
			Log.LogInfo((object)"REPO Speaking Icon 1.0.0 loaded (local speaking indicator, no config).");
		}
	}
	public sealed class SpeakingIconAnimation : MonoBehaviour
	{
		private RectTransform _rect;

		private Image _image;

		private Vector2 _show;

		private Vector2 _hideOffset;

		private AnimationCurve _inCurve;

		private AnimationCurve _outCurve;

		private float _progress;

		private bool _target;

		public void Configure(RectTransform rect, Vector2 show, Vector2 hideOffset, AnimationCurve inCurve, AnimationCurve outCurve)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			_rect = rect;
			_show = show;
			_hideOffset = hideOffset;
			_inCurve = ((inCurve != null) ? inCurve : AnimationCurve.EaseInOut(0f, 0f, 1f, 1f));
			_outCurve = ((outCurve != null) ? outCurve : AnimationCurve.EaseInOut(0f, 0f, 1f, 1f));
			_image = ((Component)this).GetComponent<Image>();
			_progress = 0f;
			Apply();
		}

		public void SetShowing(bool showing)
		{
			_target = showing;
		}

		private void Update()
		{
			float num = Time.deltaTime * 4f;
			if (_target)
			{
				_progress = Mathf.Min(1f, _progress + num);
			}
			else
			{
				_progress = Mathf.Max(0f, _progress - num);
			}
			Apply();
		}

		private void Apply()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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)
			if (!((Object)(object)_rect == (Object)null))
			{
				float num = (_target ? _inCurve : _outCurve).Evaluate(_progress);
				_rect.anchoredPosition = _show + _hideOffset * (1f - num);
				if ((Object)(object)_image != (Object)null)
				{
					((Behaviour)_image).enabled = _target || _progress > 0.001f;
				}
			}
		}
	}
	public sealed class SpeakingIconRunner : MonoBehaviour
	{
		private const string ToggleMuteName = "Toggle Mute";

		private const string IconName = "Speaking Icon";

		private const string AssetFileName = "speaking_mic.png";

		private static readonly Vector2 AnchorCorner = new Vector2(1f, 0f);

		private static readonly Vector2 ShowAnchored = new Vector2(22f, -22f);

		private static readonly Vector2 IconSize = new Vector2(50f, 50f);

		private static readonly Vector2 HideOffset = new Vector2(160f, 0f);

		private static readonly Vector2 ReferenceResolution = new Vector2(712f, 400f);

		private static readonly Vector2 GameHudSize = new Vector2(680f, 370f);

		private FieldInfo _isTalkingField;

		private bool _readFailed;

		private Sprite _sprite;

		private GameObject _icon;

		private RectTransform _iconRect;

		private SpeakingIconAnimation _animation;

		private Transform _activeParent;

		private GameObject _fallbackCanvas;

		private RectTransform _fallbackContainer;

		private float _rescanTimer;

		private string _placementMode;

		private void Awake()
		{
			_isTalkingField = typeof(PlayerVoiceChat).GetField("isTalking", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (_isTalkingField == null)
			{
				Plugin.Log.LogWarning((object)"PlayerVoiceChat.isTalking field not found; speaking indicator will stay hidden.");
			}
			_sprite = LoadSprite();
		}

		private void Update()
		{
			bool showing = ReadSpeaking();
			_rescanTimer -= Time.deltaTime;
			if (_rescanTimer <= 0f)
			{
				_rescanTimer = 0.25f;
				EnsurePlacement();
			}
			if ((Object)(object)_animation != (Object)null)
			{
				_animation.SetShowing(showing);
			}
		}

		private bool ReadSpeaking()
		{
			if (_readFailed || _isTalkingField == null)
			{
				return false;
			}
			PlayerVoiceChat instance = PlayerVoiceChat.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return false;
			}
			try
			{
				return (bool)_isTalkingField.GetValue(instance);
			}
			catch (Exception ex)
			{
				_readFailed = true;
				Plugin.Log.LogWarning((object)("Failed to read PlayerVoiceChat.isTalking; indicator disabled. " + ex.GetType().Name + ": " + ex.Message));
				return false;
			}
		}

		private void EnsurePlacement()
		{
			GameObject val = GameObject.Find("Toggle Mute");
			if ((Object)(object)val != (Object)null && (Object)(object)val.transform.parent != (Object)null)
			{
				AttachIcon(val.transform.parent);
				SetFallbackActive(active: false);
				ReportMode("hud-slot");
			}
			else
			{
				EnsureFallbackCanvas();
				SetFallbackActive(active: true);
				AttachIcon((Transform)(object)_fallbackContainer);
				ReportMode("fallback-canvas");
			}
		}

		private void ReportMode(string mode)
		{
			if (!(_placementMode == mode))
			{
				_placementMode = mode;
				Plugin.Log.LogInfo((object)("[placement] speaking indicator using " + mode + "."));
			}
		}

		private void AttachIcon(Transform parent)
		{
			if (!((Object)(object)parent == (Object)null))
			{
				if ((Object)(object)_icon == (Object)null || (Object)(object)_iconRect == (Object)null)
				{
					CreateIcon(parent);
				}
				else if ((Object)(object)_activeParent != (Object)(object)parent)
				{
					_icon.transform.SetParent(parent, false);
					_activeParent = parent;
					ApplyLayout(_iconRect);
				}
			}
		}

		private bool CreateIcon(Transform parent)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_sprite == (Object)null)
			{
				return false;
			}
			GameObject val = new GameObject("Speaking Icon", new Type[3]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Image)
			});
			val.transform.SetParent(parent, false);
			_icon = val;
			_iconRect = (RectTransform)val.transform;
			_activeParent = parent;
			Image component = val.GetComponent<Image>();
			component.sprite = _sprite;
			((Graphic)component).color = Color.white;
			component.preserveAspect = true;
			((Graphic)component).raycastTarget = false;
			ApplyLayout(_iconRect);
			_animation = val.AddComponent<SpeakingIconAnimation>();
			_animation.Configure(_iconRect, ShowAnchored, HideOffset, PickWooshIn(), PickWooshAway());
			return true;
		}

		private static void ApplyLayout(RectTransform rect)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			rect.anchorMin = AnchorCorner;
			rect.anchorMax = AnchorCorner;
			rect.pivot = AnchorCorner;
			rect.sizeDelta = IconSize;
			rect.anchoredPosition = ShowAnchored;
			((Transform)rect).localScale = Vector3.one;
		}

		private void EnsureFallbackCanvas()
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Expected O, but got Unknown
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Expected O, but got Unknown
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_fallbackCanvas != (Object)null))
			{
				GameObject val = new GameObject("RepoSpeakingIcon.FallbackCanvas", new Type[3]
				{
					typeof(RectTransform),
					typeof(Canvas),
					typeof(CanvasScaler)
				});
				Canvas component = val.GetComponent<Canvas>();
				component.renderMode = (RenderMode)0;
				component.sortingOrder = 200;
				CanvasScaler component2 = val.GetComponent<CanvasScaler>();
				component2.uiScaleMode = (ScaleMode)1;
				component2.referenceResolution = ReferenceResolution;
				component2.screenMatchMode = (ScreenMatchMode)0;
				component2.matchWidthOrHeight = 0.5f;
				GameObject val2 = new GameObject("Game Hud", new Type[1] { typeof(RectTransform) });
				val2.transform.SetParent(val.transform, false);
				_fallbackContainer = (RectTransform)val2.transform;
				_fallbackContainer.anchorMin = new Vector2(0.5f, 0.5f);
				_fallbackContainer.anchorMax = new Vector2(0.5f, 0.5f);
				_fallbackContainer.pivot = new Vector2(0.5f, 0.5f);
				_fallbackContainer.sizeDelta = GameHudSize;
				_fallbackContainer.anchoredPosition = Vector2.zero;
				_fallbackCanvas = val;
			}
		}

		private void SetFallbackActive(bool active)
		{
			if ((Object)(object)_fallbackCanvas != (Object)null && _fallbackCanvas.activeSelf != active)
			{
				_fallbackCanvas.SetActive(active);
			}
		}

		private static AnimationCurve PickWooshIn()
		{
			AssetManager instance = AssetManager.instance;
			if ((Object)(object)instance != (Object)null && instance.animationCurveWooshIn != null)
			{
				return instance.animationCurveWooshIn;
			}
			return AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
		}

		private static AnimationCurve PickWooshAway()
		{
			AssetManager instance = AssetManager.instance;
			if ((Object)(object)instance != (Object)null && instance.animationCurveWooshAway != null)
			{
				return instance.animationCurveWooshAway;
			}
			return AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
		}

		private static Sprite LoadSprite()
		{
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			//IL_00b4: 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)
			try
			{
				string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty, "speaking_mic.png");
				if (!File.Exists(text))
				{
					Plugin.Log.LogWarning((object)("Speaking icon asset not found: " + text));
					return null;
				}
				Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false)
				{
					name = "RepoSpeakingIcon.Texture"
				};
				if (!ImageConversion.LoadImage(val, File.ReadAllBytes(text), false))
				{
					Plugin.Log.LogWarning((object)("Could not decode speaking icon asset: " + text));
					Object.Destroy((Object)(object)val);
					return null;
				}
				((Texture)val).wrapMode = (TextureWrapMode)1;
				((Texture)val).filterMode = (FilterMode)1;
				Sprite obj = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
				((Object)obj).name = "RepoSpeakingIcon.Sprite";
				return obj;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Failed to load speaking icon asset. " + ex.GetType().Name + ": " + ex.Message));
				return null;
			}
		}
	}
}