Decompiled source of SakeysPScontrollerRemember v1.0.1

BepInEx/plugins/Sakey's PSController Remember/SakeysControllerRememberPS.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using Microsoft.CodeAnalysis;
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("SakeysControllerRememberPS")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SakeysControllerRememberPS")]
[assembly: AssemblyTitle("SakeysControllerRememberPS")]
[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 SakeysControllerRemember
{
	[BepInPlugin("com.sakey.controllerremember", "Sakey's PS Controller Remember", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.sakey.controllerremember";

		public const string PluginName = "Sakey's PS Controller Remember";

		public const string PluginVersion = "1.0.1";

		private Assembly _utilsAssembly;

		private Type _zInputType;

		private object _zInputInstance;

		private FieldInfo _currentGlyphBackingField;

		private PropertyInfo _currentGlyphProperty;

		private bool _glyphSet;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Sakey's PS Controller Remember 1.0.1 loaded.");
			((MonoBehaviour)this).StartCoroutine(Initialize());
		}

		private IEnumerator Initialize()
		{
			for (int frame = 0; frame < 900; frame++)
			{
				FindUtilsAssembly();
				if (_utilsAssembly != null)
				{
					FindZInput();
					if (_zInputType != null && IsZInputInitialized())
					{
						yield return null;
						TrySetPlayStationGlyphDirectly();
						if (_glyphSet)
						{
							yield break;
						}
					}
				}
				yield return null;
			}
			if (!_glyphSet)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not set PlayStation glyphs during startup.");
			}
		}

		private void FindUtilsAssembly()
		{
			if (_utilsAssembly != null)
			{
				return;
			}
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				string name;
				try
				{
					name = assembly.GetName().Name;
				}
				catch
				{
					continue;
				}
				if (string.Equals(name, "assembly_utils", StringComparison.OrdinalIgnoreCase))
				{
					_utilsAssembly = assembly;
					break;
				}
			}
		}

		private void FindZInput()
		{
			if (!(_zInputType != null) && !(_utilsAssembly == null))
			{
				_zInputType = _utilsAssembly.GetType("ZInput", throwOnError: false) ?? _utilsAssembly.GetType("Valheim.ZInput", throwOnError: false);
			}
		}

		private bool IsZInputInitialized()
		{
			if (_zInputType == null)
			{
				return false;
			}
			try
			{
				FieldInfo field = _zInputType.GetField("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null)
				{
					_zInputInstance = field.GetValue(null);
					return _zInputInstance != null;
				}
				PropertyInfo property = _zInputType.GetProperty("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (property != null)
				{
					_zInputInstance = property.GetValue(null, null);
					return _zInputInstance != null;
				}
			}
			catch
			{
			}
			return true;
		}

		private void TrySetPlayStationGlyphDirectly()
		{
			if (_glyphSet || _zInputType == null)
			{
				return;
			}
			try
			{
				FindCurrentGlyphMembers();
				if (_currentGlyphBackingField != null)
				{
					object value = Enum.ToObject(_currentGlyphBackingField.FieldType, 2);
					_currentGlyphBackingField.SetValue(null, value);
					_glyphSet = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"PlayStation glyphs forced through CurrentGlyph backing field. Native gamepad bindings were left untouched.");
				}
				else if (_currentGlyphProperty != null && _currentGlyphProperty.CanWrite)
				{
					object value2 = Enum.ToObject(_currentGlyphProperty.PropertyType, 2);
					_currentGlyphProperty.SetValue(null, value2, null);
					_glyphSet = true;
					((BaseUnityPlugin)this).Logger.LogWarning((object)"CurrentGlyph backing field was not found; used the property setter fallback.");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogDebug((object)("Could not set PlayStation glyphs: " + ex.GetType().Name + ": " + ex.Message));
			}
		}

		private void FindCurrentGlyphMembers()
		{
			if (_zInputType == null)
			{
				return;
			}
			if (_currentGlyphBackingField == null)
			{
				FieldInfo[] fields = _zInputType.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				FieldInfo[] array = fields;
				foreach (FieldInfo fieldInfo in array)
				{
					if (fieldInfo.FieldType.IsEnum && fieldInfo.Name.IndexOf("currentGlyph", StringComparison.OrdinalIgnoreCase) >= 0)
					{
						_currentGlyphBackingField = fieldInfo;
						break;
					}
				}
				if (_currentGlyphBackingField == null)
				{
					array = fields;
					foreach (FieldInfo fieldInfo2 in array)
					{
						if (!fieldInfo2.FieldType.IsEnum)
						{
							continue;
						}
						string[] names = Enum.GetNames(fieldInfo2.FieldType);
						bool flag = false;
						string[] array2 = names;
						for (int j = 0; j < array2.Length; j++)
						{
							if (array2[j].IndexOf("Playstation", StringComparison.OrdinalIgnoreCase) >= 0)
							{
								flag = true;
								break;
							}
						}
						if (flag && fieldInfo2.Name.IndexOf("glyph", StringComparison.OrdinalIgnoreCase) >= 0)
						{
							_currentGlyphBackingField = fieldInfo2;
							break;
						}
					}
				}
			}
			if (_currentGlyphProperty == null)
			{
				_currentGlyphProperty = _zInputType.GetProperty("CurrentGlyph", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			}
		}
	}
}