Decompiled source of RestingRockFace v1.1.2

plugins\RestingRockFace.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Jotunn;
using Jotunn.Managers;
using Jotunn.Utils;
using RestingRockFace.Components;
using RestingRockFace.Gui;
using RestingRockFace.Helpers;
using RestingRockFace.Models;
using Splatform;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("RestingRockFace")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RestingRockFace")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.1.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.2.0")]
namespace RestingRockFace
{
	[BepInPlugin("DeathWizsh.RestingRockFace", "RestingRockFace", "1.1.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class RestingRockFace : BaseUnityPlugin
	{
		public const string PluginGUID = "DeathWizsh.RestingRockFace";

		public const string PluginName = "RestingRockFace";

		public const string PluginVersion = "1.1.2";

		public static RestingRockFace Instance;

		private static readonly Harmony harmony = new Harmony("DeathWizsh.RestingRockFace");

		public RockyGUI rockyGUI;

		public void Awake()
		{
			Instance = this;
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			PrefabManager.OnVanillaPrefabsAvailable += SetupRocky;
		}

		private void SetupRocky()
		{
			try
			{
				GameObject prefab = PrefabManager.Instance.GetPrefab("Placeable_HardRock");
				prefab.transform.Find("default");
				prefab.AddComponent<RockyControls>();
				PrefabManager.OnVanillaPrefabsAvailable -= SetupRocky;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Could not update rocky: " + ex));
				PrefabManager.OnVanillaPrefabsAvailable -= SetupRocky;
			}
		}
	}
}
namespace RestingRockFace.Types
{
	internal enum RockyFaceEnum
	{
		Dead = 5,
		LookLeft = 8,
		LookRight = 7,
		NoFeeling = 2,
		Nothing = 6,
		Random = -1,
		Sad = 1,
		Scream = 3,
		Smile = 4,
		SmileBig = 0
	}
	internal class RockyFaceTypes
	{
		public const string Dead = "Dead";

		public const string LookLeft = "Look left";

		public const string LookRight = "Look right";

		public const string NoFeeling = "No feeling";

		public const string Nothing = "Nothing";

		public const string Random = "Random (Let the game decide)";

		public const string Sad = "Sad";

		public const string Scream = "Scream";

		public const string Smile = "Smile";

		public const string SmileBig = "Smile big";
	}
}
namespace RestingRockFace.Models
{
	internal class RockyGuiResult
	{
		public string name;

		public int face;

		public RockyGuiResult(string name, int face)
		{
			this.face = face;
			this.name = name;
		}
	}
}
namespace RestingRockFace.Helpers
{
	internal static class RockyFaceHelper
	{
		public static void ResetToGameDecided(MaterialVariation materialVar, ZNetView netView)
		{
			netView.GetZDO().RemoveInt("MatVar" + materialVar.m_materialIndex);
			Traverse obj = Traverse.Create((object)materialVar);
			obj.Field("m_variation").SetValue((object)(-1));
			obj.Field("m_isSet").SetValue((object)false);
			obj.Method("CheckMaterial", Array.Empty<object>()).GetValue();
		}
	}
	internal static class RockyGuiState
	{
		private static RockyGUI s_gui;

		public static bool IsOpen
		{
			get
			{
				if (s_gui != null)
				{
					return s_gui.IsVisible;
				}
				return false;
			}
		}

		public static void SetGui(RockyGUI gui)
		{
			s_gui = gui;
		}

		public static void CloseOpenPanel()
		{
			if (IsOpen)
			{
				s_gui.Close();
			}
		}
	}
}
namespace RestingRockFace.Harmony
{
	[HarmonyPatch]
	public class PatchesRRF
	{
		[HarmonyPrefix]
		[HarmonyPatch(typeof(Tameable), "Interact")]
		public static bool Interact_Prefix(ref Tameable __instance, Humanoid user, bool hold, bool alt)
		{
			try
			{
				if ((Object)(object)__instance == (Object)null || hold || !alt)
				{
					return true;
				}
				if (((Object)((Component)__instance).gameObject).name != "Placeable_HardRock(Clone)")
				{
					return true;
				}
				RockyControls component = ((Component)__instance).gameObject.GetComponent<RockyControls>();
				if ((Object)(object)component == (Object)null || component.m_gui == null)
				{
					return true;
				}
				component.m_gui.ShowGUI();
				return false;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Somethign went wrong in Interact_Prefix: " + ex));
				return true;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(GameCamera), "UpdateCamera")]
		public static bool UpdateCamera_Prefix()
		{
			if (RockyGuiState.IsOpen)
			{
				return ZInput.GetMouseScrollWheel() == 0f;
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Player), "OnDeath")]
		public static void OnDeath_Postfix(Player __instance)
		{
			if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer)
			{
				RockyGuiState.CloseOpenPanel();
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Pet), "SetFace")]
		public static bool SetFace_Prefix(Pet __instance, int index)
		{
			try
			{
				if ((Object)(object)__instance == (Object)null)
				{
					return true;
				}
				if (((Object)((Component)__instance).gameObject).name != "Placeable_HardRock(Clone)")
				{
					return true;
				}
				RockyControls component = ((Component)__instance).gameObject.GetComponent<RockyControls>();
				if ((Object)(object)component == (Object)null)
				{
					throw new Exception("RockyControls is null somehow");
				}
				if (component.m_face == -1)
				{
					return true;
				}
				return false;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Somethign went wrong in SetFace_Prefix: " + ex));
				return true;
			}
		}
	}
}
namespace RestingRockFace.Gui
{
	internal class RockyGUI
	{
		private GameObject panel;

