Decompiled source of IrnBruScrap v1.0.0

BepInEx/plugins/irnbruscrap/IrnBruScrap.dll

Decompiled 35 minutes ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using LethalLib.Modules;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace IrnBruScrap;

[BepInPlugin("sara.irnbruscrap", "IrnBruScrap", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "sara.irnbruscrap";

	public const string NAME = "IrnBruScrap";

	public const string VERSION = "1.0.0";

	private const float Radius = 0.04f;

	private const float BottomTaper = 0.008f;

	private const float LabelHeight = 0.1136f;

	private const float TopTaper = 0.014f;

	private const float Height = 0.1356f;

	private const int Segments = 24;

	internal static ManualLogSource Log;

	private Material labelMaterial;

	private Material metalMaterial;

	private Texture2D labelTexture;

	private void Awake()
	{
		//IL_0080: 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_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c4: Expected O, but got Unknown
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0337: Unknown result type (might be due to invalid IL or missing references)
		//IL_033c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0351: Unknown result type (might be due to invalid IL or missing references)
		//IL_0356: Unknown result type (might be due to invalid IL or missing references)
		//IL_035e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0363: Unknown result type (might be due to invalid IL or missing references)
		//IL_038f: Unknown result type (might be due to invalid IL or missing references)
		//IL_039e: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		ConfigEntry<int> val = ((BaseUnityPlugin)this).Config.Bind<int>("Spawning", "Rarity", 8, "Spawn weight on every moon. Vanilla scrap usually sits between 5 and 100, so lower = rarer. 0 disables spawning.");
		ConfigEntry<int> val2 = ((BaseUnityPlugin)this).Config.Bind<int>("Value", "MinValue", 20, "Lowest scrap value in credits.");
		ConfigEntry<int> val3 = ((BaseUnityPlugin)this).Config.Bind<int>("Value", "MaxValue", 55, "Highest scrap value in credits.");
		ConfigEntry<Vector3> val4 = ((BaseUnityPlugin)this).Config.Bind<Vector3>("Holding", "PositionOffset", new Vector3(0f, 0.06f, 0f), "Position of the can relative to the player's hand. Tweak if it looks off when held.");
		ConfigEntry<Vector3> val5 = ((BaseUnityPlugin)this).Config.Bind<Vector3>("Holding", "RotationOffset", new Vector3(0f, 90f, 0f), "Rotation (degrees) of the can relative to the player's hand.");
		labelTexture = LoadTexture("label.png", mipmaps: true);
		Texture2D val6 = LoadTexture("icon.png", mipmaps: false);
		AudioClip grabSFX = LoadWav("canabru.wav", "canabru");
		GameObject val7 = NetworkPrefabs.CreateNetworkPrefab("IrnBruCan");
		val7.tag = "PhysicsProp";
		val7.layer = LayerMask.NameToLayer("Props");
		val7.AddComponent<MeshFilter>().sharedMesh = BuildCanMesh();
		MeshRenderer val8 = val7.AddComponent<MeshRenderer>();
		BuildMaterials();
		((Renderer)val8).sharedMaterials = (Material[])(object)new Material[2] { labelMaterial, metalMaterial };
		BoxCollider val9 = val7.AddComponent<BoxCollider>();
		val9.size = new Vector3(0.096f, 0.1356f, 0.096f);
		AudioSource val10 = val7.AddComponent<AudioSource>();
		val10.playOnAwake = false;
		val10.spatialBlend = 1f;
		val10.rolloffMode = (AudioRolloffMode)1;
		val10.minDistance = 1f;
		val10.maxDistance = 20f;
		GameObject val11 = new GameObject("ScanNode");
		val11.transform.SetParent(val7.transform, false);
		val11.layer = LayerMask.NameToLayer("ScanNode");
		val11.AddComponent<BoxCollider>().size = new Vector3(0.25f, 0.25f, 0.25f);
		ScanNodeProperties val12 = val11.AddComponent<ScanNodeProperties>();
		val12.headerText = "Irn-Bru";
		val12.subText = "Value: ";
		val12.nodeType = 2;
		val12.minRange = 1;
		val12.maxRange = 13;
		val12.requiresLineOfSight = true;
		val12.creatureScanID = -1;
		int num = Mathf.Max(1, Mathf.Min(val2.Value, val3.Value));
		int num2 = Mathf.Max(num + 1, Mathf.Max(val2.Value, val3.Value));
		Item val13 = ScriptableObject.CreateInstance<Item>();
		((Object)val13).name = "IrnBruCan";
		val13.itemName = "Irn-Bru";
		val13.isScrap = true;
		val13.minValue = Mathf.RoundToInt((float)num / 0.4f);
		val13.maxValue = Mathf.RoundToInt((float)num2 / 0.4f);
		val13.weight = 1.01f;
		val13.twoHanded = false;
		val13.twoHandedAnimation = false;
		val13.requiresBattery = false;
		val13.isConductiveMetal = false;
		val13.itemSpawnsOnGround = true;
		val13.canBeGrabbedBeforeGameStart = true;
		val13.allowDroppingAheadOfPlayer = true;
		val13.syncGrabFunction = false;
		val13.syncDiscardFunction = false;
		val13.syncUseFunction = false;
		val13.restingRotation = Vector3.zero;
		val13.verticalOffset = 0.0678f;
		val13.rotationOffset = val5.Value;
		val13.positionOffset = val4.Value;
		val13.grabSFX = grabSFX;
		val13.itemIcon = Sprite.Create(val6, new Rect(0f, 0f, (float)((Texture)val6).width, (float)((Texture)val6).height), new Vector2(0.5f, 0.5f), 100f);
		((Object)val13.itemIcon).name = "IrnBruIcon";
		((Object)val13.itemIcon).hideFlags = (HideFlags)61;
		((Object)val13).hideFlags = (HideFlags)61;
		val13.spawnPrefab = val7;
		PhysicsProp val14 = val7.AddComponent<PhysicsProp>();
		((GrabbableObject)val14).itemProperties = val13;
		((GrabbableObject)val14).grabbable = true;
		((GrabbableObject)val14).grabbableToEnemies = true;
		((GrabbableObject)val14).mainObjectRenderer = val8;
		if (val.Value > 0)
		{
			Items.RegisterScrap(val13, val.Value, (LevelTypes)(-1));
			Log.LogInfo((object)("Irn-Bru scrap registered (rarity " + val.Value + ", value " + num + "-" + num2 + ")."));
		}
		else
		{
			Log.LogInfo((object)"Irn-Bru scrap loaded but spawning is disabled (rarity 0).");
		}
		if ((Object)(object)labelMaterial.shader == (Object)null || ((Object)labelMaterial.shader).name != "HDRP/Lit")
		{
			SceneManager.sceneLoaded += OnSceneLoaded;
		}
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		Shader val = Shader.Find("HDRP/Lit");
		if (!((Object)(object)val == (Object)null))
		{
			labelMaterial.shader = val;
			metalMaterial.shader = val;
			ApplyMaterialProperties();
			SceneManager.sceneLoaded -= OnSceneLoaded;
		}
	}

	private void BuildMaterials()
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Expected O, but got Unknown
		Shader val = Shader.Find("HDRP/Lit");
		if ((Object)(object)val == (Object)null)
		{
			Log.LogWarning((object)"HDRP/Lit shader not found yet, will retry when a scene loads.");
			val = Shader.Find("Standard");
			if ((Object)(object)val == (Object)null)
			{
				val = Shader.Find("Sprites/Default");
			}
		}
		labelMaterial = new Material(val);
		((Object)labelMaterial).name = "IrnBruLabel";
		metalMaterial = new Material(val);
		((Object)metalMaterial).name = "IrnBruMetal";
		((Object)labelMaterial).hideFlags = (HideFlags)61;
		((Object)metalMaterial).hideFlags = (HideFlags)61;
		ApplyMaterialProperties();
	}

	private void ApplyMaterialProperties()
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		labelMaterial.mainTexture = (Texture)(object)labelTexture;
		labelMaterial.SetTexture("_BaseColorMap", (Texture)(object)labelTexture);
		labelMaterial.SetColor("_BaseColor", Color.white);
		labelMaterial.SetFloat("_Metallic", 0.3f);
		labelMaterial.SetFloat("_Smoothness", 0.55f);
		Color val = default(Color);
		((Color)(ref val))..ctor(0.72f, 0.74f, 0.76f, 1f);
		metalMaterial.color = val;
		metalMaterial.SetColor("_BaseColor", val);
		metalMaterial.SetFloat("_Metallic", 0.9f);
		metalMaterial.SetFloat("_Smoothness", 0.6f);
	}

	private static Mesh BuildCanMesh()
	{
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Expected O, but got Unknown
		List<Vector3> list = new List<Vector3>();
		List<Vector3> list2 = new List<Vector3>();
		List<Vector2> list3 = new List<Vector2>();
		List<int> list4 = new List<int>();
		List<int> list5 = new List<int>();
		float num = -0.0678f;
		float num2 = num + 0.008f;
		float num3 = num2 + 0.1136f;
		float num4 = 0.0678f;
		float num5 = 0.0328f;
		float num6 = 0.031199997f;
		AddBand(list, list2, list3, list4, 0.04f, num2, 0.04f, num3, labelUv: true);
		AddBand(list, list2, list3, list5, num5, num, 0.04f, num2, labelUv: false);
		AddBand(list, list2, list3, list5, 0.04f, num3, num6, num4, labelUv: false);
		AddCap(list, list2, list3, list5, num6, num4, up: true);
		AddCap(list, list2, list3, list5, num5, num, up: false);
		Mesh val = new Mesh();
		((Object)val).name = "IrnBruCan";
		val.SetVertices(list);
		val.SetNormals(list2);
		val.SetUVs(0, list3);
		val.subMeshCount = 2;
		val.SetTriangles(list4, 0);
		val.SetTriangles(list5, 1);
		val.RecalculateBounds();
		val.RecalculateTangents();
		((Object)val).hideFlags = (HideFlags)61;
		return val;
	}

	private static void AddBand(List<Vector3> verts, List<Vector3> norms, List<Vector2> uvs, List<int> tris, float rLow, float yLow, float rHigh, float yHigh, bool labelUv)
	{
		//IL_0052: 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_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		int count = verts.Count;
		float num = (rLow - rHigh) / (yHigh - yLow);
		Vector3 val = default(Vector3);
		for (int i = 0; i <= 24; i++)
		{
			float num2 = (float)i / 24f;
			float num3 = num2 * (float)Math.PI * 2f;
			float num4 = Mathf.Cos(num3);
			float num5 = Mathf.Sin(num3);
			((Vector3)(ref val))..ctor(num4, num, num5);
			Vector3 normalized = ((Vector3)(ref val)).normalized;
			verts.Add(new Vector3(num4 * rLow, yLow, num5 * rLow));
			verts.Add(new Vector3(num4 * rHigh, yHigh, num5 * rHigh));
			norms.Add(normalized);
			norms.Add(normalized);
			uvs.Add((!labelUv) ? new Vector2(0.01f, 0.01f) : new Vector2(num2, 0f));
			uvs.Add((!labelUv) ? new Vector2(0.01f, 0.01f) : new Vector2(num2, 1f));
		}
		for (int j = 0; j < 24; j++)
		{
			int num6 = count + j * 2;
			int item = num6 + 1;
			int item2 = num6 + 2;
			int item3 = num6 + 3;
			tris.Add(num6);
			tris.Add(item);
			tris.Add(item3);
			tris.Add(num6);
			tris.Add(item3);
			tris.Add(item2);
		}
	}

	private static void AddCap(List<Vector3> verts, List<Vector3> norms, List<Vector2> uvs, List<int> tris, float r, float y, bool up)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: 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_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: 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)
		//IL_0082: 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_009e: Unknown result type (might be due to invalid IL or missing references)
		int count = verts.Count;
		Vector3 item = ((!up) ? Vector3.down : Vector3.up);
		verts.Add(new Vector3(0f, y, 0f));
		norms.Add(item);
		uvs.Add(new Vector2(0.01f, 0.01f));
		for (int i = 0; i <= 24; i++)
		{
			float num = (float)i / 24f * (float)Math.PI * 2f;
			verts.Add(new Vector3(Mathf.Cos(num) * r, y, Mathf.Sin(num) * r));
			norms.Add(item);
			uvs.Add(new Vector2(0.01f, 0.01f));
		}
		for (int j = 0; j < 24; j++)
		{
			int num2 = count + 1 + j;
			if (up)
			{
				tris.Add(count);
				tris.Add(num2 + 1);
				tris.Add(num2);
			}
			else
			{
				tris.Add(count);
				tris.Add(num2);
				tris.Add(num2 + 1);
			}
		}
	}

	private static byte[] ReadResource(string fileName)
	{
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string[] manifestResourceNames = executingAssembly.GetManifestResourceNames();
		foreach (string text in manifestResourceNames)
		{
			if (!text.EndsWith(fileName, StringComparison.OrdinalIgnoreCase))
			{
				continue;
			}
			using Stream stream = executingAssembly.GetManifestResourceStream(text);
			using MemoryStream memoryStream = new MemoryStream();
			stream.CopyTo(memoryStream);
			return memoryStream.ToArray();
		}
		throw new FileNotFoundException("Embedded resource not found: " + fileName);
	}

	private static Texture2D LoadTexture(string fileName, bool mipmaps)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Expected O, but got Unknown
		Texture2D val = new Texture2D(2, 2, (TextureFormat)4, mipmaps);
		((Object)val).name = Path.GetFileNameWithoutExtension(fileName);
		ImageConversion.LoadImage(val, ReadResource(fileName));
		((Texture)val).wrapMode = (TextureWrapMode)0;
		((Texture)val).filterMode = (FilterMode)1;
		((Object)val).hideFlags = (HideFlags)61;
		return val;
	}

	private static AudioClip LoadWav(string fileName, string clipName)
	{
		byte[] array = ReadResource(fileName);
		if (array.Length < 44 || array[0] != 82 || array[1] != 73 || array[2] != 70 || array[3] != 70)
		{
			throw new InvalidDataException(fileName + " is not a RIFF/WAV file");
		}
		int num = 1;
		int num2 = 44100;
		int num3 = 16;
		int num4 = -1;
		int num5 = 0;
		int num6 = 12;
		while (num6 + 8 <= array.Length)
		{
			string text = Encoding.ASCII.GetString(array, num6, 4);
			int num7 = BitConverter.ToInt32(array, num6 + 4);
			int num8 = num6 + 8;
			if (text == "fmt ")
			{
				num = BitConverter.ToInt16(array, num8 + 2);
				num2 = BitConverter.ToInt32(array, num8 + 4);
				num3 = BitConverter.ToInt16(array, num8 + 14);
			}
			else if (text == "data")
			{
				num4 = num8;
				num5 = Math.Min(num7, array.Length - num8);
				break;
			}
			num6 = num8 + num7 + (num7 & 1);
		}
		if (num4 < 0)
		{
			throw new InvalidDataException(fileName + " has no data chunk");
		}
		int num9 = num3 / 8;
		int num10 = num5 / num9;
		float[] array2 = new float[num10];
		for (int i = 0; i < num10; i++)
		{
			int num11 = num4 + i * num9;
			switch (num3)
			{
			case 8:
				array2[i] = (float)(array[num11] - 128) / 128f;
				break;
			case 16:
				array2[i] = (float)BitConverter.ToInt16(array, num11) / 32768f;
				break;
			case 24:
				array2[i] = (float)(array[num11] | (array[num11 + 1] << 8) | ((sbyte)array[num11 + 2] << 16)) / 8388608f;
				break;
			case 32:
				array2[i] = (float)BitConverter.ToInt32(array, num11) / 2.1474836E+09f;
				break;
			default:
				throw new InvalidDataException("Unsupported WAV bit depth: " + num3);
			}
		}
		AudioClip val = AudioClip.Create(clipName, num10 / num, num, num2, false);
		val.SetData(array2, 0);
		((Object)val).hideFlags = (HideFlags)61;
		return val;
	}
}