using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SmartWarningIcons")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SmartWarningIcons")]
[assembly: AssemblyTitle("SmartWarningIcons")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[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;
}
}
}
internal static class PluginInfo
{
public const string PLUGIN_GUID = "SmartWarningIcons";
public const string PLUGIN_NAME = "SmartWarningIcons";
public const string PLUGIN_VERSION = "1.0.0";
}
namespace SmartWarningIcons
{
internal static class InsufficientInputSign
{
private static ConfigEntry<bool> _enable;
internal static bool Enabled => _enable.Value;
internal static void Init(ConfigFile config)
{
_enable = config.Bind<bool>("InsufficientInput", "Enable", true, "Mark buildings that have a recipe but lack input materials with a dedicated red warning icon instead of the generic not-working icon.\n给设置了配方但缺少原料的建筑显示专用的红色「缺原料」图标,替代笼统的「不工作」图标。");
}
}
internal static class OutputPileSign
{
private static ConfigEntry<bool> _enable;
internal static void Init(ConfigFile config)
{
_enable = config.Bind<bool>("OutputPile", "Enable", true, "Show the yellow warning only when production stalls because a finished batch cannot fit in the output buffer.\n产物缓冲区装不下已完成的一批产物,导致建筑停工时,才显示黄色「产物堆砌」图标。");
}
private static void ScanFactory(FactorySystem fs, int currentTech, bool redEnabled, bool yellowEnabled)
{
SignData[] entitySignPool = fs.factory.entitySignPool;
for (int i = 1; i < fs.assemblerCursor; i++)
{
ref AssemblerComponent reference = ref fs.assemblerPool[i];
if (reference.id == i && reference.recipeId > 0)
{
UpdateProductionSign(ref entitySignPool[reference.entityId].signType, reference.recipeExecuteData, reference.time, reference.replicating, reference.served, redEnabled, yellowEnabled);
}
}
for (int j = 1; j < fs.labCursor; j++)
{
ref LabComponent reference2 = ref fs.labPool[j];
if (reference2.id != j)
{
continue;
}
ref uint signType = ref entitySignPool[reference2.entityId].signType;
if (reference2.researchMode)
{
if (redEnabled && currentTech > 0 && reference2.techId == currentTech && !reference2.replicating && CanRefine(signType) && HasShortage(reference2.matrixServed, LabComponent.matrixPoints))
{
signType = 31u;
}
}
else if (reference2.recipeId > 0)
{
UpdateProductionSign(ref signType, reference2.recipeExecuteData, reference2.time, reference2.replicating, reference2.served, redEnabled, yellowEnabled);
}
}
}
private static bool CanRefine(uint sign)
{
if (sign != 0)
{
return sign == 6;
}
return true;
}
private static void UpdateProductionSign(ref uint sign, RecipeExecuteData recipe, int time, bool replicating, int[] served, bool redEnabled, bool yellowEnabled)
{
if (!CanRefine(sign) || replicating || recipe == null || recipe.timeSpend <= 0)
{
return;
}
if (time >= recipe.timeSpend)
{
if (yellowEnabled)
{
sign = 25u;
}
}
else if (redEnabled && HasShortage(served, recipe.requireCounts))
{
sign = 31u;
}
}
private static bool HasShortage(int[] served, int[] counts)
{
if (served == null || counts == null || served.Length != counts.Length)
{
return false;
}
for (int i = 0; i < counts.Length; i++)
{
if (counts[i] > 0 && served[i] < counts[i])
{
return true;
}
}
return false;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameLogic), "OnFactoryFrameEnd")]
private static void OnFactoryFrameEnd()
{
bool enabled = InsufficientInputSign.Enabled;
bool value = _enable.Value;
bool enabled2 = VeinDepletionSign.Enabled;
if (!enabled && !value && !enabled2)
{
return;
}
GameData data = GameMain.data;
PlanetFactory val = ((data != null) ? data.localLoadedPlanetFactory : null);
if (val?.factorySystem != null)
{
if ((enabled || value) && SignIcons.HealAtlas())
{
FactorySystem factorySystem = val.factorySystem;
GameHistoryData history = data.history;
ScanFactory(factorySystem, (history != null) ? history.currentTech : 0, enabled, value);
}
if (enabled2)
{
VeinDepletionSign.ScanMiners(val.factorySystem);
}
}
}
}
internal static class SignIcons
{
internal const uint SignTypeInsufficientInput = 31u;
internal const uint SignTypeOutputPile = 25u;
internal const int SignalIdInsufficientInput = 531;
internal const int SignalIdOutputPile = 525;
private const int AtlasColumns = 4;
private const int AtlasRows = 8;
private const int AtlasCell = 256;
private const int IconCell = 80;
private static Texture2D _redCell;
private static Texture2D _yellowCell;
private static Texture2D _redIcon;
private static Texture2D _yellowIcon;
private static Sprite _redSprite;
private static Sprite _yellowSprite;
private static bool _assetsAttempted;
private static bool _assetsReady;
private static Material _patchedMaterial;
private static Texture _sourceAtlas;
private static Texture _failedAtlas;
private static Texture2D _patchedAtlas;
private static bool EnsureAssets()
{
//IL_008e: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Unknown result type (might be due to invalid IL or missing references)
//IL_009f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
if (_assetsAttempted)
{
return _assetsReady;
}
_assetsAttempted = true;
try
{
_redCell = LoadTexture("InsufficientInput.png", 256);
_yellowCell = LoadTexture("OutputPile.png", 256);
_redIcon = LoadTexture("InsufficientInputSmall.png", 80);
_yellowIcon = LoadTexture("OutputPileSmall.png", 80);
Rect val = default(Rect);
((Rect)(ref val))..ctor(0f, 0f, 80f, 80f);
Vector2 val2 = default(Vector2);
((Vector2)(ref val2))..ctor(0.5f, 0.5f);
_redSprite = Sprite.Create(_redIcon, val, val2);
_yellowSprite = Sprite.Create(_yellowIcon, val, val2);
return _assetsReady = true;
}
catch (Exception ex)
{
ReleaseAssets();
ManualLogSource log = SmartWarningIconsPlugin.Log;
if (log != null)
{
log.LogWarning((object)("Custom warning icons unavailable; retaining native signs: " + ex.Message));
}
return false;
}
}
private static Texture2D LoadTexture(string name, int size)
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Expected O, but got Unknown
byte[] array = File.ReadAllBytes(Path.Combine(SmartWarningIconsPlugin.PluginDir, "assets", name));
Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
try
{
if (!ImageConversion.LoadImage(val, array) || ((Texture)val).width != size || ((Texture)val).height != size)
{
throw new InvalidDataException($"{name} must be a {size}x{size} PNG.");
}
((Object)val).name = "SmartWarningIcons/" + name;
return val;
}
catch
{
Object.Destroy((Object)(object)val);
throw;
}
}
internal static bool HealAtlas()
{
//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
//IL_010a: Unknown result type (might be due to invalid IL or missing references)
//IL_010c: Unknown result type (might be due to invalid IL or missing references)
//IL_0116: Unknown result type (might be due to invalid IL or missing references)
//IL_0118: Unknown result type (might be due to invalid IL or missing references)
//IL_0122: Unknown result type (might be due to invalid IL or missing references)
//IL_0124: Unknown result type (might be due to invalid IL or missing references)
//IL_012e: 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_013a: Unknown result type (might be due to invalid IL or missing references)
//IL_0146: Unknown result type (might be due to invalid IL or missing references)
//IL_0153: Expected O, but got Unknown
//IL_0199: Unknown result type (might be due to invalid IL or missing references)
Material val = (((Object)(object)Configs.builtin != (Object)null) ? Configs.builtin.entitySignMat : null);
Texture val2 = (((Object)(object)val != (Object)null) ? val.mainTexture : null);
if ((Object)(object)val2 == (Object)null || (Object)(object)val2 == (Object)(object)_failedAtlas)
{
return false;
}
if ((Object)(object)val2 == (Object)(object)_patchedAtlas)
{
return true;
}
if (!EnsureAssets())
{
return false;
}
if ((Object)(object)val2 == (Object)(object)_sourceAtlas && (Object)(object)_patchedAtlas != (Object)null && (Object)(object)val == (Object)(object)_patchedMaterial)
{
val.mainTexture = (Texture)(object)_patchedAtlas;
return true;
}
Texture2D val3 = null;
RenderTexture val4 = null;
RenderTexture active = RenderTexture.active;
bool sRGBWrite = GL.sRGBWrite;
try
{
if (val2.width != 1024 || val2.height != 2048)
{
throw new InvalidDataException($"Unsupported sign atlas size: {val2.width}x{val2.height}.");
}
val3 = new Texture2D(val2.width, val2.height, (TextureFormat)4, val2.mipmapCount > 1)
{
name = "SmartWarningIcons/sign-states",
filterMode = val2.filterMode,
wrapModeU = val2.wrapModeU,
wrapModeV = val2.wrapModeV,
wrapModeW = val2.wrapModeW,
anisoLevel = val2.anisoLevel,
mipMapBias = val2.mipMapBias
};
val4 = RenderTexture.GetTemporary(val2.width, val2.height, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
GL.sRGBWrite = val4.sRGB;
Graphics.Blit(val2, val4);
RenderTexture.active = val4;
val3.ReadPixels(new Rect(0f, 0f, (float)val2.width, (float)val2.height), 0, 0, false);
WriteSignCell(val3, 31u, _redCell);
WriteSignCell(val3, 25u, _yellowCell);
val3.Apply();
ReleaseAtlas();
val.mainTexture = (Texture)(object)val3;
_patchedMaterial = val;
_sourceAtlas = val2;
_patchedAtlas = val3;
_failedAtlas = null;
val3 = null;
ManualLogSource log = SmartWarningIconsPlugin.Log;
if (log != null)
{
log.LogInfo((object)$"Registered world signs {31u}/{25u} (4-column atlas).");
}
return true;
}
catch (Exception ex)
{
_failedAtlas = val2;
ManualLogSource log2 = SmartWarningIconsPlugin.Log;
if (log2 != null)
{
log2.LogWarning((object)("Could not patch sign atlas; retaining native signs: " + ex.Message));
}
return false;
}
finally
{
GL.sRGBWrite = sRGBWrite;
RenderTexture.active = active;
if ((Object)(object)val4 != (Object)null)
{
RenderTexture.ReleaseTemporary(val4);
}
if ((Object)(object)val3 != (Object)null)
{
Object.Destroy((Object)(object)val3);
}
}
}
private static void WriteSignCell(Texture2D target, uint signType, Texture2D source)
{
int num = (int)(signType % 4 * 256);
int num2 = (int)(signType / 4 * 256);
target.SetPixels(num, num2, 256, 256, source.GetPixels());
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Configs), "Awake")]
private static void OnConfigsAwake()
{
HealAtlas();
}
[HarmonyPrefix]
[HarmonyPatch(typeof(IconSet), "Create")]
private static void OnIconSetCreating(IconSet __instance, out bool __state)
{
__state = !__instance.loaded;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(IconSet), "Create")]
private static void OnIconSetCreate(IconSet __instance, bool __state)
{
if (!__state || !EnsureAssets())
{
return;
}
int num = 1;
foreach (uint value in __instance.spriteIndexMap.Values)
{
if (value >= num)
{
num = (int)(value + 1);
}
}
if (num + 2 > 625 || __instance.signalIconIndex.Length <= 531)
{
ManualLogSource log = SmartWarningIconsPlugin.Log;
if (log != null)
{
log.LogWarning((object)"IconSet has no room for two custom warning icons.");
}
}
else
{
RegisterIcon(__instance, _yellowIcon, _yellowSprite, 525, num);
RegisterIcon(__instance, _redIcon, _redSprite, 531, num + 1);
__instance.texture.Apply(true);
__instance.signalIconIndexBuffer.SetData((Array)__instance.signalIconIndex);
}
}
private static void RegisterIcon(IconSet set, Texture2D texture, Sprite sprite, int signalId, int id)
{
int num = id % 25 * 80;
int num2 = id / 25 * 80;
set.texture.SetPixels(num, num2, 80, 80, texture.GetPixels());
set.spriteIndexMap[sprite] = (uint)id;
set.signalIconIndex[signalId] = (uint)id;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(SignalProtoSet), "IconSprite")]
private static void OnSignalIconSprite(int signalId, ref Sprite __result)
{
if (_assetsReady && !((Object)(object)__result != (Object)null))
{
switch (signalId)
{
case 531:
__result = _redSprite;
break;
case 525:
__result = _yellowSprite;
break;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIOptionWindow), "_OnCreate")]
private static void OnWarnMaskCreated(UIOptionWindow __instance)
{
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_004b: 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_007b: Unknown result type (might be due to invalid IL or missing references)
//IL_0080: Unknown result type (might be due to invalid IL or missing references)
//IL_0083: Unknown result type (might be due to invalid IL or missing references)
//IL_009a: Unknown result type (might be due to invalid IL or missing references)
UIButton[] buildingWarnButtons = __instance.buildingWarnButtons;
if (buildingWarnButtons == null || buildingWarnButtons.Length == 0 || !EnsureAssets())
{
return;
}
UIButton val = buildingWarnButtons[0];
float num = ((RectTransform)((Component)val).transform).anchoredPosition.x;
UIButton[] array = buildingWarnButtons;
foreach (UIButton val2 in array)
{
float x = ((RectTransform)((Component)val2).transform).anchoredPosition.x;
if (x > num)
{
val = val2;
num = x;
}
}
Vector2 sizeDelta = ((RectTransform)((Component)val).transform).sizeDelta;
UIButton val3 = AddWarnButton(val, num + sizeDelta.x, _yellowSprite, 25u);
UIButton val4 = AddWarnButton(val, num + sizeDelta.x * 2f, _redSprite, 31u);
UIButton[] array2 = (UIButton[])(object)new UIButton[buildingWarnButtons.Length + 2];
Array.Copy(buildingWarnButtons, array2, buildingWarnButtons.Length);
array2[buildingWarnButtons.Length] = val3;
array2[buildingWarnButtons.Length + 1] = val4;
__instance.buildingWarnButtons = array2;
UpdateWarnTips(__instance);
}
private static UIButton AddWarnButton(UIButton template, float x, Sprite sprite, uint signType)
{
//IL_002d: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Expected O, but got Unknown
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
UIButton obj = Object.Instantiate<UIButton>(template, ((Component)template).transform.parent);
((Object)obj).name = $"smart-warning-icon-{signType}";
RectTransform val = (RectTransform)((Component)obj).transform;
val.anchoredPosition = new Vector2(x, val.anchoredPosition.y);
Transform obj2 = ((Component)obj).transform.Find("image");
Image val2 = ((obj2 != null) ? ((Component)obj2).GetComponent<Image>() : null);
if ((Object)(object)val2 != (Object)null)
{
val2.sprite = sprite;
}
obj.data = 1 << (int)(signType - 1);
return obj;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIOptionWindow), "OnLanguageChange")]
private static void UpdateWarnTips(UIOptionWindow __instance)
{
UIButton[] buildingWarnButtons = __instance.buildingWarnButtons;
if (!_assetsReady || buildingWarnButtons == null)
{
return;
}
bool isZHCN = Localization.isZHCN;
UIButton[] array = buildingWarnButtons;
foreach (UIButton val in array)
{
string name = ((Object)val).name;
if (!(name == "smart-warning-icon-25"))
{
if (name == "smart-warning-icon-31")
{
val.tips.tipTitle = (isZHCN ? "缺原料" : "Insufficient Input");
val.tips.tipText = (isZHCN ? "显示建筑上方的缺原料警告图标。" : "Show the insufficient-input sign above buildings.");
}
}
else
{
val.tips.tipTitle = (isZHCN ? "产物堆砌" : "Output Piled Up");
val.tips.tipText = (isZHCN ? "建筑因产物缓冲区满而停工时,在其上方显示警告图标。" : "Show the warning above a building that has stopped because its output buffer is full.");
}
}
}
internal static void Dispose()
{
ReleaseAtlas();
ReleaseAssets();
_assetsAttempted = false;
_failedAtlas = null;
}
private static void ReleaseAtlas()
{
if ((Object)(object)_patchedMaterial != (Object)null && (Object)(object)_patchedMaterial.mainTexture == (Object)(object)_patchedAtlas)
{
_patchedMaterial.mainTexture = _sourceAtlas;
}
if ((Object)(object)_patchedAtlas != (Object)null)
{
Object.Destroy((Object)(object)_patchedAtlas);
}
_patchedMaterial = null;
_sourceAtlas = null;
_patchedAtlas = null;
}
private static void ReleaseAssets()
{
Object.Destroy((Object)(object)_redSprite);
Object.Destroy((Object)(object)_yellowSprite);
Object.Destroy((Object)(object)_redCell);
Object.Destroy((Object)(object)_yellowCell);
Object.Destroy((Object)(object)_redIcon);
Object.Destroy((Object)(object)_yellowIcon);
_redSprite = (_yellowSprite = null);
_redCell = (_yellowCell = (_redIcon = (_yellowIcon = null)));
_assetsReady = false;
}
}
[BepInPlugin("TTenYX.DSP.SmartWarningIcons", "SmartWarningIcons", "1.0.0")]
public class SmartWarningIconsPlugin : BaseUnityPlugin
{
public const string Guid = "TTenYX.DSP.SmartWarningIcons";
public const string Name = "SmartWarningIcons";
public const string Version = "1.0.0";
internal static ManualLogSource Log;
internal static string PluginDir;
private Harmony _harmony;
private void Awake()
{
//IL_0070: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
PluginDir = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
if (string.IsNullOrEmpty(PluginDir))
{
PluginDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
}
InsufficientInputSign.Init(((BaseUnityPlugin)this).Config);
OutputPileSign.Init(((BaseUnityPlugin)this).Config);
VeinDepletionSign.Init(((BaseUnityPlugin)this).Config);
_harmony = new Harmony("TTenYX.DSP.SmartWarningIcons");
_harmony.PatchAll(typeof(SignIcons));
_harmony.PatchAll(typeof(OutputPileSign));
Log.LogInfo((object)"SmartWarningIcons 1.0.0 loaded (GUID: TTenYX.DSP.SmartWarningIcons)");
}
private void OnDestroy()
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
SignIcons.Dispose();
Log = null;
}
}
internal static class VeinDepletionSign
{
private static ConfigEntry<bool> _enable;
private static ConfigEntry<bool> _useTotal;
private static ConfigEntry<long> _threshold;
internal static bool Enabled => _enable.Value;
internal static void Init(ConfigFile config)
{
_enable = config.Bind<bool>("VeinDepletion", "Enable", true, "Take over the vein-depletion warning so its threshold and amount mode follow this mod's config.\n接管矿脉枯竭警告,使阈值与判断方式按本插件配置生效。");
_useTotal = config.Bind<bool>("VeinDepletion", "UseTotalVeinAmount", true, "true = sum the amount over every vein spot the miner covers; false = use the single smallest vein spot.\ntrue = 按矿机覆盖的所有矿点总量判断;false = 按单个最小矿点的量判断。");
_threshold = config.Bind<long>("VeinDepletion", "WarnThreshold", 1000L, "Show the cut-production-soon sign when the compared amount is below this value.\n判断量低于该值时显示「即将减产」警告。");
}
internal static void ScanMiners(FactorySystem fs)
{
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Invalid comparison between Unknown and I4
SignData[] entitySignPool = fs.factory.entitySignPool;
VeinData[] veinPool = fs.factory.veinPool;
bool value = _useTotal.Value;
long value2 = _threshold.Value;
for (int i = 1; i < fs.minerCursor; i++)
{
ref MinerComponent reference = ref fs.minerPool[i];
if (reference.id != i || (int)reference.type != 2)
{
continue;
}
ref uint signType = ref entitySignPool[reference.entityId].signType;
if (signType == 0 || signType == 7)
{
long num;
if (value)
{
((MinerComponent)(ref reference)).GetTotalVeinAmount(veinPool);
num = reference.totalVeinAmount;
}
else
{
((MinerComponent)(ref reference)).GetMinimumVeinAmount(fs.factory, veinPool);
num = reference.minimumVeinAmount;
}
signType = ((num < value2) ? 7u : 0u);
}
}
}
}
}