Files
萌泪酱 9fb75f70b8 feat(uxassist): add a shortcut to reverse conveyor belts
- Add a Factory toggle and a configurable Alt+R shortcut for the built belt under the cursor.
- Reuse native reversal while preserving the belt inspector context and native limits.
- Add English and Chinese settings labels, usage notes, and changelog entries.
2026-11-16 09:06:15 +08:00

254 lines
15 KiB
C#

using System;
using System.IO;
using System.Runtime.CompilerServices;
using BepInEx.Configuration;
using CommonAPI.Systems;
using UnityEngine;
using UXAssist.Common;
using UXAssist.Common.ModFeatures;
using GameLogicProc = UXAssist.Common.GameLogic;
namespace UXAssist.Patches.Factory;
[ModFeature("Factory", Order = 10)]
public static class FactoryPatch
{
/// <summary>Returns the mining cost rate shared by vein-protection display calculations.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static float GetMiningCostRate(GameHistoryData history) => history.miningCostRate;
public static ConfigEntry<bool> UnlimitInteractiveEnabled;
public static ConfigEntry<bool> RemoveSomeConditionEnabled;
public static ConfigEntry<bool> NightLightEnabled;
public static ConfigEntry<float> NightLightAngleX;
public static ConfigEntry<float> NightLightAngleY;
public static ConfigEntry<bool> RemoveBuildRangeLimitEnabled;
public static ConfigEntry<bool> LargerAreaForUpgradeAndDismantleEnabled;
public static ConfigEntry<bool> LargerAreaForTerraformEnabled;
public static ConfigEntry<bool> OffGridBuildingEnabled;
public static ConfigEntry<bool> TreatStackingAsSingleEnabled;
public static ConfigEntry<bool> QuickBuildAndDismantleLabsEnabled;
public static ConfigEntry<bool> ProtectVeinsFromExhaustionEnabled;
public static ConfigEntry<bool> DoNotRenderEntitiesEnabled;
public static ConfigEntry<bool> DoNotRenderEntitiesHideSortersEnabled;
public static ConfigEntry<bool> DragBuildPowerPolesEnabled;
public static ConfigEntry<bool> DragBuildPowerPolesAlternatelyEnabled;
public static ConfigEntry<bool> AutoConstructButtonEnabled;
public static ConfigEntry<bool> AutoConstructEnabled;
public static ConfigEntry<bool> BeltSignalsForBuyOutEnabled;
public static ConfigEntry<bool> TankFastFillInAndTakeOutEnabled;
public static ConfigEntry<int> TankFastFillInAndTakeOutMultiplier;
public static ConfigEntry<bool> CutConveyorBeltEnabled;
public static ConfigEntry<bool> ReverseConveyorBeltEnabled;
public static ConfigEntry<bool> TweakBuildingBufferEnabled;
public static ConfigEntry<int> AssemblerBufferTimeMultiplier;
public static ConfigEntry<int> AssemblerBufferMininumMultiplier;
public static ConfigEntry<int> LabBufferMaxCountForAssemble;
public static ConfigEntry<int> LabBufferExtraCountForAdvancedAssemble;
public static ConfigEntry<int> LabBufferMaxCountForResearch;
public static ConfigEntry<int> ReceiverBufferCount;
public static ConfigEntry<int> EjectorBufferCount;
public static ConfigEntry<int> SiloBufferCount;
public static ConfigEntry<bool> ShortcutKeysForBlueprintCopyEnabled;
public static ConfigEntry<bool> PressShiftToTakeWholeBeltItemsEnabled;
public static ConfigEntry<bool> PressShiftToTakeWholeBeltItemsIncludeBranches;
public static ConfigEntry<bool> PressShiftToTakeWholeBeltItemsIncludeInserters;
internal static PressKeyBind _doNotRenderEntitiesKey;
internal static PressKeyBind _offgridfForPathsKey;
internal static PressKeyBind _cutConveyorBeltKey;
internal static PressKeyBind _reverseConveyorBeltKey;
internal static PressKeyBind _dismantleBlueprintSelectionKey;
internal static PressKeyBind _selectAllBuildingsInBlueprintCopyKey;
internal static int _tankFastFillInAndTakeOutMultiplierRealValue = 2;
public static void Init()
{
_doNotRenderEntitiesKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
{
key = new CombineKey(0, 0, ECombineKeyAction.OnceClick, true),
conflictGroup = KeyBindConflict.MOVEMENT | KeyBindConflict.FLYING | KeyBindConflict.BUILD_MODE_1 | KeyBindConflict.KEYBOARD_KEYBIND,
name = "ToggleDoNotRenderEntities",
canOverride = true
}
);
_offgridfForPathsKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
{
key = new CombineKey(0, 0, ECombineKeyAction.OnceClick, true),
conflictGroup = KeyBindConflict.MOVEMENT | KeyBindConflict.UI | KeyBindConflict.FLYING | KeyBindConflict.BUILD_MODE_1 | KeyBindConflict.KEYBOARD_KEYBIND,
name = "OffgridForPaths",
canOverride = true
}
);
_cutConveyorBeltKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
{
key = new CombineKey((int)KeyCode.X, CombineKey.ALT_COMB, ECombineKeyAction.OnceClick, false),
conflictGroup = KeyBindConflict.MOVEMENT | KeyBindConflict.FLYING | KeyBindConflict.SAILING | KeyBindConflict.BUILD_MODE_1 | KeyBindConflict.KEYBOARD_KEYBIND,
name = "CutConveyorBelt",
canOverride = true
}
);
_reverseConveyorBeltKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
{
key = new CombineKey((int)KeyCode.R, CombineKey.ALT_COMB, ECombineKeyAction.OnceClick, false),
conflictGroup = KeyBindConflict.MOVEMENT | KeyBindConflict.FLYING | KeyBindConflict.SAILING | KeyBindConflict.BUILD_MODE_1 | KeyBindConflict.KEYBOARD_KEYBIND,
name = "ReverseConveyorBelt",
canOverride = true
}
);
_dismantleBlueprintSelectionKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
{
key = new CombineKey((int)KeyCode.X, CombineKey.CTRL_COMB, ECombineKeyAction.OnceClick, false),
conflictGroup = KeyBindConflict.KEYBOARD_KEYBIND,
name = "DismantleBlueprintSelection",
canOverride = true
}
);
_selectAllBuildingsInBlueprintCopyKey = KeyBindings.RegisterKeyBinding(new BuiltinKey
{
key = new CombineKey((int)KeyCode.A, CombineKey.CTRL_COMB, ECombineKeyAction.OnceClick, false),
conflictGroup = KeyBindConflict.KEYBOARD_KEYBIND,
name = "SelectAllBuildingsInBlueprintCopy",
canOverride = true
}
);
BeltSignalPatch.InitPersist();
VeinProtectionPatch.InitConfig();
UnlimitInteractiveEnabled.SettingChanged += (_, _) => ArchitectModePatch.UnlimitInteractive.Enable(UnlimitInteractiveEnabled.Value);
RemoveSomeConditionEnabled.SettingChanged += (_, _) => ArchitectModePatch.RemoveSomeConditionBuild.Enable(RemoveSomeConditionEnabled.Value);
NightLightEnabled.SettingChanged += (_, _) => RenderingPatch.NightLight.Enable(NightLightEnabled.Value);
RemoveBuildRangeLimitEnabled.SettingChanged += (_, _) => ArchitectModePatch.RemoveBuildRangeLimit.Enable(RemoveBuildRangeLimitEnabled.Value);
LargerAreaForUpgradeAndDismantleEnabled.SettingChanged += (_, _) => ArchitectModePatch.LargerAreaForUpgradeAndDismantle.Enable(LargerAreaForUpgradeAndDismantleEnabled.Value);
LargerAreaForTerraformEnabled.SettingChanged += (_, _) => ArchitectModePatch.LargerAreaForTerraform.Enable(LargerAreaForTerraformEnabled.Value);
OffGridBuildingEnabled.SettingChanged += (_, _) => BuildToolPatch.OffGridBuilding.Enable(OffGridBuildingEnabled.Value);
TreatStackingAsSingleEnabled.SettingChanged += (_, _) => BuildToolPatch.TreatStackingAsSingle.Enable(TreatStackingAsSingleEnabled.Value);
QuickBuildAndDismantleLabsEnabled.SettingChanged += (_, _) => FactoryBuildPatches.QuickBuildAndDismantleLab.Enable(QuickBuildAndDismantleLabsEnabled.Value);
ProtectVeinsFromExhaustionEnabled.SettingChanged += (_, _) => VeinProtectionPatch.ProtectVeinsFromExhaustion.Enable(ProtectVeinsFromExhaustionEnabled.Value);
DoNotRenderEntitiesEnabled.SettingChanged += (_, _) => RenderingPatch.DoNotRenderEntities.Enable(DoNotRenderEntitiesEnabled.Value);
DoNotRenderEntitiesHideSortersEnabled.SettingChanged += (_, _) => RenderingPatch.DoNotRenderEntities.HideSorters = DoNotRenderEntitiesHideSortersEnabled.Value;
DragBuildPowerPolesEnabled.SettingChanged += (_, _) => BuildToolPatch.DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value);
DragBuildPowerPolesAlternatelyEnabled.SettingChanged += (_, _) => BuildToolPatch.DragBuildPowerPoles.AlternatelyChanged();
// The button config only controls UI visibility; the behaviour itself follows
// AutoConstructEnabled, so hiding the button cannot silently disable auto-construct.
AutoConstructButtonEnabled.SettingChanged += (_, _) => Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility();
AutoConstructEnabled.SettingChanged += (_, _) =>
{
FactoryBuildPatches.AutoConstructPatch.Enable(AutoConstructEnabled.Value);
Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility();
};
BeltSignalsForBuyOutEnabled.SettingChanged += (_, _) => BeltSignalPatch.BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value);
TankFastFillInAndTakeOutEnabled.SettingChanged += (_, _) => BuildToolPatch.TankFastFillInAndTakeOut.Enable(TankFastFillInAndTakeOutEnabled.Value);
TankFastFillInAndTakeOutMultiplier.SettingChanged += (_, _) => UpdateTankFastFillInAndTakeOutMultiplierRealValue();
TweakBuildingBufferEnabled.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.Enable(TweakBuildingBufferEnabled.Value);
AssemblerBufferTimeMultiplier.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshAssemblerBufferMultipliers();
AssemblerBufferMininumMultiplier.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshAssemblerBufferMultipliers();
LabBufferMaxCountForAssemble.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshLabBufferMaxCountForAssemble();
LabBufferExtraCountForAdvancedAssemble.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshLabBufferMaxCountForAssemble();
LabBufferMaxCountForResearch.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshLabBufferMaxCountForResearch();
ReceiverBufferCount.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshReceiverBufferCount();
EjectorBufferCount.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshEjectorBufferCount();
SiloBufferCount.SettingChanged += (_, _) => BuildingBufferPatch.TweakBuildingBuffer.RefreshSiloBufferCount();
PressShiftToTakeWholeBeltItemsEnabled.SettingChanged += (_, _) => BuildToolPatch.PressShiftToTakeWholeBeltItems.Enable(PressShiftToTakeWholeBeltItemsEnabled.Value);
UXAssist.RegisterExporter(Export);
UXAssist.RegisterImporter((r, version) => Import(r, version));
}
public static void Start()
{
ArchitectModePatch.UnlimitInteractive.Enable(UnlimitInteractiveEnabled.Value);
ArchitectModePatch.RemoveSomeConditionBuild.Enable(RemoveSomeConditionEnabled.Value);
RenderingPatch.NightLight.Enable(NightLightEnabled.Value);
ArchitectModePatch.RemoveBuildRangeLimit.Enable(RemoveBuildRangeLimitEnabled.Value);
ArchitectModePatch.LargerAreaForUpgradeAndDismantle.Enable(LargerAreaForUpgradeAndDismantleEnabled.Value);
ArchitectModePatch.LargerAreaForTerraform.Enable(LargerAreaForTerraformEnabled.Value);
BuildToolPatch.OffGridBuilding.Enable(OffGridBuildingEnabled.Value);
BuildToolPatch.TreatStackingAsSingle.Enable(TreatStackingAsSingleEnabled.Value);
FactoryBuildPatches.QuickBuildAndDismantleLab.Enable(QuickBuildAndDismantleLabsEnabled.Value);
VeinProtectionPatch.ProtectVeinsFromExhaustion.Enable(ProtectVeinsFromExhaustionEnabled.Value);
RenderingPatch.DoNotRenderEntities.Enable(DoNotRenderEntitiesEnabled.Value);
RenderingPatch.DoNotRenderEntities.HideSorters = DoNotRenderEntitiesHideSortersEnabled.Value;
BuildToolPatch.DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value);
FactoryBuildPatches.AutoConstructPatch.Enable(AutoConstructEnabled.Value);
BeltSignalPatch.BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value);
BuildToolPatch.TankFastFillInAndTakeOut.Enable(TankFastFillInAndTakeOutEnabled.Value);
BuildingBufferPatch.TweakBuildingBuffer.Enable(TweakBuildingBufferEnabled.Value);
BuildToolPatch.PressShiftToTakeWholeBeltItems.Enable(PressShiftToTakeWholeBeltItemsEnabled.Value);
BuildToolPatch.EnableAlwaysOnPatches(true);
UpdateTankFastFillInAndTakeOutMultiplierRealValue();
}
public static void Uninit()
{
BuildToolPatch.EnableAlwaysOnPatches(false);
BuildToolPatch.PressShiftToTakeWholeBeltItems.Enable(false);
BuildingBufferPatch.TweakBuildingBuffer.Enable(false);
BuildToolPatch.TankFastFillInAndTakeOut.Enable(false);
BeltSignalPatch.BeltSignalsForBuyOut.Enable(false);
FactoryBuildPatches.AutoConstructPatch.Enable(false);
BuildToolPatch.DragBuildPowerPoles.Enable(false);
RenderingPatch.DoNotRenderEntities.Enable(false);
VeinProtectionPatch.ProtectVeinsFromExhaustion.Enable(false);
FactoryBuildPatches.QuickBuildAndDismantleLab.Enable(false);
BuildToolPatch.TreatStackingAsSingle.Enable(false);
BuildToolPatch.OffGridBuilding.Enable(false);
ArchitectModePatch.LargerAreaForTerraform.Enable(false);
ArchitectModePatch.LargerAreaForUpgradeAndDismantle.Enable(false);
ArchitectModePatch.RemoveBuildRangeLimit.Enable(false);
RenderingPatch.NightLight.Enable(false);
ArchitectModePatch.RemoveSomeConditionBuild.Enable(false);
ArchitectModePatch.UnlimitInteractive.Enable(false);
BeltSignalPatch.UninitPersist();
}
private static void UpdateTankFastFillInAndTakeOutMultiplierRealValue()
{
_tankFastFillInAndTakeOutMultiplierRealValue = Mathf.Max(1, TankFastFillInAndTakeOutMultiplier.Value) * 2;
}
public static void OnInputUpdate()
{
if (DSPGame.IsMenuDemo) return;
if (_doNotRenderEntitiesKey.keyValue)
DoNotRenderEntitiesEnabled.Value = !DoNotRenderEntitiesEnabled.Value;
if (CutConveyorBeltEnabled.Value && _cutConveyorBeltKey.keyValue)
{
var raycast = GameMain.mainPlayer.controller?.cmd.raycast;
int beltId;
if (raycast != null && raycast.castEntity.id > 0 && (beltId = raycast.castEntity.beltId) > 0)
{
var cargoTraffic = raycast.planet.factory.cargoTraffic;
Functions.FactoryFunctions.CutConveyorBelt(cargoTraffic, beltId);
}
}
if (ReverseConveyorBeltEnabled.Value && _reverseConveyorBeltKey.keyValue
&& GameMain.isRunning && !GameMain.isPaused && !VFInput.onGUI)
{
var raycast = GameMain.mainPlayer?.controller?.cmd.raycast;
if (raycast != null && raycast.castEntity.id > 0 && raycast.castEntity.beltId > 0)
Functions.FactoryFunctions.ReverseConveyorBelt(raycast.planet?.factory, raycast.castEntity.beltId);
}
if (ShortcutKeysForBlueprintCopyEnabled.Value)
{
if (_dismantleBlueprintSelectionKey.keyValue)
Functions.FactoryFunctions.DismantleBlueprintSelectedBuildings();
if (_selectAllBuildingsInBlueprintCopyKey.keyValue)
Functions.FactoryFunctions.SelectAllBuildingsInBlueprintCopy();
}
}
public static void Export(BinaryWriter w)
{
BeltSignalPatch.Export(w);
}
public static void Import(BinaryReader r, ushort version)
{
BeltSignalPatch.Import(r);
}
}