Decompiled source of HanestadScrapPack v1.0.5

HanestadScrapPack.dll

Decompiled a month 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 BepInEx.Logging;
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.5")]
[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 bool UseFullBounds;

		public GameObject Model;

		public GameObject Prefab;

		public Item Item;
	}

	[HarmonyPatch(typeof(GameNetworkManager), "Start")]
	private static class EarlyNetworkRegistrationPatch
	{
		[HarmonyPrefix]
		private static void Prefix(GameNetworkManager __instance)
		{
			if ((Object)(object)Instance != (Object)null)
			{
				Instance.RegisterEarly(__instance);
			}
		}
	}

	[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 = "Tuborg Pint",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_TuborgPint.prefab",
			MinValue = 12,
			MaxValue = 35,
			Rarity = 15,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Monster Energy",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_MonsterEnergy.prefab",
			MinValue = 10,
			MaxValue = 30,
			Rarity = 17,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Battery",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_Battery.prefab",
			MinValue = 10,
			MaxValue = 30,
			Rarity = 17,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Mariestads Export",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_MariestadsExport.prefab",
			MinValue = 20,
			MaxValue = 50,
			Rarity = 13,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Mack Isbjørn",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_MackIsbjørn.prefab",
			MinValue = 15,
			MaxValue = 45,
			Rarity = 15,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Frydenlund Fatøl",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_FrydenlundFatol.prefab",
			MinValue = 15,
			MaxValue = 35,
			Rarity = 16,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Austmann Pilsner",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_AustmannPils.prefab",
			MinValue = 20,
			MaxValue = 55,
			Rarity = 13,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Norrlands Djup",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_NorrlandsDjup.prefab",
			MinValue = 25,
			MaxValue = 65,
			Rarity = 10,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Frydenlund Juicy IPA",
			AssetPath = "Assets/BeerCans/Prefabs/Hanestad_JuicyIPA.prefab",
			MinValue = 40,
			MaxValue = 85,
			Rarity = 8,
			TargetHeight = 0.175f
		},
		new Scrap
		{
			Name = "Mystery Ball 1",
			AssetPath = "Assets/FaceBalls/Prefabs/Hanestad_FaceBall01.prefab",
			MinValue = 20,
			MaxValue = 60,
			Rarity = 14,
			TargetHeight = 0.5f
		},
		new Scrap
		{
			Name = "Mystery Ball 2",
			AssetPath = "Assets/FaceBalls/Prefabs/Hanestad_FaceBall02.prefab",
			MinValue = 20,
			MaxValue = 60,
			Rarity = 14,
			TargetHeight = 0.5f
		},
		new Scrap
		{
			Name = "Mystery Ball 3",
			AssetPath = "Assets/FaceBalls/Prefabs/Hanestad_FaceBall03.prefab",
			MinValue = 20,
			MaxValue = 60,
			Rarity = 14,
			TargetHeight = 0.5f
		},
		new Scrap
		{
			Name = "Mystery Ball 4",
			AssetPath = "Assets/FaceBalls/Prefabs/Hanestad_FaceBall04.prefab",
			MinValue = 20,
			MaxValue = 60,
			Rarity = 14,
			TargetHeight = 0.5f
		},
		new Scrap
		{
			Name = "Nissan Maxima V6 Key",
			AssetPath = "Assets/CarKeys/Prefabs/Hanestad_NissanMaximaV6.prefab",
			MinValue = 170,
			MaxValue = 250,
			Rarity = 5,
			TargetHeight = 0.16f
		},
		new Scrap
		{
			Name = "Mustang Ecoboost Key",
			AssetPath = "Assets/CarKeys/Prefabs/Hanestad_MustangEcoboost.prefab",
			MinValue = 65,
			MaxValue = 110,
			Rarity = 5,
			TargetHeight = 0.16f
		},
		new Scrap
		{
			Name = "KIA Ceed Key",
			AssetPath = "Assets/CarKeys/Prefabs/Hanestad_KIACeed.prefab",
			MinValue = 10,
			MaxValue = 30,
			Rarity = 18,
			TargetHeight = 0.16f
		},
		new Scrap
		{
			Name = "MG EV Key",
			AssetPath = "Assets/CarKeys/Prefabs/Hanestad_MGEV.prefab",
			MinValue = 10,
			MaxValue = 30,
			Rarity = 18,
			TargetHeight = 0.16f
		},
		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
		},
		new Scrap
		{
			Name = "Engelsvatnet-hytta",
			AssetPath = "Assets/Paintings/Prefabs/Hanestad_EngelsvatnetHytte.prefab",
			MinValue = 250,
			MaxValue = 450,
			Rarity = 5,
			TargetHeight = 1.5f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "RBK Banner",
			AssetPath = "Assets/FootballBanners/Prefabs/Hanestad_RBKBanner.prefab",
			MinValue = 40,
			MaxValue = 80,
			Rarity = 25,
			TargetHeight = 0.6f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "LSK Banner",
			AssetPath = "Assets/FootballBanners/Prefabs/Hanestad_LSKBanner.prefab",
			MinValue = 10,
			MaxValue = 30,
			Rarity = 25,
			TargetHeight = 0.6f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "VIF Banner",
			AssetPath = "Assets/FootballBanners/Prefabs/Hanestad_VIFBanner.prefab",
			MinValue = 10,
			MaxValue = 30,
			Rarity = 25,
			TargetHeight = 0.6f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Roasted Pig",
			AssetPath = "Assets/RoastedPig/Prefabs/Hanestad_RoastedPig.prefab",
			MinValue = 250,
			MaxValue = 450,
			Rarity = 8,
			TargetHeight = 0.9f,
			UseFullBounds = true
		},
		new Scrap
		{
			Name = "Trollspark IPA",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_TrollsparkIPA.prefab",
			MinValue = 38,
			MaxValue = 74,
			Rarity = 100,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Bryggolfs Jul",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_BryggolfsJul.prefab",
			MinValue = 38,
			MaxValue = 74,
			Rarity = 15,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Duvel",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_Duvel.prefab",
			MinValue = 40,
			MaxValue = 80,
			Rarity = 15,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Grønn Gajol",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_GronnGajol.prefab",
			MinValue = 45,
			MaxValue = 90,
			Rarity = 15,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Tyrkisk Peber",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_TyrkiskPeber.prefab",
			MinValue = 45,
			MaxValue = 90,
			Rarity = 15,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Chili Klaus Habanero Shot",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_ChiliKlaus.prefab",
			MinValue = 60,
			MaxValue = 120,
			Rarity = 15,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Gul Hooch",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_GulHooch.prefab",
			MinValue = 30,
			MaxValue = 65,
			Rarity = 15,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "Clausthaler",
			AssetPath = "Assets/Bottles/Prefabs/Hanestad_Clausthaler.prefab",
			MinValue = 1,
			MaxValue = 1,
			Rarity = 18,
			TargetHeight = 0.345f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "ON FIRE Plate",
			AssetPath = "Assets/LicensePlates/Prefabs/Hanestad_OnFirePlate.prefab",
			MinValue = 35,
			MaxValue = 85,
			Rarity = 15,
			TargetHeight = 0.165f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "HIMSELF Plate",
			AssetPath = "Assets/LicensePlates/Prefabs/Hanestad_HimselfPlate.prefab",
			MinValue = 35,
			MaxValue = 85,
			Rarity = 15,
			TargetHeight = 0.165f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "JA JEG Plate",
			AssetPath = "Assets/LicensePlates/Prefabs/Hanestad_JaJegPlate.prefab",
			MinValue = 35,
			MaxValue = 85,
			Rarity = 15,
			TargetHeight = 0.165f,
			IsPainting = true
		},
		new Scrap
		{
			Name = "JOA MEN Plate",
			AssetPath = "Assets/LicensePlates/Prefabs/Hanestad_JoaMenPlate.prefab",
			MinValue = 35,
			MaxValue = 85,
			Rarity = 15,
			TargetHeight = 0.165f,
			IsPainting = true
		}
	};

	private bool registered;

	private int generationCount;

	private int activatedCount;

	private const float PickupMargin = 0.045f;

	private const float HanestadShare = 0.8f;

	private const bool Test5OnlyNewItems = false;

	private const bool TestOnlyTrollsparkIPA = false;

	private const bool TestOnlyNewBottlesAndPlates = false;

	private Harmony harmony;

	private static bool IsTest5Item(Scrap scrap)
	{
		if (scrap == null)
		{
			return false;
		}
		return scrap.Name == "RBK Banner" || scrap.Name == "LSK Banner" || scrap.Name == "VIF Banner" || scrap.Name == "Roasted Pig";
	}

	private void Awake()
	{
		//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f0: Expected O, but got Unknown
		Instance = this;
		string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
		string[] array = new string[2]
		{
			Path.Combine(directoryName, "hanestadscrap"),
			Path.Combine(directoryName, "Assets", "hanestadscrap")
		};
		AssetBundle val = null;
		string[] array2 = array;
		foreach (string text in array2)
		{
			if (!File.Exists(text))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] Ingen AssetBundle på: " + text));
				continue;
			}
			try
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] Forsøker AssetBundle: " + text));
				val = AssetBundle.LoadFromFile(text);
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Feil ved lasting fra " + text + ": " + ex));
			}
			if ((Object)(object)val != (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD] AssetBundle lastet fra: " + text));
				break;
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD] Ingen gyldig AssetBundle funnet. Sjekket: " + string.Join(" ; ", array)));
			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 RegisterEarly(GameNetworkManager gameManager)
	{
		if (registered)
		{
			return;
		}
		try
		{
			NetworkManager component = ((Component)gameManager).GetComponent<NetworkManager>();
			if ((Object)(object)component == (Object)null || component.NetworkConfig == null || component.NetworkConfig.Prefabs == null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"[HANESTAD NET] Fant ingen nettverks-prefabliste ved oppstart. Ingen scrap registreres.");
				return;
			}
			Item val = null;
			foreach (NetworkPrefab prefab in component.NetworkConfig.Prefabs.Prefabs)
			{
				if (prefab != null && !((Object)(object)prefab.Prefab == (Object)null))
				{
					PhysicsProp component2 = prefab.Prefab.GetComponent<PhysicsProp>();
					if (!((Object)(object)component2 == (Object)null) && !((Object)(object)((GrabbableObject)component2).itemProperties == (Object)null) && string.Equals(((GrabbableObject)component2).itemProperties.itemName, "Big bolt", StringComparison.OrdinalIgnoreCase))
					{
						val = ((GrabbableObject)component2).itemProperties;
						break;
					}
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				Item[] array = Resources.FindObjectsOfTypeAll<Item>();
				foreach (Item val2 in array)
				{
					if ((Object)(object)val2 != (Object)null && (Object)(object)val2.spawnPrefab != (Object)null && string.Equals(val2.itemName, "Big bolt", StringComparison.OrdinalIgnoreCase))
					{
						val = val2;
						break;
					}
				}
			}
			if ((Object)(object)val == (Object)null || (Object)(object)val.spawnPrefab == (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"[HANESTAD NET] Fant ikke Big bolt før nettverket starter. Modden vil ikke registrere usikre scrap-prefabs.");
				return;
			}
			int num = 0;
			foreach (Scrap scrap in scraps)
			{
				if ((Object)(object)scrap.Model == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD NET] Mangler modell: " + scrap.Name));
					continue;
				}
				try
				{
					if (RegisterScrap(scrap, val))
					{
						num++;
					}
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD NET] Kunne ikke registrere " + scrap.Name + ": " + ex));
				}
			}
			registered = num == scraps.Count;
			if (registered)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD NET] V1.0.5 NORMAL REGISTRERT TIDLIG! " + num + " av " + scraps.Count + " med egne nettverks-ID-er."));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD NET] Bare " + num + " av " + scraps.Count + " registrert. Fordelingspatchen holdes deaktivert."));
			}
		}
		catch (Exception ex2)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("[HANESTAD NET] Oppstartsfeil: " + ex2));
		}
	}

	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 unsafe 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_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_018e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0193: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_021c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0221: 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));
		ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
		string[] obj = new string[10]
		{
			"[HANESTAD SIZE] ",
			scrap.Name,
			" | Før: ",
			((object)((Bounds)(ref result)).size/*cast due to .constrained prefix*/).ToString(),
			" | Faktor: ",
			num.ToString("F3"),
			" | Etter: ",
			((object)((Bounds)(ref result3)).size/*cast due to .constrained prefix*/).ToString(),
			" | Collider: ",
			null
		};
		Vector3 val = colliderSize;
		obj[9] = ((object)(*(Vector3*)(&val))/*cast due to .constrained prefix*/).ToString();
		logger.LogInfo((object)string.Concat(obj));
		return true;
	}

	private unsafe bool PrepareFullBounds(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_0048: 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_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: 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_00e1: 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_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: 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_0144: 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_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0208: Unknown result type (might be due to invalid IL or missing references)
		//IL_020d: Unknown result type (might be due to invalid IL or missing references)
		colliderCenter = Vector3.zero;
		colliderSize = Vector3.zero;
		if (!CalculateMeshBounds(visual.transform, prefab.transform, out var result))
		{
			return false;
		}
		float num = Mathf.Max(new float[3]
		{
			((Bounds)(ref result)).size.x,
			((Bounds)(ref result)).size.y,
			((Bounds)(ref result)).size.z
		});
		if (num < 1E-06f)
		{
			return false;
		}
		float num2 = scrap.TargetHeight / num;
		Transform transform = visual.transform;
		transform.localScale *= num2;
		if (!CalculateMeshBounds(visual.transform, prefab.transform, out var result2))
		{
			return false;
		}
		Transform transform2 = visual.transform;
		transform2.localPosition -= ((Bounds)(ref result2)).center;
		if (!CalculateMeshBounds(visual.transform, prefab.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.15f), Mathf.Max(((Bounds)(ref result3)).size.z + 0.09f, 0.15f));
		ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
		string[] obj = new string[10]
		{
			"[HANESTAD PIG SIZE] ",
			scrap.Name,
			" | before=",
			((object)((Bounds)(ref result)).size/*cast due to .constrained prefix*/).ToString(),
			" | factor=",
			num2.ToString("F3"),
			" | after=",
			((object)((Bounds)(ref result3)).size/*cast due to .constrained prefix*/).ToString(),
			" | collider=",
			null
		};
		Vector3 val = colliderSize;
		obj[9] = ((object)(*(Vector3*)(&val))/*cast due to .constrained prefix*/).ToString();
		logger.LogInfo((object)string.Concat(obj));
		return true;
	}

	private bool RegisterScrap(Scrap scrap, Item template)
	{
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: 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_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_022c: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0279: Unknown result type (might be due to invalid IL or missing references)
		//IL_0267: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_043b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0440: Unknown result type (might be due to invalid IL or missing references)
		//IL_0444: Unknown result type (might be due to invalid IL or missing references)
		//IL_0449: Unknown result type (might be due to invalid IL or missing references)
		//IL_0460: Unknown result type (might be due to invalid IL or missing references)
		//IL_046a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0474: Unknown result type (might be due to invalid IL or missing references)
		//IL_044f: Unknown result type (might be due to invalid IL or missing references)
		//IL_06b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_06c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_05b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_05b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_05bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_05c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_05cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_05d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_05db: Unknown result type (might be due to invalid IL or missing references)
		//IL_05e0: 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)
		//IL_05ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_05f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_05fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0603: Unknown result type (might be due to invalid IL or missing references)
		//IL_0608: Unknown result type (might be due to invalid IL or missing references)
		//IL_0612: Unknown result type (might be due to invalid IL or missing references)
		//IL_0617: Unknown result type (might be due to invalid IL or missing references)
		//IL_061c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0620: Unknown result type (might be due to invalid IL or missing references)
		//IL_062c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0638: Unknown result type (might be due to invalid IL or missing references)
		//IL_064b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0662: Unknown result type (might be due to invalid IL or missing references)
		//IL_0679: Unknown result type (might be due to invalid IL or missing references)
		//IL_04a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0543: Unknown result type (might be due to invalid IL or missing references)
		//IL_0548: Unknown result type (might be due to invalid IL or missing references)
		//IL_054c: Unknown result type (might be due to invalid IL or missing references)
		//IL_055b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0560: Unknown result type (might be due to invalid IL or missing references)
		//IL_0564: Unknown result type (might be due to invalid IL or missing references)
		//IL_0573: Unknown result type (might be due to invalid IL or missing references)
		//IL_0578: Unknown result type (might be due to invalid IL or missing references)
		//IL_057c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0778: Unknown result type (might be due to invalid IL or missing references)
		//IL_086a: Unknown result type (might be due to invalid IL or missing references)
		//IL_086f: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = NetworkPrefabs.CloneNetworkPrefab(template.spawnPrefab, "Hanestad_" + scrap.Name.Replace(" ", "") + "_Network");
		val.SetActive(true);
		val.AddComponent<HanestadSpawnCleanup>();
		if (scrap.UseFullBounds)
		{
			val.AddComponent<HanestadPigRuntimeScale>();
		}
		if (scrap.IsPainting || scrap.UseFullBounds)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD SCALE] " + scrap.Name + " | Rot før: " + ((object)val.transform.localScale/*cast due to .constrained prefix*/).ToString()));
			val.transform.localScale = Vector3.one;
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD SCALE] " + scrap.Name + " | Rot etter: " + ((object)val.transform.localScale/*cast due to .constrained prefix*/).ToString()));
		}
		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;
		bool flag = scrap.AssetPath.StartsWith("Assets/LicensePlates/", StringComparison.OrdinalIgnoreCase);
		val3.transform.localScale = (flag ? scrap.Model.transform.localScale : Vector3.one);
		if (flag)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD PLATE SCALE] " + scrap.Name + " | Original prefab scale=" + ((object)val3.transform.localScale/*cast due to .constrained prefix*/).ToString()));
		}
		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.UseFullBounds)
		{
			if (!PrepareFullBounds(scrap, val3, val, out colliderCenter, out colliderSize))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"[HANESTAD PIG SIZE] Kunne ikke beregne geometri");
				Object.Destroy((Object)(object)val);
				return false;
			}
		}
		else if (scrap.IsPainting)
		{
			if (!PreparePainting(scrap, val3, val, out colliderCenter, out colliderSize))
			{
				Object.Destroy((Object)(object)val);
				return false;
			}
		}
		else
		{
			bool flag2 = scrap.AssetPath.StartsWith("Assets/CarKeys/", StringComparison.OrdinalIgnoreCase);
			Bounds bounds = ((Renderer)componentInChildren2).bounds;
			Vector3 size = ((Bounds)(ref bounds)).size;
			float num = (flag2 ? Mathf.Max(new float[3] { size.x, size.y, size.z }) : size.y);
			if (num > 0.0001f)
			{
				float num2 = scrap.TargetHeight / num;
				Transform transform = val3.transform;
				transform.localScale *= num2;
				if (flag2)
				{
					ManualLogSource logger = ((BaseUnityPlugin)this).Logger;
					string[] obj = new string[11]
					{
						"[HANESTAD KEY SIZE] ",
						scrap.Name,
						" | Lengde før=",
						num.ToString("F4"),
						" m | Mål=",
						scrap.TargetHeight.ToString("F4"),
						" m | Faktor=",
						num2.ToString("F4"),
						" | Lengde etter=",
						null,
						null
					};
					float[] array5 = new float[3];
					bounds = ((Renderer)componentInChildren2).bounds;
					array5[0] = ((Bounds)(ref bounds)).size.x;
					bounds = ((Renderer)componentInChildren2).bounds;
					array5[1] = ((Bounds)(ref bounds)).size.y;
					bounds = ((Renderer)componentInChildren2).bounds;
					array5[2] = ((Bounds)(ref bounds)).size.z;
					obj[9] = Mathf.Max(array5).ToString("F4");
					obj[10] = " m";
					logger.LogInfo((object)string.Concat(obj));
				}
			}
			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 num3 = LayerMask.NameToLayer("Props");
		val.layer = ((num3 >= 0) ? num3 : 6);
		Collider[] componentsInChildren5 = val.GetComponentsInChildren<Collider>(true);
		Collider[] array6 = componentsInChildren5;
		foreach (Collider val10 in array6)
		{
			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;
		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_01ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Expected O, but got Unknown
		//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cd: Expected O, but got Unknown
		if (!registered || (Object)(object)StartOfRound.Instance == (Object)null || (Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
		{
			return;
		}
		SelectableLevel currentLevel = StartOfRound.Instance.currentLevel;
		if ((Object)(object)currentLevel == (Object)null || currentLevel.spawnableScrap == null)
		{
			return;
		}
		EnsureLevelScrap(currentLevel);
		bool flag = false;
		bool flag2 = false;
		bool flag3 = false;
		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;
		int num9 = 0;
		int num10 = 0;
		int num11 = 0;
		foreach (SpawnableItemWithRarity item3 in currentLevel.spawnableScrap)
		{
			if (item3 != null && !((Object)(object)item3.spawnableItem == (Object)null))
			{
				int num12 = Mathf.Max(0, item3.rarity);
				if (FindScrap(item3.spawnableItem) != null)
				{
					num10 += num12;
				}
				else
				{
					num11 += num12;
				}
				num9++;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD DIAG TABLE] " + item3.spawnableItem.itemName + " | weight=" + num12 + " | own=" + (FindScrap(item3.spawnableItem) != null)));
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD DIAG TABLE] TOTAL entries=" + num9 + " hanestad=" + num10 + " vanilla=" + num11));
		((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" || scrap.Name == "Clausthaler") && val.scrapValue != 1)
				{
					val.SetScrapValue(1);
				}
				if (!((Component)val).gameObject.activeSelf)
				{
					((Component)val).gameObject.SetActive(true);
				}
			}
		}
	}

	private void LogActualScrap(string stage)
	{
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
		{
			return;
		}
		GrabbableObject[] array = Resources.FindObjectsOfTypeAll<GrabbableObject>();
		Dictionary<string, int> dictionary = new Dictionary<string, int>();
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		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() && !((Object)(object)val.itemProperties == (Object)null) && val.itemProperties.isScrap && val.isInFactory)
			{
				num++;
				Scrap scrap = FindScrap(val.itemProperties);
				if (scrap != null)
				{
					num2++;
				}
				if (!((Component)val).gameObject.activeSelf)
				{
					num3++;
				}
				string key = val.itemProperties.itemName ?? "<unnamed>";
				if (!dictionary.ContainsKey(key))
				{
					dictionary[key] = 0;
				}
				dictionary[key]++;
			}
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD DIAG ACTUAL] " + stage + " | factory scrap=" + num + " | hanestad=" + num2 + " | inactive=" + num3));
		foreach (KeyValuePair<string, int> item in dictionary)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)("[HANESTAD DIAG ACTUAL] " + stage + " | " + item.Key + " x" + item.Value));
		}
	}

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

	private IEnumerator ActivateGeneratedScrap()
	{
		yield return (object)new WaitForSeconds(1f);
		ActivateNaturalScrap();
		LogActualScrap("+1s");
		yield return (object)new WaitForSeconds(4f);
		ActivateNaturalScrap();
		LogActualScrap("+5s");
		yield return (object)new WaitForSeconds(10f);
		ActivateNaturalScrap();
		LogActualScrap("+15s");
	}
}
public class HanestadPigRuntimeScale : MonoBehaviour
{
	private IEnumerator Start()
	{
		NetworkObject network = ((Component)this).GetComponent<NetworkObject>();
		Scene scene;
		for (int i = 0; i < 60; i++)
		{
			int num;
			if ((Object)(object)network != (Object)null && network.IsSpawned)
			{
				scene = ((Component)this).gameObject.scene;
				num = (((Scene)(ref scene)).IsValid() ? 1 : 0);
			}
			else
			{
				num = 0;
			}
			if (num != 0)
			{
				break;
			}
			yield return (object)new WaitForSeconds(0.1f);
		}
		int num2;
		if (!((Object)(object)network == (Object)null) && network.IsSpawned)
		{
			scene = ((Component)this).gameObject.scene;
			num2 = ((!((Scene)(ref scene)).IsValid()) ? 1 : 0);
		}
		else
		{
			num2 = 1;
		}
		if (num2 == 0)
		{
			yield return (object)new WaitForSeconds(0.2f);
			CorrectSize();
		}
	}