		public UnityEvent<RockyGuiResult> onAccept = new UnityEvent<RockyGuiResult>();

		public UnityEvent onClose = new UnityEvent();

		public UnityEvent onSelect = new UnityEvent();

		private RockyControls rockyControls;

		private RockyGuiResult currentForm;

		private float panelWidth = 420f;

		private float panelHeight = 230f;

		private float fieldWidth = 385f;

		private float fieldHeight = 30f;

		private float labelHeight = 30f;

		private float buttonWidth = 190f;

		private float buttonHeight = 50f;

		private float buttonYoffset = 42f;

		private static readonly int[] dropdownFaces = new int[10] { -1, 5, 8, 7, 2, 6, 1, 3, 4, 0 };

		public bool IsVisible
		{
			get
			{
				if ((Object)(object)panel != (Object)null)
				{
					return panel.activeInHierarchy;
				}
				return false;
			}
		}

		public RockyGUI(RockyControls rockyControls)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			this.rockyControls = rockyControls;
		}

		public void ShowGUI()
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			if (GUIManager.Instance == null)
			{
				Logger.LogError((object)"GUIManager instance is null");
				return;
			}
			if (!Object.op_Implicit((Object)(object)GUIManager.CustomGUIFront))
			{
				Logger.LogError((object)"GUIManager CustomGUI is null");
				return;
			}
			if ((Object)(object)panel == (Object)null)
			{
				panel = GUIManager.Instance.CreateWoodpanel(GUIManager.CustomGUIFront.transform, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0f, 0f), panelWidth, panelHeight, false);
			}
			currentForm = new RockyGuiResult(rockyControls.m_tameable.GetName(), rockyControls.m_face);
			panel.SetActive(false);
			ClearGUI();
			CreateGUI();
			RockyGuiState.SetGui(this);
			panel.SetActive(true);
			GUIManager.BlockInput(true);
		}

		public void Close()
		{
			onClose.Invoke();
			panel.SetActive(false);
			GUIManager.BlockInput(false);
		}

		public void Accept()
		{
			onAccept.Invoke(currentForm);
			onClose.Invoke();
			panel.SetActive(false);
			GUIManager.BlockInput(false);
		}

		public void SetName(string value)
		{
			currentForm.name = value;
		}

		public void SetFace(int value)
		{
			currentForm.face = GetRockyFace(value);
		}

		public void UpdateGUI()
		{
			ClearGUI();
			CreateGUI();
		}

		private void ClearGUI()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			foreach (Transform item in panel.transform)
			{
				Object.Destroy((Object)(object)((Component)item).gameObject);
			}
		}

		private void CreateGUI()
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0302: Expected O, but got Unknown
			//IL_0321: Unknown result type (might be due to invalid IL or missing references)
			//IL_0330: Unknown result type (might be due to invalid IL or missing references)
			//IL_0340: Unknown result type (might be due to invalid IL or missing references)
			//IL_036e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Expected O, but got Unknown
			GUIManager.Instance.CreateText("Name", panel.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(0f, -30f), GUIManager.Instance.AveriaSerifBold, 20, GUIManager.Instance.ValheimOrange, true, Color.black, fieldWidth, labelHeight, false);
			InputField component = GUIManager.Instance.CreateInputField(panel.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(0f, -60f), (ContentType)0, "Name", 18, fieldWidth, fieldHeight).GetComponent<InputField>();
			component.text = currentForm.name;
			((UnityEvent<string>)(object)component.onValueChanged).AddListener((UnityAction<string>)SetName);
			component.characterLimit = 64;
			GUIManager.Instance.CreateText("Face", panel.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(0f, -100f), GUIManager.Instance.AveriaSerifBold, 20, GUIManager.Instance.ValheimOrange, true, Color.black, fieldWidth, labelHeight, false);
			Dropdown component2 = GUIManager.Instance.CreateDropDown(panel.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(0f, -130f), 18, fieldWidth, fieldHeight).GetComponent<Dropdown>();
			((Component)component2).GetComponent<Dropdown>().AddOptions(new List<string> { "Random (Let the game decide)", "Dead", "Look left", "Look right", "No feeling", "Nothing", "Sad", "Scream", "Smile", "Smile big" });
			component2.value = GetDropdownValue(currentForm.face);
			((UnityEvent<int>)(object)component2.onValueChanged).AddListener((UnityAction<int>)SetFace);
			ScrollRect component3 = ((Component)component2.template).GetComponent<ScrollRect>();
			if ((Object)(object)component3 != (Object)null)
			{
				component3.scrollSensitivity = 1000f;
			}
			GameObject obj = GUIManager.Instance.CreateButton("Close", panel.transform, new Vector2(0.5f, 0f), new Vector2(0.5f, 0f), new Vector2(-100f, buttonYoffset), buttonWidth, buttonHeight);
			obj.SetActive(true);
			((UnityEvent)obj.GetComponent<Button>().onClick).AddListener(new UnityAction(Close));
			GameObject obj2 = GUIManager.Instance.CreateButton("Accept", panel.transform, new Vector2(0.5f, 0f), new Vector2(0.5f, 0f), new Vector2(100f, buttonYoffset), buttonWidth, buttonHeight);
			obj2.SetActive(true);
			((UnityEvent)obj2.GetComponent<Button>().onClick).AddListener(new UnityAction(Accept));
		}

		private int GetRockyFace(int value)
		{
			if (value < 0 || value >= dropdownFaces.Length)
			{
				return -1;
			}
			return dropdownFaces[value];
		}

		private int GetDropdownValue(int value)
		{
			int num = Array.IndexOf(dropdownFaces, value);
			if (num < 0)
			{
				return 0;
			}
			return num;
		}
	}
}
namespace RestingRockFace.Components
{
	internal class RockyControls : MonoBehaviour
	{
		public ZNetView m_netView;

