Decompiled source of Lantern Peddler v0.2.0

LanternPeddler.dll

Decompiled 10 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Unity.Collections;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Audio;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.UI;

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

internal static class Art
{
	internal sealed class Body
	{
		public Transform Root;

		public Transform Hips;

		public Transform Torso;

		public Transform Head;

		public Transform LegL;

		public Transform LegR;

		public Transform ArmL;

		public Transform ArmR;

		public Transform Pack;

		public Material CoreMat;

		public Light Lamp;
	}

	private static Material _template;

	private static bool _searched;

	private static readonly Dictionary<string, Material> Cache = new Dictionary<string, Material>();

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

	internal static readonly Color GemColor = new Color(0.1f, 0.78f, 0.32f);

	private static Mesh _gem;

	private static readonly Color Canvas = new Color(0.34f, 0.29f, 0.22f);

	private static readonly Color Patch = new Color(0.45f, 0.36f, 0.24f);

	private static readonly Color Rust = new Color(0.24f, 0.15f, 0.1f);

	private static readonly Color Iron = new Color(0.13f, 0.12f, 0.12f);

	private static readonly Color Wood = new Color(0.42f, 0.29f, 0.16f);

	private static readonly Color WoodDark = new Color(0.28f, 0.19f, 0.11f);

	private static readonly Color Skin = new Color(0.2f, 0.18f, 0.17f);

	private static readonly Color Strap = new Color(0.16f, 0.11f, 0.08f);

	internal static readonly Color CoreGlow = new Color(1f, 0.62f, 0.22f);

	private static Mesh _cube;

	private const int Rate = 44100;

	private static AudioClip _chime;

	private static AudioClip _buy;

	private static AudioClip _leave;

	private static AudioClip _knock;

	private static readonly AudioClip[] Mutters = (AudioClip[])(object)new AudioClip[4];

