Decompiled source of MinerInfo v1.1.4

MinerInfo.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("MinerInfo")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyInformationalVersion("1.1.4")]
[assembly: AssemblyProduct("MinerInfo")]
[assembly: AssemblyTitle("MinerInfo")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.4.0")]
[module: UnverifiableCode]
namespace MinerInfo;

internal class CurrentOutputPatch
{
	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIEntityBriefInfo), "_OnUpdate")]
	private static void UIEntityBriefInfo_OnUpdate(UIEntityBriefInfo __instance)
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Invalid comparison between Unknown and I4
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Invalid comparison between Unknown and I4
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Invalid comparison between Unknown and I4
		if (__instance.frame % 4 != 0)
		{
			return;
		}
		PrefabDesc val = __instance.entityInfo.itemProto?.prefabDesc;
		if (val == null || (int)val.minerType <= 0 || __instance.factory == null)
		{
			return;
		}
		int minerId = __instance.factory.entityPool[__instance.entityId].minerId;
		ref MinerComponent reference = ref __instance.factory.factorySystem.minerPool[minerId];
		if ((int)reference.type == 2)
		{
			float num = 60f / (float)reference.period * (float)reference.speed * GameMain.history.miningSpeedScale * (float)reference.veinCount;
			float num2 = (((int)reference.workstate <= 0) ? 0f : (reference.speedDamper * (float)__instance.entityInfo.powerConsumerRatio));
			float num3 = num * num2;
			if (Plugin.ShowItemsPerSecond)
			{
				Text entityNameText = __instance.entityNameText;
				entityNameText.text += $" {num3:F2}/s ";
			}
			if (Plugin.ShowItemsPerMinute)
			{
				Text entityNameText2 = __instance.entityNameText;
				entityNameText2.text += $" {num3 * 60f:F1}/m ";
			}
			Text entityNameText3 = __instance.entityNameText;
			entityNameText3.text += num2.ToString("P0");
		}
	}
}
public class MaxOutputPatch
{
	private static float[] maxArr = new float[0];

	private static int[] minedNodeCounts = new int[0];

	private static readonly StringBuilder stringBuilder = new StringBuilder();

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIVeinDetail), "_OnUpdate")]
	private static void UIVeinDetail_OnUpdate(UIVeinDetail __instance)
	{
		if (VFInput.inFullscreenGUI || GameMain.gameTick % 60 != 0L)
		{
			return;
		}
		PlanetFactory val = __instance.inspectPlanet?.factory;
		if (val == null || !__instance.inspectPlanet.factoryLoaded)
		{
			return;
		}
		if (maxArr.Length != val.veinGroups.Length)
		{
			maxArr = new float[val.veinGroups.Length];
		}
		else
		{
			Array.Clear(maxArr, 0, maxArr.Length);
		}
		for (int i = 1; i < val.factorySystem.minerCursor; i++)
		{
			ref MinerComponent reference = ref val.factorySystem.minerPool[i];
			if (reference.id == i && reference.veinCount > 0)
			{
				float num = 60f / (float)reference.period * (float)reference.speed * GameMain.history.miningSpeedScale * (float)reference.veinCount;
				int groupIndex = val.veinPool[reference.veins[0]].groupIndex;
				maxArr[groupIndex] += num;
			}
		}
		if (!Plugin.ShowMinedNodesCount)
		{
			return;
		}
		if (minedNodeCounts.Length != val.veinGroups.Length)
		{
			minedNodeCounts = new int[val.veinGroups.Length];
		}
		else
		{
			Array.Clear(minedNodeCounts, 0, minedNodeCounts.Length);
		}
		for (int j = 1; j < val.veinCursor; j++)
		{
			ref VeinData reference2 = ref val.veinPool[j];
			if (reference2.minerCount > 0)
			{
				minedNodeCounts[reference2.groupIndex]++;
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIVeinDetailNode), "_OnUpdate")]
	private static bool UIVeinDetailNode_OnUpdate(UIVeinDetailNode __instance)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: 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_00e2: Invalid comparison between Unknown and I4
		if (__instance.inspectFactory == null)
		{
			((ManualBehaviour)__instance)._Close();
			return false;
		}
		ref VeinGroup reference = ref __instance.inspectFactory.veinGroups[__instance.veinGroupIndex];
		if (reference.count == 0 || (int)reference.type == 0)
		{
			((ManualBehaviour)__instance)._Close();
			return false;
		}
		if ((int)VeinFilterPatch.filterType != 0 && __instance.veinGroupIndex < maxArr.Length)
		{
			if (VeinFilterPatch.veinDisplayFilter == 1 && maxArr[__instance.veinGroupIndex] == 0f)
			{
				((ManualBehaviour)__instance)._Close();
				return false;
			}
			if (VeinFilterPatch.veinDisplayFilter == 2 && maxArr[__instance.veinGroupIndex] != 0f)
			{
				((ManualBehaviour)__instance)._Close();
				return false;
			}
		}
		if (__instance.counter % 4 == 0 && (__instance.showingAmount != reference.amount || __instance.counter % 60 == 0))
		{
			__instance.showingAmount = reference.amount;
			stringBuilder.Clear();
			if ((int)reference.type != 7)
			{
				stringBuilder.Append(reference.count);
				stringBuilder.Append(Localization.Translate("空格个"));
				stringBuilder.Append(((Proto)__instance.veinProto).name);
				if (Plugin.ShowMinedNodesCount && __instance.veinGroupIndex < minedNodeCounts.Length)
				{
					stringBuilder.Append($" ({minedNodeCounts[__instance.veinGroupIndex]}/{reference.count})");
				}
				stringBuilder.Append(Localization.Translate("储量"));
				stringBuilder.Append(__instance.AmountString(reference.amount));
				MinerInfoString(__instance.veinGroupIndex);
				__instance.infoText.text = stringBuilder.ToString();
			}
			else
			{
				stringBuilder.Append(reference.count);
				stringBuilder.Append(Localization.Translate("空格个"));
				stringBuilder.Append(((Proto)__instance.veinProto).name);
				stringBuilder.Append(Localization.Translate("产量"));
				stringBuilder.Append(((float)reference.amount * VeinData.oilSpeedMultiplier).ToString("0.0000"));
				stringBuilder.Append("/s");
				__instance.infoText.text = stringBuilder.ToString();
			}
		}
		__instance.counter++;
		return false;
	}

	private static void MinerInfoString(int veinGroupIndex)
	{
		if (veinGroupIndex < maxArr.Length && maxArr[veinGroupIndex] != 0f)
		{
			stringBuilder.AppendLine();
			stringBuilder.Append(Plugin.VeinMaxMinerOutputText);
			if (Plugin.ShowItemsPerSecond)
			{
				stringBuilder.Append($" {maxArr[veinGroupIndex]:F1}/s ");
			}
			if (Plugin.ShowItemsPerMinute)
			{
				stringBuilder.Append($" {maxArr[veinGroupIndex] * 60f:F0}/m");
			}
		}
	}
}
[BepInPlugin("starfi5h.plugin.MinerInfo", "MinerInfo", "1.1.4")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "starfi5h.plugin.MinerInfo";

	public const string NAME = "MinerInfo";

	public const string VERSION = "1.1.4";

	public static bool ShowItemsPerSecond;

	public static bool ShowItemsPerMinute;

	public static bool ShowMinedNodesCount;

	public static string VeinMaxMinerOutputText;

	public static ManualLogSource Log;

	private Harmony harmony;

	public void Awake()
	{
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Expected O, but got Unknown
		ShowItemsPerSecond = ((BaseUnityPlugin)this).Config.Bind<bool>("MinerInfo", "ShowItemsPerSecond", false, "If true, display unit per second.").Value;
		ShowItemsPerMinute = ((BaseUnityPlugin)this).Config.Bind<bool>("MinerInfo", "ShowItemsPerMinute", true, "If true, display unit per minute.").Value;
		ShowMinedNodesCount = ((BaseUnityPlugin)this).Config.Bind<bool>("MinerInfo", "ShowMinedNodesCount", false, "If true, display (mined node/total node) for each veinGroup").Value;
		bool value = ((BaseUnityPlugin)this).Config.Bind<bool>("MaxMinerOutput", "Enable", true, "Show the maximum number of items per time period output by all miners on a vein.").Value;
		VeinMaxMinerOutputText = ((BaseUnityPlugin)this).Config.Bind<string>("MaxMinerOutput", "Text", "Max Output:", "Prefix text show before numbers.").Value;
		bool value2 = ((BaseUnityPlugin)this).Config.Bind<bool>("Other", "ShowCurrentMinerOutput", true, "Show mining efficiency in miner hovering tip").Value;
		Log = ((BaseUnityPlugin)this).Logger;
		harmony = new Harmony("starfi5h.plugin.MinerInfo");
		if (value2)
		{
			harmony.PatchAll(typeof(CurrentOutputPatch));
		}
		if (value)
		{
			harmony.PatchAll(typeof(MaxOutputPatch));
		}
		harmony.PatchAll(typeof(VeinFilterPatch));
	}
}
internal class VeinFilterPatch
{
	public static EVeinType filterType;

