Decompiled source of HanestadScrapPack v1.0.2

HanestadScrapPack.dll

Decompiled 13 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using LethalLib.Modules;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("HanestadScrapPack")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HanestadScrapPack")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8c6ef502-8b4e-4860-9beb-d2cae84e7dd6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace HanestadScrapPack;

[BepInPlugin("no.hanestad.scrappack", "Hanestad Scrap Pack", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
	private class Scrap
	{
		public string Name;

		public string AssetPath;

		public int MinValue;

		public int MaxValue;

		public int Rarity;

		public float TargetHeight;

		public bool IsPainting;

		public GameObject Model;

		public GameObject Prefab;

		public Item Item;
	}

	[HarmonyPatch(typeof(RoundManager), "SpawnScrapInLevel")]
	private static class ScrapGenerationPatch
	{
		[HarmonyPrefix]
		private static void Prefix()
		{
			if ((Object)(object)Instance != (Object)null)
			{
				Instance.ApplyScrapDistribution();
			}
		}

		[HarmonyPostfix]
		private static void Postfix()
		{
			if ((Object)(object)Instance != (Object)null)
			{
				Instance.ActivateAfterGeneration();
			}
		}
	}

	internal static Plugin Instance;

	private readonly List<Scrap> scraps = new List<Scrap>
	{
		new Scrap
		{
			Name = "Dahls Pils",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_DahlsCan.prefab",
			MinValue = 15,
			MaxValue = 45,
			Rarity = 25,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Ringnes Brun",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_RingnesBrun.prefab",
			MinValue = 5,
			MaxValue = 15,
			Rarity = 18,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Aass Pilsner",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_AassPilsner.prefab",
			MinValue = 20,
			MaxValue = 40,
			Rarity = 20,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Grans Bare",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_GransBare.prefab",
			MinValue = 5,
			MaxValue = 20,
			Rarity = 25,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Norrlands Guld",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_NorrlandsGuld.prefab",
			MinValue = 30,
			MaxValue = 60,
			Rarity = 15,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Ringnes Platinum",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_RingnesPlatinum.prefab",
			MinValue = 60,
			MaxValue = 100,
			Rarity = 6,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Grans Tropisk IPA",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_GransTropiskIPA.prefab",
			MinValue = 100,
			MaxValue = 160,
			Rarity = 2,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Heineken Original",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_Heineken.prefab",
			MinValue = 20,
			MaxValue = 50,
			Rarity = 20,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Hansa IPA Extra",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_HansaIPAExtra.prefab",
			MinValue = 150,
			MaxValue = 250,
			Rarity = 2,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Aass Halling",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_AassHalling.prefab",
			MinValue = 35,
			MaxValue = 70,
			Rarity = 12,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Pepsi Max",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_PepsiMax.prefab",
			MinValue = 1,
			MaxValue = 1,
			Rarity = 12,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Mr B Portrait",
			AssetPath = "Assets/Paintings/Prefabs/MrB_Painting.prefab",
			MinValue = 40,
			MaxValue = 90,
			Rarity = 20,
			TargetHeight = 1.5f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Mr F Portrait",
			AssetPath = "Assets/Paintings/Prefabs/MrF_Painting.prefab",
			MinValue = 60,
			MaxValue = 120,
			Rarity = 12,
			TargetHeight = 1.5f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Mr Z Portrait",
			AssetPath = "Assets/Paintings/Prefabs/MrZ_Painting.prefab",
			MinValue = 80,
			MaxValue = 150,
			Rarity = 12,
			TargetHeight = 1.5f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Mr K Portrait",
			AssetPath = "Assets/Paintings/Prefabs/MrK_Painting.prefab",
			MinValue = 100,
			MaxValue = 180,
			Rarity = 20,
			TargetHeight = 1.5f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "TheKing",
			AssetPath = "Assets/Paintings/Prefabs/TheKing.prefab",
			MinValue = 120,
			MaxValue = 200,
			Rarity = 15,
			TargetHeight = 1.5f,
			IsPainting = true
		}
	};

	private bool registered;

	private float nextAttempt;

	private const float PickupMargin = 0.045f;

	private const float HanestadShare = 0.8f;

	private Harmony harmony;

	private void Awake()
	{
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		//IL_020b: Expected O, but got Unknown
		Instance = this;
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string text = Path.Combine(directoryName, "hanestadscrap");
		string text2 = Path.Combine(directoryName, "Assets", "hanestadscrap");
		AssetBundle val = null;
		string[] array = new string[2] { text, text2 };
		foreach (string text3 in array)
		{
			if (!File.Exists(text3))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] Ingen AssetBundle på: " + text3));
				continue;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] Forsøker å laste: " + text3));
			try
			{
				val = AssetBundle.LoadFromFile(text3);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Feil ved lasting fra " + text3 + ": " + ex));
			}
			if ((Object)(object)val != (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] AssetBundle lastet fra: " + text3));
				break;
			}
			((BaseUnityPlugin)this).Logger.LogWarning((object)("[HANESTAD] Kunne ikke åpne AssetBundle: " + text3));
		}
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Fant ingen gyldig AssetBundle. Kontrollerte: " + text + " og " + text2));
			return;
		}
		foreach (Scrap scrap in scraps)
		{
			scrap.Model = val.LoadAsset<GameObject>(scrap.AssetPath);
			if ((Object)(object)scrap.Model == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Mangler prefab: " + scrap.AssetPath));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] Modell lastet: " + scrap.Name));
			}
		}
		try
		{
			harmony = new Harmony("no.hanestad.scrappack");
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[HANESTAD] Harmony aktiv.");
		}
		catch (Exception ex2)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Harmony-feil: " + ex2));
		}
	}

	private void OnDestroy()
	{
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		if ((Object)(object)Instance == (Object)(object)this)
		{
			Instance = null;
		}
	}

	private void Update()
	{
		if (!registered && Time.unscaledTime >= nextAttempt)
		{
			nextAttempt = Time.unscaledTime + 2f;
			TryRegisterAll();
		}
	}

	private bool CalculateMeshBounds(Transform modelRoot, Transform relativeTo, out Bounds result)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: 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_006e: 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_009f: 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_00b3: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: 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_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		result = new Bounds(Vector3.zero, Vector3.zero);
		MeshFilter[] componentsInChildren = ((Component)modelRoot).GetComponentsInChildren<MeshFilter>(true);
		bool flag = false;
		MeshFilter[] array = componentsInChildren;
		Vector3 val2 = default(Vector3);
		foreach (MeshFilter val in array)
		{
			if ((Object)(object)val == (Object)null || (Object)(object)val.sharedMesh == (Object)null)
			{
				continue;
			}
			Bounds bounds = val.sharedMesh.bounds;
			Vector3 min = ((Bounds)(ref bounds)).min;
			Vector3 max = ((Bounds)(ref bounds)).max;
			for (int j = 0; j < 2; j++)
			{
				for (int k = 0; k < 2; k++)
				{
					for (int l = 0; l < 2; l++)
					{
						((Vector3)(ref val2))..ctor((j == 0) ? min.x : max.x, (k == 0) ? min.y : max.y, (l == 0) ? min.z : max.z);
						Vector3 val3 = ((Component)val).transform.TransformPoint(val2);
						Vector3 val4 = relativeTo.InverseTransformPoint(val3);
						if (!flag)
						{
							result = new Bounds(val4, Vector3.zero);
							flag = true;
						}
						else
						{
							((Bounds)(ref result)).Encapsulate(val4);
						}
					}
				}
			}
		}
		return flag;
	}

	private bool PreparePainting(Scrap scrap, GameObject visual, GameObject prefab, out Vector3 colliderCenter, out Vector3 colliderSize)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: 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)
		//IL_00b9: 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_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: 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_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0190: Unknown result type (might be due to invalid IL or missing references)
		colliderCenter = Vector3.zero;
		colliderSize = Vector3.zero;
		Transform transform = prefab.transform;
		if (!CalculateMeshBounds(visual.transform, transform, out var result))
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD SIZE] Fant ingen geometri: " + scrap.Name));
			return false;
		}
		if (((Bounds)(ref result)).size.y <= 1E-06f)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD SIZE] Ugyldig høyde: " + scrap.Name));
			return false;
		}
		float num = scrap.TargetHeight / ((Bounds)(ref result)).size.y;
		Transform transform2 = visual.transform;
		transform2.localScale *= num;
		if (!CalculateMeshBounds(visual.transform, transform, out var result2))
		{
			return false;
		}
		Transform transform3 = visual.transform;
		transform3.localPosition -= ((Bounds)(ref result2)).center;
		if (!CalculateMeshBounds(visual.transform, transform, out var result3))
		{
			return false;
		}
		colliderCenter = ((Bounds)(ref result3)).center;
		colliderSize = new Vector3(Mathf.Max(((Bounds)(ref result3)).size.x + 0.09f, 0.15f), Mathf.Max(((Bounds)(ref result3)).size.y + 0.09f, 0.24f), Mathf.Max(((Bounds)(ref result3)).size.z + 0.09f, 0.08f));
		return true;
	}

	private void TryRegisterAll()
	{
		if ((Object)(object)StartOfRound.Instance == (Object)null || (Object)(object)StartOfRound.Instance.allItemsList == (Object)null || StartOfRound.Instance.allItemsList.itemsList == null)
		{
			return;
		}
		Item val = StartOfRound.Instance.allItemsList.itemsList.Find((Item i) => (Object)(object)i != (Object)null && i.itemName != null && i.itemName.Equals("Big bolt", StringComparison.OrdinalIgnoreCase) && (Object)(object)i.spawnPrefab != (Object)null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		int num = 0;
		foreach (Scrap scrap in scraps)
		{
			if ((Object)(object)scrap.Model == (Object)null)
			{
				continue;
			}
			try
			{
				if (RegisterScrap(scrap, val))
				{
					num++;
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Registreringsfeil " + scrap.Name + ": " + ex));
			}
		}
		if (num == 0)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)"[HANESTAD] Ingen scrap registrert.");
			return;
		}
		registered = true;
		EnsureGlobalItemList();
		EnsureAllLevelsScrap();
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] V1.0.2 REGISTRERT! " + num + " av " + scraps.Count + " gjenstander."));
	}

	private bool RegisterScrap(Scrap scrap, Item template)
	{
		//IL_0053: 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_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02de: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0326: Unknown result type (might be due to invalid IL or missing references)
		//IL_032b: Unknown result type (might be due to invalid IL or missing references)
		//IL_032f: Unknown result type (might be due to invalid IL or missing references)
		//IL_033a: Unknown result type (might be due to invalid IL or missing references)
		//IL_033f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0344: Unknown result type (might be due to invalid IL or missing references)
		//IL_034e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0353: Unknown result type (might be due to invalid IL or missing references)
		//IL_0355: Unknown result type (might be due to invalid IL or missing references)
		//IL_0362: Unknown result type (might be due to invalid IL or missing references)
		//IL_0367: Unknown result type (might be due to invalid IL or missing references)
		//IL_0371: Unknown result type (might be due to invalid IL or missing references)
		//IL_0376: Unknown result type (might be due to invalid IL or missing references)
		//IL_037b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0385: Unknown result type (might be due to invalid IL or missing references)
		//IL_038a: 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_0393: Unknown result type (might be due to invalid IL or missing references)
		//IL_039f: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_03be: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0311: Unknown result type (might be due to invalid IL or missing references)
		//IL_0318: Unknown result type (might be due to invalid IL or missing references)
		//IL_0429: Unknown result type (might be due to invalid IL or missing references)
		//IL_0433: Unknown result type (might be due to invalid IL or missing references)
		//IL_04eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_05dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_05e2: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = Object.Instantiate<GameObject>(template.spawnPrefab);
		((Object)val).name = "Hanestad_" + scrap.Name.Replace(" ", "") + "_Network";
		val.SetActive(false);
		if (scrap.IsPainting)
		{
			val.transform.localScale = Vector3.one;
		}
		PhysicsProp component = val.GetComponent<PhysicsProp>();
		NetworkObject component2 = val.GetComponent<NetworkObject>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Mangler PhysicsProp / NetworkObject: " + scrap.Name));
			Object.Destroy((Object)(object)val);
			return false;
		}
		Renderer[] componentsInChildren = val.GetComponentsInChildren<Renderer>(true);
		Renderer[] array = componentsInChildren;
		foreach (Renderer val2 in array)
		{
			val2.enabled = false;
		}
		MeshFilter component3 = val.GetComponent<MeshFilter>();
		if ((Object)(object)component3 != (Object)null)
		{
			component3.sharedMesh = null;
		}
		ScanNodeProperties componentInChildren = val.GetComponentInChildren<ScanNodeProperties>(true);
		if ((Object)(object)componentInChildren != (Object)null)
		{
			componentInChildren.headerText = scrap.Name;
			componentInChildren.subText = "Value: ";
			componentInChildren.nodeType = 2;
			componentInChildren.minRange = 0;
			componentInChildren.maxRange = 13;
			componentInChildren.requiresLineOfSight = false;
		}
		GameObject val3 = Object.Instantiate<GameObject>(scrap.Model, val.transform, false);
		((Object)val3).name = scrap.Name.Replace(" ", "") + "_Visual";
		val3.transform.localPosition = Vector3.zero;
		val3.transform.localRotation = Quaternion.identity;
		val3.transform.localScale = Vector3.one;
		Rigidbody[] componentsInChildren2 = val3.GetComponentsInChildren<Rigidbody>(true);
		Rigidbody[] array2 = componentsInChildren2;
		foreach (Rigidbody val4 in array2)
		{
			Object.DestroyImmediate((Object)(object)val4);
		}
		Collider[] componentsInChildren3 = val3.GetComponentsInChildren<Collider>(true);
		Collider[] array3 = componentsInChildren3;
		foreach (Collider val5 in array3)
		{
			Object.DestroyImmediate((Object)(object)val5);
		}
		MeshRenderer componentInChildren2 = val3.GetComponentInChildren<MeshRenderer>(true);
		if ((Object)(object)componentInChildren2 == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Mangler MeshRenderer: " + scrap.Name));
			Object.Destroy((Object)(object)val);
			return false;
		}
		Renderer[] componentsInChildren4 = val3.GetComponentsInChildren<Renderer>(true);
		Renderer[] array4 = componentsInChildren4;
		foreach (Renderer val6 in array4)
		{
			val6.enabled = true;
		}
		Vector3 colliderCenter;
		Vector3 colliderSize = default(Vector3);
		if (scrap.IsPainting)
		{
			if (!PreparePainting(scrap, val3, val, out colliderCenter, out colliderSize))
			{
				Object.Destroy((Object)(object)val);
				return false;
			}
		}
		else
		{
			Bounds bounds = ((Renderer)componentInChildren2).bounds;
			float y = ((Bounds)(ref bounds)).size.y;
			if (y > 0.0001f)
			{
				float num = scrap.TargetHeight / y;
				Transform transform = val3.transform;
				transform.localScale *= num;
			}
			bounds = ((Renderer)componentInChildren2).bounds;
			Vector3 val7 = ((Bounds)(ref bounds)).center - val.transform.position;
			Transform transform2 = val3.transform;
			transform2.position -= val7;
			Bounds bounds2 = ((Renderer)componentInChildren2).bounds;
			colliderCenter = val.transform.InverseTransformPoint(((Bounds)(ref bounds2)).center);
			Vector3 val8 = val.transform.InverseTransformVector(((Bounds)(ref bounds2)).size);
			((Vector3)(ref val8))..ctor(Mathf.Abs(val8.x), Mathf.Abs(val8.y), Mathf.Abs(val8.z));
			((Vector3)(ref colliderSize))..ctor(Mathf.Max(val8.x + 0.09f, 0.15f), Mathf.Max(val8.y + 0.09f, 0.24f), Mathf.Max(val8.z + 0.09f, 0.15f));
		}
		BoxCollider val9 = val.GetComponent<BoxCollider>();
		if ((Object)(object)val9 == (Object)null)
		{
			val9 = val.AddComponent<BoxCollider>();
		}
		val9.center = colliderCenter;
		val9.size = colliderSize;
		((Collider)val9).isTrigger = false;
		((Collider)val9).enabled = true;
		int num2 = LayerMask.NameToLayer("Props");
		val.layer = ((num2 >= 0) ? num2 : 6);
		Collider[] componentsInChildren5 = val.GetComponentsInChildren<Collider>(true);
		Collider[] array5 = componentsInChildren5;
		foreach (Collider val10 in array5)
		{
			if (!((Object)(object)val10 == (Object)(object)val9) && (!((Object)(object)componentInChildren != (Object)null) || !((Component)val10).transform.IsChildOf(((Component)componentInChildren).transform)))
			{
				val10.enabled = false;
			}
		}
		if ((Object)(object)componentInChildren != (Object)null)
		{
			((Component)componentInChildren).transform.localPosition = Vector3.zero;
		}
		Rigidbody component4 = val.GetComponent<Rigidbody>();
		if ((Object)(object)component4 != (Object)null)
		{
			component4.mass = (scrap.IsPainting ? 0.5f : 0.05f);
			component4.useGravity = true;
		}
		Item val11 = Object.Instantiate<Item>(template);
		((Object)val11).name = "Hanestad_" + scrap.Name.Replace(" ", "") + "_Item";
		val11.itemName = scrap.Name;
		val11.spawnPrefab = val;
		val11.minValue = scrap.MinValue;
		val11.maxValue = scrap.MaxValue;
		val11.isScrap = true;
		val11.twoHanded = false;
		val11.weight = (scrap.IsPainting ? 1.1f : 1f);
		val11.meshVariants = (Mesh[])(object)new Mesh[0];
		val11.materialVariants = (Material[])(object)new Material[0];
		val11.positionOffset = Vector3.zero;
		((GrabbableObject)component).itemProperties = val11;
		((GrabbableObject)component).grabbable = true;
		((GrabbableObject)component).mainObjectRenderer = componentInChildren2;
		((GrabbableObject)component).propColliders = (Collider[])(object)new Collider[1] { (Collider)val9 };
		val.tag = "PhysicsProp";
		scrap.Prefab = val;
		scrap.Item = val11;
		NetworkPrefabs.RegisterNetworkPrefab(val);
		Items.RegisterScrap(val11, scrap.Rarity, (LevelTypes)(-1));
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] Registrert: " + scrap.Name + " | Verdi: " + scrap.MinValue + "-" + scrap.MaxValue + " | Grunnvekt: " + scrap.Rarity));
		return true;
	}

	private Scrap FindScrap(Item item)
	{
		if ((Object)(object)item == (Object)null)
		{
			return null;
		}
		foreach (Scrap scrap in scraps)
		{
			if ((Object)(object)scrap.Item == (Object)null || (!((Object)(object)item == (Object)(object)scrap.Item) && !(item.itemName == scrap.Item.itemName)))
			{
				continue;
			}
			return scrap;
		}
		return null;
	}

	private void EnsureGlobalItemList()
	{
		if ((Object)(object)StartOfRound.Instance == (Object)null || (Object)(object)StartOfRound.Instance.allItemsList == (Object)null || StartOfRound.Instance.allItemsList.itemsList == null)
		{
			return;
		}
		List<Item> itemsList = StartOfRound.Instance.allItemsList.itemsList;
		foreach (Scrap scrap in scraps)
		{
			if ((Object)(object)scrap.Item == (Object)null)
			{
				continue;
			}
			bool flag = false;
			foreach (Item item in itemsList)
			{
				if ((Object)(object)item == (Object)(object)scrap.Item || ((Object)(object)item != (Object)null && item.itemName == scrap.Item.itemName))
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				itemsList.Add(scrap.Item);
			}
		}
	}

	private void EnsureAllLevelsScrap()
	{
		if (!((Object)(object)StartOfRound.Instance == (Object)null) && StartOfRound.Instance.levels != null)
		{
			SelectableLevel[] levels = StartOfRound.Instance.levels;
			foreach (SelectableLevel level in levels)
			{
				EnsureLevelScrap(level);
			}
		}
	}

	private void EnsureLevelScrap(SelectableLevel level)
	{
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Expected O, but got Unknown
		if ((Object)(object)level == (Object)null || level.spawnableScrap == null)
		{
			return;
		}
		foreach (Scrap scrap in scraps)
		{
			if ((Object)(object)scrap.Item == (Object)null)
			{
				continue;
			}
			bool flag = false;
			foreach (SpawnableItemWithRarity item in level.spawnableScrap)
			{
				if (item != null && (Object)(object)item.spawnableItem != (Object)null && ((Object)(object)item.spawnableItem == (Object)(object)scrap.Item || item.spawnableItem.itemName == scrap.Item.itemName))
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				level.spawnableScrap.Add(new SpawnableItemWithRarity(scrap.Item, scrap.Rarity));
			}
		}
	}

	private void ApplyScrapDistribution()
	{
		//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ce: Expected O, but got Unknown
		//IL_0298: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a2: Expected O, but got Unknown
		if (!registered || (Object)(object)StartOfRound.Instance == (Object)null)
		{
			return;
		}
		SelectableLevel currentLevel = StartOfRound.Instance.currentLevel;
		if ((Object)(object)currentLevel == (Object)null || currentLevel.spawnableScrap == null)
		{
			return;
		}
		EnsureLevelScrap(currentLevel);
		List<SpawnableItemWithRarity> list = new List<SpawnableItemWithRarity>();
		List<Scrap> list2 = new List<Scrap>();
		int num = 0;
		int num2 = 0;
		foreach (SpawnableItemWithRarity item in currentLevel.spawnableScrap)
		{
			if (item != null && !((Object)(object)item.spawnableItem == (Object)null) && FindScrap(item.spawnableItem) == null)
			{
				list.Add(item);
				num += Mathf.Max(0, item.rarity);
			}
		}
		foreach (Scrap scrap2 in scraps)
		{
			if (!((Object)(object)scrap2.Item == (Object)null))
			{
				list2.Add(scrap2);
				num2 += scrap2.Rarity;
			}
		}
		if (list2.Count == 0 || num2 <= 0)
		{
			return;
		}
		if (num <= 0)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("[HANESTAD 80/20] Ingen vanlig scrap på " + currentLevel.PlanetName + ". Bruker bare Hanestad-scrap."));
			List<SpawnableItemWithRarity> list3 = new List<SpawnableItemWithRarity>(list);
			foreach (Scrap item2 in list2)
			{
				list3.Add(new SpawnableItemWithRarity(item2.Item, item2.Rarity));
			}
			currentLevel.spawnableScrap = list3;
			return;
		}
		int num3 = Mathf.RoundToInt((float)num * 0.8f / 0.19999999f);
		num3 = Mathf.Max(num3, list2.Count);
		List<SpawnableItemWithRarity> list4 = new List<SpawnableItemWithRarity>(list);
		int num4 = num3;
		int num5 = num2;
		for (int i = 0; i < list2.Count; i++)
		{
			Scrap scrap = list2[i];
			int num6 = list2.Count - i;
			int num7;
			if (i == list2.Count - 1)
			{
				num7 = num4;
			}
			else
			{
				int num8 = Mathf.RoundToInt((float)num4 * (float)scrap.Rarity / (float)num5);
				num7 = Mathf.Clamp(num8, 1, num4 - (num6 - 1));
			}
			list4.Add(new SpawnableItemWithRarity(scrap.Item, num7));
			num4 -= num7;
			num5 -= scrap.Rarity;
		}
		currentLevel.spawnableScrap = list4;
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD 80/20] " + currentLevel.PlanetName + " | Vanlig vekt: " + num + " | Hanestad vekt: " + num3 + " | Hanestad-andel: " + (100f * (float)num3 / (float)(num + num3)).ToString("F1") + "%"));
	}

	private void ActivateNaturalScrap()
	{
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		if (!registered || (Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
		{
			return;
		}
		GrabbableObject[] array = Resources.FindObjectsOfTypeAll<GrabbableObject>();
		GrabbableObject[] array2 = array;
		foreach (GrabbableObject val in array2)
		{
			if ((Object)(object)val == (Object)null || (Object)(object)((Component)val).gameObject == (Object)null)
			{
				continue;
			}
			Scene scene = ((Component)val).gameObject.scene;
			if (!((Scene)(ref scene)).IsValid())
			{
				continue;
			}
			Scrap scrap = FindScrap(val.itemProperties);
			if (scrap != null && !((Object)(object)((Component)val).gameObject == (Object)(object)scrap.Prefab) && val.isInFactory)
			{
				if (scrap.Name == "Pepsi Max" && val.scrapValue != 1)
				{
					val.SetScrapValue(1);
				}
				if (!((Component)val).gameObject.activeSelf)
				{
					((Component)val).gameObject.SetActive(true);
				}
			}
		}
	}

	private void ActivateAfterGeneration()
	{
		((MonoBehaviour)this).StartCoroutine(ActivateGeneratedScrap());
	}

	private IEnumerator ActivateGeneratedScrap()
	{
		yield return (object)new WaitForSeconds(1f);
		ActivateNaturalScrap();
		yield return (object)new WaitForSeconds(4f);
		ActivateNaturalScrap();
		yield return (object)new WaitForSeconds(10f);
		ActivateNaturalScrap();
	}
}