	private static Material Template()
	{
		if ((Object)(object)_template != (Object)null || _searched)
		{
			return _template;
		}
		StartOfRound instance = StartOfRound.Instance;
		if ((Object)(object)instance == (Object)null || (Object)(object)instance.allItemsList == (Object)null)
		{
			return null;
		}
		_searched = true;
		foreach (Item items in instance.allItemsList.itemsList)
		{
			if ((Object)(object)items == (Object)null || (Object)(object)items.spawnPrefab == (Object)null)
			{
				continue;
			}
			MeshRenderer[] componentsInChildren = items.spawnPrefab.GetComponentsInChildren<MeshRenderer>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				Renderer val = (Renderer)(object)componentsInChildren[i];
				Material sharedMaterial = val.sharedMaterial;
				if (!((Object)(object)sharedMaterial == (Object)null) && !((Object)(object)sharedMaterial.shader == (Object)null) && !(((Object)sharedMaterial.shader).name != "HDRP/Lit") && sharedMaterial.HasProperty("_BaseColor") && sharedMaterial.renderQueue < 2450 && (!sharedMaterial.HasProperty("_SurfaceType") || sharedMaterial.GetFloat("_SurfaceType") == 0f))
				{
					string text = ((Object)sharedMaterial).name.ToLowerInvariant();
					if (!text.Contains("glass") && !text.Contains("decal") && !text.Contains("particle"))
					{
						_template = sharedMaterial;
						break;
					}
				}
			}
			if (!((Object)(object)_template != (Object)null))
			{
				continue;
			}
			break;
		}
		return _template;
	}

	internal unsafe static Material Mat(Color c, float metal, float smooth, Color emissive, bool shared = true)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Expected O, but got Unknown
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		string key = ((object)(*(Color*)(&c))/*cast due to .constrained prefix*/).ToString() + metal + smooth + emissive;
		if (shared && Cache.TryGetValue(key, out var value) && (Object)(object)value != (Object)null)
		{
			return value;
		}
		Material val = Template();
		value = ((!((Object)(object)val != (Object)null)) ? new Material(Shader.Find("HDRP/Lit")) : new Material(val));
		((Object)value).name = "LP_Mat";
		string[] array = new string[6] { "_NormalMap", "_MaskMap", "_DetailMap", "_EmissiveColorMap", "_BentNormalMap", "_BaseColorMap" };
		foreach (string text in array)
		{
			if (value.HasProperty(text))
			{
				value.SetTexture(text, (Texture)null);
			}
		}
		value.DisableKeyword("_NORMALMAP");
		value.DisableKeyword("_MASKMAP");
		value.DisableKeyword("_DETAIL_MAP");
		if (value.HasProperty("_BaseColor"))
		{
			value.SetColor("_BaseColor", c);
		}
		if (value.HasProperty("_Metallic"))
		{
			value.SetFloat("_Metallic", metal);
		}
		if (value.HasProperty("_Smoothness"))
		{
			value.SetFloat("_Smoothness", smooth);
		}
		if (value.HasProperty("_UseEmissiveIntensity"))
		{
			value.SetFloat("_UseEmissiveIntensity", 0f);
		}
		if (value.HasProperty("_EmissiveColor"))
		{
			value.SetColor("_EmissiveColor", emissive);
		}
		try
		{
			HDMaterial.ValidateMaterial(value);
		}
		catch
		{
		}
		if (shared)
		{
			Object.DontDestroyOnLoad((Object)(object)value);
			Cache[key] = value;
		}
		return value;
	}

	internal static Material FromProxy(Material src)
	{
		//IL_0080: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Expected O, but got Unknown
		//IL_0178: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)src == (Object)null || (Object)(object)src.shader == (Object)null || ((Object)src.shader).name != "LanternPeddler/Proxy")
		{
			return src;
		}
		if (Proxied.TryGetValue(src, out var value) && (Object)(object)value != (Object)null)
		{
			return value;
		}
		Material val = Template();
		value = ((!((Object)(object)val != (Object)null)) ? new Material(Shader.Find("HDRP/Lit")) : new Material(val));
		((Object)value).name = "LP_" + ((Object)src).name;
		string[] array = new string[6] { "_NormalMap", "_MaskMap", "_DetailMap", "_EmissiveColorMap", "_BentNormalMap", "_BaseColorMap" };
		foreach (string text in array)
		{
			if (value.HasProperty(text))
			{
				value.SetTexture(text, (Texture)null);
			}
		}
		value.DisableKeyword("_NORMALMAP");
		value.DisableKeyword("_MASKMAP");
		value.DisableKeyword("_DETAIL_MAP");
		Texture texture = src.GetTexture("_BaseColorMap");
		if ((Object)(object)texture != (Object)null && value.HasProperty("_BaseColorMap"))
		{
			value.SetTexture("_BaseColorMap", texture);
		}
		if (value.HasProperty("_BaseColor"))
		{
			value.SetColor("_BaseColor", src.GetColor("_BaseColor"));
		}
		if (value.HasProperty("_Metallic"))
		{
			value.SetFloat("_Metallic", src.GetFloat("_Metallic"));
		}
		if (value.HasProperty("_Smoothness"))
		{
			value.SetFloat("_Smoothness", src.GetFloat("_Smoothness"));
		}
		if (value.HasProperty("_UseEmissiveIntensity"))
		{
			value.SetFloat("_UseEmissiveIntensity", 0f);
		}
		if (value.HasProperty("_EmissiveColor"))
		{
			value.SetColor("_EmissiveColor", src.GetColor("_EmissiveColor"));
		}
		Texture val2 = ((!src.HasProperty("_NormalMap")) ? null : src.GetTexture("_NormalMap"));
		if ((Object)(object)val2 != (Object)null && value.HasProperty("_NormalMap"))
		{
			value.SetTexture("_NormalMap", val2);
			if (value.HasProperty("_NormalScale"))
			{
				value.SetFloat("_NormalScale", (!src.HasProperty("_NormalScale")) ? 1f : src.GetFloat("_NormalScale"));
			}
			value.EnableKeyword("_NORMALMAP");
			value.EnableKeyword("_NORMALMAP_TANGENT_SPACE");
		}
		Texture val3 = ((!src.HasProperty("_MaskMap")) ? null : src.GetTexture("_MaskMap"));
		if ((Object)(object)val3 != (Object)null && value.HasProperty("_MaskMap"))
		{
			value.SetTexture("_MaskMap", val3);
			value.EnableKeyword("_MASKMAP");
			string[] array2 = new string[3] { "_MetallicRemapMin", "_SmoothnessRemapMin", "_AORemapMin" };
			foreach (string text2 in array2)
			{
				if (value.HasProperty(text2))
				{
					value.SetFloat(text2, 0f);
				}
			}
			string[] array3 = new string[3] { "_MetallicRemapMax", "_SmoothnessRemapMax", "_AORemapMax" };
			foreach (string text3 in array3)
			{
				if (value.HasProperty(text3))
				{
					value.SetFloat(text3, 1f);
				}
			}
		}
		try
		{
			HDMaterial.ValidateMaterial(value);
		}
		catch
		{
		}
		Object.DontDestroyOnLoad((Object)(object)value);
		Proxied[src] = value;
		return value;
	}

	internal static void SetGlow(Material m, Color emissive)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)m != (Object)null && m.HasProperty("_EmissiveColor"))
		{
			m.SetColor("_EmissiveColor", emissive);
		}
	}

	internal static Material GemMaterial()
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		return Mat(GemColor, 0.1f, 0.92f, new Color(0.02f, 0.35f, 0.1f));
	}

	internal static Mesh GemMesh()
	{
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0200: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: 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_022f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_024e: Expected O, but got Unknown
		if ((Object)(object)_gem != (Object)null)
		{
			return _gem;
		}
		float[] array = new float[4] { -0.075f, -0.03f, 0.035f, 0.062f };
		float[] array2 = new float[4] { 0.012f, 0.046f, 0.046f, 0.03f };
		Vector3[][] array3 = new Vector3[array.Length][];
		for (int i = 0; i < array.Length; i++)
		{
			array3[i] = (Vector3[])(object)new Vector3[8];
			for (int j = 0; j < 8; j++)
			{
				float num = ((float)j + 0.5f) * (float)Math.PI * 2f / 8f;
				ref Vector3 reference = ref array3[i][j];
				reference = new Vector3(Mathf.Cos(num) * array2[i], array[i], Mathf.Sin(num) * array2[i] * 0.72f);
			}
		}
		List<Vector3> v = new List<Vector3>();
		List<int> t = new List<int>();
		Action<Vector3, Vector3, Vector3> action = delegate(Vector3 a, Vector3 b, Vector3 c)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			int count = v.Count;
			v.Add(a);
			v.Add(b);
			v.Add(c);
			t.Add(count);
			t.Add(count + 1);
			t.Add(count + 2);
		};
		for (int num2 = 0; num2 < array.Length - 1; num2++)
		{
			for (int num3 = 0; num3 < 8; num3++)
			{
				int num4 = (num3 + 1) % 8;
				action(array3[num2][num3], array3[num2 + 1][num3], array3[num2 + 1][num4]);
				action(array3[num2][num3], array3[num2 + 1][num4], array3[num2][num4]);
			}
		}
		Vector3 arg = default(Vector3);
		((Vector3)(ref arg))..ctor(0f, -0.082f, 0f);
		Vector3 arg2 = default(Vector3);
		((Vector3)(ref arg2))..ctor(0f, array[^1], 0f);
		for (int num5 = 0; num5 < 8; num5++)
		{
			int num6 = (num5 + 1) % 8;
			action(arg, array3[0][num5], array3[0][num6]);
			action(arg2, array3[array.Length - 1][num6], array3[array.Length - 1][num5]);
		}
		Mesh val = new Mesh();
		((Object)val).name = "LP_Gem";
		_gem = val;
		_gem.SetVertices(v);
		_gem.SetTriangles(t, 0);
		_gem.RecalculateNormals();
		_gem.RecalculateBounds();
		Object.DontDestroyOnLoad((Object)(object)_gem);
		return _gem;
	}

	internal static Sprite GemIcon()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Expected O, but got Unknown
		//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: 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_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = new Texture2D(128, 128, (TextureFormat)4, false);
		Color val2 = default(Color);
		((Color)(ref val2))..ctor(0f, 0f, 0f, 0f);
		for (int i = 0; i < 128; i++)
		{
			for (int j = 0; j < 128; j++)
			{
				float num = ((float)j - 64f) / 40f;
				float num2 = ((float)i - 64f) / 54f;
				if (!(Mathf.Abs(num) <= 1f) || !(Mathf.Abs(num2) <= 1f) || !(Mathf.Abs(num) + Mathf.Abs(num2) <= 1.45f))
				{
					val.SetPixel(j, i, val2);
					continue;
				}
				bool flag = Mathf.Abs(num) > 0.9f || Mathf.Abs(num2) > 0.92f || Mathf.Abs(num) + Mathf.Abs(num2) > 1.36f;
				Color val3 = GemColor * (0.75f + 0.35f * (num2 + 1f) * 0.5f);
				if (num < -0.15f && num2 > 0.25f && num > -0.6f)
				{
					val3 = Color.Lerp(val3, new Color(0.75f, 1f, 0.8f), 0.55f);
				}
				if (flag)
				{
					((Color)(ref val3))..ctor(0.03f, 0.25f, 0.08f);
				}
				val3.a = 1f;
				val.SetPixel(j, i, val3);
			}
		}
		val.Apply();
		Sprite val4 = Sprite.Create(val, new Rect(0f, 0f, 128f, 128f), new Vector2(0.5f, 0.5f), 100f);
		((Object)val4).name = "LP_GemIcon";
		Object.DontDestroyOnLoad((Object)(object)val);
		Object.DontDestroyOnLoad((Object)(object)val4);
		return val4;
	}

	private static Mesh Cube()
	{
		if ((Object)(object)_cube != (Object)null)
		{
			return _cube;
		}
		GameObject val = GameObject.CreatePrimitive((PrimitiveType)3);
		_cube = val.GetComponent<MeshFilter>().sharedMesh;
		Object.Destroy((Object)(object)val);
		return _cube;
	}

	internal static Transform Box(Transform parent, string name, Vector3 pos, Vector3 size, Color c, float metal = 0f, float smooth = 0.25f, Vector3 euler = default(Vector3))
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Expected O, but got Unknown
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: 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)
		GameObject val = new GameObject(name);
		val.transform.SetParent(parent, false);
		val.transform.localPosition = pos;
		val.transform.localEulerAngles = euler;
		val.transform.localScale = size;
		val.AddComponent<MeshFilter>().sharedMesh = Cube();
		MeshRenderer val2 = val.AddComponent<MeshRenderer>();
		((Renderer)val2).sharedMaterial = Mat(c, metal, smooth, Color.black);
		return val.transform;
	}

	private static Transform Pivot(Transform parent, string name, Vector3 pos)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		Transform transform = new GameObject(name).transform;
		transform.SetParent(parent, false);
		transform.localPosition = pos;
		return transform;
	}

	internal static Body BuildPeddler(Transform root)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: 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_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_013a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_015d: 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_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: 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_01e4: 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_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)
		//IL_0232: Unknown result type (might be due to invalid IL or missing references)
		//IL_0238: Unknown result type (might be due to invalid IL or missing references)
		//IL_025a: Unknown result type (might be due to invalid IL or missing references)
		//IL_026e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0273: Unknown result type (might be due to invalid IL or missing references)
		//IL_0284: Unknown result type (might be due to invalid IL or missing references)
		//IL_028a: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0312: Unknown result type (might be due to invalid IL or missing references)
		//IL_0317: Unknown result type (might be due to invalid IL or missing references)
		//IL_0328: Unknown result type (might be due to invalid IL or missing references)
		//IL_032e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0350: Unknown result type (might be due to invalid IL or missing references)
		//IL_0364: Unknown result type (might be due to invalid IL or missing references)
		//IL_0369: Unknown result type (might be due to invalid IL or missing references)
		//IL_037a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0380: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_03f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0408: Unknown result type (might be due to invalid IL or missing references)
		//IL_040d: Unknown result type (might be due to invalid IL or missing references)
		//IL_041e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0424: Unknown result type (might be due to invalid IL or missing references)
		//IL_0447: Unknown result type (might be due to invalid IL or missing references)
		//IL_0470: Unknown result type (might be due to invalid IL or missing references)
		//IL_0484: Unknown result type (might be due to invalid IL or missing references)
		//IL_0489: Unknown result type (might be due to invalid IL or missing references)
		//IL_049a: Unknown result type (might be due to invalid IL or missing references)
		//IL_04a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04db: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_04f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0514: Unknown result type (might be due to invalid IL or missing references)
		//IL_0528: Unknown result type (might be due to invalid IL or missing references)
		//IL_052d: Unknown result type (might be due to invalid IL or missing references)
		//IL_053e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0544: Unknown result type (might be due to invalid IL or missing references)
		//IL_0566: Unknown result type (might be due to invalid IL or missing references)
		//IL_057a: Unknown result type (might be due to invalid IL or missing references)
		//IL_057f: Unknown result type (might be due to invalid IL or missing references)
		//IL_059d: Unknown result type (might be due to invalid IL or missing references)
		//IL_05c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_05d6: 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_05ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_05f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0614: Unknown result type (might be due to invalid IL or missing references)
		//IL_0628: Unknown result type (might be due to invalid IL or missing references)
		//IL_062d: Unknown result type (might be due to invalid IL or missing references)
		//IL_063e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0644: Unknown result type (might be due to invalid IL or missing references)
		//IL_0666: Unknown result type (might be due to invalid IL or missing references)
		//IL_067a: Unknown result type (might be due to invalid IL or missing references)
		//IL_068e: Unknown result type (might be due to invalid IL or missing references)
		//IL_069f: Unknown result type (might be due to invalid IL or missing references)
		//IL_06a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_06c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_06f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0740: Unknown result type (might be due to invalid IL or missing references)
		//IL_0754: Unknown result type (might be due to invalid IL or missing references)
		//IL_0759: Unknown result type (might be due to invalid IL or missing references)
		//IL_076a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0770: Unknown result type (might be due to invalid IL or missing references)
		//IL_078e: Unknown result type (might be due to invalid IL or missing references)
		//IL_07a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_07a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_07b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_07be: Unknown result type (might be due to invalid IL or missing references)
		//IL_07dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_07f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_07f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0806: Unknown result type (might be due to invalid IL or missing references)
		//IL_080c: Unknown result type (might be due to invalid IL or missing references)
		//IL_083f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0853: Unknown result type (might be due to invalid IL or missing references)
		//IL_0858: Unknown result type (might be due to invalid IL or missing references)
		//IL_0869: 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)
		//IL_0892: Unknown result type (might be due to invalid IL or missing references)
		//IL_08bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_08cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_08d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_08e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_08eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_090d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0921: Unknown result type (might be due to invalid IL or missing references)
		//IL_0926: Unknown result type (might be due to invalid IL or missing references)
		//IL_0937: Unknown result type (might be due to invalid IL or missing references)
		//IL_093d: Unknown result type (might be due to invalid IL or missing references)
		//IL_095f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0973: Unknown result type (might be due to invalid IL or missing references)
		//IL_0978: Unknown result type (might be due to invalid IL or missing references)
		//IL_0989: Unknown result type (might be due to invalid IL or missing references)
		//IL_098f: Unknown result type (might be due to invalid IL or missing references)
		//IL_09b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_09c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_09ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_09db: Unknown result type (might be due to invalid IL or missing references)
		//IL_09e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ab0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ac4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ac9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ada: Unknown result type (might be due to invalid IL or missing references)
		//IL_0ae0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b02: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b16: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b1b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b39: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b5e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b72: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b77: Unknown result type (might be due to invalid IL or missing references)
		//IL_0b95: Unknown result type (might be due to invalid IL or missing references)
		//IL_0bba: Unknown result type (might be due to invalid IL or missing references)
		//IL_0bce: Unknown result type (might be due to invalid IL or missing references)
		//IL_0bd3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0be4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0bea: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c03: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c12: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c1c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c43: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c4a: Expected O, but got Unknown
		//IL_0c73: Unknown result type (might be due to invalid IL or missing references)
		//IL_0c9c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a50: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a64: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a69: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a7a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0a80: Unknown result type (might be due to invalid IL or missing references)
		Body body = new Body();
		body.Root = root;
		Body body2 = body;
		body2.Hips = Pivot(root, "Hips", new Vector3(0f, 1.02f, 0f));
		body2.LegL = Pivot(body2.Hips, "LegL", new Vector3(-0.13f, 0f, 0f));
		body2.LegR = Pivot(body2.Hips, "LegR", new Vector3(0.13f, 0f, 0f));
		Transform[] array = (Transform[])(object)new Transform[2] { body2.LegL, body2.LegR };
		foreach (Transform parent in array)
		{
			Box(parent, "Shin", new Vector3(0f, -0.5f, 0f), new Vector3(0.1f, 1f, 0.1f), Skin);
			Box(parent, "Wrap", new Vector3(0f, -0.22f, 0f), new Vector3(0.13f, 0.3f, 0.13f), Canvas);
			Box(parent, "Boot", new Vector3(0f, -0.98f, 0.05f), new Vector3(0.15f, 0.08f, 0.24f), Strap);
		}
		body2.Torso = Pivot(body2.Hips, "Torso", new Vector3(0f, 0.02f, 0f));
		body2.Torso.localEulerAngles = new Vector3(18f, 0f, 0f);
		Box(body2.Torso, "Belly", new Vector3(0f, 0.28f, 0f), new Vector3(0.44f, 0.56f, 0.3f), Canvas);
		Box(body2.Torso, "Poncho", new Vector3(0f, 0.5f, 0.01f), new Vector3(0.62f, 0.2f, 0.4f), Patch);
		Box(body2.Torso, "PatchA", new Vector3(0.14f, 0.2f, 0.155f), new Vector3(0.12f, 0.12f, 0.01f), Patch);
		Box(body2.Torso, "Belt", new Vector3(0f, 0.04f, 0f), new Vector3(0.47f, 0.06f, 0.33f), Strap);
		Box(body2.Torso, "Pouch", new Vector3(-0.17f, 0.02f, 0.15f), new Vector3(0.1f, 0.12f, 0.06f), WoodDark);
		Box(body2.Torso, "StrapL", new Vector3(-0.15f, 0.36f, 0.16f), new Vector3(0.05f, 0.42f, 0.02f), Strap);
		Box(body2.Torso, "StrapR", new Vector3(0.15f, 0.36f, 0.16f), new Vector3(0.05f, 0.42f, 0.02f), Strap);
		body2.Pack = Pivot(body2.Torso, "Pack", new Vector3(0f, 0.36f, -0.34f));
		Box(body2.Pack, "CrateBig", new Vector3(0f, 0f, 0f), new Vector3(0.56f, 0.52f, 0.36f), Wood);
		Box(body2.Pack, "CrateSlatA", new Vector3(0f, 0.12f, -0.185f), new Vector3(0.58f, 0.05f, 0.02f), WoodDark);
		Box(body2.Pack, "CrateSlatB", new Vector3(0f, -0.12f, -0.185f), new Vector3(0.58f, 0.05f, 0.02f), WoodDark);
		Box(body2.Pack, "CrateSmall", new Vector3(0.06f, 0.4f, 0.02f), new Vector3(0.36f, 0.28f, 0.28f), WoodDark, 0f, 0.2f, new Vector3(0f, 12f, 0f));
		Box(body2.Pack, "Bedroll", new Vector3(0f, -0.33f, 0f), new Vector3(0.62f, 0.14f, 0.16f), Patch);
		Box(body2.Pack, "Pot", new Vector3(-0.33f, 0.05f, 0f), new Vector3(0.12f, 0.16f, 0.12f), Iron, 0.7f, 0.4f);
		Box(body2.Pack, "Bell", new Vector3(0.33f, 0.18f, 0f), new Vector3(0.08f, 0.1f, 0.08f), new Color(0.55f, 0.42f, 0.18f), 0.8f, 0.6f);
		body2.ArmL = Pivot(body2.Torso, "ArmL", new Vector3(-0.3f, 0.5f, 0f));
		body2.ArmR = Pivot(body2.Torso, "ArmR", new Vector3(0.3f, 0.5f, 0f));
		Transform[] array2 = (Transform[])(object)new Transform[2] { body2.ArmL, body2.ArmR };
		foreach (Transform parent2 in array2)
		{
			Box(parent2, "Sleeve", new Vector3(0f, -0.18f, 0f), new Vector3(0.13f, 0.36f, 0.13f), Canvas);
			Box(parent2, "Forearm", new Vector3(0f, -0.52f, 0f), new Vector3(0.08f, 0.36f, 0.08f), Skin);
			Box(parent2, "Hand", new Vector3(0f, -0.74f, 0.01f), new Vector3(0.11f, 0.1f, 0.08f), Strap);
		}
		Box(body2.Torso, "Neck", new Vector3(0f, 0.66f, 0.04f), new Vector3(0.08f, 0.14f, 0.08f), Skin);
		body2.Head = Pivot(body2.Torso, "Head", new Vector3(0f, 0.74f, 0.06f));
		Box(body2.Head, "Base", new Vector3(0f, 0.02f, 0f), new Vector3(0.3f, 0.05f, 0.3f), Rust, 0.6f, 0.3f);
		Box(body2.Head, "Cap", new Vector3(0f, 0.36f, 0f), new Vector3(0.32f, 0.05f, 0.32f), Rust, 0.6f, 0.3f);
		Box(body2.Head, "Roof", new Vector3(0f, 0.42f, 0f), new Vector3(0.2f, 0.07f, 0.2f), Iron, 0.7f, 0.35f);
		Box(body2.Head, "Ring", new Vector3(0f, 0.5f, 0f), new Vector3(0.03f, 0.1f, 0.12f), Iron, 0.7f, 0.35f);
		for (int k = 0; k < 4; k++)
		{
			float num = ((k % 2 != 0) ? 1f : (-1f)) * 0.13f;
			float num2 = ((k >= 2) ? 1f : (-1f)) * 0.13f;
			Box(body2.Head, "Post" + k, new Vector3(num, 0.19f, num2), new Vector3(0.035f, 0.3f, 0.035f), Iron, 0.7f, 0.35f);
		}
		Box(body2.Head, "BarF", new Vector3(0f, 0.19f, 0.13f), new Vector3(0.02f, 0.3f, 0.02f), Iron, 0.7f, 0.35f);
		Box(body2.Head, "BarS", new Vector3(0.13f, 0.19f, 0f), new Vector3(0.02f, 0.3f, 0.02f), Iron, 0.7f, 0.35f, new Vector3(0f, 0f, 8f));
		Box(body2.Head, "Crack", new Vector3(-0.05f, 0.25f, 0.125f), new Vector3(0.1f, 0.012f, 0.005f), Iron, 0f, 0.1f, new Vector3(0f, 0f, 35f));
		Transform val = Box(body2.Head, "Flame", new Vector3(0f, 0.18f, 0f), new Vector3(0.14f, 0.2f, 0.14f), CoreGlow, 0f, 0.6f);
		body2.CoreMat = Mat(new Color(1f, 0.7f, 0.35f), 0f, 0.6f, CoreGlow * 2f, shared: false);
		((Renderer)((Component)val).GetComponent<MeshRenderer>()).sharedMaterial = body2.CoreMat;
		GameObject val2 = new GameObject("Lamp");
		val2.transform.SetParent(body2.Head, false);
		val2.transform.localPosition = new Vector3(0f, 0.18f, 0f);
		body2.Lamp = val2.AddComponent<Light>();
		body2.Lamp.type = (LightType)2;
		body2.Lamp.color = CoreGlow;
		body2.Lamp.shadows = (LightShadows)0;
		body2.Lamp.range = 7f;
		body2.Lamp.intensity = 1.5f;
		try
		{
			HDAdditionalLightData val3 = val2.AddComponent<HDAdditionalLightData>();
			val3.intensity = 1200f;
			val3.range = 7f;
		}
		catch
		{
		}
		Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
		foreach (Transform val4 in componentsInChildren)
		{
			((Component)val4).gameObject.layer = 6;
		}
		return body2;
	}

	private static AudioClip Make(string name, float seconds, Func<float, float> f)
	{
		int num = Mathf.CeilToInt(seconds * 44100f);
		float[] array = new float[num];
		for (int i = 0; i < num; i++)
		{
			array[i] = Mathf.Clamp(f((float)i / 44100f), -1f, 1f);
		}
		AudioClip val = AudioClip.Create(name, num, 1, 44100, false);
		val.SetData(array, 0);
		Object.DontDestroyOnLoad((Object)(object)val);
		return val;
	}

	private static float Bell(float t, float f, float decay)
	{
		float num = Mathf.Exp((0f - t) * decay) * Mathf.Clamp01(t * 400f);
		return num * (0.6f * Mathf.Sin((float)Math.PI * 2f * f * t) + 0.25f * Mathf.Sin((float)Math.PI * 2f * f * 2.76f * t) + 0.12f * Mathf.Sin((float)Math.PI * 2f * f * 5.4f * t));
	}

	internal static AudioClip Chime()
	{
		return _chime ?? (_chime = Make("LP_Chime", 1.6f, (float t) => 0.5f * Bell(t, 1174f, 3.2f)));
	}

	internal static AudioClip Buy()
	{
		return _buy ?? (_buy = Make("LP_Buy", 1.4f, (float t) => 0.42f * (Bell(t, 880f, 4f) + ((!(t > 0.12f)) ? 0f : Bell(t - 0.12f, 1318f, 3.5f)))));
	}

	internal static AudioClip Leave()
	{
		return _leave ?? (_leave = Make("LP_Leave", 1.6f, (float t) => 0.42f * (Bell(t, 1318f, 4f) + ((!(t > 0.15f)) ? 0f : Bell(t - 0.15f, 988f, 3.5f)) + ((!(t > 0.3f)) ? 0f : Bell(t - 0.3f, 740f, 3f)))));
	}

	internal static AudioClip Knock()
	{
		Random random = new Random(7);
		float[] noise = new float[4096];
		for (int i = 0; i < noise.Length; i++)
		{
			noise[i] = (float)random.NextDouble() * 2f - 1f;
		}
		return _knock ?? (_knock = Make("LP_Knock", 0.25f, (float t) => Mathf.Exp((0f - t) * 38f) * (0.5f * Mathf.Sin((float)Math.PI * 380f * t) + 0.3f * noise[(int)(t * 44100f) % noise.Length] * Mathf.Exp((0f - t) * 90f))));
	}

	internal static AudioClip Mutter(int which)
	{
		which = Mathf.Abs(which) % Mutters.Length;
		if ((Object)(object)Mutters[which] != (Object)null)
		{
			return Mutters[which];
		}
		Random random = new Random(1000 + which * 17);
		int syl = 4 + random.Next(5);
		float[] starts = new float[syl];
		float[] pitch = new float[syl];
		float[] len = new float[syl];
		float num = 0.02f;
		for (int i = 0; i < syl; i++)
		{
			starts[i] = num;
			len[i] = 0.05f + (float)random.NextDouble() * 0.07f;
			pitch[i] = 320f + (float)random.NextDouble() * 420f;
			num += len[i] + 0.03f + (float)random.NextDouble() * 0.06f;
		}
		float seconds = num + 0.15f;
		Mutters[which] = Make("LP_Mutter" + which, seconds, delegate(float t)
		{
			float num2 = 0f;
			for (int j = 0; j < syl; j++)
			{
				float num3 = t - starts[j];
				if (!(num3 < 0f) && !(num3 > len[j]))
				{
					float num4 = Mathf.Sin((float)Math.PI * num3 / len[j]);
					float num5 = pitch[j] * (1f + 0.25f * (num3 / len[j]) * ((j % 2 != 0) ? (-1f) : 1f));
					float num6 = Mathf.Sign(Mathf.Sin((float)Math.PI * 2f * num5 * num3)) * 0.25f + 0.5f * Mathf.Sin((float)Math.PI * 2f * num5 * 2f * num3);
					num2 += num4 * num6 * 0.35f;
					if (num3 < 0.006f)
					{
						num2 += 0.4f * (1f - num3 / 0.006f);
					}
				}
			}
			return num2;
		});
		return Mutters[which];
	}
}
internal static class Bundle
{
	private static AssetBundle _bundle;