	public static int veinDisplayFilter;

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIPlanetDetail), "OnPlanetDataSet")]
	public static void OnPlanetDataSet(UIPlanetDetail __instance)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		filterType = (EVeinType)0;
		veinDisplayFilter = __instance.uiGame.veinAmountDisplayFilter;
		if (__instance.planet?.factory == null)
		{
			return;
		}
		foreach (UIResAmountEntry entry in __instance.entries)
		{
			entry.iconButton.data = entry.refId;
			entry.iconButton.BindOnClickSafe((Action<int>)OnVeinIconClick);
		}
	}

	public static void OnVeinIconClick(int data)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Invalid comparison between Unknown and I4
		UIVeinDetail veinDetail = UIRoot.instance.uiGame.veinDetail;
		UIPlanetDetail planetDetail = UIRoot.instance.uiGame.planetDetail;
		if (planetDetail.planet != veinDetail.inspectPlanet || planetDetail.planet == null)
		{
			return;
		}
		filterType = (EVeinType)(((int)filterType == 0) ? ((byte)data) : 0);
		foreach (UIResAmountEntry entry in planetDetail.entries)
		{
			((Component)entry).gameObject.SetActive(entry.refId == data || (int)filterType == 0);
		}
		UIRoot.instance.uiGame.veinDetail.SetInspectPlanet(UIRoot.instance.uiGame.veinDetail.inspectPlanet);
	}

	[HarmonyTranspiler]
	[HarmonyPatch(typeof(UIVeinDetailNode), "Refresh")]
	private static IEnumerable<CodeInstruction> FitlerVeinType(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Expected O, but got Unknown
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Expected O, but got Unknown
		try
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(VeinGroup), "type");
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[3]
			{
				new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)fieldInfo, (string)null),
				new CodeMatch((OpCode?)OpCodes.Brtrue, (object)null, (string)null)
			}).Advance(2).Insert((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<EVeinType, bool>>((Func<EVeinType, bool>)((EVeinType type) => ((int)filterType == 0) ? ((int)type > 0) : (type == filterType))) })
				.InstructionEnumeration();
		}
		catch (Exception ex)
		{
			Plugin.Log.LogWarning((object)"Transpiler UIVeinDetailNode.Refresh fail!");
			Plugin.Log.LogWarning((object)ex);
			return instructions;
		}
	}
}