	private void CorrectSize()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Expected O, but got Unknown
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0122: 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_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: 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)
		//IL_00c2: 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_02c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_030b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0325: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0212: Unknown result type (might be due to invalid IL or missing references)
		//IL_0209: Unknown result type (might be due to invalid IL or missing references)
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_0226: Unknown result type (might be due to invalid IL or missing references)
		//IL_022b: Unknown result type (might be due to invalid IL or missing references)
		//IL_024f: Unknown result type (might be due to invalid IL or missing references)
		//IL_023b: Unknown result type (might be due to invalid IL or missing references)
		//IL_023d: Unknown result type (might be due to invalid IL or missing references)
		Transform val = null;
		foreach (Transform item in ((Component)this).transform)
		{
			Transform val2 = item;
			if (((Object)val2).name == "RoastedPig_Visual")
			{
				val = val2;
				break;
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Renderer[] componentsInChildren = ((Component)val).GetComponentsInChildren<Renderer>(true);
		Bounds val3 = default(Bounds);
		bool flag = false;
		Renderer[] array = componentsInChildren;
		foreach (Renderer val4 in array)
		{
			if (!((Object)(object)val4 == (Object)null) && val4.enabled)
			{
				if (!flag)
				{
					val3 = val4.bounds;
					flag = true;
				}
				else
				{
					((Bounds)(ref val3)).Encapsulate(val4.bounds);
				}
			}
		}
		if (!flag)
		{
			return;
		}
		float num = Mathf.Max(new float[3]
		{
			((Bounds)(ref val3)).size.x,
			((Bounds)(ref val3)).size.y,
			((Bounds)(ref val3)).size.z
		});
		if (num < 1E-05f)
		{
			return;
		}
		float num2 = 0.9f / num;
		Transform obj = val;
		obj.localScale *= num2;
		Bounds val5 = default(Bounds);
		bool flag2 = false;
		Renderer[] array2 = componentsInChildren;
		Vector3 val7 = default(Vector3);
		foreach (Renderer val6 in array2)
		{
			if ((Object)(object)val6 == (Object)null || !val6.enabled)
			{
				continue;
			}
			Bounds bounds = val6.bounds;
			Vector3 min = ((Bounds)(ref bounds)).min;
			Vector3 max = ((Bounds)(ref bounds)).max;
			for (int k = 0; k < 2; k++)
			{
				for (int l = 0; l < 2; l++)
				{
					for (int m = 0; m < 2; m++)
					{
						((Vector3)(ref val7))..ctor((k == 0) ? min.x : max.x, (l == 0) ? min.y : max.y, (m == 0) ? min.z : max.z);
						Vector3 val8 = ((Component)this).transform.InverseTransformPoint(val7);
						if (!flag2)
						{
							((Bounds)(ref val5))..ctor(val8, Vector3.zero);
							flag2 = true;
						}
						else
						{
							((Bounds)(ref val5)).Encapsulate(val8);
						}
					}
				}
			}
		}
		BoxCollider component = ((Component)this).GetComponent<BoxCollider>();
		if ((Object)(object)component != (Object)null && flag2)
		{
			component.center = ((Bounds)(ref val5)).center;
			component.size = new Vector3(Mathf.Max(((Bounds)(ref val5)).size.x + 0.09f, 0.15f), Mathf.Max(((Bounds)(ref val5)).size.y + 0.09f, 0.15f), Mathf.Max(((Bounds)(ref val5)).size.z + 0.09f, 0.15f));
		}
		Debug.Log((object)("[HANESTAD PIG RUNTIME] Før=" + num.ToString("F3") + " m | skaleringsfaktor=" + num2.ToString("F3") + " | ønsket lengde=" + 0.9f.ToString("F2") + " m"));
	}
}
public class HanestadSpawnCleanup : MonoBehaviour
{
	private void Awake()
	{
		((Object)((Component)this).gameObject).hideFlags = (HideFlags)0;
	}
}
[HarmonyPatch(typeof(RoundManager), "GenerateNewFloor")]
internal static class ExperimentationInteriorPatch
{
	private sealed class InteriorState
	{
		public SelectableLevel Level;

