Decompiled source of ImprovedDvergerCirclet v1.0.7

ImprovedDvergerCirclet.dll

Decompiled 6 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ImprovedDvergerCirclet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Randy Knapp Mods")]
[assembly: AssemblyProduct("ImprovedDvergerCirclet")]
[assembly: AssemblyCopyright("Copyright © Randy Knapp 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cbed8160-747f-4022-98ce-61a8ee201652")]
[assembly: AssemblyFileVersion("1.0.7.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.7.0")]
[module: UnverifiableCode]
public static class PlayerExtensions
{
	public static ZDO GetZDO(this Player player)
	{
		return ((Character)player).m_nview.GetZDO();
	}
}
namespace Common
{
	public class ConfigPositionedElement : MonoBehaviour
	{
		public ConfigEntry<TextAnchor> AnchorConfig;

		public ConfigEntry<Vector2> PositionConfig;

		protected RectTransform _rt;

		protected TextAnchor _currentAnchor;

		public virtual void Awake()
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			_rt = (RectTransform)((Component)this).transform;
			EnsureCorrectPosition();
		}

		public virtual void Update()
		{
			EnsureCorrectPosition();
		}

		public virtual void EnsureCorrectPosition()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Expected I4, but got Unknown
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: 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_0148: 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_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: 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_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d1: 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_0208: 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)
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_0241: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_024b: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_0289: 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_0291: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: 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_02c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			if (AnchorConfig != null && PositionConfig != null && (_currentAnchor != AnchorConfig.Value || !(_rt.anchoredPosition == PositionConfig.Value)))
			{
				_currentAnchor = AnchorConfig.Value;
				TextAnchor currentAnchor = _currentAnchor;
				TextAnchor val = currentAnchor;
				Vector2 val2 = default(Vector2);
				switch ((int)val)
				{
				case 0:
				{
					RectTransform rt25 = _rt;
					RectTransform rt26 = _rt;
					RectTransform rt27 = _rt;
					((Vector2)(ref val2))..ctor(0f, 1f);
					rt27.anchorMax = val2;
					Vector2 pivot = (rt26.anchorMin = val2);
					rt25.pivot = pivot;
					break;
				}
				case 1:
				{
					RectTransform rt22 = _rt;
					RectTransform rt23 = _rt;
					RectTransform rt24 = _rt;
					((Vector2)(ref val2))..ctor(0.5f, 1f);
					rt24.anchorMax = val2;
					Vector2 pivot = (rt23.anchorMin = val2);
					rt22.pivot = pivot;
					break;
				}
				case 2:
				{
					RectTransform rt19 = _rt;
					RectTransform rt20 = _rt;
					RectTransform rt21 = _rt;
					((Vector2)(ref val2))..ctor(1f, 1f);
					rt21.anchorMax = val2;
					Vector2 pivot = (rt20.anchorMin = val2);
					rt19.pivot = pivot;
					break;
				}
				case 3:
				{
					RectTransform rt16 = _rt;
					RectTransform rt17 = _rt;
					RectTransform rt18 = _rt;
					((Vector2)(ref val2))..ctor(0f, 0.5f);
					rt18.anchorMax = val2;
					Vector2 pivot = (rt17.anchorMin = val2);
					rt16.pivot = pivot;
					break;
				}
				case 4:
				{
					RectTransform rt13 = _rt;
					RectTransform rt14 = _rt;
					RectTransform rt15 = _rt;
					((Vector2)(ref val2))..ctor(0.5f, 0.5f);
					rt15.anchorMax = val2;
					Vector2 pivot = (rt14.anchorMin = val2);
					rt13.pivot = pivot;
					break;
				}
				case 5:
				{
					RectTransform rt10 = _rt;
					RectTransform rt11 = _rt;
					RectTransform rt12 = _rt;
					((Vector2)(ref val2))..ctor(1f, 0.5f);
					rt12.anchorMax = val2;
					Vector2 pivot = (rt11.anchorMin = val2);
					rt10.pivot = pivot;
					break;
				}
				case 6:
				{
					RectTransform rt7 = _rt;
					RectTransform rt8 = _rt;
					RectTransform rt9 = _rt;
					((Vector2)(ref val2))..ctor(0f, 0f);
					rt9.anchorMax = val2;
					Vector2 pivot = (rt8.anchorMin = val2);
					rt7.pivot = pivot;
					break;
				}
				case 7:
				{
					RectTransform rt4 = _rt;
					RectTransform rt5 = _rt;
					RectTransform rt6 = _rt;
					((Vector2)(ref val2))..ctor(0.5f, 0f);
					rt6.anchorMax = val2;
					Vector2 pivot = (rt5.anchorMin = val2);
					rt4.pivot = pivot;
					break;
				}
				case 8:
				{
					RectTransform rt = _rt;
					RectTransform rt2 = _rt;
					RectTransform rt3 = _rt;
					((Vector2)(ref val2))..ctor(1f, 0f);
					rt3.anchorMax = val2;
					Vector2 pivot = (rt2.anchorMin = val2);
					rt.pivot = pivot;
					break;
				}
				}
				_rt.anchoredPosition = PositionConfig.Value;
			}
		}
	}
	[Serializable]
	public class RecipeRequirementConfig
	{
		public string item = "";

		public int amount = 1;
	}
	[Serializable]
	public class RecipeConfig
	{
		public string name = "";

		public string item = "";

		public int amount = 1;

		public string craftingStation = "";

		public int minStationLevel = 1;

		public bool enabled = true;

		public string repairStation = "";

		public List<RecipeRequirementConfig> resources = new List<RecipeRequirementConfig>();
	}
	[Serializable]
	public class RecipesConfig
	{
		public List<RecipeConfig> recipes = new List<RecipeConfig>();
	}
	public static class GameObjectExtensions
	{
		public static RectTransform RectTransform(this GameObject go)
		{
			Transform transform = go.transform;
			return (RectTransform)(object)((transform is RectTransform) ? transform : null);
		}

		public static T RequireComponent<T>(this GameObject go) where T : Component
		{
			T component = go.GetComponent<T>();
			return ((Object)(object)component == (Object)null) ? go.AddComponent<T>() : component;
		}
	}
	public class GotDestroyed : MonoBehaviour
	{
		public void OnDisable()
		{
			Debug.LogError((object)("I got destroyed! (" + ((Object)((Component)this).gameObject).name + ")"));
			Debug.Log((object)Environment.StackTrace);
		}
	}
	public class MultiValueDictionary<TKey, TValue> : Dictionary<TKey, List<TValue>>
	{
		public void Add(TKey key, TValue value)
		{
			if (!TryGetValue(key, out var value2))
			{
				value2 = new List<TValue>();
				Add(key, value2);
			}
			value2.Add(value);
		}

		public bool ContainsValue(TKey key, TValue value)
		{
			if (TryGetValue(key, out var value2))
			{
				return value2.Contains(value);
			}
			return false;
		}

		public void Remove(TKey key, TValue value)
		{
			if (TryGetValue(key, out var value2))
			{
				value2.Remove(value);
				if (value2.Count <= 0)
				{
					Remove(key);
				}
			}
		}

		public void Merge(MultiValueDictionary<TKey, TValue> toMergeWith)
		{
			if (toMergeWith == null)
			{
				return;
			}
			foreach (KeyValuePair<TKey, List<TValue>> item in toMergeWith)
			{
				foreach (TValue item2 in item.Value)
				{
					Add(item.Key, item2);
				}
			}
		}

		public List<TValue> GetValues(TKey key, bool returnEmptySet = false)
		{
			if (!TryGetValue(key, out var value) && returnEmptySet)
			{
				return new List<TValue>();
			}
			return value;
		}
	}
	public static class PrefabCreator
	{
		public static ManualLogSource Logger;

		public static Dictionary<string, CraftingStation> CraftingStations;

		public static T RequireComponent<T>(GameObject go) where T : Component
		{
			T val = go.GetComponent<T>();
			if ((Object)(object)val == (Object)null)
			{
				val = go.AddComponent<T>();
			}
			return val;
		}

		public static void Reset()
		{
			CraftingStations = null;
		}

		private static void InitCraftingStations()
		{
			if (CraftingStations != null)
			{
				return;
			}
			CraftingStations = new Dictionary<string, CraftingStation>();
			foreach (Recipe recipe in ObjectDB.instance.m_recipes)
			{
				if ((Object)(object)recipe.m_craftingStation != (Object)null && !CraftingStations.ContainsKey(((Object)recipe.m_craftingStation).name))
				{
					CraftingStations.Add(((Object)recipe.m_craftingStation).name, recipe.m_craftingStation);
				}
			}
		}

		public static Recipe CreateRecipe(string name, string itemId, RecipeConfig recipeConfig)
		{
			//IL_024b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_025d: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Expected O, but got Unknown
			InitCraftingStations();
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(itemId);
			if ((Object)(object)itemPrefab == (Object)null)
			{
				ManualLogSource logger = Logger;
				if (logger != null)
				{
					logger.LogWarning((object)("[PrefabCreator] Could not find item prefab (" + itemId + ")"));
				}
				return null;
			}
			Recipe val = ScriptableObject.CreateInstance<Recipe>();
			((Object)val).name = name;
			val.m_amount = recipeConfig.amount;
			val.m_minStationLevel = recipeConfig.minStationLevel;
			val.m_item = itemPrefab.GetComponent<ItemDrop>();
			val.m_enabled = recipeConfig.enabled;
			if (!string.IsNullOrEmpty(recipeConfig.craftingStation))
			{
				if (!CraftingStations.ContainsKey(recipeConfig.craftingStation))
				{
					ManualLogSource logger2 = Logger;
					if (logger2 != null)
					{
						logger2.LogWarning((object)("[PrefabCreator] Could not find crafting station (" + itemId + "): " + recipeConfig.craftingStation));
					}
					string text = string.Join(", ", CraftingStations.Keys);
					ManualLogSource logger3 = Logger;
					if (logger3 != null)
					{
						logger3.LogInfo((object)("[PrefabCreator] Available Stations: " + text));
					}
				}
				else
				{
					val.m_craftingStation = CraftingStations[recipeConfig.craftingStation];
				}
			}
			if (!string.IsNullOrEmpty(recipeConfig.repairStation))
			{
				if (!CraftingStations.ContainsKey(recipeConfig.repairStation))
				{
					ManualLogSource logger4 = Logger;
					if (logger4 != null)
					{
						logger4.LogWarning((object)("[PrefabCreator] Could not find repair station (" + itemId + "): " + recipeConfig.repairStation));
					}
					string text2 = string.Join(", ", CraftingStations.Keys);
					ManualLogSource logger5 = Logger;
					if (logger5 != null)
					{
						logger5.LogInfo((object)("[PrefabCreator] Available Stations: " + text2));
					}
				}
				else
				{
					val.m_repairStation = CraftingStations[recipeConfig.repairStation];
				}
			}
			List<Requirement> list = new List<Requirement>();
			foreach (RecipeRequirementConfig resource in recipeConfig.resources)
			{
				GameObject itemPrefab2 = ObjectDB.instance.GetItemPrefab(resource.item);
				if ((Object)(object)itemPrefab2 == (Object)null)
				{
					ManualLogSource logger6 = Logger;
					if (logger6 != null)
					{
						logger6.LogError((object)("[PrefabCreator] Could not find requirement item (" + itemId + "): " + resource.item));
					}
				}
				else
				{
					list.Add(new Requirement
					{
						m_amount = resource.amount,
						m_resItem = itemPrefab2.GetComponent<ItemDrop>()
					});
				}
			}
			val.m_resources = list.ToArray();
			return val;
		}

		public static Recipe AddNewRecipe(string name, string itemId, RecipeConfig recipeConfig)
		{
			Recipe val = CreateRecipe(name, itemId, recipeConfig);
			if ((Object)(object)val == (Object)null)
			{
				ManualLogSource logger = Logger;
				if (logger != null)
				{
					logger.LogError((object)("[PrefabCreator] Failed to create recipe (" + name + ")"));
				}
				return null;
			}
			return AddNewRecipe(val);
		}

		public static Recipe AddNewRecipe(Recipe recipe)
		{
			int num = ObjectDB.instance.m_recipes.RemoveAll((Recipe x) => ((Object)x).name == ((Object)recipe).name);
			if (num > 0)
			{
				ManualLogSource logger = Logger;
				if (logger != null)
				{
					logger.LogInfo((object)$"[PrefabCreator] Removed recipe ({((Object)recipe).name}): {num}");
				}
			}
			ObjectDB.instance.m_recipes.Add(recipe);
			ManualLogSource logger2 = Logger;
			if (logger2 != null)
			{
				logger2.LogInfo((object)("[PrefabCreator] Added recipe: " + ((Object)recipe).name));
			}
			return recipe;
		}
	}
	[Serializable]
	public struct SerializableVector3
	{
		public float x;

		public float y;

		public float z;

		public SerializableVector3(float x, float y, float z)
		{
			this.x = x;
			this.y = y;
			this.z = z;
		}

		public void ToPackage(ZPackage pkg)
		{
			pkg.Write(x);
			pkg.Write(y);
			pkg.Write(z);
		}

		public static SerializableVector3 FromPackage(ZPackage pkg)
		{
			SerializableVector3 result = default(SerializableVector3);
			result.x = pkg.ReadSingle();
			result.y = pkg.ReadSingle();
			result.z = pkg.ReadSingle();
			return result;
		}

		public override string ToString()
		{
			return $"[{x}, {y}, {z}]";
		}

		public static implicit operator Vector3(SerializableVector3 s)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(s.x, s.y, s.z);
		}

		public static implicit operator SerializableVector3(Vector3 v)
		{
			//IL_0000: 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_000c: Unknown result type (might be due to invalid IL or missing references)
			return new SerializableVector3(v.x, v.y, v.z);
		}

		public static SerializableVector3 operator +(SerializableVector3 a, SerializableVector3 b)
		{
			return new SerializableVector3(a.x + b.x, a.y + b.y, a.z + b.z);
		}

		public static SerializableVector3 operator -(SerializableVector3 a, SerializableVector3 b)
		{
			return new SerializableVector3(a.x - b.x, a.y - b.y, a.z - b.z);
		}

		public static SerializableVector3 operator -(SerializableVector3 a)
		{
			return new SerializableVector3(0f - a.x, 0f - a.y, 0f - a.z);
		}

		public static SerializableVector3 operator *(SerializableVector3 a, float m)
		{
			return new SerializableVector3(a.x * m, a.y * m, a.z * m);
		}

		public static SerializableVector3 operator *(float m, SerializableVector3 a)
		{
			return new SerializableVector3(a.x * m, a.y * m, a.z * m);
		}

		public static SerializableVector3 operator /(SerializableVector3 a, float d)
		{
			return new SerializableVector3(a.x / d, a.y / d, a.z / d);
		}
	}
	public static class Utils
	{
		public static void PrintObject(object o)
		{
			if (o == null)
			{
				Debug.Log((object)"null");
			}
			else
			{
				Debug.Log((object)(o?.ToString() + ":\n" + GetObjectString(o, "  ")));
			}
		}

		public static string GetObjectString(object obj, string indent)
		{
			string text = "";
			Type type = obj.GetType();
			IEnumerable<FieldInfo> enumerable = from f in type.GetFields()
				where f.IsPublic
				select f;
			foreach (FieldInfo item in enumerable)
			{
				object value = item.GetValue(obj);
				string text2 = ((value == null) ? "null" : value.ToString());
				text = text + "\n" + indent + item.Name + ": " + text2;
			}
			return text;
		}

		public static Sprite LoadSpriteFromFile(string spritePath)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: 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)
			spritePath = Path.Combine(Paths.PluginPath, spritePath);
			if (File.Exists(spritePath))
			{
				byte[] array = File.ReadAllBytes(spritePath);
				Texture2D val = new Texture2D(20, 20);
				if (ImageConversion.LoadImage(val, array))
				{
					return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), default(Vector2), 100f);
				}
			}
			return null;
		}

		public static Sprite LoadSpriteFromFile(string modFolder, string iconName)
		{
			string spritePath = Path.Combine(modFolder, iconName);
			return LoadSpriteFromFile(spritePath);
		}

		public static string RemoveBetween(string s, string from, string to)
		{
			int num = 0;
			while (num >= 0)
			{
				num = s.IndexOf(from, StringComparison.InvariantCulture);
				if (num < 0)
				{
					break;
				}
				int num2 = s.IndexOf(to, num, StringComparison.InvariantCulture);
				if (num2 < 0)
				{
					break;
				}
				s = s.Remove(num, num2 - num + to.Length);
			}
			return s;
		}

		public static void CopyFields(object originalObject, object cloneObject, Type typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public)
		{
			FieldInfo[] fields = typeToReflect.GetFields(bindingFlags);
			foreach (FieldInfo fieldInfo in fields)
			{
				object value = fieldInfo.GetValue(originalObject);
				fieldInfo.SetValue(cloneObject, value);
			}
		}

		public static bool IsServer()
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Invalid comparison between Unknown and I4
			return ZNet.instance.IsServer() || ZNet.instance.IsDedicated() || (int)SystemInfo.graphicsDeviceType == 4;
		}
	}
	public static class ArrayUtils
	{
		public static bool IsNullOrEmpty<T>(T[] a)
		{
			return a == null || a.Length == 0;
		}

		public static T[] Copy<T>(T[] other)
		{
			return other?.ToArray();
		}
	}
	public static class ListExtensions
	{
		public static bool TryFind<T>(this List<T> list, Predicate<T> predicate, out T result)
		{
			int num = list.FindIndex(predicate);
			if (num != -1)
			{
				result = list[num];
				return true;
			}
			result = default(T);
			return false;
		}
	}
	public class WeightedRandomCollection<T>
	{
		private readonly Random _random;

		private List<T> _list;

		private Func<T, float> _weightSelector;

		private bool _removeOnSelect;

		public float TotalWeight { get; private set; }

		public WeightedRandomCollection(Random random)
		{
			_random = random;
		}

		public WeightedRandomCollection(Random random, IEnumerable<T> collection, Func<T, float> weightSelector, bool removeOnSelect = false)
		{
			_random = random ?? new Random();
			Setup(collection, weightSelector, removeOnSelect);
		}

		public void Setup(IEnumerable<T> collection, Func<T, float> weightSelector, bool removeOnSelect = false)
		{
			_list = collection.ToList();
			_weightSelector = weightSelector;
			_removeOnSelect = removeOnSelect;
			TotalWeight = _list.Sum(_weightSelector);
		}

		public T Roll()
		{
			if (_list == null)
			{
				return default(T);
			}
			float num = (float)_random.NextDouble() * TotalWeight;
			float num2 = 0f;
			T val = default(T);
			foreach (var item in _list.Select((T weightedItem) => new
			{
				Value = weightedItem,
				Weight = _weightSelector(weightedItem)
			}))
			{
				num2 += item.Weight;
				if (num2 >= num)
				{
					val = item.Value;
					break;
				}
			}
			if (_removeOnSelect)
			{
				_list.Remove(val);
				TotalWeight = _list.Sum(_weightSelector);
			}
			return val;
		}

		public List<T> Roll(int numberOfRolls)
		{
			if (_list == null)
			{
				return null;
			}
			List<T> list = new List<T>();
			for (int i = 0; i < numberOfRolls; i++)
			{
				T x = Roll();
				if (!EqualityComparer<T>.Default.Equals(x, default(T)))
				{
					list.Add(Roll());
				}
			}
			return list;
		}

		public void Reset()
		{
			_list = null;
			_weightSelector = null;
			_removeOnSelect = false;
			TotalWeight = 0f;
		}
	}
}
namespace DvergerColor
{
	[BepInPlugin("randyknapp.mods.dvergercolor", "Dverger Color", "1.0.6")]
	[BepInProcess("valheim.exe")]
	public class DvergerColor : BaseUnityPlugin
	{
		public const string PluginId = "randyknapp.mods.dvergercolor";