	private static readonly Dictionary<string, GameObject> Prefabs = new Dictionary<string, GameObject>();

	private static readonly Dictionary<string, AudioClip> Clips = new Dictionary<string, AudioClip>();

	private static bool _tried;

	private static void Load()
	{
		if (_tried)
		{
			return;
		}
		_tried = true;
		try
		{
			string directoryName = Path.GetDirectoryName(typeof(Plugin).Assembly.Location);
			string text = Path.Combine(directoryName, "lanternpeddler");
			if (!File.Exists(text))
			{
				Plugin.Log.LogWarning((object)("Asset bundle not found (" + text + "); using the built-in models and sounds."));
				return;
			}
			_bundle = AssetBundle.LoadFromFile(text);
			if ((Object)(object)_bundle == (Object)null)
			{
				Plugin.Log.LogWarning((object)"Asset bundle failed to load; using the built-in models and sounds.");
				return;
			}
			string[] allAssetNames = _bundle.GetAllAssetNames();
			foreach (string text2 in allAssetNames)
			{
				string key = Path.GetFileNameWithoutExtension(text2).ToLowerInvariant();
				if (text2.EndsWith(".prefab"))
				{
					Prefabs[key] = _bundle.LoadAsset<GameObject>(text2);
				}
				else if (text2.EndsWith(".wav") || text2.EndsWith(".ogg"))
				{
					Clips[key] = _bundle.LoadAsset<AudioClip>(text2);
				}
			}
			Plugin.Log.LogInfo((object)("Asset bundle loaded: " + Prefabs.Count + " models, " + Clips.Count + " sounds."));
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Asset bundle error: " + ex.Message));
		}
	}