		public IntWithRarity[] Original;

		public IntWithRarity[] Modified;
	}

	private static readonly ManualLogSource Log = Logger.CreateLogSource("Hanestad Interiors");

	private const int FactoryId = 0;

	private const int MansionId = 1;

	private const int MineshaftId = 4;

	private const int FactoryWeight = 200;

	private const int MansionWeight = 100;

	private const int MineshaftWeight = 100;

	private static bool IsExperimentation(SelectableLevel level)
	{
		if ((Object)(object)level == (Object)null)
		{
			return false;
		}
		string text = ((Object)level).name ?? "";
		string text2 = level.PlanetName ?? "";
		return text.IndexOf("Experimentation", StringComparison.OrdinalIgnoreCase) >= 0 || text2.IndexOf("Experimentation", StringComparison.OrdinalIgnoreCase) >= 0;
	}

	private static string GetFlowName(RoundManager manager, int id)
	{
		try
		{
			if ((Object)(object)manager == (Object)null || manager.dungeonFlowTypes == null || id < 0 || id >= manager.dungeonFlowTypes.Length)
			{
				return "";
			}
			IndoorMapType val = manager.dungeonFlowTypes[id];
			if (val == null || (Object)(object)val.dungeonFlow == (Object)null)
			{
				return "";
			}
			return ((Object)val.dungeonFlow).name ?? "";
		}
		catch
		{
			return "";
		}
	}

