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
{
/// Returns the mining cost rate shared by vein-protection display calculations.
[MethodImpl(MethodImplOptions.NoInlining)]
public static float GetMiningCostRate(GameHistoryData history) => history.miningCostRate;
public static ConfigEntry UnlimitInteractiveEnabled;
public static ConfigEntry RemoveSomeConditionEnabled;
public static ConfigEntry NightLightEnabled;
public static ConfigEntry NightLightAngleX;
public static ConfigEntry NightLightAngleY;
public static ConfigEntry RemoveBuildRangeLimitEnabled;
public static ConfigEntry LargerAreaForUpgradeAndDismantleEnabled;
public static ConfigEntry LargerAreaForTerraformEnabled;
public static ConfigEntry OffGridBuildingEnabled;
public static ConfigEntry TreatStackingAsSingleEnabled;
public static ConfigEntry QuickBuildAndDismantleLabsEnabled;
public static ConfigEntry ProtectVeinsFromExhaustionEnabled;
public static ConfigEntry DoNotRenderEntitiesEnabled;
public static ConfigEntry DoNotRenderEntitiesHideSortersEnabled;
public static ConfigEntry DragBuildPowerPolesEnabled;
public static ConfigEntry DragBuildPowerPolesAlternatelyEnabled;
public static ConfigEntry AutoConstructButtonEnabled;
public static ConfigEntry AutoConstructEnabled;
public static ConfigEntry BeltSignalsForBuyOutEnabled;
public static ConfigEntry TankFastFillInAndTakeOutEnabled;
public static ConfigEntry TankFastFillInAndTakeOutMultiplier;
public static ConfigEntry CutConveyorBeltEnabled;
public static ConfigEntry ReverseConveyorBeltEnabled;
public static ConfigEntry TweakBuildingBufferEnabled;
public static ConfigEntry AssemblerBufferTimeMultiplier;
public static ConfigEntry AssemblerBufferMininumMultiplier;
public static ConfigEntry LabBufferMaxCountForAssemble;
public static ConfigEntry LabBufferExtraCountForAdvancedAssemble;
public static ConfigEntry LabBufferMaxCountForResearch;
public static ConfigEntry ReceiverBufferCount;
public static ConfigEntry EjectorBufferCount;
public static ConfigEntry SiloBufferCount;
public static ConfigEntry ShortcutKeysForBlueprintCopyEnabled;
public static ConfigEntry PressShiftToTakeWholeBeltItemsEnabled;
public static ConfigEntry PressShiftToTakeWholeBeltItemsIncludeBranches;
public static ConfigEntry 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);
}
}