	internal static AudioClip Clip(string name)
	{
		Load();
		AudioClip value;
		return (!Clips.TryGetValue(name, out value)) ? null : value;
	}

	internal static GameObject Spawn(string name, Transform parent)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		Load();
		if (!Prefabs.TryGetValue(name, out var value) || (Object)(object)value == (Object)null)
		{
			return null;
		}
		GameObject val = Object.Instantiate<GameObject>(value, parent, false);
		((Object)val).name = "Model";
		val.transform.localPosition = Vector3.zero;
		val.transform.localRotation = Quaternion.identity;
		Renderer[] componentsInChildren = val.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val2 in componentsInChildren)
		{
			Material[] sharedMaterials = val2.sharedMaterials;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				sharedMaterials[j] = Art.FromProxy(sharedMaterials[j]);
			}
			val2.sharedMaterials = sharedMaterials;
		}
		Transform[] componentsInChildren2 = val.GetComponentsInChildren<Transform>(true);
		foreach (Transform val3 in componentsInChildren2)
		{
			((Component)val3).gameObject.layer = 6;
		}
		return val;
	}

	internal static GameObject SpawnPeddler(Transform parent)
	{
		return Spawn("lanternpeddler", parent);
	}

	internal static Material GlowMat(Transform model, string rendererName, out Color baseEmissive)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: 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_004d: 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)
		baseEmissive = Color.black;
		MeshRenderer[] componentsInChildren = ((Component)model).GetComponentsInChildren<MeshRenderer>(true);
		foreach (MeshRenderer val in componentsInChildren)
		{
			if (((Object)val).name == rendererName)
			{
				Material material = ((Renderer)val).material;
				baseEmissive = ((!material.HasProperty("_EmissiveColor")) ? Color.black : material.GetColor("_EmissiveColor"));
				return material;
			}
		}
		return null;
	}
}
internal static class Combat
{
	private static readonly Dictionary<EnemyAI, Peddler> Hunters = new Dictionary<EnemyAI, Peddler>();

	private static bool _patched;

	private static readonly HashSet<string> Passive = new HashSet<string>
	{
		"DocileLocustBeesAI", "DoublewingAI", "FlowerSnakeEnemy", "RedLocustBees", "ButlerBeesEnemyAI", "ButlerEnemyAI", "PufferAI", "DressGirlAI", "CentipedeAI", "HoarderBugAI",
		"LassoManAI", "HerobrineAI", "GiantKiwiAI"
	};

	private static readonly HashSet<string> Deadly = new HashSet<string> { "ForestGiantAI", "RadMechAI", "SandWormAI", "JesterAI", "CaveDwellerAI" };

	internal static bool Hostile(EnemyAI e)
	{
		if ((Object)(object)e == (Object)null || e.isEnemyDead || (Object)(object)e.enemyType == (Object)null || !((Component)e).gameObject.activeInHierarchy)
		{
			return false;
		}
		string name = ((object)e).GetType().Name;
		if (Passive.Contains(name))
		{
			return false;
		}
		if (e.enemyType.isDaytimeEnemy)
		{
			return false;
		}
		string text = e.enemyType.enemyName ?? string.Empty;
		if (text.IndexOf("herobrine", StringComparison.OrdinalIgnoreCase) >= 0)
		{
			return false;
		}
		if (name == "JesterAI" && e.currentBehaviourStateIndex != 2)
		{
			return false;
		}
		return true;
	}

	internal static float Reach(EnemyAI e)
	{
		switch (((object)e).GetType().Name)
		{
		case "ForestGiantAI":
		case "RadMechAI":
			return 4f;
		case "SandWormAI":
			return 6f;
		case "MouthDogAI":
			return 2.4f;
		default:
			return 1.9f;
		}
	}

	internal static int Bite(EnemyAI e)
	{
		if (Deadly.Contains(((object)e).GetType().Name))
		{
			return 9999;
		}
		float num = ((!((Object)(object)e.enemyType != (Object)null)) ? 1f : e.enemyType.PowerLevel);
		return Mathf.Clamp(Mathf.RoundToInt(12f + num * 14f), 10, 70);
	}

	internal static void Hunt(EnemyAI e, Peddler p)
	{
		Hunters[e] = p;
	}

	internal static void Unhunt(EnemyAI e, Peddler p)
	{
		if ((Object)(object)e != (Object)null && Hunters.TryGetValue(e, out var value) && (Object)(object)value == (Object)(object)p)
		{
			Hunters.Remove(e);
		}
	}

	internal static void Forget(Peddler p)
	{
		List<EnemyAI> list = new List<EnemyAI>();
		foreach (KeyValuePair<EnemyAI, Peddler> hunter in Hunters)
		{
			if ((Object)(object)hunter.Value == (Object)(object)p || (Object)(object)hunter.Value == (Object)null || (Object)(object)hunter.Key == (Object)null)
			{
				list.Add(hunter.Key);
			}
		}
		foreach (EnemyAI item in list)
		{
			Hunters.Remove(item);
		}
	}

	internal static void Clear()
	{
		Hunters.Clear();
	}

	internal static void EnsurePatched()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		if (_patched)
		{
			return;
		}
		_patched = true;
		Harmony val = new Harmony("grayl.lanternpeddler.combat");
		HarmonyMethod val2 = new HarmonyMethod(typeof(Combat), "AfterAI", (Type[])null);
		val2.priority = 0;
		HarmonyMethod val3 = val2;
		int num = 0;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			Type[] types;
			try
			{
				types = assembly.GetTypes();
			}
			catch (ReflectionTypeLoadException ex)
			{
				types = ex.Types;
			}
			catch
			{
				continue;
			}
			if (types == null)
			{
				continue;
			}
			Type[] array = types;
			foreach (Type type in array)
			{
				if (type == null || (type != typeof(EnemyAI) && !type.IsSubclassOf(typeof(EnemyAI))))
				{
					continue;
				}
				MethodInfo method;
				try
				{
					method = type.GetMethod("DoAIInterval", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
				}
				catch
				{
					continue;
				}
				if (!(method == null) && !method.IsAbstract)
				{
					try
					{
						MethodInfo methodInfo = method;
						val2 = val3;
						val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						num++;
					}
					catch (Exception ex2)
					{
						Plugin.Log.LogDebug((object)("Could not hook " + type.Name + ": " + ex2.Message));
					}
				}
			}
		}
		Plugin.Log.LogInfo((object)("Peddler combat: " + num + " kinds of creature can hunt him."));
	}

	private static void AfterAI(EnemyAI __instance)
	{
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if ((Object)(object)__instance == (Object)null || !Hunters.TryGetValue(__instance, out var value))
			{
				return;
			}
			if ((Object)(object)value == (Object)null || value.Dead || value.Leaving || !Plugin.Fights.Value)
			{
				Hunters.Remove(__instance);
			}
			else if (((NetworkBehaviour)__instance).IsOwner && !__instance.isEnemyDead && !(__instance.stunNormalizedTimer > 0f))
			{
				if (__instance.currentSearch != null && __instance.currentSearch.inProgress)
				{
					__instance.StopSearch(__instance.currentSearch, false);
				}
				__instance.movingTowardsTargetPlayer = false;
				__instance.moveTowardsDestination = true;
				__instance.SetDestinationToPosition(((Component)value).transform.position, false);
				if ((Object)(object)__instance.agent != (Object)null && ((Behaviour)__instance.agent).enabled && __instance.agent.isOnNavMesh)
				{
					__instance.agent.SetDestination(((Component)value).transform.position);
				}
			}
		}
		catch
		{
		}
	}
}
internal static class Icons
{
	private static readonly Dictionary<char, Color32> Pal = new Dictionary<char, Color32>
	{
		{
			'K',
			new Color32((byte)40, (byte)36, (byte)34, byte.MaxValue)
		},
		{
			'k',
			new Color32((byte)70, (byte)58, (byte)50, byte.MaxValue)
		},
		{
			'R',
			new Color32((byte)120, (byte)66, (byte)38, byte.MaxValue)
		},
		{
			'Y',
			new Color32(byte.MaxValue, (byte)204, (byte)84, byte.MaxValue)
		},
		{
			'O',
			new Color32(byte.MaxValue, (byte)138, (byte)40, byte.MaxValue)
		},
		{
			'L',
			new Color32(byte.MaxValue, (byte)246, (byte)214, byte.MaxValue)
		},
		{
			'G',
			new Color32((byte)38, (byte)200, (byte)86, byte.MaxValue)
		},
		{
			'g',
			new Color32((byte)14, (byte)96, (byte)40, byte.MaxValue)
		},
		{
			'W',
			new Color32((byte)176, (byte)132, (byte)78, byte.MaxValue)
		},
		{
			'w',
			new Color32((byte)104, (byte)72, (byte)40, byte.MaxValue)
		},
		{
			'B',
			new Color32((byte)214, (byte)170, (byte)64, byte.MaxValue)
		},
		{
			'b',
			new Color32((byte)140, (byte)100, (byte)32, byte.MaxValue)
		},
		{
			'C',
			new Color32((byte)160, (byte)112, (byte)60, byte.MaxValue)
		},
		{
			'U',
			new Color32((byte)196, (byte)150, (byte)48, byte.MaxValue)
		},
		{
			'D',
			new Color32((byte)74, (byte)52, (byte)30, byte.MaxValue)
		},
		{
			'S',
			new Color32((byte)150, (byte)150, (byte)150, byte.MaxValue)
		},
		{
			's',
			new Color32((byte)90, (byte)90, (byte)92, byte.MaxValue)
		},
		{
			'r',
			new Color32((byte)200, (byte)50, (byte)40, byte.MaxValue)
		},
		{
			'P',
			new Color32((byte)150, (byte)90, (byte)230, byte.MaxValue)
		},
		{
			'M',
			new Color32((byte)176, (byte)226, (byte)194, byte.MaxValue)
		},
		{
			'm',
			new Color32((byte)92, (byte)140, (byte)110, byte.MaxValue)
		}
	};

	private static readonly Dictionary<string, Texture2D> Cache = new Dictionary<string, Texture2D>();

	internal static readonly string[] Lantern = new string[12]
	{
		"......KKKK......", ".....K....K.....", "......KKKK......", "....RRRRRRRR....", "....K.YYYY.K....", "....K.YLLY.K....", "....KYLLLLYK....", "....KYOLLOYK....", "....K.OYYO.K....", "....K.OOOO.K....",
		"....RRRRRRRR....", "...RRRRRRRRRR..."
	};

	internal static readonly string[] Bell = new string[12]
	{
		".......ww.......", ".......ww.......", "......bBBb......", ".....bBBBBb.....", ".....BBLBBB.....", "....bBLBBBBb....", "....BBLBBBBB....", "....BBBBBBBB....", "...bBBBBBBBBb...", "..bBBBBBBBBBBb..",
		"..bbbbbbbbbbbb..", ".......bb......."
	};