		public RockyGUI m_gui;

		public Tameable m_tameable;

		public MaterialVariation materialVar;

		public Transform m_faceTransform;

		private readonly int faceDataHash = StringExtensionMethods.GetStableHashCode("FaceData_RRF");

		public int m_face
		{
			get
			{
				if (!((Object)(object)m_netView != (Object)null) || m_netView.GetZDO() == null)
				{
					return -1;
				}
				return m_netView.GetZDO().GetInt(faceDataHash, -1);
			}
		}

		public void Awake()
		{
			m_netView = ((Component)this).gameObject.GetComponent<ZNetView>();
			if (!((Object)(object)m_netView == (Object)null) && m_netView.GetZDO() != null)
			{
				m_netView.Register<int>("SetFace_RRF", (Action<long, int>)RPC_SetFace);
				m_netView.Register<string, string>("SetNameDirect_RRF", (Action<long, string, string>)RPC_SetNameDirect);
				m_faceTransform = ((Component)this).gameObject.transform.Find("default");
				materialVar = ((Component)m_faceTransform).gameObject.GetComponent<MaterialVariation>();
				m_tameable = ((Component)this).gameObject.GetComponent<Tameable>();
				m_gui = new RockyGUI(this);
				m_gui.onAccept.AddListener((UnityAction<RockyGuiResult>)SetNameAndFace);
			}
		}

		public void Start()
		{
			if (!((Object)(object)m_netView == (Object)null) && m_netView.GetZDO() != null && m_face != -1)
			{
				materialVar.SetMaterial(m_face);
			}
		}

		public void SetNameAndFace(RockyGuiResult result)
		{
			if (m_netView.IsValid())
			{
				bool num = result.name != m_tameable.GetName();
				if (num || result.face != m_face)
				{
					m_netView.ClaimOwnership();
				}
				if (num)
				{
					SetName(result.name);
				}
				m_netView.InvokeRPC("SetFace_RRF", new object[1] { result.face });
			}
		}

		private unsafe void SetName(string name)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (m_tameable.IsTamed())
			{
				m_tameable.SetText(name);
				return;
			}
			PlatformUserID platformUserID = ((IUser)PlatformManager.DistributionPlatform.LocalUser).PlatformUserID;
			m_netView.InvokeRPC("SetNameDirect_RRF", new object[2]
			{
				name,
				PlatformManager.DistributionPlatform.LocalUser.IsSignedIn ? ((object)(*(PlatformUserID*)(&platformUserID))/*cast due to .constrained prefix*/).ToString() : "host"
			});
		}

		public void RPC_SetNameDirect(long sender, string name, string authorId)
		{
			if (m_netView.IsValid() && m_netView.IsOwner())
			{
				m_netView.GetZDO().Set(ZDOVars.s_tamedName, name);
				m_netView.GetZDO().Set(ZDOVars.s_tamedNameAuthor, authorId);
			}
		}

		public void RPC_SetFace(long sender, int face)
		{
			if (m_netView.IsValid() && m_netView.IsOwner())
			{
				UpdateFace(face);
				if (face == -1)
				{
					RockyFaceHelper.ResetToGameDecided(materialVar, m_netView);
				}
				else
				{
					materialVar.SetMaterial(face);
				}
			}
		}

		public void UpdateFace(int face)
		{
			if (face == -1)
			{
				m_netView.GetZDO().RemoveInt(faceDataHash);
			}
			else
			{
				m_netView.GetZDO().Set(faceDataHash, face, false);
			}
		}
	}
}