Decompiled source of Epic Loot Skulds Thread Graphic v0.1.0

BepInEx/plugins/OctoberJemini-Epic_Loot_Skulds_Thread_Graphic/SkuldsThreadRope.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.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 OctoberJemini.SkuldsThreadRope
{
	[BepInPlugin("OctoberJemini.SkuldsThreadRope", "Skuld's Thread Rope", "0.1.0")]
	[BepInDependency("randyknapp.mods.epicloot", "0.16.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private static Plugin instance;

		private static Sprite icon;

		private static Mesh rope;

		private static Material material;

		private const string VisualName = "OCT_SkuldsThreadRope";

		private Harmony harmony;

		private void Awake()
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0126: Expected O, but got Unknown
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Expected O, but got Unknown
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Expected O, but got Unknown
			instance = this;
			try
			{
				Type type = AccessTools.TypeByName("EpicLoot.EpicLoot");
				MethodInfo methodInfo = AccessTools.Method(type, "SetupSkuldsThread", (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					throw new MissingMethodException("EpicLoot.SetupSkuldsThread");
				}
				using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("rope.png"))
				{
					byte[] array = new byte[stream.Length];
					int num;
					for (int i = 0; i < array.Length; i += num)
					{
						if ((num = stream.Read(array, i, array.Length - i)) <= 0)
						{
							break;
						}
					}
					Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
					if (!ImageConversion.LoadImage(val, array))
					{
						throw new InvalidDataException("Rope icon could not load");
					}
					((Object)val).name = "OCT_SkuldsThreadIcon";
					icon = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
				}
				rope = RopeMesh.Build();
				harmony = new Harmony("OctoberJemini.SkuldsThreadRope");
				harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "AfterSetup", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				harmony.Patch((MethodBase)AccessTools.Method(typeof(ItemDrop), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "AfterItemAwake", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Skuld's Thread rope appearance ready.");
			}
			catch (Exception ex)
			{
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
				((BaseUnityPlugin)this).Logger.LogError((object)ex);
			}
		}

		private static void AfterSetup()
		{
			try
			{
				CustomItem item = ItemManager.Instance.GetItem("SkuldsThread");
				GameObject val = ((item != null) ? item.ItemPrefab : (Object.op_Implicit((Object)(object)ObjectDB.instance) ? ObjectDB.instance.GetItemPrefab("SkuldsThread") : null));
				if (!Object.op_Implicit((Object)(object)val))
				{
					((BaseUnityPlugin)instance).Logger.LogWarning((object)"SkuldsThread prefab unavailable after setup");
				}
				else
				{
					Apply(val.GetComponent<ItemDrop>());
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)instance).Logger.LogError((object)("Rope appearance: " + ex));
			}
		}

		private static void AfterItemAwake(ItemDrop __instance)
		{
			if (Object.op_Implicit((Object)(object)__instance) && (((Object)__instance).name == "SkuldsThread" || ((Object)__instance).name == "SkuldsThread(Clone)"))
			{
				try
				{
					Apply(__instance);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)instance).Logger.LogError((object)("Rope appearance: " + ex));
				}
			}
		}

		private static void Apply(ItemDrop item)
		{
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_023f: Expected O, but got Unknown
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Expected O, but got Unknown
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)item) || (Object)(object)icon == (Object)null || (Object)(object)rope == (Object)null)
			{
				return;
			}
			item.m_itemData.m_shared.m_icons = (Sprite[])(object)new Sprite[1] { icon };
			StatusEffect equipStatusEffect = item.m_itemData.m_shared.m_equipStatusEffect;
			if (Object.op_Implicit((Object)(object)equipStatusEffect) && ((Object)equipStatusEffect).name == "SkuldsThread")
			{
				equipStatusEffect.m_icon = icon;
			}
			Transform val = ((Component)item).transform.Find("attach") ?? ((Component)item).transform;
			if (Object.op_Implicit((Object)(object)val.Find("OCT_SkuldsThreadRope")))
			{
				return;
			}
			MeshRenderer[] componentsInChildren = ((Component)val).GetComponentsInChildren<MeshRenderer>(true);
			if ((Object)(object)material == (Object)null)
			{
				Material val2 = null;
				MeshRenderer[] array = componentsInChildren;
				foreach (MeshRenderer val3 in array)
				{
					if (Object.op_Implicit((Object)(object)((Renderer)val3).sharedMaterial))
					{
						val2 = ((Renderer)val3).sharedMaterial;
						break;
					}
				}
				if ((Object)(object)val2 == (Object)null)
				{
					throw new InvalidOperationException("No native thread material found");
				}
				material = new Material(val2);
				((Object)material).name = "OCT_NaturalRope";
				if (material.HasProperty("_MainTex"))
				{
					material.SetTexture("_MainTex", (Texture)(object)Texture2D.whiteTexture);
				}
				if (material.HasProperty("_Color"))
				{
					material.SetColor("_Color", new Color(0.78f, 0.69f, 0.52f, 1f));
				}
				if (material.HasProperty("_BumpMap"))
				{
					material.SetTexture("_BumpMap", (Texture)null);
				}
				if (material.HasProperty("_Glossiness"))
				{
					material.SetFloat("_Glossiness", 0f);
				}
				if (material.HasProperty("_Metallic"))
				{
					material.SetFloat("_Metallic", 0f);
				}
			}
			GameObject val4 = new GameObject("OCT_SkuldsThreadRope");
			val4.layer = ((Component)item).gameObject.layer;
			val4.transform.SetParent(val, false);
			val4.AddComponent<MeshFilter>().sharedMesh = rope;
			((Renderer)val4.AddComponent<MeshRenderer>()).sharedMaterial = material;
			MeshRenderer[] array2 = componentsInChildren;
			foreach (MeshRenderer val5 in array2)
			{
				((Renderer)val5).enabled = false;
			}
		}

		private void OnDestroy()
		{
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	internal static class RopeMesh
	{
		private static readonly List<Vector3> vertices = new List<Vector3>();

		private static readonly List<Vector2> uv = new List<Vector2>();

		private static readonly List<int> triangles = new List<int>();

		internal static Mesh Build()
		{
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Expected O, but got Unknown
			vertices.Clear();
			uv.Clear();
			triangles.Clear();
			for (int i = 0; i < 5; i++)
			{
				float rx = 0.092f + (float)i * 0.009f;
				float rz = 0.125f + (float)i * 0.009f;
				float height = 0.014f + (float)i * 0.002f;
				Rope(delegate(float t)
				{
					//IL_002f: Unknown result type (might be due to invalid IL or missing references)
					//IL_0034: Unknown result type (might be due to invalid IL or missing references)
					//IL_0037: Unknown result type (might be due to invalid IL or missing references)
					float num3 = t * (float)Math.PI * 2f;
					return new Vector3(rx * Mathf.Cos(num3), height, rz * Mathf.Sin(num3));
				}, 180, 0.006f, 24, closed: true);
			}
			Rope(delegate(float t)
			{
				//IL_0040: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: Unknown result type (might be due to invalid IL or missing references)
				//IL_0048: Unknown result type (might be due to invalid IL or missing references)
				float num3 = t * (float)Math.PI * 6f;
				return new Vector3(0.014f * Mathf.Cos(num3), 0.02f + 0.018f * Mathf.Sin(num3), 0.14f + (t - 0.5f) * 0.035f);
			}, 90, 0.0045f, 9, closed: false);
			for (int num = -1; num <= 1; num += 2)
			{
				int direction = num;
				Rope((float t) => new Vector3((float)direction * (0.005f + 0.025f * t) + 0.005f * Mathf.Sin(t * 8f), 0.012f, 0.15f + 0.13f * t), 64, 0.005f, 9, closed: false);
				for (int num2 = 0; num2 < 3; num2++)
				{
					int s = num2;
					Rope((float t) => new Vector3((float)direction * 0.03f + (float)(s - 1) * 0.004f * t, 0.012f + 0.003f * (float)s * t, 0.28f + 0.024f * t), 12, 0.0014f, 2, closed: false);
				}
			}
			Rope(delegate(float t)
			{
				//IL_0040: Unknown result type (might be due to invalid IL or missing references)
				//IL_0045: Unknown result type (might be due to invalid IL or missing references)
				//IL_0048: Unknown result type (might be due to invalid IL or missing references)
				float num3 = t * (float)Math.PI * 4f;
				return new Vector3(0.024f * Mathf.Cos(num3), 0.016f + 0.012f * Mathf.Sin(num3), 0.235f + (t - 0.5f) * 0.018f);
			}, 64, 0.004f, 6, closed: false);
			Mesh val = new Mesh();
			((Object)val).name = "OCT_CoiledRope";
			val.SetVertices(vertices);
			val.SetUVs(0, uv);
			val.SetTriangles(triangles, 0);
			val.RecalculateNormals();
			val.RecalculateBounds();
			return val;
		}

		private static void Rope(Func<float, Vector3> path, int steps, float radius, int twists, bool closed)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: 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)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: 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_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: 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)
			for (int i = 0; i < 3; i++)
			{
				int count = vertices.Count;
				for (int j = 0; j <= steps; j++)
				{
					float num = (float)j / (float)steps;
					float arg = (closed ? ((num - 0.001f + 1f) % 1f) : Mathf.Max(0f, num - 0.001f));
					float arg2 = (closed ? ((num + 0.001f) % 1f) : Mathf.Min(1f, num + 0.001f));
					Vector3 val = path(arg2) - path(arg);
					Vector3 normalized = ((Vector3)(ref val)).normalized;
					val = Vector3.Cross(normalized, Vector3.up);
					Vector3 val2 = ((Vector3)(ref val)).normalized;
					if (((Vector3)(ref val2)).sqrMagnitude < 0.01f)
					{
						val2 = Vector3.right;
					}
					val = Vector3.Cross(normalized, val2);
					Vector3 normalized2 = ((Vector3)(ref val)).normalized;
					float num2 = (float)Math.PI * 2f * (num * (float)twists + (float)i / 3f);
					Vector3 val3 = path(num) + radius * 0.47f * (val2 * Mathf.Cos(num2) + normalized2 * Mathf.Sin(num2));
					for (int k = 0; k < 6; k++)
					{
						float num3 = (float)k * (float)Math.PI / 3f;
						vertices.Add(val3 + radius * 0.58f * (val2 * Mathf.Cos(num3) + normalized2 * Mathf.Sin(num3)));
						uv.Add(new Vector2((float)k / 6f, num * (float)twists));
						if (j != steps)
						{
							int num4 = count + j * 6 + k;
							int num5 = count + j * 6 + (k + 1) % 6;
							int item = num4 + 6;
							int item2 = num5 + 6;
							triangles.Add(num4);
							triangles.Add(num5);
							triangles.Add(item);
							triangles.Add(num5);
							triangles.Add(item2);
							triangles.Add(item);
						}
					}
				}
			}
		}
	}
}