	internal static readonly string[] Charm = new string[11]
	{
		"......kkkk......", ".....k....k.....", ".....k....k.....", "......k..k......", ".......BB.......", "......BGGB......", ".....BGLGGB.....", "....BGLGGGGB....", ".....BGGGGB.....", "......BGGB......",
		".......BB......."
	};

	internal static readonly string[] Mystery = new string[13]
	{
		"......wwww......", ".......ww.......", "......WWWW......", ".....WWWWWW.....", "....WWWLLWWW....", "...WWWLWWLWWW...", "...WWWWWWLWWW...", "...WWWWWLWWWW...", "...WWWWLWWWWW...", "...WWWWWWWWWW...",
		"...WWWWLWWWWW...", "....WWWWWWWW....", ".....wwwwww....."
	};

	internal static readonly string[] Crate = new string[14]
	{
		"....LLLLLLLL....", "...LLLLLLLLLL...", "..LL..L..L..LL..", "..k..k....k..k..", "...k..k..k..k...", "....k.k..k.k....", ".....kCCCCk.....", "....CCCCCCCC....", "....CwCCCCwC....", "....CCwCCwCC....",
		"....CCCwwCCC....", "....CCwCCwCC....", "....CwCCCCwC....", "....CCCCCCCC...."
	};

	internal static readonly string[] Bug = new string[12]
	{
		".....D....D.....", "......D..D......", ".....UUUUUU.....", "....UUKUUKUU....", "....UUUUUUUU....", "...DUUUUUUUUD...", "..D.UUUUUUUU.D..", "...DUUUUUUUUD...", "..D.UUUUUUUU.D..", "....UUUUUUUU....",
		".....UUUUUU.....", "......UUUU......"
	};

	internal static readonly string[] Bulb = new string[12]
	{
		".....LLLLLL.....", "....LYYYYYYL....", "...LYYLLYYYYL...", "...LYLYYYYYYL...", "...LYYYYYYYYL...", "...LYYYYYYYYL...", "....LYYYYYYL....", ".....LYYYYL.....", "......SSSS......", "......ssss......",
		"......SSSS......", ".......ss......."
	};

	internal static readonly string[] Lucky = new string[12]
	{
		"..L..........L..", ".LLL........LLL.", "..L....gg....L..", "......gLGg......", ".....gLGGGg.....", "....gLGGGGGg....", ".....gGGGGg.....", "......gGGg......", ".......gg.......", "..L.........L...",
		".LLL.......LLL..", "..L.........L..."
	};

	internal static readonly string[] Dowser = new string[12]
	{
		"...w........w...", "...w........w...", "....w......w....", ".....w....w.....", "......w..w......", ".......ww.......", ".......ww.......", ".......ww.......", "......gGGg......", ".....gGLGGg.....",
		"......gGGg......", ".......gg......."
	};

	internal static readonly string[] Hourglass = new string[11]
	{
		"...bBBBBBBBb....", "....w......w....", "....wYYYYYYw....", ".....wYYYYw.....", "......wYYw......", ".......YY.......", "......w..w......", ".....w.YY.w.....", "....w.YYYY.w....", "....wYYYYYYw....",
		"...bBBBBBBBBb..."
	};

	internal static readonly string[] Rift = new string[10] { "......kkkk......", ".....k....k.....", "......k..k......", ".......BB.......", "......BPPB......", ".....BPLPPB.....", "....BPLPPPPB....", ".....BPPPPB.....", "......BPPB......", ".......BB......." };

	internal static readonly string[] Stew = new string[8] { ".....L..L.......", "......L..L......", ".....L..L.......", "..ssssssssssss..", "..sOOrOOOOrOOs..", "...sOOOOrOOOs...", "...ssssssssss...", "....ssssssss...." };

	internal static readonly string[] Seed = new string[9] { ".......G........", "......GgG.GG....", ".......g.Gg.....", ".......gg.......", "......DDDD......", ".....DwwwwD.....", ".....DwWwwD.....", ".....DwwwwD.....", "......DDDD......" };

	internal static readonly string[] Scroll = new string[9] { "..wWWWWWWWWWw...", "..WLLLLLLLLLW...", "...LKKLKKKLL....", "...LLLLLLLLL....", "...LKKKLKKLL....", "...LLLLLLLLL....", "...LKKLKKKKL....", "..WLLLLLLLLLW...", "..wWWWWWWWWWw..." };

	internal static readonly string[] Gem = new string[7] { "....gggg....", "...gLGGGg...", "..gLGGGGGg..", "..gGGGGGGg..", "...gGGGGg...", "....gGGg....", ".....gg....." };

	internal static readonly string[] FakeGem = new string[7] { "....mmmm....", "...mLMMMm...", "..mLMMMMMm..", "..BBBBBBBB..", "...mMMMMm...", "....mMMm....", ".....mm....." };

	internal static readonly string[] Flask = new string[11]
	{
		"....WW....", "....WW....", "...BBBB...", "..CCCCCC..", ".CCCCCCCC.", ".DDDDDDDD.", ".DDDOODDD.", ".DDDOODDD.", ".CCCCCCCC.", ".CCCCCCCC.",
		"..BBBBBB.."
	};

	internal static readonly string[] Compass = new string[9] { "....BB....", "...BbbB...", "..BLLLLB..", ".BLLLOLLB.", ".BLLOOLLB.", ".BLLsLLLB.", ".BLsLLLLB.", "..BLLLLB..", "...BBBB..." };

	internal static readonly string[] Pouch = new string[9] { "....kk....", "...wWWw...", "....ww....", "..WWWWWW..", ".WWWBBWWW.", ".WWBYYBWW.", ".WWBYYBWW.", ".WWWBBWWW.", "..WWWWWW.." };

	internal static readonly string[] Moon = new string[8] { "...YYY....", "..YYK.....", ".YYK....L.", ".YY....LL.", ".YY.....L.", ".YYK......", "..YYKK....", "...YYYY..." };

	internal static readonly string[] Key = new string[9] { "..BBB.....", ".B...B....", ".B...B....", "..BBB.....", "...B......", "...B......", "...BBB....", "...B......", "...BB....." };

	internal static readonly string[] Sparkle = new string[7] { "....L.....", "....L.....", "...LYL....", "LLLYYYLLL.", "...LYL....", "....L.....", "....L....." };

	internal static readonly string[] Clock = new string[8] { "..BBBBB...", ".BLLLLLB..", "BLLLKLLLB.", "BLLLKLLLB.", "BLLLKKKLB.", "BLLLLLLLB.", ".BLLLLLB..", "..BBBBB..." };

	internal static readonly string[] Geode = new string[7] { "...ssss...", "..sSSSSs..", ".sSgGGgSs.", ".sSGLGGSs.", ".sSgGGgSs.", "..sSSSSs..", "...ssss..." };

	internal static readonly string[] Refresh = new string[8] { "...OOOO...", "..O....OO.", ".O....OOO.", ".O........", "........O.", ".OOO....O.", ".OO....O..", "...OOOO..." };

	internal static readonly string[] Torch = new string[10] { "....OY....", "...OYYO...", "...OYYO...", "....OO....", "....WW....", "....ww....", "....ww....", "....ww....", "....DD....", "....DD...." };

	internal static readonly string[] Arrow = new string[7] { ".....K....", ".....KK...", "KKKKKKKK..", "KKKKKKKKK.", "KKKKKKKK..", ".....KK...", ".....K...." };

	internal static Texture2D Tex(string name, string[] map)
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		if (Cache.TryGetValue(name, out var value) && (Object)(object)value != (Object)null)
		{
			return value;
		}
		int num = map.Length;
		int num2 = 0;
		foreach (string text in map)
		{
			num2 = Mathf.Max(num2, text.Length);
		}
		int num3 = Mathf.Max(num2, num);
		Texture2D val = new Texture2D(num3, num3, (TextureFormat)4, false);
		((Texture)val).filterMode = (FilterMode)0;
		((Texture)val).wrapMode = (TextureWrapMode)1;
		value = val;
		Color32[] array = (Color32[])(object)new Color32[num3 * num3];
		int num4 = (num3 - num2) / 2;
		int num5 = (num3 - num) / 2;
		for (int j = 0; j < num; j++)
		{
			for (int k = 0; k < map[j].Length; k++)
			{
				if (Pal.TryGetValue(map[j][k], out var value2))
				{
					array[(num3 - 1 - (j + num5)) * num3 + k + num4] = value2;
				}
			}
		}
		value.SetPixels32(array);
		value.Apply();
		((Object)value).hideFlags = (HideFlags)61;
		Cache[name] = value;
		return value;
	}

	internal static Sprite Sprite(string name, string[] map)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = Tex(name, map);
		Sprite val2 = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 16f);
		((Object)val2).name = "LP_" + name;
		Object.DontDestroyOnLoad((Object)(object)val2);
		return val2;
	}
}
internal static class Net
{
	private const string MsgBuy = "LP_Buy";

	private const string MsgHit = "LP_Hit";

	private const string MsgFx = "LP_Fx";

	private const string MsgStock = "LP_Stock";

	private const string MsgTip = "LP_Tip";

	private const string MsgGild = "LP_Gild";

	private const string MsgGive = "LP_Give";

	private const string MsgBack = "LP_Back";

	private const string MsgHand = "LP_Hand";

	private const string MsgRoll = "LP_Roll";

	private const string MsgSync = "LP_Sync";

	private const string MsgRekindle = "LP_Rekindle";

	private const string MsgRise = "LP_Rise";

	private const string MsgHelm = "LP_Helm";

	internal const int FxBuy = 1;

	internal const int FxLeave = 2;

	internal const int FxNo = 3;

	internal const int FxGive = 4;

	internal const int FxSwing = 5;

	internal const int FxHurt = 6;

	internal const int FxDie = 7;

	private static CustomMessagingManager _registered;

	private static bool IsHost => (Object)(object)NetworkManager.Singleton != (Object)null && NetworkManager.Singleton.IsServer;

	internal static ulong Me => (!((Object)(object)NetworkManager.Singleton != (Object)null)) ? 0 : NetworkManager.Singleton.LocalClientId;

