Decompiled source of AvatarLock v1.0.0

Mods/AvatarLock.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using AvatarLock;
using BoneLib;
using BoneLib.BoneMenu;
using Il2CppSLZ.Marrow;
using Il2CppSLZ.Marrow.Warehouse;
using Il2CppSystem;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(AvatarLockMod), "Avatar Lock", "1.0.0", "User", null)]
[assembly: MelonGame("Stress Level Zero", "BONELAB")]
[assembly: MelonPriority(-500)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("AvatarLock")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("AvatarLock")]
[assembly: AssemblyTitle("AvatarLock")]
[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 AvatarLock
{
	public class AvatarLockMod : MelonMod
	{
		private static readonly string CategoryName = "AvatarLock";

		private static MelonPreferences_Category _prefs;

		private static MelonPreferences_Entry<bool> _pLocked;

		private static Page _page;

		private static bool _locked;

		private static string _pinnedBarcode = string.Empty;

		private static float _lastRevertTime;

		private const float RevertCooldown = 1.5f;

		private static PropertyInfo _avatarOverrideProp;

		private static bool _overrideResolved;

		public override void OnLateInitializeMelon()
		{
			try
			{
				_prefs = MelonPreferences.CreateCategory(CategoryName, "Avatar Lock");
				_pLocked = _prefs.CreateEntry<bool>("Locked", false, "Lock Avatar", (string)null, false, false, (ValueValidator)null, (string)null);
				_locked = _pLocked.Value;
				if (_locked)
				{
					_pinnedBarcode = _prefs.CreateEntry<string>("PinnedBarcode", string.Empty, "Pinned Avatar", (string)null, false, false, (ValueValidator)null, (string)null).Value;
				}
				CreateMenu();
				MelonLogger.Msg("Avatar Lock loaded (locked=" + _locked + ")");
			}
			catch (Exception ex)
			{
				MelonLogger.Msg("Avatar Lock init error: " + ex.Message);
			}
		}

		private void CreateMenu()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				_page = Page.Root.CreatePage("Avatar Lock", Color.yellow, 0, true);
				_page.CreateBool("Lock Avatar", Color.yellow, _locked, (Action<bool>)delegate(bool value)
				{
					SetLocked(value);
				});
				_page.CreateFunction("Pin Current Avatar", Color.cyan, (Action)delegate
				{
					LockToCurrent(fromToggle: false);
				});
				_page.CreateFunction("Unlock", Color.grey, (Action)delegate
				{
					SetLocked(value: false);
				});
			}
			catch (Exception ex)
			{
				MelonLogger.Msg("Avatar Lock menu error: " + ex.Message);
			}
		}

		private static void SetLocked(bool value)
		{
			_locked = value;
			if (_pLocked != null)
			{
				_pLocked.Value = value;
				MelonPreferences_Entry<string> entry = _prefs.GetEntry<string>("PinnedBarcode");
				if (entry != null)
				{
					entry.Value = _pinnedBarcode;
				}
				_prefs.SaveToFile(false);
			}
			if (_locked)
			{
				LockToCurrent(fromToggle: true);
			}
			else
			{
				MelonLogger.Msg("Avatar Lock unlocked - avatar can be changed freely");
			}
		}

		private static void LockToCurrent(bool fromToggle)
		{
			try
			{
				RigManager rigManager = Player.RigManager;
				if ((Object)(object)rigManager == (Object)null || rigManager.AvatarCrate == null || string.IsNullOrWhiteSpace(((ScannableReference)rigManager.AvatarCrate).Barcode.ID))
				{
					MelonLogger.Msg("Avatar Lock: no rig to pin yet");
					return;
				}
				_pinnedBarcode = ((ScannableReference)rigManager.AvatarCrate).Barcode.ID;
				_locked = true;
				if (_pLocked != null)
				{
					_pLocked.Value = true;
					MelonPreferences_Entry<string> entry = _prefs.GetEntry<string>("PinnedBarcode");
					if (entry != null)
					{
						entry.Value = _pinnedBarcode;
					}
					_prefs.SaveToFile(false);
				}
				MelonLogger.Msg("Avatar Lock pinned: " + _pinnedBarcode);
			}
			catch (Exception ex)
			{
				MelonLogger.Msg("Avatar Lock pin error: " + ex.Message);
			}
		}

		public override void OnUpdate()
		{
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Expected O, but got Unknown
			if (!_locked || string.IsNullOrEmpty(_pinnedBarcode))
			{
				return;
			}
			try
			{
				NeutralizeFusionOverride();
				RigManager rigManager = Player.RigManager;
				if ((Object)(object)rigManager == (Object)null || rigManager.AvatarCrate == null)
				{
					return;
				}
				string iD = ((ScannableReference)rigManager.AvatarCrate).Barcode.ID;
				if (!string.IsNullOrEmpty(iD) && !(iD == _pinnedBarcode))
				{
					float unscaledTime = Time.unscaledTime;
					if (!(unscaledTime - _lastRevertTime < 1.5f))
					{
						_lastRevertTime = unscaledTime;
						MelonLogger.Msg("Avatar Lock: blocked " + iD + " -> restoring " + _pinnedBarcode);
						rigManager.SwapAvatarCrate(new Barcode(_pinnedBarcode), true, Action<bool>.op_Implicit((Action<bool>)null));
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private static void NeutralizeFusionOverride()
		{
			try
			{
				if (!_overrideResolved)
				{
					_overrideResolved = true;
					Type type = Type.GetType("LabFusion.Player.LocalAvatar, LabFusion");
					if (type != null)
					{
						_avatarOverrideProp = type.GetProperty("AvatarOverride", BindingFlags.Static | BindingFlags.Public);
					}
				}
				if (!(_avatarOverrideProp == null))
				{
					string text = _avatarOverrideProp.GetValue(null) as string;
					if (!string.IsNullOrEmpty(text) && text != _pinnedBarcode)
					{
						_avatarOverrideProp.SetValue(null, _pinnedBarcode);
					}
				}
			}
			catch (Exception)
			{
			}
		}
	}
}