mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
394 lines
17 KiB
C#
394 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UXAssist.Common;
|
|
using UXAssist.Production;
|
|
using UXAssist.UI;
|
|
using Util = UXAssist.UI.Util;
|
|
|
|
namespace UXAssist.Patches;
|
|
|
|
public class BlueprintDescriptionPatch : PatchImpl<BlueprintDescriptionPatch>
|
|
{
|
|
private static readonly Dictionary<UIBlueprintInspector, InspectorControls> Inspectors = new();
|
|
|
|
public static void Start() => Enable(true);
|
|
|
|
public static void Uninit() => Enable(false);
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
foreach (var controls in Inspectors.Values) controls.Destroy();
|
|
Inspectors.Clear();
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnCreate))]
|
|
private static void OnCreate(UIBlueprintInspector __instance)
|
|
{
|
|
GetControls(__instance);
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnOpen))]
|
|
private static void OnOpen(UIBlueprintInspector __instance)
|
|
{
|
|
GetControls(__instance)?.ResetSession();
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnClose))]
|
|
private static void OnClose(UIBlueprintInspector __instance)
|
|
{
|
|
if (Inspectors.TryGetValue(__instance, out var controls)) controls.ResetSession();
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnFree))]
|
|
private static void OnFree(UIBlueprintInspector __instance)
|
|
{
|
|
if (!Inspectors.TryGetValue(__instance, out var controls)) return;
|
|
controls.Destroy();
|
|
Inspectors.Remove(__instance);
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector._OnDestroy))]
|
|
private static void OnDestroy(UIBlueprintInspector __instance)
|
|
{
|
|
OnFree(__instance);
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector.Refresh))]
|
|
private static void Refresh(UIBlueprintInspector __instance)
|
|
{
|
|
try
|
|
{
|
|
GetControls(__instance)?.Refresh();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
UXAssist.Logger.LogError($"Blueprint description inspector refresh failed: {exception}");
|
|
}
|
|
}
|
|
|
|
[HarmonyPostfix, HarmonyPatch(typeof(UIBlueprintInspector), nameof(UIBlueprintInspector.OnLanguageChanged))]
|
|
private static void OnLanguageChanged(UIBlueprintInspector __instance)
|
|
{
|
|
try
|
|
{
|
|
if (Inspectors.TryGetValue(__instance, out var controls)) controls.UpdateLabels();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
UXAssist.Logger.LogError($"Blueprint description localization refresh failed: {exception}");
|
|
}
|
|
}
|
|
|
|
private static InspectorControls GetControls(UIBlueprintInspector inspector)
|
|
{
|
|
if (!inspector) return null;
|
|
if (Inspectors.TryGetValue(inspector, out var controls)) return controls;
|
|
try
|
|
{
|
|
controls = new InspectorControls(inspector);
|
|
Inspectors.Add(inspector, controls);
|
|
return controls;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
UXAssist.Logger.LogError($"Blueprint description controls could not be created: {exception}");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private sealed class InspectorControls
|
|
{
|
|
private const float Left = 12f;
|
|
private const float ControlsGap = 12f;
|
|
|
|
private readonly UIBlueprintInspector _inspector;
|
|
private RectTransform _panel;
|
|
private UIButton _entry;
|
|
private Text _entryText;
|
|
private UIButton _button;
|
|
private Text _buttonText;
|
|
private UIButton _cancelButton;
|
|
private Text _cancelText;
|
|
private MyCheckBox _checkbox;
|
|
private MyCornerComboBox _basisCombo;
|
|
private Text _basisLabel;
|
|
private UIButton _basisHelp;
|
|
private BlueprintData _currentBlueprint;
|
|
private bool _manualProliferation;
|
|
|
|
public InspectorControls(UIBlueprintInspector inspector)
|
|
{
|
|
_inspector = inspector;
|
|
try
|
|
{
|
|
// The native description has a 22px title row above its editable text.
|
|
_entry = LayoutHelper.AddButton(0, 0, inspector.descGroupTrans,
|
|
I18NKeys.BlueprintDescriptionOptions, 13, "blueprint-description-options", ToggleOptions);
|
|
_entryText = _entry.transform.Find("button-text").GetComponent<Text>();
|
|
_entry.tips = inspector.tipButton2.tips;
|
|
_entry.tips.topLevel = true;
|
|
Util.NormalizeRectWithTopRight(_entry, 0, 0, inspector.descGroupTrans);
|
|
|
|
// Reuse the native background, including its blur material, only while options are open.
|
|
var source = UIRoot.instance.uiGame.inserterWindow.transform.Find("panel-bg").GetComponent<Image>();
|
|
_panel = UnityEngine.Object.Instantiate(source, inspector.rectTrans).rectTransform;
|
|
_panel.gameObject.name = "uxassist-blueprint-description-options";
|
|
for (var i = _panel.childCount - 1; i >= 0; i--)
|
|
{
|
|
var child = _panel.GetChild(i).gameObject;
|
|
child.SetActive(false);
|
|
UnityEngine.Object.Destroy(child);
|
|
}
|
|
_panel.anchorMin = new Vector2(0, 1);
|
|
_panel.anchorMax = Vector2.one;
|
|
_panel.pivot = new Vector2(0, 1);
|
|
_panel.anchoredPosition = new Vector2(Left, -Left);
|
|
_panel.sizeDelta = new Vector2(-Left * 2, 152);
|
|
_button = LayoutHelper.AddButton(Left, 12, _panel,
|
|
I18NKeys.BlueprintAutoFillDescription, 15, "blueprint-auto-fill-description", Generate);
|
|
_buttonText = _button.transform.Find("button-text").GetComponent<Text>();
|
|
_cancelButton = LayoutHelper.AddButton(0, 0, _panel,
|
|
I18NKeys.Cancel, 14, "blueprint-description-cancel", CloseOptions);
|
|
_cancelText = _cancelButton.transform.Find("button-text").GetComponent<Text>();
|
|
Util.NormalizeRectWithTopRight(_cancelButton, Left, 14, _panel);
|
|
_checkbox = MyCheckBox.CreateCheckBox(Left, 52, _panel, false,
|
|
I18NKeys.BlueprintProliferation.Translate(), 15).WithSmallerBox(22f);
|
|
_checkbox.gameObject.name = "blueprint-proliferation";
|
|
_checkbox.OnChecked += OnProliferationChecked;
|
|
_basisLabel = LayoutHelper.AddText(Left, 80, _panel, I18NKeys.BlueprintProductionBasis, 14);
|
|
_basisCombo = MyCornerComboBox.CreateComboBox(Left, 108, _panel).WithFontSize(14)
|
|
.WithItems(I18NKeys.BlueprintFinalProducts, I18NKeys.BlueprintBottleneckIntermediate)
|
|
.WithIndex(0);
|
|
var combo = _basisCombo.GetComponent<UIComboBox>();
|
|
combo.CanInput = false;
|
|
combo.autoWidth = false;
|
|
var comboRect = (RectTransform)_basisCombo.transform;
|
|
comboRect.anchorMax = Vector2.one;
|
|
comboRect.sizeDelta = new Vector2(-Left * 2, 28);
|
|
_basisHelp = LayoutHelper.AddTipsButton(Left, 80, _panel,
|
|
I18NKeys.BlueprintProductionBasis.Translate(), I18NKeys.BlueprintProductionBasisTip.Translate(), "");
|
|
UpdateLabels();
|
|
_panel.gameObject.SetActive(false);
|
|
}
|
|
catch
|
|
{
|
|
Destroy();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void ResetSession()
|
|
{
|
|
_currentBlueprint = null;
|
|
_manualProliferation = false;
|
|
_basisCombo.SetIndex(0);
|
|
if (_inspector.blueprint != null) SyncDefault();
|
|
else _checkbox.Checked = false;
|
|
CloseOptions();
|
|
_entry.gameObject.SetActive(_inspector.blueprint != null);
|
|
}
|
|
|
|
public void Refresh()
|
|
{
|
|
SyncDefault();
|
|
_entry.gameObject.SetActive(_inspector.blueprint != null);
|
|
}
|
|
|
|
private void ToggleOptions()
|
|
{
|
|
_panel.SetAsLastSibling();
|
|
_panel.gameObject.SetActive(!_panel.gameObject.activeSelf);
|
|
}
|
|
|
|
private void CloseOptions() => _panel.gameObject.SetActive(false);
|
|
|
|
public void UpdateLabels()
|
|
{
|
|
_entryText.text = I18NKeys.BlueprintDescriptionOptions.Translate();
|
|
((RectTransform)_entry.transform).sizeDelta =
|
|
new Vector2(Util.GetPreferredWidth(_entryText) + 16, 20);
|
|
_entry.tips.tipTitle = I18NKeys.BlueprintAutoFillDescription.Translate();
|
|
_entry.tips.tipText = I18NKeys.BlueprintDescriptionOptionsTip.Translate();
|
|
_entry.UpdateTip();
|
|
_buttonText.text = I18NKeys.BlueprintAutoFillDescription.Translate();
|
|
_cancelText.text = I18NKeys.Cancel.Translate();
|
|
((RectTransform)_cancelButton.transform).sizeDelta =
|
|
new Vector2(Util.GetPreferredWidth(_cancelText) + 20, 24);
|
|
_checkbox.SetLabelText(I18NKeys.BlueprintProliferation.Translate());
|
|
_basisLabel.text = I18NKeys.BlueprintProductionBasis.Translate();
|
|
var combo = _basisCombo.GetComponent<UIComboBox>();
|
|
var index = combo.itemIndex;
|
|
_basisCombo.SetItems(I18NKeys.BlueprintFinalProducts, I18NKeys.BlueprintBottleneckIntermediate);
|
|
_basisCombo.SetIndex(index);
|
|
_basisHelp.tips.tipTitle = I18NKeys.BlueprintProductionBasis.Translate();
|
|
_basisHelp.tips.tipText = I18NKeys.BlueprintProductionBasisTip.Translate();
|
|
_basisHelp.UpdateTip();
|
|
var width = Math.Max(160f, Util.GetPreferredWidth(_buttonText) + 26f);
|
|
_button.transform.GetComponent<RectTransform>().sizeDelta =
|
|
new Vector2(width, _button.transform.GetComponent<RectTransform>().sizeDelta.y);
|
|
var labelWidth = Util.GetPreferredWidth(_basisLabel);
|
|
_basisLabel.rectTransform.sizeDelta = new Vector2(labelWidth, _basisLabel.rectTransform.sizeDelta.y);
|
|
Util.NormalizeRectWithTopLeft(_basisHelp, Left + labelWidth + ControlsGap, 80, _panel);
|
|
}
|
|
|
|
public void Destroy()
|
|
{
|
|
if (_entry)
|
|
{
|
|
_entry.button.onClick.RemoveListener(ToggleOptions);
|
|
UnityEngine.Object.Destroy(_entry.gameObject);
|
|
}
|
|
if (_button)
|
|
_button.button.onClick.RemoveListener(Generate);
|
|
if (_cancelButton)
|
|
_cancelButton.button.onClick.RemoveListener(CloseOptions);
|
|
if (_checkbox)
|
|
_checkbox.OnChecked -= OnProliferationChecked;
|
|
if (!_panel) return;
|
|
_panel.gameObject.SetActive(false);
|
|
UnityEngine.Object.Destroy(_panel.gameObject);
|
|
}
|
|
|
|
private void SyncDefault()
|
|
{
|
|
var blueprint = _inspector.blueprint;
|
|
if (!ReferenceEquals(blueprint, _currentBlueprint))
|
|
{
|
|
CloseOptions();
|
|
_currentBlueprint = blueprint;
|
|
_manualProliferation = false;
|
|
}
|
|
|
|
if (!_manualProliferation)
|
|
{
|
|
var models = LDB.models;
|
|
_checkbox.Checked = blueprint?.buildings?.Any(building => building != null &&
|
|
models?.Select(building.modelIndex)?.prefabDesc?.isSpraycoster == true) == true;
|
|
}
|
|
}
|
|
|
|
private void OnProliferationChecked()
|
|
{
|
|
_manualProliferation = true;
|
|
}
|
|
|
|
private void Generate()
|
|
{
|
|
var blueprint = _inspector.blueprint;
|
|
if (blueprint == null || !blueprint.isValid)
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintInvalid.Translate());
|
|
return;
|
|
}
|
|
|
|
if (blueprint.buildings == null || blueprint.buildings.Length == 0)
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintEmpty.Translate());
|
|
return;
|
|
}
|
|
|
|
var catalog = ProductionCatalogService.Current;
|
|
if (catalog == null)
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintDataUnavailable.Translate());
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (!BlueprintDescriptionFields.IsNativeFormatValid(blueprint.externalFields))
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintInvalidFields.Translate());
|
|
return;
|
|
}
|
|
|
|
_inspector.OnCustomFieldEndEdit(null);
|
|
blueprint = _inspector.blueprint;
|
|
var capture = BlueprintSelectionReader.FromBlueprint(blueprint.buildings);
|
|
if (capture.Diagnostics.Any(diagnostic => diagnostic.Code == ProductionDiagnosticCode.InvalidRequest ||
|
|
diagnostic.Code == ProductionDiagnosticCode.UnknownItem ||
|
|
diagnostic.Code == ProductionDiagnosticCode.DataNotReady))
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintInvalid.Translate());
|
|
return;
|
|
}
|
|
|
|
var request = capture.CreateRequest(_checkbox.Checked);
|
|
request.ProductionBasis = (BlueprintProductionBasis)_basisCombo.GetComponent<UIComboBox>().itemIndex;
|
|
var report = new FactoryBlackBoxAnalyzer(catalog).Analyze(request);
|
|
if (report.Diagnostics.Any(diagnostic => diagnostic.Code == ProductionDiagnosticCode.BlueprintBasisUnavailable))
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintBasisUnavailable.Translate());
|
|
return;
|
|
}
|
|
if (report.Status == ProductionStatus.Failed || report.Status == ProductionStatus.DataNotReady)
|
|
{
|
|
UIRealtimeTip.Popup((report.Status == ProductionStatus.DataNotReady
|
|
? I18NKeys.BlueprintDataUnavailable : I18NKeys.BlueprintCalculationFailed).Translate());
|
|
return;
|
|
}
|
|
|
|
var labels = new BlueprintDescriptionLabels(I18NKeys.BlueprintFactoryPower.Translate(),
|
|
I18NKeys.BlueprintLogisticsPower.Translate(), I18NKeys.BlueprintMissing.Translate(),
|
|
I18NKeys.BlueprintExcess.Translate(), I18NKeys.Unknown.Translate(),
|
|
I18NKeys.BlueprintKnownPortion.Translate(), I18NKeys.BlueprintInputLine.Translate(),
|
|
I18NKeys.BlueprintOutputLine.Translate(), I18NKeys.BlueprintResearchLine.Translate());
|
|
var description = BlueprintDescriptionFormatter.Format(report,
|
|
itemId => LDB.signals.IconTag(itemId, includeName: true), labels);
|
|
if (description.Description.Length > BlueprintDescriptionText.MaximumDescriptionLength)
|
|
{
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintDescriptionTooLong.Translate());
|
|
return;
|
|
}
|
|
|
|
var powerField = new BlueprintDescriptionField(I18NKeys.BlueprintPower.Translate(),
|
|
description.Power.Length > 0 ? description.Power : null,
|
|
I18NKeys.BlueprintPowerEnglish, I18NKeys.BlueprintPowerChinese);
|
|
var result = BlueprintDescriptionFields.TryUpdate(blueprint.externalFields, powerField,
|
|
UIBlueprintInspector.kMaxFieldCount,
|
|
value => BlueprintData.Validate(blueprint.ExternalFieldEscape(value)),
|
|
blueprint.ExternalFieldUnescape, out var updated);
|
|
if (result != BlueprintFieldUpdateResult.Updated)
|
|
{
|
|
UIRealtimeTip.Popup(result == BlueprintFieldUpdateResult.CapacityExceeded
|
|
? string.Format(I18NKeys.BlueprintFieldLimit.Translate(), UIBlueprintInspector.kMaxFieldCount)
|
|
: I18NKeys.BlueprintInvalidFields.Translate());
|
|
return;
|
|
}
|
|
|
|
var previousFields = blueprint.externalFields;
|
|
var previousDescription = blueprint.desc;
|
|
var previousInput = _inspector.descTextInput.text;
|
|
try
|
|
{
|
|
blueprint.externalFields = updated;
|
|
blueprint.desc = description.Description;
|
|
_inspector.descTextInput.text = description.Description;
|
|
_inspector.Refresh(forModify: true, refreshComponent: false, refreshCode: true);
|
|
}
|
|
catch
|
|
{
|
|
blueprint.externalFields = previousFields;
|
|
blueprint.desc = previousDescription;
|
|
_inspector.descTextInput.text = previousInput;
|
|
throw;
|
|
}
|
|
|
|
CloseOptions();
|
|
if (!report.MaterialComplete || !report.PowerBreakdown.FactoryComplete ||
|
|
!report.PowerBreakdown.LogisticsComplete)
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintPartial.Translate());
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
UXAssist.Logger.LogError($"Blueprint description generation failed: {exception}");
|
|
UIRealtimeTip.Popup(I18NKeys.BlueprintCalculationFailed.Translate());
|
|
}
|
|
}
|
|
}
|
|
}
|