	internal static void Tick()
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Expected O, but got Unknown
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Expected O, but got Unknown
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Expected O, but got Unknown
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Expected O, but got Unknown
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Expected O, but got Unknown
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Expected O, but got Unknown
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Expected O, but got Unknown
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Expected O, but got Unknown
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Expected O, but got Unknown
		//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Expected O, but got Unknown
		//IL_021b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0225: Expected O, but got Unknown
		//IL_0247: Unknown result type (might be due to invalid IL or missing references)
		//IL_0251: Expected O, but got Unknown
		//IL_0273: Unknown result type (might be due to invalid IL or missing references)
		//IL_027d: Expected O, but got Unknown
		//IL_029f: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a9: Expected O, but got Unknown
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || singleton.CustomMessagingManager == null || !singleton.IsListening)
		{
			_registered = null;
		}
		else if (_registered != singleton.CustomMessagingManager)
		{
			_registered = singleton.CustomMessagingManager;
			_registered.RegisterNamedMessageHandler("LP_Buy", new HandleNamedMessageDelegate(OnBuy));
			_registered.RegisterNamedMessageHandler("LP_Hit", new HandleNamedMessageDelegate(OnHit));
			_registered.RegisterNamedMessageHandler("LP_Fx", new HandleNamedMessageDelegate(OnFx));
			_registered.RegisterNamedMessageHandler("LP_Stock", new HandleNamedMessageDelegate(OnStock));
			_registered.RegisterNamedMessageHandler("LP_Tip", new HandleNamedMessageDelegate(OnTip));
			_registered.RegisterNamedMessageHandler("LP_Gild", new HandleNamedMessageDelegate(OnGild));
			_registered.RegisterNamedMessageHandler("LP_Give", new HandleNamedMessageDelegate(OnGive));
			_registered.RegisterNamedMessageHandler("LP_Back", new HandleNamedMessageDelegate(OnBack));
			_registered.RegisterNamedMessageHandler("LP_Hand", new HandleNamedMessageDelegate(OnHand));
			_registered.RegisterNamedMessageHandler("LP_Roll", new HandleNamedMessageDelegate(OnRoll));
			_registered.RegisterNamedMessageHandler("LP_Sync", new HandleNamedMessageDelegate(OnSync));
			_registered.RegisterNamedMessageHandler("LP_Rekindle", new HandleNamedMessageDelegate(OnRekindle));
			_registered.RegisterNamedMessageHandler("LP_Rise", new HandleNamedMessageDelegate(OnRise));
			_registered.RegisterNamedMessageHandler("LP_Helm", new HandleNamedMessageDelegate(OnHelm));
		}
	}

	internal static void SendBuy(Peddler p, int offer)
	{
		if (IsHost)
		{
			p.Deliver(offer, Me);
			return;
		}
		Send("LP_Buy", 0uL, delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
			w.Write(offer);
		});
	}

	internal static void SendGive(Peddler p, bool fake)
	{
		if (IsHost)
		{
			p.Receive(Me, fake);
			return;
		}
		Send("LP_Give", 0uL, delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
			w.Write(fake);
		});
	}

	internal static void SendBack(Peddler p)
	{
		if (IsHost)
		{
			p.GiveBack(Me);
			return;
		}
		Send("LP_Back", 0uL, delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
		});
	}

	internal static void SendHand(Peddler p, ulong client, int real, int fake)
	{
		p.SetHand(client, real, fake);
		ToAll("LP_Hand", delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
			w.Write(client);
			w.Write(real);
			w.Write(fake);
		});
	}

	internal static void SendRoll(Peddler p, int roll)
	{
		p.SetRoll(roll);
		ToAll("LP_Roll", delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
			w.Write(roll);
		});
	}

	internal static void SendSyncRequest(Peddler p)
	{
		if (!IsHost && !((Object)(object)p == (Object)null))
		{
			Send("LP_Sync", 0uL, delegate(BinaryWriter w)
			{
				w.Write(((NetworkBehaviour)p).NetworkObjectId);
			});
		}
	}

	private static void OnSync(ulong sender, FastBufferReader r)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader == null)
			{
				return;
			}
			Peddler peddler = Find(binaryReader.ReadUInt64());
			if ((Object)(object)peddler == (Object)null)
			{
				return;
			}
			ulong id = ((NetworkBehaviour)peddler).NetworkObjectId;
			int roll = peddler.Roll;
			Send("LP_Roll", sender, delegate(BinaryWriter w)
			{
				w.Write(id);
				w.Write(roll);
			});
			List<Offer> offers = peddler.Offers;
			for (int num = 0; num < offers.Count; num++)
			{
				int idx = num;
				int stock = offers[num].Stock;
				Send("LP_Stock", sender, delegate(BinaryWriter w)
				{
					w.Write(id);
					w.Write(idx);
					w.Write(stock);
				});
			}
			foreach (KeyValuePair<ulong, int[]> hand in peddler.Hands)
			{
				ulong c = hand.Key;
				int a = hand.Value[0];
				int b = hand.Value[1];
				Send("LP_Hand", sender, delegate(BinaryWriter w)
				{
					w.Write(id);
					w.Write(c);
					w.Write(a);
					w.Write(b);
				});
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler sync failed: " + ex.Message));
		}
	}

	internal static void SendRekindle(PeddlerRemains body)
	{
		if (IsHost)
		{
			Rekindle.Begin(body);
			return;
		}
		Send("LP_Rekindle", 0uL, delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)body).NetworkObjectId);
		});
	}

	internal static void SendHelmLit(DarkLantern helm)
	{
		if (IsHost)
		{
			ToAll("LP_Helm", delegate(BinaryWriter w)
			{
				w.Write(((NetworkBehaviour)helm).NetworkObjectId);
			});
		}
		else
		{
			Send("LP_Helm", 0uL, delegate(BinaryWriter w)
			{
				w.Write(((NetworkBehaviour)helm).NetworkObjectId);
			});
		}
	}

	private static void OnHelm(ulong sender, FastBufferReader r)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			BinaryReader binaryReader = Read(r);
			if (binaryReader == null)
			{
				return;
			}
			ulong id = binaryReader.ReadUInt64();
			if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(id, out var value) && (Object)(object)value != (Object)null)
			{
				DarkLantern component = ((Component)value).GetComponent<DarkLantern>();
				if ((Object)(object)component != (Object)null)
				{
					component.SetLit(lit: true);
				}
			}
			if (IsHost)
			{
				ToAll("LP_Helm", delegate(BinaryWriter w)
				{
					w.Write(id);
				});
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Helm message failed: " + ex.Message));
		}
	}

	private static void OnRekindle(ulong sender, FastBufferReader r)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				ulong key = binaryReader.ReadUInt64();
				if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(key, out var value) && (Object)(object)value != (Object)null)
				{
					Rekindle.Begin(((Component)value).GetComponent<PeddlerRemains>());
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Rekindle message failed: " + ex.Message));
		}
	}

	internal static void SendRise(Vector3 at)
	{
		//IL_0007: 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_000e: Unknown result type (might be due to invalid IL or missing references)
		Rekindle.Effect(at);
		ToAll("LP_Rise", delegate(BinaryWriter w)
		{
			w.Write(at.x);
			w.Write(at.y);
			w.Write(at.z);
		});
	}

	private static void OnRise(ulong sender, FastBufferReader r)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (sender == 0)
			{
				BinaryReader binaryReader = Read(r);
				if (binaryReader != null)
				{
					Rekindle.Effect(new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()));
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Rekindle effect message failed: " + ex.Message));
		}
	}

	private static void OnGive(ulong sender, FastBufferReader r)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				bool fake = binaryReader.ReadBoolean();
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.Receive(sender, fake);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler give message failed: " + ex.Message));
		}
	}

	private static void OnBack(ulong sender, FastBufferReader r)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.GiveBack(sender);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler give-back message failed: " + ex.Message));
		}
	}

	private static void OnHand(ulong sender, FastBufferReader r)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (sender != 0)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				ulong client = binaryReader.ReadUInt64();
				int real = binaryReader.ReadInt32();
				int fake = binaryReader.ReadInt32();
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.SetHand(client, real, fake);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler hand message failed: " + ex.Message));
		}
	}

	private static void OnRoll(ulong sender, FastBufferReader r)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (sender != 0)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				int roll = binaryReader.ReadInt32();
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.SetRoll(roll);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler repack message failed: " + ex.Message));
		}
	}

	private static void ToAll(string msg, Action<BinaryWriter> body)
	{
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || !singleton.IsServer)
		{
			return;
		}
		foreach (ulong connectedClientsId in singleton.ConnectedClientsIds)
		{
			if (connectedClientsId != singleton.LocalClientId)
			{
				Send(msg, connectedClientsId, body);
			}
		}
	}

	internal static void SendStock(Peddler p, int index, int stock)
	{
		p.SetStock(index, stock);
		ToAll("LP_Stock", delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
			w.Write(index);
			w.Write(stock);
		});
	}

	internal static void SendTip(string header, string body)
	{
		ShowTip(header, body);
		ToAll("LP_Tip", delegate(BinaryWriter w)
		{
			w.Write(header ?? string.Empty);
			w.Write(body ?? string.Empty);
		});
	}

	internal static void SendTipTo(PlayerControllerB p, string header, string body)
	{
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)p == (Object)null || (Object)(object)singleton == (Object)null)
		{
			return;
		}
		if ((Object)(object)p == (Object)(object)Peddler.Local)
		{
			ShowTip(header, body);
			return;
		}
		Send("LP_Tip", p.actualClientId, delegate(BinaryWriter w)
		{
			w.Write(header ?? string.Empty);
			w.Write(body ?? string.Empty);
		});
	}

	internal static void SendGild(GrabbableObject g)
	{
		if (IsHost)
		{
			Gilded.Apply(g, Peddler.Local);
			return;
		}
		Send("LP_Gild", 0uL, delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)g).NetworkObjectId);
		});
	}

	private static void OnGild(ulong sender, FastBufferReader r)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader == null)
			{
				return;
			}
			ulong key = binaryReader.ReadUInt64();
			PlayerControllerBLookup(sender, out var idx);
			if (idx >= 0)
			{
				PlayerControllerB val = StartOfRound.Instance.allPlayerScripts[idx];
				if (Gilded.Active(val) && NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(key, out var value) && !((Object)(object)value == (Object)null))
				{
					Gilded.Apply(((Component)value).GetComponent<GrabbableObject>(), val);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Gild message failed: " + ex.Message));
		}
	}

	private static void ShowTip(string header, string body)
	{
		try
		{
			if ((Object)(object)HUDManager.Instance != (Object)null)
			{
				HUDManager.Instance.DisplayTip(header, body, false, false, "LC_Tip1");
			}
		}
		catch
		{
		}
	}

	private static void OnStock(ulong sender, FastBufferReader r)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (sender != 0)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				int index = binaryReader.ReadInt32();
				int stock = binaryReader.ReadInt32();
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.SetStock(index, stock);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler stock message failed: " + ex.Message));
		}
	}

	private static void OnTip(ulong sender, FastBufferReader r)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (sender == 0)
			{
				BinaryReader binaryReader = Read(r);
				if (binaryReader != null)
				{
					ShowTip(binaryReader.ReadString(), binaryReader.ReadString());
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler tip message failed: " + ex.Message));
		}
	}

	internal static void SendHit(Peddler p)
	{
		Send("LP_Hit", 0uL, delegate(BinaryWriter w)
		{
			w.Write(((NetworkBehaviour)p).NetworkObjectId);
		});
	}

	internal static void SendFx(Peddler p, int fx)
	{
		p.OnFx(fx);
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || !singleton.IsServer)
		{
			return;
		}
		foreach (ulong connectedClientsId in singleton.ConnectedClientsIds)
		{
			if (connectedClientsId != singleton.LocalClientId)
			{
				Send("LP_Fx", connectedClientsId, delegate(BinaryWriter w)
				{
					w.Write(((NetworkBehaviour)p).NetworkObjectId);
					w.Write(fx);
				});
			}
		}
	}

	private static Peddler Find(ulong id)
	{
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || singleton.SpawnManager == null || !singleton.SpawnManager.SpawnedObjects.TryGetValue(id, out var value) || (Object)(object)value == (Object)null)
		{
			return null;
		}
		return ((Component)value).GetComponent<Peddler>();
	}

	private static void OnBuy(ulong sender, FastBufferReader r)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				int offerIndex = binaryReader.ReadInt32();
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.Deliver(offerIndex, sender);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler buy message failed: " + ex.Message));
		}
	}

	private static void OnHit(ulong sender, FastBufferReader r)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!IsHost)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				PlayerControllerBLookup(sender, out var idx);
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.OnHit((idx < 0) ? null : StartOfRound.Instance.allPlayerScripts[idx]);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler hit message failed: " + ex.Message));
		}
	}

	private static void PlayerControllerBLookup(ulong client, out int idx)
	{
		idx = -1;
		if (!((Object)(object)StartOfRound.Instance == (Object)null) && StartOfRound.Instance.ClientPlayerList != null && !StartOfRound.Instance.ClientPlayerList.TryGetValue(client, out idx))
		{
			idx = -1;
		}
	}

	private static void OnFx(ulong sender, FastBufferReader r)
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (sender != 0)
			{
				return;
			}
			BinaryReader binaryReader = Read(r);
			if (binaryReader != null)
			{
				Peddler peddler = Find(binaryReader.ReadUInt64());
				int fx = binaryReader.ReadInt32();
				if ((Object)(object)peddler != (Object)null)
				{
					peddler.OnFx(fx);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler effect message failed: " + ex.Message));
		}
	}

	private unsafe static void Send(string name, ulong to, Action<BinaryWriter> body)
	{
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		NetworkManager singleton = NetworkManager.Singleton;
		if ((Object)(object)singleton == (Object)null || singleton.CustomMessagingManager == null)
		{
			return;
		}
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter obj = new BinaryWriter(memoryStream);
		body(obj);
		byte[] array = memoryStream.ToArray();
		FastBufferWriter val = default(FastBufferWriter);
		((FastBufferWriter)(ref val))..ctor(array.Length + 8, (Allocator)2, -1);
		try
		{
			((FastBufferWriter)(ref val)).WriteBytesSafe(BitConverter.GetBytes(array.Length), 4, 0);
			((FastBufferWriter)(ref val)).WriteBytesSafe(array, array.Length, 0);
			singleton.CustomMessagingManager.SendNamedMessage(name, to, val, (NetworkDelivery)3);
		}
		finally
		{
			((IDisposable)(*(FastBufferWriter*)(&val))/*cast due to .constrained prefix*/).Dispose();
		}
	}

	private static BinaryReader Read(FastBufferReader r)
	{
		byte[] value = new byte[4];
		((FastBufferReader)(ref r)).ReadBytesSafe(ref value, 4, 0);
		int num = BitConverter.ToInt32(value, 0);
		if (num < 0 || num > 4096)
		{
			return null;
		}
		byte[] buffer = new byte[num];
		((FastBufferReader)(ref r)).ReadBytesSafe(ref buffer, num, 0);
		return new BinaryReader(new MemoryStream(buffer));
	}
}
internal static class Patches
{
	private static bool _today;