	private static bool IsCorrectInterior(RoundManager manager, int id)
	{
		string text = GetFlowName(manager, id).ToLowerInvariant();
		return id switch
		{
			0 => text.Contains("factory") || text.Contains("facility") || text.Contains("level1flow"), 
			1 => text.Contains("mansion") || text.Contains("manor") || text.Contains("level2flow"), 
			4 => text.Contains("mine") || text.Contains("cave") || text.Contains("level3flow") || text.Contains("level4flow"), 
			_ => false, 
		};
	}

	[HarmonyPrefix]
	[HarmonyPriority(0)]
	private static void Prefix(RoundManager __instance, out InteriorState __state)
	{
		//IL_0257: Unknown result type (might be due to invalid IL or missing references)
		//IL_025d: Expected O, but got Unknown
		__state = null;
		try
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			SelectableLevel currentLevel = __instance.currentLevel;
			if (!IsExperimentation(currentLevel))
			{
				return;
			}
			IntWithRarity[] dungeonFlowTypes = currentLevel.dungeonFlowTypes;
			if (dungeonFlowTypes == null || dungeonFlowTypes.Length == 0)
			{
				Log.LogWarning((object)"[HANESTAD INTERIOR] Ingen interiørvalg funnet.");
				return;
			}
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			IntWithRarity[] array = dungeonFlowTypes;
			foreach (IntWithRarity val in array)
			{
				if (val == null)
				{
					num4++;
					continue;
				}
				switch (val.id)
				{
				case 0:
					num++;
					break;
				case 1:
					num2++;
					break;
				case 4:
					num3++;
					break;
				default:
					num4++;
					break;
				}
			}
			if (num != 1 || num2 != 1 || num3 != 1 || num4 != 0)
			{
				Log.LogWarning((object)("[HANESTAD INTERIOR] Uventet interiørutvalg på Experimentation. Factory=" + num + ", Mansion=" + num2 + ", Mineshaft=" + num3 + ", Andre=" + num4 + ". Beholder eksisterende vekter."));
				return;
			}
			if (!IsCorrectInterior(__instance, 0) || !IsCorrectInterior(__instance, 1) || !IsCorrectInterior(__instance, 4))
			{
				Log.LogWarning((object)("[HANESTAD INTERIOR] Kunne ikke bekrefte vanilla-ID-ene. ID 0=" + GetFlowName(__instance, 0) + ", ID 1=" + GetFlowName(__instance, 1) + ", ID 4=" + GetFlowName(__instance, 4) + ". Endrer ikke fordelingen."));
				return;
			}
			IntWithRarity[] array2 = (IntWithRarity[])(object)new IntWithRarity[dungeonFlowTypes.Length];
			for (int j = 0; j < dungeonFlowTypes.Length; j++)
			{
				IntWithRarity val2 = dungeonFlowTypes[j];
				int num5 = 0;
				switch (val2.id)
				{
				case 0:
					num5 = 200;
					break;
				case 1:
					num5 = 100;
					break;
				case 4:
					num5 = 100;
					break;
				}
				array2[j] = new IntWithRarity(val2.id, num5, val2.overrideLevelAmbience);
			}
			__state = new InteriorState
			{
				Level = currentLevel,
				Original = dungeonFlowTypes,
				Modified = array2
			};
			currentLevel.dungeonFlowTypes = array2;
			Log.LogInfo((object)"[HANESTAD INTERIOR] Experimentation: Factory 50 %, Mansion 25 %, Mineshaft 25 %. Vekter: 200 / 100 / 100.");
		}
		catch (Exception ex)
		{
			if (__state != null && (Object)(object)__state.Level != (Object)null && __state.Original != null)
			{
				__state.Level.dungeonFlowTypes = __state.Original;
			}
			__state = null;
			Log.LogError((object)("[HANESTAD INTERIOR] Feil under oppsett. Standard interiørvalg beholdes. " + ex));
		}
	}

	[HarmonyFinalizer]
	private static Exception Finalizer(Exception __exception, RoundManager __instance, InteriorState __state)
	{
		if (__state == null)
		{
			return __exception;
		}
		try
		{
			if ((Object)(object)__instance != (Object)null && __instance.dungeonFlowTypes != null)
			{
				int currentDungeonType = __instance.currentDungeonType;
				string flowName = GetFlowName(__instance, currentDungeonType);
				Log.LogInfo((object)("[HANESTAD INTERIOR] Spillets valgte interiør: " + flowName + " (ID " + currentDungeonType + ")."));
			}
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("[HANESTAD INTERIOR] Kunne ikke loggføre interiørvalget: " + ex.Message));
		}
		finally
		{
			if ((Object)(object)__state.Level != (Object)null && __state.Original != null && __state.Level.dungeonFlowTypes == __state.Modified)
			{
				__state.Level.dungeonFlowTypes = __state.Original;
				Log.LogInfo((object)"[HANESTAD INTERIOR] Opprinnelige interiørvekter gjenopprettet.");
			}
		}
		return __exception;
	}
}