Decompiled source of LandingLootToChat v1.8.2

LandingLootToChat.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("LandingLootToChat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LandingLootToChat")]
[assembly: AssemblyTitle("LandingLootToChat")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace LandingLootToChat
{
	[BepInPlugin("yourname.LandingLootToChat", "LandingLootToChat by mrlistvin", "1.8.1")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource? Log;

		internal static MethodInfo? AddTextToChatOnServerMethod;

		internal static MethodInfo? AddChatMessageMethod;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("yourname.LandingLootToChat").PatchAll();
			Type typeFromHandle = typeof(HUDManager);
			AddTextToChatOnServerMethod = typeFromHandle.GetMethod("AddTextToChatOnServer", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[2]
			{
				typeof(string),
				typeof(int)
			}, null);
			AddChatMessageMethod = typeFromHandle.GetMethod("AddChatMessage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[2]
			{
				typeof(string),
				typeof(string)
			}, null);
			ManualLogSource? log = Log;
			if (log != null)
			{
				log.LogInfo((object)"yourname.LandingLootToChat loaded (By mrlistvin).");
			}
		}

		internal static string GetDungeonTypeName(int dungeonType)
		{
			return dungeonType switch
			{
				0 => "Фабрика", 
				1 => "Особняк", 
				4 => "Шахта", 
				_ => "Фабрика", 
			};
		}

		internal static void SendChatMessageToAll(string message)
		{
			try
			{
				HUDManager instance = HUDManager.Instance;
				if ((Object)(object)instance == (Object)null)
				{
					return;
				}
				string text = message?.Trim() ?? string.Empty;
				if (text.Length != 0)
				{
					if (text.Length > 512)
					{
						text = text.Substring(0, 512);
					}
					if (AddTextToChatOnServerMethod != null)
					{
						AddTextToChatOnServerMethod.Invoke(instance, new object[2] { text, -1 });
					}
					else if (AddChatMessageMethod != null)
					{
						AddChatMessageMethod.Invoke(instance, new object[2]
						{
							text,
							string.Empty
						});
						instance.lastChatMessage = "";
					}
				}
			}
			catch (Exception arg)
			{
				ManualLogSource? log = Log;
				if (log != null)
				{
					log.LogError((object)$"[LandingLootToChat] Error sending chat message: {arg}");
				}
			}
		}
	}
	internal static class LootCache
	{
		internal static bool HasData;

		internal static int DungeonType;

		internal static int OneHanded;

		internal static int TwoHanded;

		internal static int TotalValue;

		internal static int HivesCount;

		internal static void Set(int dungeonType, int oneHanded, int twoHanded, int totalValue, int hivesCount)
		{
			DungeonType = dungeonType;
			OneHanded = oneHanded;
			TwoHanded = twoHanded;
			TotalValue = totalValue;
			HivesCount = hivesCount;
			HasData = true;
		}

		internal static void Clear()
		{
			HasData = false;
			DungeonType = 0;
			OneHanded = 0;
			TwoHanded = 0;
			TotalValue = 0;
			HivesCount = 0;
		}
	}
	[HarmonyPatch(typeof(RoundManager), "FinishGeneratingNewLevelClientRpc")]
	internal static class LandingLootPatch
	{
		[CompilerGenerated]
		private sealed class <CountAndAnnounceLoot>d__2 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public RoundManager rm;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <CountAndAnnounceLoot>d__2(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_001d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Expected O, but got Unknown
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<>2__current = (object)new WaitForSeconds(10f);
					<>1__state = 1;
					return true;
				case 1:
				{
					<>1__state = -1;
					CountLootOnce(out var oneHanded, out var twoHanded, out var totalValue, out var hivesCount);
					LootCache.Set(rm.currentDungeonType, oneHanded, twoHanded, totalValue, hivesCount);
					try
					{
						Plugin.SendChatMessageToAll(BuildLootMessage(rm.currentDungeonType, oneHanded, twoHanded, totalValue, hivesCount));
					}
					catch (Exception arg)
					{
						ManualLogSource? log = Plugin.Log;
						if (log != null)
						{
							log.LogError((object)$"[LandingLootToChat] Error while building chat message: {arg}");
						}
					}
					return false;
				}
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		[HarmonyPostfix]
		private static void AfterLevelGenerated(RoundManager __instance)
		{
			try
			{
				LootCache.Clear();
				if (!(((object)RoundManager.Instance?.currentLevel)?.ToString() ?? string.Empty).StartsWith("CompanyBuildingLevel", StringComparison.Ordinal))
				{
					((MonoBehaviour)__instance).StartCoroutine(CountAndAnnounceLoot(__instance));
				}
			}
			catch (Exception arg)
			{
				ManualLogSource? log = Plugin.Log;
				if (log != null)
				{
					log.LogError((object)$"[LandingLootToChat] Error starting coroutine: {arg}");
				}
			}
		}

		private static void CountLootOnce(out int oneHanded, out int twoHanded, out int totalValue, out int hivesCount)
		{
			oneHanded = 0;
			twoHanded = 0;
			totalValue = 0;
			hivesCount = 0;
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			foreach (GrabbableObject val in array)
			{
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val.itemProperties == (Object)null) && val.itemProperties.isScrap && !val.isInShipRoom && !val.isInElevator && !val.scrapPersistedThroughRounds)
				{
					totalValue += val.scrapValue;
					if (val.itemProperties.twoHanded)
					{
						twoHanded++;
					}
					else
					{
						oneHanded++;
					}
					if ((val.itemProperties.itemName ?? string.Empty).ToLowerInvariant().Contains("hive"))
					{
						hivesCount++;
					}
				}
			}
		}

		[IteratorStateMachine(typeof(<CountAndAnnounceLoot>d__2))]
		private static IEnumerator CountAndAnnounceLoot(RoundManager rm)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <CountAndAnnounceLoot>d__2(0)
			{
				rm = rm
			};
		}

		internal static string BuildLootMessage(int dungeonType, int oneHanded, int twoHanded, int totalValue, int hivesCount)
		{
			string dungeonTypeName = Plugin.GetDungeonTypeName(dungeonType);
			string text = "<color=#00ffff>" + dungeonTypeName + "</color>";
			string text2 = $"<color=#ff0000>Одноручного</color>: <color=#ffff00>{oneHanded}</color>";
			string text3 = $"<color=#ff0000>Двуручного</color>: <color=#ffff00>{twoHanded}</color>";
			string text4 = $"<color=#ff0000>Общая ценность</color>: <color=#ffff00>${totalValue}</color>";
			string text5 = text + "\n" + text2 + "\n" + text3 + "\n" + text4;
			if (hivesCount > 0)
			{
				string text6 = $"<color=#ff0000>Ульи пчёл</color>: <color=#ffff00>{hivesCount}</color>";
				text5 = text5 + "\n" + text6;
			}
			return text5;
		}
	}
	[HarmonyPatch(typeof(HUDManager), "AddTextToChatOnServer")]
	internal static class LootChatCommandPatch
	{
		[HarmonyPrefix]
		private static bool Prefix(string chatMessage, int playerId)
		{
			try
			{
				if (string.IsNullOrWhiteSpace(chatMessage))
				{
					return true;
				}
				if (chatMessage.StartsWith("/loot", StringComparison.OrdinalIgnoreCase))
				{
					if (LootCache.HasData)
					{
						Plugin.SendChatMessageToAll(LandingLootPatch.BuildLootMessage(LootCache.DungeonType, LootCache.OneHanded, LootCache.TwoHanded, LootCache.TotalValue, LootCache.HivesCount));
					}
					else
					{
						Plugin.SendChatMessageToAll("<color=#ff0000>Данные о луте ещё не готовы</color>");
					}
					return false;
				}
				return true;
			}
			catch (Exception arg)
			{
				ManualLogSource? log = Plugin.Log;
				if (log != null)
				{
					log.LogError((object)$"[LandingLootToChat] Error in /loot command patch: {arg}");
				}
				return true;
			}
		}
	}
	internal static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "yourname.LandingLootToChat";

		public const string PLUGIN_NAME = "LandingLootToChat by mrlistvin";

		public const string PLUGIN_VERSION = "1.8.1";
	}
}