	private static bool _decided;

	private static bool _peddlerDay;

	internal static void Apply(Harmony h)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Expected O, but got Unknown
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Expected O, but got Unknown
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Expected O, but got Unknown
		TradeMenu.Ensure();
		Type typeFromHandle = typeof(Patches);
		MethodInfo methodInfo = AccessTools.Method(typeof(RoundManager), "SpawnScrapInLevel", (Type[])null, (Type[])null);
		if (methodInfo != null)
		{
			HarmonyMethod val = new HarmonyMethod(typeFromHandle, "BeforeScrapSpawn", (Type[])null);
			val.priority = 0;
			HarmonyMethod obj = val;
			val = new HarmonyMethod(typeFromHandle, "AfterScrapSpawn", (Type[])null);
			val.priority = 0;
			h.Patch((MethodBase)methodInfo, obj, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		Post(h, AccessTools.Method(typeof(RoundManager), "DespawnPropsAtEndOfRound", (Type[])null, (Type[])null), "EndOfRound");
		Post(h, AccessTools.Method(typeof(RoundManager), "FinishGeneratingNewLevelClientRpc", (Type[])null, (Type[])null), "LevelReady");
		Post(h, AccessTools.Method(typeof(StartOfRound), "Awake", (Type[])null, (Type[])null), "NewLobby");
		Post(h, AccessTools.Method(typeof(GrabbableObject), "GrabItemOnClient", (Type[])null, (Type[])null), "Grabbed");
		MethodInfo methodInfo2 = AccessTools.Method(typeof(HUDManager), "SubmitChat_performed", (Type[])null, (Type[])null);
		if (methodInfo2 != null)
		{
			h.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeFromHandle, "ChatCommand", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		string[] array = new string[12]
		{
			"ActivateItem_performed", "ActivateItem_canceled", "Interact_performed", "ItemSecondaryUse_performed", "ItemTertiaryUse_performed", "SwitchItem_performed", "Discard_performed", "Jump_performed", "Crouch_performed", "OpenMenu_performed",
			"InspectItem_performed", "QEItemInteract_performed"
		};
		foreach (string text in array)
		{
			MethodInfo methodInfo3 = AccessTools.Method(typeof(PlayerControllerB), text, (Type[])null, (Type[])null);
			if (!(methodInfo3 == null))
			{
				try
				{
					h.Patch((MethodBase)methodInfo3, new HarmonyMethod(typeFromHandle, "BlockWhileTrading", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				catch (Exception ex)
				{
					Plugin.Log.LogWarning((object)("Could not hook " + text + ": " + ex.Message));
				}
			}
		}
	}

	private static void Post(Harmony h, MethodInfo target, string name)
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Expected O, but got Unknown
		if (target == null)
		{
			Plugin.Log.LogWarning((object)("Peddler: missing game method for " + name));
			return;
		}
		try
		{
			HarmonyMethod val = new HarmonyMethod(typeof(Patches), name, (Type[])null);
			h.Patch((MethodBase)target, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler: could not hook " + target.Name + ": " + ex.Message));
		}
	}

	private static bool BlockWhileTrading(PlayerControllerB __instance)
	{
		return !TradeMenu.IsOpenFor(__instance);
	}

	private static void NewLobby()
	{
		_decided = false;
		_today = false;
		Plugin.ResetPool();
		Plugin.CopyHandling();
		if (Plugin.Fights.Value)
		{
			try
			{
				Combat.EnsurePatched();
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Peddler combat setup: " + ex.Message));
			}
		}
	}

	private static void Decide()
	{
		if (!_decided)
		{
			_decided = true;
			_today = Random.Range(0, 100) < Mathf.Clamp(Plugin.SpawnChance.Value, 0, 100);
			_peddlerDay = _today;
			string text = ((!((Object)(object)RoundManager.Instance != (Object)null) || !((Object)(object)RoundManager.Instance.currentLevel != (Object)null)) ? "?" : RoundManager.Instance.currentLevel.PlanetName);
			Plugin.Log.LogInfo((object)("Peddler roll on " + text + ": " + ((!_today) ? "not today." : "he's out today.")));
		}
	}

	private static void BeforeScrapSpawn(RoundManager __instance)
	{
		//IL_0171: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Expected O, but got Unknown
		try
		{
			if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
			{
				return;
			}
			Decide();
			SelectableLevel currentLevel = __instance.currentLevel;
			if ((Object)(object)currentLevel == (Object)null || currentLevel.spawnableScrap == null)
			{
				return;
			}
			int num = 0;
			foreach (SpawnableItemWithRarity item in currentLevel.spawnableScrap)
			{
				if (item != null && (Object)(object)item.spawnableItem != (Object)(object)Plugin.GemItem)
				{
					num += Mathf.Max(0, item.rarity);
				}
			}
			if (num > 0)
			{
				float num2 = (float)Mathf.Clamp((!_peddlerDay) ? Plugin.GemShare.Value : Plugin.GemSharePeddlerDay.Value, 0, 90) / 100f;
				int num3 = ((!(num2 <= 0f)) ? Mathf.Max(1, Mathf.RoundToInt((float)num * num2 / (1f - num2))) : 0);
				SpawnableItemWithRarity val = currentLevel.spawnableScrap.Find((SpawnableItemWithRarity x) => x != null && (Object)(object)x.spawnableItem == (Object)(object)Plugin.GemItem);
				if (val != null)
				{
					val.rarity = num3;
				}
				else if (num3 > 0)
				{
					currentLevel.spawnableScrap.Add(new SpawnableItemWithRarity(Plugin.GemItem, num3));
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler day setup failed: " + ex.Message));
		}
	}

	private static void TopUpGems(RoundManager rm)
	{
		//IL_00bf: 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_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: Unknown result type (might be due to invalid IL or missing references)
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Unknown result type (might be due to invalid IL or missing references)
		//IL_0196: Unknown result type (might be due to invalid IL or missing references)
		int num = Mathf.Max(0, (!_peddlerDay) ? Plugin.GemMin.Value : Plugin.GemMinPeddlerDay.Value);
		int num2 = 0;
		GreenGem[] array = Object.FindObjectsOfType<GreenGem>();
		foreach (GreenGem greenGem in array)
		{
			if ((Object)(object)greenGem != (Object)null && !((GrabbableObject)greenGem).isInShipRoom && !((GrabbableObject)greenGem).isInElevator && !((GrabbableObject)greenGem).isHeld)
			{
				num2++;
			}
		}
		if (num2 >= num)
		{
			return;
		}
		RandomScrapSpawn[] array2 = Object.FindObjectsOfType<RandomScrapSpawn>();
		List<Vector3> list = new List<Vector3>();
		RandomScrapSpawn[] array3 = array2;
		foreach (RandomScrapSpawn val in array3)
		{
			if ((Object)(object)val != (Object)null)
			{
				list.Add(((Component)val).transform.position);
			}
		}
		if (list.Count == 0)
		{
			GameObject[] array4 = Peddler.Nodes(outside: false);
			foreach (GameObject val2 in array4)
			{
				if ((Object)(object)val2 != (Object)null)
				{
					list.Add(val2.transform.position);
				}
			}
		}
		int num3 = 0;
		NavMeshHit val4 = default(NavMeshHit);
		for (int l = 0; l < 80; l++)
		{
			if (num2 + num3 >= num)
			{
				break;
			}
			if (list.Count <= 0)
			{
				break;
			}
			Vector3 val3 = list[Random.Range(0, list.Count)];
			if (NavMesh.SamplePosition(val3, ref val4, (l >= 40) ? 8f : 3f, -1))
			{
				Peddler.SpawnItem(Plugin.GemItem, ((NavMeshHit)(ref val4)).position + Vector3.up * 0.4f, inFactory: true);
				num3++;
			}
		}
		if (num3 > 0)
		{
			Plugin.Log.LogInfo((object)("Placed " + num3 + " extra Green Gems inside (" + (num2 + num3) + " in total)."));
		}
		else if (num2 < num)
		{
			Plugin.Log.LogWarning((object)"Couldn't find anywhere inside to place Green Gems on this moon.");
		}
	}

	private static void AfterScrapSpawn(RoundManager __instance)
	{
		if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
		{
			return;
		}
		Decide();
		try
		{
			TopUpGems(__instance);
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Gem top-up failed: " + ex.Message));
		}
		try
		{
			if (_today)
			{
				PlacePeddler();
			}
		}
		catch (Exception ex2)
		{
			Plugin.Log.LogWarning((object)("Peddler spawn failed: " + ex2.Message));
		}
	}

	private static void LevelReady(RoundManager __instance)
	{
		try
		{
			if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer || (Object)(object)StartOfRound.Instance == (Object)null || (Object)(object)__instance.currentLevel == (Object)null)
			{
				return;
			}
			if (!_decided)
			{
				if ((Object)(object)__instance.currentLevel != (Object)null && !__instance.currentLevel.spawnEnemiesAndScrap && !Plugin.SpawnNoLoot.Value)
				{
					return;
				}
				Decide();
			}
			if (_today && Peddler.All.Count == 0)
			{
				PlacePeddler();
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler late spawn failed: " + ex.Message));
		}
	}

	private static void PlacePeddler()
	{
		//IL_0085: 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)
		string value = Plugin.Roams.Value;
		bool flag = value == "Outside" || (value == "Either" && Random.value < 0.5f);
		Vector3 pos;
		bool flag2 = TryNodes(flag, out pos);
		if (!flag2 && TryNodes(!flag, out pos))
		{
			flag2 = true;
			flag = !flag;
		}
		if (!flag2 && TryAnchors(out pos))
		{
			flag2 = true;
			flag = true;
		}
		if (flag2)
		{
			GameObject val = Object.Instantiate<GameObject>(Plugin.PeddlerPrefab, pos, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f));
			val.GetComponent<Peddler>().Outdoors = flag;
			val.GetComponent<NetworkObject>().Spawn(true);
			_today = false;
			Plugin.Log.LogInfo((object)("The Peddler is out today, roaming " + ((!flag) ? "inside the facility." : "outside.")));
		}
		else
		{
			Plugin.Log.LogWarning((object)"No walkable ground found for the Peddler on this moon; he stays away today.");
		}
	}

	private static bool TryNodes(bool outside, out Vector3 pos)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		pos = Vector3.zero;
		GameObject[] array = Peddler.Nodes(outside);
		int num = 0;
		GameObject[] array2 = array;
		foreach (GameObject val in array2)
		{
			if ((Object)(object)val != (Object)null)
			{
				num++;
			}
		}
		if (num == 0)
		{
			return false;
		}
		NavMeshHit val3 = default(NavMeshHit);
		for (int j = 0; j < 40; j++)
		{
			GameObject val2 = array[Random.Range(0, array.Length)];
			if (!((Object)(object)val2 == (Object)null) && NavMesh.SamplePosition(val2.transform.position, ref val3, (j >= 20) ? 10f : 3f, -1))
			{
				pos = ((NavMeshHit)(ref val3)).position;
				return true;
			}
		}
		return false;
	}

	private static bool TryAnchors(out Vector3 pos)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: 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)
		pos = Vector3.zero;
		List<Vector3> list = new List<Vector3>();
		EntranceTeleport[] array = Object.FindObjectsOfType<EntranceTeleport>();
		foreach (EntranceTeleport val in array)
		{
			if ((Object)(object)val != (Object)null && val.isEntranceToBuilding)
			{
				list.Add(((Component)val).transform.position);
			}
		}
		StartOfRound instance = StartOfRound.Instance;
		if ((Object)(object)instance != (Object)null && (Object)(object)instance.shipLandingPosition != (Object)null)
		{
			list.Add(instance.shipLandingPosition.position + instance.shipLandingPosition.forward * 12f);
		}
		NavMeshHit val2 = default(NavMeshHit);
		foreach (Vector3 item in list)
		{
			if (NavMesh.SamplePosition(item, ref val2, 20f, -1))
			{
				pos = ((NavMeshHit)(ref val2)).position;
				return true;
			}
		}
		return false;
	}

	private static bool ChatCommand(HUDManager __instance)
	{
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: 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_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (!Plugin.ChatCommand.Value || (Object)(object)__instance == (Object)null || (Object)(object)__instance.chatTextField == (Object)null)
			{
				return true;
			}
			string text = (__instance.chatTextField.text ?? string.Empty).Trim().ToLowerInvariant();
			if (text != "/peddler")
			{
				return true;
			}
			__instance.chatTextField.text = string.Empty;
			PlayerControllerB local = Peddler.Local;
			if ((Object)(object)local != (Object)null)
			{
				local.isTypingChat = false;
				__instance.chatTextField.DeactivateInputField(false);
			}
			if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
			{
				__instance.DisplayTip("Lantern Peddler", "Only the host can summon the Peddler.", false, false, "LC_Tip1");
				return false;
			}
			if ((Object)(object)local == (Object)null || (Object)(object)StartOfRound.Instance == (Object)null || StartOfRound.Instance.inShipPhase)
			{
				__instance.DisplayTip("Lantern Peddler", "Land on a moon first.", false, false, "LC_Tip1");
				return false;
			}
			Vector3 val = ((Component)local).transform.position + ((Component)local).transform.forward * 2.5f;
			NavMeshHit val2 = default(NavMeshHit);
			if (!NavMesh.SamplePosition(val, ref val2, 6f, -1) && !NavMesh.SamplePosition(((Component)local).transform.position, ref val2, 6f, -1))
			{
				__instance.DisplayTip("Lantern Peddler", "No room for him here.", false, false, "LC_Tip1");
				return false;
			}
			Vector3 val3 = ((Component)local).transform.position - ((NavMeshHit)(ref val2)).position;
			val3.y = 0f;
			GameObject val4 = Object.Instantiate<GameObject>(Plugin.PeddlerPrefab, ((NavMeshHit)(ref val2)).position, (!(((Vector3)(ref val3)).sqrMagnitude > 0.01f)) ? Quaternion.identity : Quaternion.LookRotation(val3));
			val4.GetComponent<Peddler>().Outdoors = !local.isInsideFactory;
			val4.GetComponent<NetworkObject>().Spawn(true);
			__instance.DisplayTip("Lantern Peddler", "A lantern flickers to life nearby...", false, false, "LC_Tip1");
			return false;
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Peddler chat command failed: " + ex.Message));
			return true;
		}
	}

	private static void Grabbed(GrabbableObject __instance)
	{
		try
		{
			PlayerControllerB local = Peddler.Local;
			if (!((Object)(object)__instance == (Object)null) && Gilded.Active(local) && !((Object)(object)__instance.itemProperties == (Object)null) && __instance.itemProperties.isScrap)
			{
				Net.SendGild(__instance);
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)("Gild check failed: " + ex.Message));
		}
	}

	private static void EndOfRound()
	{
		_decided = false;
		_today = false;
		Gilded.NewDay();
		Combat.Clear();
		Boost.Clear();
		TradeMenu.Close();
		if ((Object)(object)NetworkManager.Singleton == (Object)null || !NetworkManager.Singleton.IsServer)
		{
			return;
		}
		Peddler[] array = Peddler.All.ToArray();
		foreach (Peddler peddler in array)
		{
			try
			{
				if ((Object)(object)peddler != (Object)null && ((NetworkBehaviour)peddler).IsSpawned)
				{
					((NetworkBehaviour)peddler).NetworkObject.Despawn(true);
				}
			}
			catch
			{
			}
		}
	}
}
internal class Peddler : NetworkBehaviour, IHittable
{
	private enum Mode
	{
		Wander,
		Wait,
		Greet,
		Flee,
		Fight
	}

