Decompiled source of RestingRockFace v1.0.1

plugins\RestingRockFace.dll

Decompiled a week 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.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.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RestingRockFace
{
	[BepInPlugin("DeathWizsh.RestingRockFace", "RestingRockFace", "1.0.0")]
	[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.0.0";

		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,
		Sad = 1,
		Scream = 3,
		Smile = 4,
		SmileBig = 0
	}
	internal class RockyFaceTypes
	{
		public string Dead => "Dead";

		public string LookLeft => "Look left";

		public string LookRight => "Look right";

		public string NoFeeling => "No feeling";

		public string Nothing => "Nothing";

		public string Sad => "Sad";

		public string Scream => "Scream";

		public string Smile => "Smile";

		public 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.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;
				}
				((Component)__instance).gameObject.GetComponent<RockyControls>().m_gui.ShowGUI();
				return false;
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("Somethign went wrong in GetText_Prefix: " + ex));
				return true;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(Pet), "SetFace")]
		public static bool SetFace_Prefix(ref Tameable __instance, int index)
		{
			try
			{
				if ((Object)(object)__instance == (Object)null)
				{
					return true;
				}
				if (((Object)((Component)__instance).gameObject).name != "Placeable_HardRock(Clone)")
				{
					return true;
				}
				Logger.LogWarning((object)("Harmony SetFace: " + index));
				RockyControls component = ((Component)__instance).gameObject.GetComponent<RockyControls>();
				if ((Object)(object)component == (Object)null)
				{
					throw new Exception("RockyControls is null somehow");
				}
				if (component.m_face == 6)
				{
					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;

		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_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			Logger.LogWarning((object)"ShowGUI()");
			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_name, (rockyControls.m_face == -1) ? 2 : rockyControls.m_face);
			panel.SetActive(false);
			CreateGUI();
			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)
		{
			Logger.LogWarning((object)("SetName(), " + value));
			currentForm.name = value;
		}

		public void SetFace(int value)
		{
			Logger.LogWarning((object)("SetFace(), " + value));
			currentForm.face = GetRockyFace(value);
		}

		public void UpdateGUI()
		{
			//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);
			}
			CreateGUI();
		}

		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_0280: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d7: Expected O, but got Unknown
			//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0305: Unknown result type (might be due to invalid IL or missing references)
			//IL_0315: Unknown result type (might be due to invalid IL or missing references)
			//IL_0343: Unknown result type (might be due to invalid IL or missing references)
			//IL_034d: 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> { "Dead", "LookLeft", "LookRight", "NoFeeling", "Nothing", "Sad", "Scream", "Smile", "SmileBig" });
			component2.value = GetDropdownValue(currentForm.face);
			((UnityEvent<int>)(object)component2.onValueChanged).AddListener((UnityAction<int>)SetFace);
			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)
		{
			return value switch
			{
				0 => 5, 
				1 => 8, 
				2 => 7, 
				3 => 2, 
				5 => 1, 
				6 => 3, 
				7 => 4, 
				8 => 0, 
				_ => 6, 
			};
		}

		private int GetDropdownValue(int value)
		{
			return value switch
			{
				5 => 0, 
				8 => 1, 
				7 => 2, 
				2 => 3, 
				1 => 5, 
				3 => 6, 
				4 => 7, 
				0 => 8, 
				_ => 4, 
			};
		}
	}
}
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 string m_name;

		public int m_face;

		public void Awake()
		{
			m_netView = ((Component)this).gameObject.GetComponent<ZNetView>();
			if (!((Object)(object)m_netView == (Object)null) && m_netView.GetZDO() != null)
			{
				m_netView.Register<string, string, int>("SetNameAndFace", (Action<long, string, string, int>)RPC_SetNameAndFace);
				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);
				m_name = m_tameable.GetName();
				m_face = m_netView.GetZDO().GetInt(faceDataHash, 6);
				UpdateFace(m_face);
				Logger.LogWarning((object)("Awake name: " + m_name));
				Logger.LogWarning((object)("Awake face: " + m_face));
			}
		}

		public void Start()
		{
			if (!((Object)(object)m_netView == (Object)null) && m_netView.GetZDO() != null)
			{
				Logger.LogWarning((object)("Start SetMaterial: " + (m_face != 6)));
				if (m_face != 6)
				{
					materialVar.SetMaterial(m_face);
				}
			}
		}

		public unsafe void SetNameAndFace(RockyGuiResult result)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			Logger.LogWarning((object)$"Result: {result.name}, {result.face}");
			m_name = result.name;
			m_face = result.face;
			if (m_netView.IsValid())
			{
				PlatformUserID platformUserID = ((IUser)PlatformManager.DistributionPlatform.LocalUser).PlatformUserID;
				m_netView.InvokeRPC("SetNameAndFace", new object[3]
				{
					result.name,
					PlatformManager.DistributionPlatform.LocalUser.IsSignedIn ? ((object)(*(PlatformUserID*)(&platformUserID))/*cast due to .constrained prefix*/).ToString() : "host",
					result.face
				});
			}
		}

		public void RPC_SetNameAndFace(long sender, string name, string authorId, int face)
		{
			if (m_netView.IsValid() && m_netView.IsOwner())
			{
				m_netView.GetZDO().Set(ZDOVars.s_tamedName, name);
				m_netView.GetZDO().Set(ZDOVars.s_tamedNameAuthor, authorId);
				UpdateFace(face);
				materialVar.SetMaterial(face);
			}
		}

		public void UpdateFace(int face)
		{
			if (face == 6)
			{
				m_netView.GetZDO().RemoveInt(faceDataHash);
				Logger.LogWarning((object)"UpdateFace: removed ZDO");
			}
			else
			{
				m_netView.GetZDO().Set(faceDataHash, face, false);
				Logger.LogWarning((object)"UpdateFace: set ZDO");
			}
		}
	}
}