Decompiled source of BetterWarningIcons v0.0.5

BetterWarningIcons.dll

Decompiled 10 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("BetterWarningIcons")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+5c343bea0c5495ff8051950a191d98b6bb502beb")]
[assembly: AssemblyProduct("BetterWarningIcons")]
[assembly: AssemblyTitle("BetterWarningIcons")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 DysonSphereProgram.Modding.BetterWarningIcons
{
	[BepInPlugin("dev.raptor.dsp.BetterWarningIcons", "BetterWarningIcons", "0.0.5")]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "dev.raptor.dsp.BetterWarningIcons";

		public const string NAME = "BetterWarningIcons";

		public const string VERSION = "0.0.5";

		private Harmony _harmony;

		public static ManualLogSource Log;

		internal static string Path;

		private void Awake()
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Path = ((BaseUnityPlugin)this).Info.Location;
			InsufficientInputIconPatch.InitConfig(((BaseUnityPlugin)this).Config);
			VeinDepletionIconPatch.InitConfig(((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("dev.raptor.dsp.BetterWarningIcons");
			if (InsufficientInputIconPatch.enablePatch.Value)
			{
				_harmony.PatchAll(typeof(InsufficientInputIconPatch));
			}
			if (VeinDepletionIconPatch.enablePatch.Value)
			{
				_harmony.PatchAll(typeof(VeinDepletionIconPatch));
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BetterWarningIcons Awake() called");
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"BetterWarningIcons OnDestroy() called");
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			Log = null;
		}
	}
	public static class InsufficientInputIconPatch
	{
		public const string ConfigSection = "Insufficient Input Warning";

		public static ConfigEntry<bool> enablePatch;

		public static Texture entitySignRendererTextureOriginal;

		public static Texture2D entitySignRendererTextureModified;

		public static Texture2D insufficientInputTex;

		public static Texture2D insufficientInputTexSmall;

		public static Sprite iconSprite;

		public static bool contentLoaded;

		private const int texAtlasEntrySize = 256;

		public const uint INSUFFICIENT_INPUT_SIGN_TYPE = 31u;

		public const int InsufficientInputSignalId = 531;

		public static void InitConfig(ConfigFile confFile)
		{
			enablePatch = confFile.Bind<bool>("Insufficient Input Warning", "Enable Patch", true, ConfigDescription.Empty);
		}

		private static void EnsureLoaded()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			if (!contentLoaded)
			{
				insufficientInputTex = new Texture2D(1, 1, (TextureFormat)4, true);
				insufficientInputTexSmall = new Texture2D(1, 1, (TextureFormat)4, true);
				byte[] array = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(Plugin.Path), "InsufficientInputIcon.png"));
				ImageConversion.LoadImage(insufficientInputTex, array);
				byte[] array2 = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(Plugin.Path), "InsufficientInputIconSmall.png"));
				ImageConversion.LoadImage(insufficientInputTexSmall, array2);
				iconSprite = Sprite.Create(insufficientInputTexSmall, new Rect(0f, 0f, 80f, 80f), new Vector2(0.5f, 0.5f));
				contentLoaded = true;
			}
		}

		private static bool CheckIfMaterialIsPresent()
		{
			BuiltinConfig builtin = Configs.builtin;
			if ((Object)(object)builtin == (Object)null)
			{
				Plugin.Log.LogError((object)"Configs.builtin is null");
				return false;
			}
			Material entitySignMat = builtin.entitySignMat;
			if ((Object)(object)entitySignMat == (Object)null)
			{
				Plugin.Log.LogError((object)"Configs.builtin.entitySignMat is null");
				return false;
			}
			if ((Object)(object)entitySignMat.mainTexture == (Object)null)
			{
				Plugin.Log.LogError((object)"Configs.builtin.entitySignMat.mainTexture is null");
				return false;
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(Configs), "Awake")]
		private static void PatchMaterialTexture()
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			if (CheckIfMaterialIsPresent())
			{
				EnsureLoaded();
				Material entitySignMat = Configs.builtin.entitySignMat;
				entitySignRendererTextureOriginal = entitySignMat.mainTexture;
				RenderTexture temporary = RenderTexture.GetTemporary(entitySignRendererTextureOriginal.width, entitySignRendererTextureOriginal.height, 0, (RenderTextureFormat)7, (RenderTextureReadWrite)2);
				Graphics.Blit(entitySignRendererTextureOriginal, temporary);
				RenderTexture active = RenderTexture.active;
				RenderTexture.active = temporary;
				entitySignRendererTextureModified = new Texture2D(entitySignRendererTextureOriginal.width, entitySignRendererTextureOriginal.height);
				entitySignRendererTextureModified.ReadPixels(new Rect(0f, 0f, (float)((Texture)temporary).width, (float)((Texture)temporary).height), 0, 0);
				entitySignRendererTextureModified.Apply();
				RenderTexture.active = active;
				RenderTexture.ReleaseTemporary(temporary);
				Color[] pixels = insufficientInputTex.GetPixels(0, 0, 256, 256);
				entitySignRendererTextureModified.SetPixels(768, 1792, 256, 256, pixels);
				entitySignRendererTextureModified.Apply();
				entitySignMat.mainTexture = (Texture)(object)entitySignRendererTextureModified;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(IconSet), "Create")]
		private static void PatchIconSet(IconSet __instance)
		{
			EnsureLoaded();
			uint num = __instance.spriteIndexMap.Values.Max() + 1;
			if (num < 625)
			{
				int num2 = (int)(num % 25);
				int num3 = (int)(num / 25);
				Graphics.CopyTexture((Texture)(object)iconSprite.texture, 0, 0, 0, 0, 80, 80, (Texture)(object)__instance.texture, 0, 0, num2 * 80, num3 * 80);
				__instance.spriteIndexMap[iconSprite] = num;
				__instance.signalIconIndex[531] = num;
				__instance.signalIconIndexBuffer.SetData((Array)__instance.signalIconIndex);
				__instance.texture.Apply(true);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(SignalProtoSet), "IconSprite")]
		private static void SignalProtoSet__IconSprite(int signalId, ref Sprite __result)
		{
			if (signalId == 531)
			{
				__result = iconSprite;
			}
		}

		private static void Patch_Assemblers(FactorySystem __instance, int start, int end)
		{
			SignData[] entitySignPool = __instance.factory.entitySignPool;
			for (int i = start; i < end; i++)
			{
				ref AssemblerComponent reference = ref __instance.assemblerPool[i];
				if (reference.id != i || reference.replicating || reference.time >= reference.timeSpend || entitySignPool[reference.entityId].signType != 6)
				{
					continue;
				}
				for (int j = 0; j < reference.requireCounts.Length; j++)
				{
					if (reference.served[j] < reference.requireCounts[j])
					{
						entitySignPool[reference.entityId].signType = 31u;
						break;
					}
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTick", new Type[]
		{
			typeof(long),
			typeof(bool)
		})]
		private static void Patch_Assemblers_SingleThread(FactorySystem __instance)
		{
			Patch_Assemblers(__instance, 1, __instance.assemblerCursor);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTick", new Type[]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int),
			typeof(int)
		})]
		private static void Patch_Assemblers_MultiThread(FactorySystem __instance, int _usedThreadCnt, int _curThreadIdx, int _minimumMissionCnt)
		{
			int start = default(int);
			int end = default(int);
			if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.assemblerCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref start, ref end))
			{
				Patch_Assemblers(__instance, start, end);
			}
		}

		private static void Patch_LabProduce(FactorySystem __instance, int start, int end)
		{
			SignData[] entitySignPool = __instance.factory.entitySignPool;
			for (int i = start; i < end; i++)
			{
				ref LabComponent reference = ref __instance.labPool[i];
				if (reference.id != i || reference.researchMode || reference.replicating || reference.time >= reference.timeSpend || entitySignPool[reference.entityId].signType != 6)
				{
					continue;
				}
				for (int j = 0; j < reference.requireCounts.Length; j++)
				{
					if (reference.served[j] < reference.requireCounts[j])
					{
						entitySignPool[reference.entityId].signType = 31u;
						break;
					}
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTickLabProduceMode", new Type[]
		{
			typeof(long),
			typeof(bool)
		})]
		private static void Patch_LabProduce_SingleThread(FactorySystem __instance)
		{
			Patch_LabProduce(__instance, 1, __instance.labCursor);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTickLabProduceMode", new Type[]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int),
			typeof(int)
		})]
		private static void Patch_LabProduce_MultiThread(FactorySystem __instance, int _usedThreadCnt, int _curThreadIdx, int _minimumMissionCnt)
		{
			int start = default(int);
			int end = default(int);
			if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.labCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref start, ref end))
			{
				Patch_LabProduce(__instance, start, end);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTickLabResearchMode")]
		private static void Patch_LabResearch(FactorySystem __instance)
		{
			SignData[] entitySignPool = __instance.factory.entitySignPool;
			for (int i = 1; i < __instance.labCursor; i++)
			{
				ref LabComponent reference = ref __instance.labPool[i];
				if (reference.id != i || !reference.researchMode || reference.replicating || entitySignPool[reference.entityId].signType != 6)
				{
					continue;
				}
				for (int j = 0; j < reference.matrixServed.Length; j++)
				{
					if (reference.matrixServed[j] < reference.matrixPoints[j])
					{
						entitySignPool[reference.entityId].signType = 31u;
						break;
					}
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIOptionWindow), "_OnCreate")]
		private static void PatchBuildingWarnMask(UIOptionWindow __instance)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: 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_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			EnsureLoaded();
			UIButton[] buildingWarnButtons = __instance.buildingWarnButtons;
			float num = buildingWarnButtons.Max(delegate(UIButton x)
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				Transform transform3 = ((Component)x).transform;
				return ((RectTransform)((transform3 is RectTransform) ? transform3 : null)).anchoredPosition.x;
			});
			UIButton obj = buildingWarnButtons.Last();
			Transform transform = ((Component)obj).transform;
			Vector2 sizeDelta = ((RectTransform)((transform is RectTransform) ? transform : null)).sizeDelta;
			UIButton val = Object.Instantiate<UIButton>(obj, ((Component)obj).transform.parent);
			Transform transform2 = ((Component)val).transform;
			RectTransform val2 = (RectTransform)(object)((transform2 is RectTransform) ? transform2 : null);
			val2.anchoredPosition = new Vector2(num + sizeDelta.x, val2.anchoredPosition.y);
			Transform obj2 = ((Component)val).transform.Find("image");
			object obj3;
			if (obj2 == null)
			{
				obj3 = null;
			}
			else
			{
				GameObject gameObject = ((Component)obj2).gameObject;
				obj3 = ((gameObject != null) ? gameObject.GetComponent<Image>() : null);
			}
			Image val3 = (Image)obj3;
			if ((Object)(object)val3 != (Object)null)
			{
				val3.sprite = iconSprite;
			}
			val.data = 1073741824;
			val.tips.tipTitle = "Insufficient Input";
			val.tips.tipText = "This sign will display when there is insufficient input to the building";
			int num2 = buildingWarnButtons.Length;
			__instance.buildingWarnButtons = (UIButton[])(object)new UIButton[num2 + 1];
			Array.Copy(buildingWarnButtons, __instance.buildingWarnButtons, num2);
			__instance.buildingWarnButtons[num2] = val;
		}
	}
	public static class VeinDepletionIconPatch
	{
		public const string ConfigSection = "Vein Depletion Warning";

		public static ConfigEntry<bool> enablePatch;

		public static ConfigEntry<bool> useTotalVeinDepletionAmountPatch;

		public static ConfigEntry<long> veinAmountToWarnFor;

		public static void InitConfig(ConfigFile confFile)
		{
			enablePatch = confFile.Bind<bool>("Vein Depletion Warning", "Enable Patch", true, ConfigDescription.Empty);
			useTotalVeinDepletionAmountPatch = confFile.Bind<bool>("Vein Depletion Warning", "Use Total Vein Amount", true, "Use total vein amount for vein depletion warning instead of minimum vein amount");
			veinAmountToWarnFor = confFile.Bind<long>("Vein Depletion Warning", "Vein Amount Threshold", 1000L, "The amount at or below which the warning will trigger");
		}

		private static void Patch_Miners_VeinDepletion(FactorySystem __instance, int start, int end)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Invalid comparison between Unknown and I4
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			SignData[] entitySignPool = __instance.factory.entitySignPool;
			VeinData[] veinPool = __instance.factory.veinPool;
			bool value = useTotalVeinDepletionAmountPatch.Value;
			long value2 = veinAmountToWarnFor.Value;
			for (int i = start; i < end; i++)
			{
				ref MinerComponent reference = ref __instance.minerPool[i];
				if (reference.id != i || (int)reference.type != 2)
				{
					continue;
				}
				int entityId = reference.entityId;
				if (entitySignPool[entityId].signType < 1 || entitySignPool[entityId].signType > 3)
				{
					if (value)
					{
						MinerComponent val = reference;
						((MinerComponent)(ref val)).GetTotalVeinAmount(veinPool);
					}
					long num = (value ? reference.totalVeinAmount : reference.minimumVeinAmount);
					entitySignPool[entityId].signType = ((num < value2) ? 7u : 0u);
				}
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTick", new Type[]
		{
			typeof(long),
			typeof(bool)
		})]
		private static void Patch_Miners_VeinDepletion_SingleThread(FactorySystem __instance)
		{
			Patch_Miners_VeinDepletion(__instance, 1, __instance.minerCapacity);
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(FactorySystem), "GameTick", new Type[]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int),
			typeof(int)
		})]
		private static void Patch_Miners_VeinDepletion_MultiThread(FactorySystem __instance, int _usedThreadCnt, int _curThreadIdx, int _minimumMissionCnt)
		{
			int start = default(int);
			int end = default(int);
			if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.minerCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref start, ref end))
			{
				Patch_Miners_VeinDepletion(__instance, start, end);
			}
		}
	}
}