		public static ConfigEntry<Color> Color;

		public static ConfigEntry<int> MaxSteps;

		public static ConfigEntry<float> MinAngle;

		public static ConfigEntry<float> MaxAngle;

		public static ConfigEntry<float> MinIntensity;

		public static ConfigEntry<float> MaxIntensity;

		public static ConfigEntry<float> MinRange;

		public static ConfigEntry<float> MaxRange;

		public static ConfigEntry<float> PointIntensity;

		public static ConfigEntry<float> PointRange;

		public static ConfigEntry<string> NarrowBeamHotkey;

		public static ConfigEntry<string> WidenBeamHotkey;

		public static ConfigEntry<string> ToggleHotkey;

		public static ConfigEntry<bool> TurnOffInBed;

		public const string StepDataKey = "randyknapp.mods.dvergercolor.step";

		public const string OnDataKey = "randyknapp.mods.dvergercolor.on";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Color = ((BaseUnityPlugin)this).Config.Bind<Color>("General", "Color", Color.white, "The color of the Dverger light beam.");
			MaxSteps = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Max Steps", 3, "Define how many steps of focus the Dverger light beam has. Must be at least 2.");
			MinAngle = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Min Angle", 30f, "The angle of the beam at the narrowest setting.");
			MaxAngle = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Max Angle", 110f, "The angle of the beam at the widest setting.");
			MinIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Min Intensity", 1.4f, "The intensity of the beam at the widest setting.");
			MaxIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Max Intensity", 2.2f, "The intensity of the beam at the narrowest setting");
			MinRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Min Range", 45f, "The range of the beam at the narrowest setting.");
			MaxRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Max Range", 15f, "The range of the beam at the widest setting");
			PointIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Point Intensity", 1.1f, "The intensity of the Dverger light pool on the point light setting.");
			PointRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Point Range", 10f, "The range of the Dverger light pool on the point light setting.");
			NarrowBeamHotkey = ((BaseUnityPlugin)this).Config.Bind<string>("Hotkeys", "Narrow Beam Hotkey", "[", "Press this button to narrow the Dverger light beam.");
			WidenBeamHotkey = ((BaseUnityPlugin)this).Config.Bind<string>("Hotkeys", "Widen Beam Hotkey", "]", "Press this button to widen the Dverger light beam.");
			ToggleHotkey = ((BaseUnityPlugin)this).Config.Bind<string>("Hotkeys", "Toggle Light On/Off", "\\", "Press this button to turn the light beam on or off.");
			TurnOffInBed = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Turn Off In Bed", true, "Whether the circlet should be turned off when you get into bed.");
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "randyknapp.mods.dvergercolor");
		}
	}
	[RequireComponent(typeof(Light))]
	public class LightChanger : MonoBehaviour
	{
		private int _maxSteps = 3;

		private float _minAngle = 30f;

		private float _maxAngle = 110f;

		private float _minIntensity = 1.4f;

		private float _maxIntensity = 2.2f;

		private float _minRange = 45f;

		private float _maxRange = 15f;

		private float _pointIntensity = 1.1f;

		private float _pointRange = 10f;

		private Light _light;

		private Light[] _lights;

		public int Step;

		public bool On;

		private void Awake()
		{
			_maxSteps = Mathf.Clamp(DvergerColor.MaxSteps.Value, 2, 50);
			_minAngle = Mathf.Clamp(DvergerColor.MinAngle.Value, 1f, 360f);
			_maxAngle = Mathf.Clamp(DvergerColor.MaxAngle.Value, 1f, 360f);
			_minIntensity = Mathf.Clamp(DvergerColor.MaxIntensity.Value, 0f, 10f);
			_maxIntensity = Mathf.Clamp(DvergerColor.MinIntensity.Value, 0f, 10f);
			_minRange = Mathf.Clamp(DvergerColor.MinRange.Value, 0f, 1000f);
			_maxRange = Mathf.Clamp(DvergerColor.MaxRange.Value, 0f, 1000f);
			_pointIntensity = Mathf.Clamp(DvergerColor.PointIntensity.Value, 0f, 10f);
			_pointRange = Mathf.Clamp(DvergerColor.PointRange.Value, 0f, 1000f);
			Step = _maxSteps - 1;
			On = true;
		}

		private void Update()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			ZDO zDO = localPlayer.GetZDO();
			bool flag = false;
			if ((Object)(object)localPlayer != (Object)null && ((Character)localPlayer).TakeInput())
			{
				if (On && Input.GetKeyDown(DvergerColor.NarrowBeamHotkey.Value))
				{
					Step = Mathf.Clamp(Step - 1, 0, _maxSteps);
					flag = true;
				}
				else if (On && Input.GetKeyDown(DvergerColor.WidenBeamHotkey.Value))
				{
					Step = Mathf.Clamp(Step + 1, 0, _maxSteps);
					flag = true;
				}
				else if (Input.GetKeyDown(DvergerColor.ToggleHotkey.Value))
				{
					On = !On;
					flag = true;
				}
			}
			if (flag)
			{
				ShowStatusMessage();
				zDO.Set("randyknapp.mods.dvergercolor.step", Step);
				zDO.Set("randyknapp.mods.dvergercolor.on", On);
			}
			if (Step == _maxSteps)
			{
				_light.type = (LightType)2;
				_light.intensity = _pointIntensity;
				_light.range = _pointRange;
			}
			else
			{
				float num = (float)Step / (float)(_maxSteps - 1);
				_light.type = (LightType)0;
				_light.spotAngle = Mathf.Lerp(_minAngle, _maxAngle, num);
				_light.intensity = Mathf.Lerp(_minIntensity, _maxIntensity, num);
				_light.range = Mathf.Lerp(_minRange, _maxRange, num);
			}
			bool flag2 = ((Character)localPlayer).InBed();
			bool value = DvergerColor.TurnOffInBed.Value;
			Light[] lights = _lights;
			foreach (Light val in lights)
			{
				if (value && flag2)
				{
					((Behaviour)val).enabled = false;
				}
				else
				{
					((Behaviour)val).enabled = On;
				}
			}
		}

		public void SetLights(Light[] lights)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			_lights = lights;
			_light = lights[0];
			if ((Object)(object)_light != (Object)null)
			{
				_light.color = DvergerColor.Color.Value;
			}
			ShowStatusMessage();
			if ((Object)(object)Player.m_localPlayer != (Object)null)
			{
				ZDO zDO = Player.m_localPlayer.GetZDO();
				Step = zDO.GetInt("randyknapp.mods.dvergercolor.step", DvergerColor.MaxSteps.Value - 1);
				On = zDO.GetBool("randyknapp.mods.dvergercolor.on", true);
			}
			else
			{
				Step = DvergerColor.MaxSteps.Value - 1;
				On = true;
			}
		}

		private void ShowStatusMessage()
		{
			if ((Object)(object)MessageHud.instance != (Object)null)
			{
				if (!On)
				{
					MessageHud.instance.ShowMessage((MessageType)1, "Dverger Circlet: Off", 0, (Sprite)null);
				}
				else
				{
					MessageHud.instance.ShowMessage((MessageType)1, (Step == _maxSteps) ? "Dverger Circlet: Pool of Radiance" : $"Dverger Circlet: Luminous Beam {Step + 1}", 0, (Sprite)null);
				}
			}
		}
	}
	[HarmonyPatch(typeof(VisEquipment), "AttachItem")]
	public static class VisEquipment_AttachItem_Patch
	{
		private const int DvergerItemHash = 703889544;

		private static void Postfix(VisEquipment __instance, GameObject __result, int itemHash)
		{
			if (__instance.m_isPlayer && !((Object)(object)__result == (Object)null) && itemHash == 703889544)
			{
				if ((Object)(object)__result.GetComponent<LightChanger>() != (Object)null)
				{
					Object.Destroy((Object)(object)__result.GetComponent<LightChanger>());
				}
				Light[] componentsInChildren = __result.GetComponentsInChildren<Light>();
				if (componentsInChildren.Length != 0)
				{
					__result.AddComponent<LightChanger>().SetLights(componentsInChildren);
				}
			}
		}
	}
}