Decompiled source of SakeysPScontrollerRemember v1.0.0

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

Decompiled 7 hours 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("SakeysControllerRemember")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SakeysControllerRemember")]
[assembly: AssemblyTitle("SakeysControllerRemember")]
[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.0")]
	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.0";

		private Assembly _utilsAssembly;

		private Type _zInputType;

		private Type _settingsGuiType;

		private PropertyInfo _currentGlyphProperty;

		private FieldInfo _currentGlyphField;

		private bool _applied;

		private bool _currentGlyphSet;

		private bool _callbackInvoked;

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

		private IEnumerator Initialize()
		{
			for (int frame = 0; frame < 900; frame++)
			{
				if (_applied)
				{
					break;
				}
				FindUtilsAssembly();
				if (_utilsAssembly != null)
				{
					FindTypes();
					TrySetCurrentGlyph();
					TryApplySettingsGlyphCallback();
				}
				if (!_applied)
				{
					yield return null;
				}
			}
			if (!_applied)
			{
				((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) || string.Equals(name, "assembly_utils(1)", StringComparison.OrdinalIgnoreCase))
				{
					_utilsAssembly = assembly;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Found Valheim assembly_utils.");
					break;
				}
			}
		}

		private void FindTypes()
		{
			if (_zInputType == null)
			{
				_zInputType = _utilsAssembly.GetType("ZInput", throwOnError: false) ?? _utilsAssembly.GetType("Valheim.ZInput", throwOnError: false);
				if (_zInputType != null)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Found ZInput: " + _zInputType.FullName));
				}
			}
			if (_settingsGuiType == null)
			{
				_settingsGuiType = _utilsAssembly.GetType("Valheim.SettingsGui", throwOnError: false) ?? _utilsAssembly.GetType("SettingsGui", throwOnError: false);
				if (_settingsGuiType != null)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Found SettingsGui in assembly_utils: " + _settingsGuiType.FullName));
				}
			}
			if (_zInputType != null && _currentGlyphProperty == null && _currentGlyphField == null)
			{
				_currentGlyphProperty = _zInputType.GetProperty("CurrentGlyph", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				if (_currentGlyphProperty == null)
				{
					_currentGlyphField = _zInputType.GetField("CurrentGlyph", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				}
			}
		}

		private void TrySetCurrentGlyph()
		{
			if (_currentGlyphSet || _zInputType == null || (_currentGlyphProperty == null && _currentGlyphField == null))
			{
				return;
			}
			try
			{
				object value = Enum.ToObject((_currentGlyphProperty != null) ? _currentGlyphProperty.PropertyType : _currentGlyphField.FieldType, 2);
				if (_currentGlyphProperty != null && _currentGlyphProperty.CanWrite)
				{
					_currentGlyphProperty.SetValue(null, value, null);
					_currentGlyphSet = true;
					_applied = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"ZInput.CurrentGlyph set to PlayStation (2).");
				}
				else if (_currentGlyphField != null)
				{
					_currentGlyphField.SetValue(null, value);
					_currentGlyphSet = true;
					_applied = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"ZInput.CurrentGlyph field set to PlayStation (2).");
				}
			}
			catch (Exception ex)
			{
				if (!_currentGlyphSet)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not set ZInput.CurrentGlyph: " + ex.GetType().Name + ": " + ex.Message));
				}
			}
		}

		private void TryApplySettingsGlyphCallback()
		{
			if (_callbackInvoked || _settingsGuiType == null)
			{
				return;
			}
			try
			{
				MethodInfo methodInfo = null;
				MethodInfo[] methods = _settingsGuiType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (MethodInfo methodInfo2 in methods)
				{
					if (methodInfo2.Name == "OnGamepadGlyphChanged" && methodInfo2.GetParameters().Length == 1)
					{
						methodInfo = methodInfo2;
						break;
					}
				}
				if (methodInfo == null)
				{
					return;
				}
				object obj = null;
				if (!methodInfo.IsStatic)
				{
					Object[] array = Resources.FindObjectsOfTypeAll(_settingsGuiType);
					if (array != null && array.Length != 0)
					{
						obj = array[0];
					}
				}
				if (methodInfo.IsStatic || obj != null)
				{
					methodInfo.Invoke(methodInfo.IsStatic ? null : obj, new object[1] { 2 });
					_callbackInvoked = true;
					_applied = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Valheim OnGamepadGlyphChanged(2) invoked for PlayStation.");
				}
			}
			catch (Exception ex)
			{
				if (!_callbackInvoked)
				{
					((BaseUnityPlugin)this).Logger.LogDebug((object)("Could not invoke OnGamepadGlyphChanged: " + ex.GetType().Name + ": " + ex.Message));
				}
			}
		}
	}
}