Decompiled source of ServersideQoL ObliteratorOfPotential v2.4.2

ServersideQoL.ObliteratorOfPotential.dll

Decompiled 16 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using ServersideQoL.Processors;
using ServersideQoL.Utilities;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ArgusMagnus")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("ArgusMagnus")]
[assembly: AssemblyDescription("Gives obliterators the power to upgrade gear by sacrificing other gear and idols")]
[assembly: AssemblyFileVersion("2.4.2.0")]
[assembly: AssemblyInformationalVersion("2.4.2+575c52b7c002629dee571275c9ada72097542c07")]
[assembly: AssemblyProduct("ServersideQoL.ObliteratorOfPotential")]
[assembly: AssemblyTitle("ServersideQoL.ObliteratorOfPotential")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/ArgusMagnus/ValheimServersideQoL")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("2.4.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ServersideQoL.ObliteratorOfPotential
{
	public sealed class Config : ConfigBase<Config>
	{
		public override ConfigEntry<bool> Enabled { get; } = ConfigBase<Config>.BindEx<bool>(cfg, true, "Enables/disables the entire mod", (IAcceptableValueBase<bool>)null, (Deprecated<Config>)null, "Enabled");

		public ConfigEntry<bool> PlaceNearForgeOfPotential { get; } = ConfigBase<Config>.BindEx<bool>(cfg, true, "True to place an Obliterator of Potential near the Forge of Potential", (IAcceptableValueBase<bool>)null, (Deprecated<Config>)null, "PlaceNearForgeOfPotential");

		public ConfigEntry<bool> EveryObliteratorCanUpgrade { get; } = ConfigBase<Config>.BindEx<bool>(cfg, false, "True to give every obliterator the power to upgrade gear, not just the one placed near the Forge of Potential", (IAcceptableValueBase<bool>)null, (Deprecated<Config>)null, "EveryObliteratorCanUpgrade");

		public Config(ConfigFile cfg, Logger logger)
			: base(cfg, logger)
		{
		}
	}
	[Processor("2006ea3b-d05a-49bb-9d59-28b19a4bd7f7")]
	[DependsOn<ContainerRegistryProcessor>]
	public sealed class IncineratorProcessor : Processor<IncineratorProcessor.PrefabInfo>
	{
		public sealed record PrefabInfo : ProcessorPrefabInfo
		{
			[MemberNotNullWhen(true, "Container")]
			public bool IsIncinerator
			{
				[MemberNotNullWhen(true, "Container")]
				get;
				[MemberNotNullWhen(true, "Container")]
				private set;
			}

			public Container? Container { get; private set; }

			public bool IsUpgradeStation { get; private set; }

			public override bool IsValid
			{
				get
				{
					CraftingStation component = ((ProcessorPrefabInfo)this).PrefabInfo.GetComponent<CraftingStation>();
					if (component != null && component.m_upgrader)
					{
						return IsUpgradeStation = true;
					}
					Container val = (Container = ((ProcessorPrefabInfo)this).PrefabInfo.GetComponent<Container>());
					if (val == null)
					{
						return false;
					}
					if (!((ProcessorPrefabInfo)this).PrefabInfo.HasComponent<Incinerator>() || !((ProcessorPrefabInfo)this).PrefabInfo.HasComponent<Piece>() || !((ProcessorPrefabInfo)this).PrefabInfo.HasComponent<PieceTable>())
					{
						return false;
					}
					return IsIncinerator = true;
				}
			}

			[CompilerGenerated]
			public override string ToString()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append("PrefabInfo");
				stringBuilder.Append(" { ");
				if (((ProcessorPrefabInfo)this).PrintMembers(stringBuilder))
				{
					stringBuilder.Append(' ');
				}
				stringBuilder.Append('}');
				return stringBuilder.ToString();
			}

			[CompilerGenerated]
			protected override bool PrintMembers(StringBuilder builder)
			{
				if (((ProcessorPrefabInfo)this).PrintMembers(builder))
				{
					builder.Append(", ");
				}
				builder.Append("IsIncinerator = ");
				builder.Append(IsIncinerator.ToString());
				builder.Append(", Container = ");
				builder.Append(Container);
				builder.Append(", IsUpgradeStation = ");
				builder.Append(IsUpgradeStation.ToString());
				return true;
			}

			[CompilerGenerated]
			public override int GetHashCode()
			{
				return ((((ProcessorPrefabInfo)this).GetHashCode() * -1521134295 + EqualityComparer<bool>.Default.GetHashCode(IsIncinerator)) * -1521134295 + EqualityComparer<Container>.Default.GetHashCode(Container)) * -1521134295 + EqualityComparer<bool>.Default.GetHashCode(IsUpgradeStation);
			}

			[CompilerGenerated]
			public sealed override bool Equals(ProcessorPrefabInfo? other)
			{
				return ((object)this).Equals((object?)other);
			}

			[CompilerGenerated]
			public bool Equals(PrefabInfo? other)
			{
				if ((object)this != other)
				{
					if (((ProcessorPrefabInfo)this).Equals((ProcessorPrefabInfo)(object)other) && EqualityComparer<bool>.Default.Equals(IsIncinerator, other.IsIncinerator) && EqualityComparer<Container>.Default.Equals(Container, other.Container))
					{
						return EqualityComparer<bool>.Default.Equals(IsUpgradeStation, other.IsUpgradeStation);
					}
					return false;
				}
				return true;
			}

			[CompilerGenerated]
			private PrefabInfo(PrefabInfo original)
				: base((ProcessorPrefabInfo)(object)original)
			{
				IsIncinerator = original.IsIncinerator;
				Container = original.Container;
				IsUpgradeStation = original.IsUpgradeStation;
			}

			public PrefabInfo()
			{
			}
		}

		private sealed class State
		{
			public required uint Revision { get; set; }

			public List<ItemData> NewItems { get; } = new List<ItemData>();

			public bool LeverPulled { get; set; }

			public bool LeverReturned { get; set; }

			public ServersideQoLZDO? Sign { get; set; }
		}

		public const string Id = "2006ea3b-d05a-49bb-9d59-28b19a4bd7f7";

		private readonly Dictionary<GameObject, HashSet<GameObject>> _itemsByUpgrader = new Dictionary<GameObject, HashSet<GameObject>>();

		private readonly HashSet<ItemData> _used = new HashSet<ItemData>();

		private readonly Dictionary<ServersideQoLZDO, State> _states = new Dictionary<ServersideQoLZDO, State>();

		private ServersideQoLZDO? _obliteratorOfPotential;

		protected override void Initialize()
		{
			//IL_01a8: 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)
			if (_obliteratorOfPotential == null)
			{
				_obliteratorOfPotential = ((IEnumerable<ServersideQoLZDO>)((Processor)this).PlacedObjects).FirstOrDefault((Func<ServersideQoLZDO, bool>)((ServersideQoLZDO x) => Processor<PrefabInfo>.GetProcessorPrefabInfo(x)?.IsIncinerator ?? false));
			}
			if (_obliteratorOfPotential != null)
			{
				if (ConfigBase<Config>.Instance.PlaceNearForgeOfPotential.Value)
				{
					_obliteratorOfPotential.Fields<Container>().Set((Func<Expression<Func<Container, string>>>)(() => (Container x) => x.m_name), Localization.instance.Localize(Processor<PrefabInfo>.GetProcessorPrefabInfo(_obliteratorOfPotential).Container.m_name) + " of Potential", "C:\\repos\\Valheim.ServersideQoL\\ServersideQoL.ObliteratorOfPotential\\IncineratorProcessor.cs", 48);
				}
				else
				{
					((Processor)this).DestroyObject(_obliteratorOfPotential);
					_obliteratorOfPotential = null;
				}
			}
			if (_itemsByUpgrader.Count == 0)
			{
				foreach (Recipe recipe in ObjectDB.instance.m_recipes)
				{
					if (recipe == null || recipe.m_item == null || recipe.m_resources == null)
					{
						continue;
					}
					Requirement[] resources = recipe.m_resources;
					foreach (Requirement val in resources)
					{
						if (val != null && val.m_resItem != null && val.m_upgraderResource)
						{
							if (!_itemsByUpgrader.TryGetValue(((Component)val.m_resItem).gameObject, out HashSet<GameObject> value))
							{
								_itemsByUpgrader.Add(((Component)val.m_resItem).gameObject, value = new HashSet<GameObject>());
							}
							value.Add(((Component)recipe.m_item).gameObject);
						}
					}
				}
			}
			Intercept.UpdateInterception(Incinerator.AnimateLever, (Delegate)new Action<ServersideQoLZDO>(RPC_AnimateLever), true);
			Intercept.UpdateInterception(Incinerator.AnimateLeverReturn, (Delegate)new Action<ServersideQoLZDO>(RPC_AnimateLeverReturn), true);
		}

		protected override ProcessResult Process(ServersideQoLZDO zdo, IReadOnlyList<Peer> peers, PrefabInfo prefabInfo)
		{
			//IL_002c: 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)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_04bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_04df: Unknown result type (might be due to invalid IL or missing references)
			//IL_04e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0559: Unknown result type (might be due to invalid IL or missing references)
			//IL_055e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0501: Unknown result type (might be due to invalid IL or missing references)
			//IL_050e: Unknown result type (might be due to invalid IL or missing references)
			//IL_05b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_05b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_05bd: 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_068b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: Unknown result type (might be due to invalid IL or missing references)
			//IL_026c: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_037c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0381: Unknown result type (might be due to invalid IL or missing references)
			if (prefabInfo.IsUpgradeStation)
			{
				if (ConfigBase<Config>.Instance.PlaceNearForgeOfPotential.Value && _obliteratorOfPotential == null)
				{
					Vector3 position = zdo.ZDO.GetPosition();
					position += zdo.ZDO.GetRotation() * Vector3.forward * 1f;
					position.y -= 0.25f;
					_obliteratorOfPotential = ((Processor)this).PlacePiece(position, Prefabs.Incinerator, zdo.ZDO.GetRotation(), (CreatorMarkers)2);
					_obliteratorOfPotential.Fields<Container>().Set((Func<Expression<Func<Container, string>>>)(() => (Container x) => x.m_name), Localization.instance.Localize(Processor<PrefabInfo>.GetProcessorPrefabInfo(_obliteratorOfPotential).Container.m_name) + " of Potential", "C:\\repos\\Valheim.ServersideQoL\\ServersideQoL.ObliteratorOfPotential\\IncineratorProcessor.cs", 90);
					return (ProcessResult)2;
				}
				return (ProcessResult)2;
			}
			State value;
			ZDOVars vars;
			if (prefabInfo.IsIncinerator)
			{
				if (zdo != _obliteratorOfPotential && !ConfigBase<Config>.Instance.EveryObliteratorCanUpgrade.Value)
				{
					return (ProcessResult)2;
				}
				if (_states.TryGetValue(zdo, out value) && !value.LeverReturned)
				{
					if (value.LeverPulled)
					{
						vars = zdo.Vars;
						if (!((ZDOVars)(ref vars)).GetInUse(false))
						{
							goto IL_014c;
						}
					}
					value.NewItems.Clear();
				}
				goto IL_014c;
			}
			return (ProcessResult)2;
			IL_014c:
			ContainerState state = Processor.Instance<ContainerRegistryProcessor>().GetState(zdo, prefabInfo.Container);
			IInventory inventory = state.GetInventory();
			foreach (ItemData item in inventory.Items)
			{
				if (!_itemsByUpgrader.TryGetValue(item.m_dropPrefab, out HashSet<GameObject> value2))
				{
					continue;
				}
				for (int num = 0; num < item.m_stack; num++)
				{
					foreach (ItemData item2 in inventory.Items)
					{
						if (item == item2 || !value2.Contains(item2.m_dropPrefab) || _used.Contains(item2))
						{
							continue;
						}
						float armor = item2.GetArmor();
						float baseBlockPower = item2.GetBaseBlockPower();
						DamageTypes damage = item2.GetDamage();
						float totalDamage = ((DamageTypes)(ref damage)).GetTotalDamage();
						if (armor <= 0f && baseBlockPower <= 0f && totalDamage <= 0f)
						{
							continue;
						}
						foreach (ItemData item3 in inventory.Items)
						{
							if (item3 == item || item3 == item2 || item3.m_shared.m_itemType != item2.m_shared.m_itemType)
							{
								continue;
							}
							SharedData shared = item3.m_shared;
							if (shared != null && shared.m_armorPerLevel <= 0f && shared.m_blockPowerPerLevel <= 0f && ((DamageTypes)(ref item3.m_shared.m_damagesPerLevel)).GetTotalDamage() <= 0f)
							{
								continue;
							}
							if (item3.GetArmor() >= armor && item3.GetBaseBlockPower() >= baseBlockPower)
							{
								damage = item3.GetDamage();
								if (((DamageTypes)(ref damage)).GetTotalDamage() >= totalDamage)
								{
									continue;
								}
							}
							if (_used.Contains(item3))
							{
								continue;
							}
							ItemData val = null;
							while ((!(item3.m_shared.m_armorPerLevel > 0f) || !((val ?? (val = item3.Clone())).GetArmor() >= armor)) && (!(item3.m_shared.m_blockPowerPerLevel > 0f) || !((val ?? (val = item3.Clone())).GetBaseBlockPower() >= baseBlockPower)))
							{
								if (((DamageTypes)(ref item3.m_shared.m_damagesPerLevel)).GetTotalDamage() > 0f)
								{
									damage = (val ?? (val = item3.Clone())).GetDamage();
									if (((DamageTypes)(ref damage)).GetTotalDamage() >= totalDamage)
									{
										break;
									}
								}
								if (val == null)
								{
									break;
								}
								ItemData obj = val;
								obj.m_quality++;
							}
							if (val != null && val.m_quality != item3.m_quality)
							{
								_used.Add(item2);
								_used.Add(item3);
								if (value == null)
								{
									Dictionary<ServersideQoLZDO, State> states = _states;
									State obj2 = new State
									{
										Revision = zdo.ZDO.DataRevision
									};
									value = obj2;
									states.Add(zdo, obj2);
									zdo.Destroyed += OnIncineratorDestroyed;
								}
								else if (value.Revision != zdo.ZDO.DataRevision)
								{
									value.Revision = zdo.ZDO.DataRevision;
									value.NewItems.Clear();
								}
								value.NewItems.Add(val);
							}
						}
					}
				}
			}
			ProcessResult result = (ProcessResult)0;
			bool flag = false;
			if (_used.Count != 0)
			{
				if (value.Sign == null)
				{
					Vector3 position2 = zdo.ZDO.GetPosition();
					if (zdo == _obliteratorOfPotential)
					{
						position2.y += 1f;
					}
					value.Sign = ((Processor)this).PlacePiece(position2, Prefabs.Sign, zdo.ZDO.GetRotation());
				}
				string text = string.Join("<br>", value.NewItems.Select((ItemData x) => $"{Localization.instance.Localize(x.m_shared.m_name)} {x.m_quality}"));
				vars = value.Sign.Vars;
				((ZDOVars)(ref vars)).SetText((zdo == _obliteratorOfPotential) ? text : ("<mark=#000000><color=white>" + text + "<size=45><br><br>"), "C:\\repos\\Valheim.ServersideQoL\\ServersideQoL.ObliteratorOfPotential\\IncineratorProcessor.cs", 181);
			}
			else if (value != null && value.LeverReturned)
			{
				if (!zdo.IsOwnerOrUnassigned())
				{
					result = ((Processor)this).ScheduleReprocessing(Processor.Instance<ContainerRegistryProcessor>().RequestOwnership(state, default(PlayerID)));
				}
				else
				{
					foreach (ItemData newItem in value.NewItems)
					{
						inventory.Inventory.AddItem(newItem);
					}
					inventory.Save();
					flag = true;
				}
			}
			else
			{
				int? num2 = value?.NewItems.Count;
				if (num2.HasValue && num2.GetValueOrDefault() == 0)
				{
					flag = true;
				}
			}
			if (flag)
			{
				zdo.Destroyed -= OnIncineratorDestroyed;
				_states.Remove(zdo);
				if (value.Sign != null)
				{
					((Processor)this).DestroyObject(value.Sign);
				}
			}
			_used.Clear();
			return result;
		}

		private void OnIncineratorDestroyed(ServersideQoLZDO zdo)
		{
			if (_states.Remove(zdo, out State value) && value.Sign != null)
			{
				((Processor)this).DestroyObject(value.Sign);
			}
		}

		private void RPC_AnimateLever(ServersideQoLZDO zdo)
		{
			if (_states.TryGetValue(zdo, out State value))
			{
				value.LeverPulled = true;
			}
		}

		private void RPC_AnimateLeverReturn(ServersideQoLZDO zdo)
		{
			if (_states.TryGetValue(zdo, out State value))
			{
				value.LeverReturned = true;
			}
		}
	}
	[BepInPlugin("ArgusMagnus.ServersideQoL.ObliteratorOfPotential", "ServersideQoL.ObliteratorOfPotential", "2.4.2")]
	public sealed class ObliteratorOfPotentialPlugin : ServersideQoLPluginBase<ObliteratorOfPotentialPlugin, Config>
	{
		public const string Author = "ArgusMagnus";

		public const string PluginName = "ServersideQoL.ObliteratorOfPotential";

		public const string PluginGuid = "ArgusMagnus.ServersideQoL.ObliteratorOfPotential";

		public const string PluginVersion = "2.4.2";

		public const string PluginInformationalVersion = "2.4.2";

		private DateTimeOffset BuildTimestamp { get; } = new DateTimeOffset(639283672021686441L, default(TimeSpan));

		protected override Config CreateConfigSingleton(ConfigFile configFile, Logger logger)
		{
			return new Config(configFile, logger);
		}

		protected override void RegisterProcessors(IProcessorCollection processors)
		{
			processors.Add<IncineratorProcessor>();
		}

		private void Awake()
		{
		}
	}
}