	private Art.Body _body;

	private AudioSource _audio;

	private NavMeshAgent _agent;

	private HDAdditionalLightData _hdLamp;

	private Light _lampLight;

	private float _lampBase = 900f;

	private Animator _anim;

	private readonly List<Material> _glowMats = new List<Material>();

	private readonly List<Color> _glowBase = new List<Color>();

	private List<Offer> _offers;

	private int _roll;

	private bool _built;

	private readonly Dictionary<ulong, int[]> _hands = new Dictionary<ulong, int[]>();

	private Transform _handBone;

	private Transform _stack;

	private Transform _model;

	private readonly List<Renderer> _feet = new List<Renderer>();

	private float _footOffset;

	private int _shownReal = -1;

	private int _shownFake = -1;

	private Mode _mode = Mode.Wait;

	private float _waitUntil;

	private float _leaveAt;

	private float _nextThreatCheck;

	private float _nextRepath;

	internal bool Leaving;

	internal bool Dead;

	internal bool Revived;

	private int _hp;

	private float _nextScan;

	private float _nextSwing;

	private float _flashUntil;

	private EnemyAI _foe;

	private readonly Dictionary<EnemyAI, float> _nextBite = new Dictionary<EnemyAI, float>();

	internal bool Outdoors;

	private Vector3 _lastPos;

	private float _speed;

	private float _phase;

	private float _nextMutter;

	private float _flicker;

	private bool _stepSide;

	internal static readonly List<Peddler> All = new List<Peddler>();

	private static Sprite _icon;

	private static readonly RaycastHit[] _hits = (RaycastHit[])(object)new RaycastHit[8];

	private AudioSource _flameSrc;

	private bool _greeted;

	private static string _ghostCfg;

	private static readonly HashSet<string> GhostNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

	private float _shyUntil;

	private static bool _inFactory = true;

	internal static PlayerControllerB Local => (!((Object)(object)GameNetworkManager.Instance != (Object)null)) ? null : GameNetworkManager.Instance.localPlayerController;

	internal List<Offer> Offers
	{
		get
		{
			if (_offers == null || _offers.Count == 0)
			{
				_offers = Specials.Pick(((NetworkBehaviour)this).NetworkObjectId, _roll);
			}
			return _offers;
		}
	}

	internal int Roll => _roll;

	internal Dictionary<ulong, int[]> Hands => _hands;

	internal static GameObject[] Nodes(bool outside)
	{
		RoundManager instance = RoundManager.Instance;
		GameObject[] array = ((!((Object)(object)instance != (Object)null)) ? null : ((!outside) ? instance.insideAINodes : instance.outsideAINodes));
		if (array == null || array.Length == 0)
		{
			array = GameObject.FindGameObjectsWithTag((!outside) ? "AINode" : "OutsideAINode");
		}
		return (GameObject[])(((object)array) ?? ((object)new GameObject[0]));
	}

	private void Awake()
	{
		All.Add(this);
	}

	public override void OnDestroy()
	{
		All.Remove(this);
		Combat.Forget(this);
		if ((Object)(object)TradeMenu.Current == (Object)(object)this)
		{
			TradeMenu.Close();
		}
		((NetworkBehaviour)this).OnDestroy();
	}

	public override void OnNetworkSpawn()
	{
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		((NetworkBehaviour)this).OnNetworkSpawn();
		Build();
		if (!((NetworkBehaviour)this).IsServer)
		{
			return;
		}
		_agent = ((Component)this).gameObject.AddComponent<NavMeshAgent>();
		_agent.speed = 1.4f;
		_agent.acceleration = 5f;
		_agent.angularSpeed = 220f;
		_agent.radius = 0.35f;
		_agent.height = 1.9f;
		_agent.stoppingDistance = 0.4f;
		_agent.autoBraking = true;
		_agent.Warp(((Component)this).transform.position);
		_waitUntil = Time.time + 2f;
		_hp = Mathf.Max(1, Plugin.PeddlerHealth.Value);
		if (Plugin.Fights.Value)
		{
			try
			{
				Combat.EnsurePatched();
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Peddler combat setup: " + ex.Message));
			}
		}
	}

	private void Build()
	{
		//IL_0369: Unkno