Decompiled source of Brefasveinn Belt v0.1.1

BrefasveinnBelt.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BrefasveinnBelt")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+60d72f92a555c81b1bbdd23c3da1b7c1f2421bd1")]
[assembly: AssemblyProduct("BrefasveinnBelt")]
[assembly: AssemblyTitle("BrefasveinnBelt")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
public static class BeltDye
{
	public static void ToRed(ref float r, ref float g, ref float b)
	{
		float num = GoldKeep(r, g, b);
		float num2 = r * 0.3f + g * 0.59f + b * 0.11f;
		float num3 = Clamp01(num2 * 1.35f + 0.05f);
		float num4 = Clamp01(num2 * 0.18f);
		float num5 = Clamp01(num2 * 0.12f);
		r = num3 + (r - num3) * num;
		g = num4 + (g - num4) * num;
		b = num5 + (b - num5) * num;
	}

	private static float GoldKeep(float r, float g, float b)
	{
		if (r < 0.01f || g < 0.01f)
		{
			return 0f;
		}
		float num = Smooth((g / r - 0.7f) / 0.08f);
		float num2 = Smooth((1f - b / g - 0.28f) / 0.22f);
		float num3 = Smooth((r - 0.38f) / 0.22f);
		return num * num2 * num3;
	}

	private static float Smooth(float t)
	{
		if (t <= 0f)
		{
			return 0f;
		}
		if (t >= 1f)
		{
			return 1f;
		}
		return t * t * (3f - 2f * t);
	}

	private static float Clamp01(float value)
	{
		if (value < 0f)
		{
			return 0f;
		}
		if (value > 1f)
		{
			return 1f;
		}
		return value;
	}
}
public static class BeltTeleport
{
	public const string PrefabName = "BrefasveinnBelt";

	public static bool AllowsTeleport(string equippedPrefabName, bool vanillaResult)
	{
		if (string.Equals(equippedPrefabName, "BrefasveinnBelt", StringComparison.Ordinal))
		{
			return true;
		}
		return vanillaResult;
	}
}
namespace BrefasveinnBelt
{
	internal static class BeltItem
	{
		public static void Register()
		{
			PrefabManager.OnVanillaPrefabsAvailable += AddClonedBelt;
		}

		private static void AddClonedBelt()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Expected O, but got Unknown
			try
			{
				if ((Object)(object)PrefabManager.Instance.GetPrefab("BeltStrength") == (Object)null)
				{
					Logger.LogError((object)"BeltStrength prefab missing. Bréfasveinn Belt was not registered.");
					return;
				}
				ItemConfig val = new ItemConfig();
				val.Name = "$item_brefasveinnbelt";
				val.Description = "$item_brefasveinnbelt_desc";
				val.CraftingStation = CraftingStations.Forge;
				val.AddRequirement("Iron", 10, 0);
				val.AddRequirement("DeerHide", 5, 0);
				val.AddRequirement("Silver", 3, 0);
				val.AddRequirement("SurtlingCore", 1, 0);
				CustomItem val2 = new CustomItem("BrefasveinnBelt", "BeltStrength", val);
				if ((Object)(object)val2.ItemDrop != (Object)null)
				{
					val2.ItemDrop.m_itemData.m_shared.m_equipStatusEffect = null;
					BeltLook.Apply(val2.ItemDrop);
				}
				ItemManager.Instance.AddItem(val2);
				Logger.LogInfo((object)"Registered Bréfasveinn Belt");
			}
			finally
			{
				PrefabManager.OnVanillaPrefabsAvailable -= AddClonedBelt;
			}
		}
	}
	internal static class BeltLook
	{
		private static readonly string[] AlbedoSlots = new string[3] { "_MainTex", "_Diffuse", "_Albedo" };

		private static readonly Dictionary<int, Texture2D> DyedTextures = new Dictionary<int, Texture2D>();

		private static readonly Dictionary<int, Material> DyedMaterials = new Dictionary<int, Material>();

		public static void Apply(ItemDrop drop)
		{
			if ((Object)(object)drop == (Object)null)
			{
				return;
			}
			try
			{
				int num = PaintRenderers(((Component)drop).gameObject);
				int num2 = PaintIcons(drop);
				if (num == 0)
				{
					Logger.LogWarning((object)"Bréfasveinn Belt mesh was not dyed. No albedo material was found.");
				}
				Logger.LogInfo((object)("Bréfasveinn Belt dyed red. Materials " + num + ", icons " + num2 + "."));
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)("Bréfasveinn Belt color skipped: " + ex.Message));
			}
		}

		private static int PaintRenderers(GameObject root)
		{
			int num = 0;
			Renderer[] componentsInChildren = root.GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				if (val is ParticleSystemRenderer)
				{
					continue;
				}
				Material[] sharedMaterials = val.sharedMaterials;
				if (sharedMaterials == null || sharedMaterials.Length == 0)
				{
					continue;
				}
				Material[] array = (Material[])(object)new Material[sharedMaterials.Length];
				for (int j = 0; j < sharedMaterials.Length; j++)
				{
					if (!((Object)(object)sharedMaterials[j] == (Object)null))
					{
						array[j] = PaintMaterial(sharedMaterials[j]);
						num++;
					}
				}
				val.sharedMaterials = array;
			}
			return num;
		}

		private static Material PaintMaterial(Material source)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			int instanceID = ((Object)source).GetInstanceID();
			if (DyedMaterials.TryGetValue(instanceID, out var value))
			{
				return value;
			}
			Material val = new Material(source);
			((Object)val).name = "BrefasveinnBeltMat";
			bool flag = false;
			string[] albedoSlots = AlbedoSlots;
			foreach (string text in albedoSlots)
			{
				if (val.HasProperty(text))
				{
					Texture2D val2 = DyeTexture(val.GetTexture(text));
					if (!((Object)(object)val2 == (Object)null))
					{
						val.SetTexture(text, (Texture)(object)val2);
						flag = true;
					}
				}
			}
			if (val.HasProperty("_Color"))
			{
				val.SetColor("_Color", (Color)(flag ? Color.white : new Color(1f, 0.25f, 0.2f, 1f)));
			}
			DyedMaterials[instanceID] = val;
			return val;
		}

		private static int PaintIcons(ItemDrop drop)
		{
			Sprite[] array = drop.m_itemData?.m_shared?.m_icons;
			if (array == null || array.Length == 0)
			{
				return 0;
			}
			Sprite[] array2 = (Sprite[])(object)new Sprite[array.Length];
			int num = 0;
			for (int i = 0; i < array.Length; i++)
			{
				Sprite val = PaintSprite(array[i]);
				if ((Object)(object)val != (Object)null)
				{
					array2[i] = val;
					num++;
				}
				else
				{
					array2[i] = array[i];
				}
			}
			if (num > 0)
			{
				drop.m_itemData.m_shared.m_icons = array2;
			}
			return num;
		}

		private static Sprite PaintSprite(Sprite sprite)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: 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_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: 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_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)sprite == (Object)null) && !((Object)(object)sprite.texture == (Object)null))
			{
				Rect val = sprite.textureRect;
				if (!(((Rect)(ref val)).width < 1f))
				{
					val = sprite.textureRect;
					if (!(((Rect)(ref val)).height < 1f))
					{
						Texture2D val2 = DyeTexture((Texture)(object)sprite.texture);
						if ((Object)(object)val2 == (Object)null)
						{
							return null;
						}
						val = sprite.rect;
						float width;
						if (!(((Rect)(ref val)).width > 0f))
						{
							val = sprite.textureRect;
							width = ((Rect)(ref val)).width;
						}
						else
						{
							val = sprite.rect;
							width = ((Rect)(ref val)).width;
						}
						float num = width;
						val = sprite.rect;
						float height;
						if (!(((Rect)(ref val)).height > 0f))
						{
							val = sprite.textureRect;
							height = ((Rect)(ref val)).height;
						}
						else
						{
							val = sprite.rect;
							height = ((Rect)(ref val)).height;
						}
						float num2 = height;
						Vector2 val3 = default(Vector2);
						((Vector2)(ref val3))..ctor(sprite.pivot.x / num, sprite.pivot.y / num2);
						return Sprite.Create(val2, sprite.textureRect, val3, sprite.pixelsPerUnit, 0u, (SpriteMeshType)0, sprite.border);
					}
				}
			}
			return null;
		}

		private static Texture2D DyeTexture(Texture source)
		{
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)source == (Object)null || source.width < 1 || source.height < 1)
			{
				return null;
			}
			int instanceID = ((Object)source).GetInstanceID();
			if (DyedTextures.TryGetValue(instanceID, out var value))
			{
				return value;
			}
			Texture2D val = CopyReadable(source);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			DyePixels(val);
			((Object)val).name = "BrefasveinnBelt_" + ((Object)source).name;
			((Texture)val).wrapMode = source.wrapMode;
			((Texture)val).filterMode = source.filterMode;
			((Texture)val).anisoLevel = source.anisoLevel;
			DyedTextures[instanceID] = val;
			return val;
		}

		private static Texture2D CopyReadable(Texture source)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected O, but got Unknown
			RenderTexture active = RenderTexture.active;
			RenderTexture val = null;
			try
			{
				val = RenderTexture.GetTemporary(source.width, source.height, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
				Graphics.Blit(source, val);
				RenderTexture.active = val;
				Texture2D val2 = new Texture2D(source.width, source.height, (TextureFormat)4, false, false);
				val2.ReadPixels(new Rect(0f, 0f, (float)source.width, (float)source.height), 0, 0);
				val2.Apply(false, false);
				return val2;
			}
			catch (Exception ex)
			{
				Logger.LogWarning((object)("Bréfasveinn Belt texture copy skipped: " + ex.Message));
				return null;
			}
			finally
			{
				RenderTexture.active = active;
				if ((Object)(object)val != (Object)null)
				{
					RenderTexture.ReleaseTemporary(val);
				}
			}
		}

		private static void DyePixels(Texture2D texture)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			Color[] pixels = texture.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				Color val = pixels[i];
				float r = val.r;
				float g = val.g;
				float b = val.b;
				BeltDye.ToRed(ref r, ref g, ref b);
				pixels[i] = new Color(r, g, b, val.a);
			}
			texture.SetPixels(pixels);
			texture.Apply(true, false);
		}
	}
	[BepInPlugin("com.frizzqol.brefasveinnbelt", "FrizzQOL Bréfasveinn Belt", "0.1.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.frizzqol.brefasveinnbelt";

		public const string PluginName = "FrizzQOL Bréfasveinn Belt";

		public const string PluginVersion = "0.1.1";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			AddLocalizations();
			BeltItem.Register();
			_harmony = new Harmony("com.frizzqol.brefasveinnbelt");
			_harmony.PatchAll();
			Logger.LogInfo((object)"FrizzQOL Bréfasveinn Belt 0.1.1 loaded");
		}

		private static void AddLocalizations()
		{
			CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
			string text = "English";
			localization.AddTranslation(ref text, new Dictionary<string, string>
			{
				{ "item_brefasveinnbelt", "Bréfasveinn Belt" },
				{ "item_brefasveinnbelt_desc", "While worn, portals allow any items, including ores and metals." }
			});
		}
	}
	[HarmonyPatch(typeof(Humanoid), "IsTeleportable")]
	internal static class TeleportPatch
	{
		private static void Postfix(Humanoid __instance, ref bool __result)
		{
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (!((Object)(object)val == (Object)null) && !((Object)(object)((Humanoid)val).m_utilityItem?.m_dropPrefab == (Object)null))
			{
				string prefabName = Utils.GetPrefabName(((Humanoid)val).m_utilityItem.m_dropPrefab);
				__result = BeltTeleport.AllowsTeleport(prefabName, __result);
			}
		}
	}
}