refactor(UXAssist): replace magic numbers with constants

This commit is contained in:
2026-06-23 20:25:13 +08:00
parent 7973cbe358
commit a133fd9e14
8 changed files with 76 additions and 124 deletions
@@ -5,6 +5,7 @@ using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UXAssist.Common;
using UXAssist.Common.GameConstants;
namespace UXAssist.Patches.Logistics;
@@ -85,7 +86,7 @@ internal class AutoConfigLogistics : PatchImpl<AutoConfigLogistics>
{
if (__instance.handPrefabDesc.isDispenser)
{
__instance.handBpParams[bpIndex][2] = (int)(long)(5000.0 * LogisticsPatch.AutoConfigDispenserChargePower.Value + 0.5);
__instance.handBpParams[bpIndex][2] = (int)(long)(LogisticsConstants.DispenserChargePowerMultiplier * LogisticsPatch.AutoConfigDispenserChargePower.Value + 0.5);
}
}
+32 -31
View File
@@ -7,6 +7,7 @@ using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UXAssist.Common;
using UXAssist.Common.GameConstants;
namespace UXAssist.Patches.Logistics;
@@ -29,13 +30,13 @@ internal class LogisticsCapacityTweaks : PatchImpl<LogisticsCapacityTweaks>
if (code != _lastKey)
{
_lastKey = code;
_nextKeyTick = main.timei + 30;
_nextKeyTick = main.timei + LogisticsConstants.KeyRepeatInitialDelay;
return true;
}
var currTick = main.timei;
if (_nextKeyTick > currTick) return false;
_nextKeyTick = currTick + 4;
_nextKeyTick = currTick + LogisticsConstants.KeyRepeatInterval;
return true;
}
@@ -54,38 +55,38 @@ internal class LogisticsCapacityTweaks : PatchImpl<LogisticsCapacityTweaks>
if (UpdateKeyPressed(KeyCode.LeftArrow))
{
if (ctrl)
delta = -100000;
delta = -LogisticsConstants.MassiveAdjustment;
else if (alt)
delta = -1000;
delta = -LogisticsConstants.LargeAdjustment;
else
delta = -10;
delta = -LogisticsConstants.SmallAdjustment;
}
else if (UpdateKeyPressed(KeyCode.RightArrow))
{
if (ctrl)
delta = 100000;
delta = LogisticsConstants.MassiveAdjustment;
else if (alt)
delta = 1000;
delta = LogisticsConstants.LargeAdjustment;
else
delta = 10;
delta = LogisticsConstants.SmallAdjustment;
}
else if (UpdateKeyPressed(KeyCode.DownArrow))
{
if (ctrl)
delta = -1000000;
delta = -LogisticsConstants.GargantuanAdjustment;
else if (alt)
delta = -10000;
delta = -LogisticsConstants.HugeAdjustment;
else
delta = -100;
delta = -LogisticsConstants.MediumAdjustment;
}
else if (UpdateKeyPressed(KeyCode.UpArrow))
{
if (ctrl)
delta = 1000000;
delta = LogisticsConstants.GargantuanAdjustment;
else if (alt)
delta = 10000;
delta = LogisticsConstants.HugeAdjustment;
else
delta = 100;
delta = LogisticsConstants.MediumAdjustment;
}
else
{
@@ -129,7 +130,7 @@ internal class LogisticsCapacityTweaks : PatchImpl<LogisticsCapacityTweaks>
int itemCountMax;
if (LogisticsPatch.AllowOverflowInLogisticsEnabled.Value)
{
itemCountMax = 90000000;
itemCountMax = LogisticsConstants.OverflowStorageMax;
}
else
{
@@ -230,7 +231,7 @@ internal class LogisticsCapacityTweaks : PatchImpl<LogisticsCapacityTweaks>
{
var max = storage[j].max;
if (max + 10 < intOldMaxCount || max >= intNewMaxCount) continue;
storage[j].max = Mathf.RoundToInt((float)(max * ratio / 50.0)) * 50;
storage[j].max = Mathf.RoundToInt((float)(max * ratio / LogisticsConstants.LocalStorageRounding)) * LogisticsConstants.LocalStorageRounding;
}
}
@@ -256,7 +257,7 @@ internal class LogisticsCapacityTweaks : PatchImpl<LogisticsCapacityTweaks>
{
var max = storage[j].max;
if (max + 10 < intOldMaxCount || max >= intNewMaxCount) continue;
storage[j].max = Mathf.RoundToInt((float)(max * ratio / 100.0)) * 100;
storage[j].max = Mathf.RoundToInt((float)(max * ratio / LogisticsConstants.RemoteStorageRounding)) * LogisticsConstants.RemoteStorageRounding;
}
}
@@ -275,7 +276,7 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
var window = UIRoot.instance?.uiGame?.stationWindow;
if (window == null) return;
window._Close();
window.maxMiningSpeedSlider.maxValue = 27f;
window.maxMiningSpeedSlider.maxValue = LogisticsConstants.MiningSpeedSliderMaxExtended;
}
protected override void OnDisable()
@@ -283,7 +284,7 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
var window = UIRoot.instance?.uiGame?.stationWindow;
if (window == null) return;
window._Close();
window.maxMiningSpeedSlider.maxValue = 20f;
window.maxMiningSpeedSlider.maxValue = LogisticsConstants.MiningSpeedSliderMaxDefault;
}
[HarmonyTranspiler]
@@ -295,13 +296,13 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
new CodeInstruction(OpCodes.Ldarg_0),
Transpilers.EmitDelegate((UIStationWindow window) =>
{
window.maxMiningSpeedSlider.maxValue = 27f;
window.maxMiningSpeedSlider.maxValue = LogisticsConstants.MiningSpeedSliderMaxExtended;
})
).MatchForward(false,
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(UIStationWindow), nameof(UIStationWindow.maxChargePowerSlider))),
new CodeMatch(ci => ci.IsLdloc()),
new CodeMatch(ci => ci.opcode == OpCodes.Ldc_I4 && ci.OperandIs(0xC350)),
new CodeMatch(ci => ci.opcode == OpCodes.Ldc_I4 && ci.OperandIs(LogisticsConstants.ChargePowerSliderScale)),
new CodeMatch(OpCodes.Conv_I8)
);
var pos = matcher.Pos + 1;
@@ -318,11 +319,11 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
new CodeInstruction(OpCodes.Ldloc_S, locWorkEnergyPerTick),
Transpilers.EmitDelegate((UIStationWindow window, long maxWorkEnergy, long workEnergyPerTick) =>
{
var maxSliderValue = maxWorkEnergy / 50000L;
var maxSliderValue = maxWorkEnergy / LogisticsConstants.ChargePowerSliderScale;
window.maxChargePowerSlider.maxValue = maxSliderValue + 9;
window.maxChargePowerSlider.minValue = maxWorkEnergy / 500000L;
window.maxChargePowerSlider.minValue = maxWorkEnergy / LogisticsConstants.ChargePowerSliderMinScale;
if (workEnergyPerTick <= maxWorkEnergy)
window.maxChargePowerSlider.Set(workEnergyPerTick / 50000L, false);
window.maxChargePowerSlider.Set(workEnergyPerTick / LogisticsConstants.ChargePowerSliderScale, false);
else
window.maxChargePowerSlider.Set(maxSliderValue + (workEnergyPerTick - 1) / maxWorkEnergy + 1, false);
})
@@ -348,9 +349,9 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
matcher.Start().Advance(pos).RemoveInstructions(pos2 - pos).Insert(
Transpilers.EmitDelegate((int speed) =>
{
if (speed <= 30000)
return (speed - 10000) / 1000;
return (speed - 30000) / 10000 + 20;
if (speed <= LogisticsConstants.MaxMiningSpeedBase)
return (speed - LogisticsConstants.MinMiningSpeedBase) / LogisticsConstants.MiningSpeedFineStep;
return (speed - LogisticsConstants.MaxMiningSpeedBase) / LogisticsConstants.MiningSpeedCoarseStep + LogisticsConstants.MiningSpeedSliderMaxDefault;
})
);
return matcher.InstructionEnumeration();
@@ -362,7 +363,7 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
{
var matcher = new CodeMatcher(instructions, generator);
matcher.MatchForward(false,
new CodeMatch(ci => ci.opcode == OpCodes.Ldc_I4 && ci.OperandIs(10000)),
new CodeMatch(ci => ci.opcode == OpCodes.Ldc_I4 && ci.OperandIs(LogisticsConstants.MinMiningSpeedBase)),
new CodeMatch(OpCodes.Ldarg_1)
);
var pos = matcher.Pos;
@@ -378,9 +379,9 @@ internal class GreaterPowerUsageInLogistics : PatchImpl<GreaterPowerUsageInLogis
Transpilers.EmitDelegate((float value) =>
{
var intval = (int)(value + 0.5f);
if (intval <= 20)
return intval * 1000 + 10000;
return (intval - 20) * 10000 + 30000;
if (intval <= LogisticsConstants.MiningSpeedSliderMaxDefault)
return intval * LogisticsConstants.MiningSpeedFineStep + LogisticsConstants.MinMiningSpeedBase;
return (intval - LogisticsConstants.MiningSpeedSliderMaxDefault) * LogisticsConstants.MiningSpeedCoarseStep + LogisticsConstants.MaxMiningSpeedBase;
})
);
return matcher.InstructionEnumeration();
+4 -3
View File
@@ -5,6 +5,7 @@ using HarmonyLib;
using UnityEngine;
using UXAssist.Common;
using UXAssist.Common.ModFeatures;
using UXAssist.Common.GameConstants;
using GameLogicProc = UXAssist.Common.GameLogic;
namespace UXAssist.Patches.Logistics;
@@ -178,7 +179,7 @@ public static class LogisticsPatch
{
ref var entity = ref factory.entityPool[station.entityId];
if (entity.id != station.entityId || entity.minerId <= 0 || entity.minerId >= factory.factorySystem.minerCursor) return false;
factory.factorySystem.minerPool[entity.minerId].speed = 10000 + AutoConfigVeinCollectorHarvestSpeed.Value * 1000;
factory.factorySystem.minerPool[entity.minerId].speed = LogisticsConstants.MinMiningSpeedBase + AutoConfigVeinCollectorHarvestSpeed.Value * LogisticsConstants.MiningSpeedFineStep;
return true;
}
@@ -186,7 +187,7 @@ public static class LogisticsPatch
station.pilerCount = AutoConfigVeinCollectorMinPilerValue.Value;
internal static void DispenserSetChargePower(PlanetFactory factory, DispenserComponent dispenser) =>
factory.powerSystem.consumerPool[dispenser.pcId].workEnergyPerTick = (long)(5000.0 * AutoConfigDispenserChargePower.Value + 0.5);
factory.powerSystem.consumerPool[dispenser.pcId].workEnergyPerTick = (long)(LogisticsConstants.DispenserChargePowerMultiplier * AutoConfigDispenserChargePower.Value + 0.5);
internal static void DispenserFillCouriers(PlanetFactory factory, DispenserComponent dispenser)
{
@@ -195,7 +196,7 @@ public static class LogisticsPatch
}
internal static void BattleBaseSetChargePower(PlanetFactory factory, BattleBaseComponent battleBase) =>
factory.powerSystem.consumerPool[battleBase.pcId].workEnergyPerTick = (long)(5000.0 * AutoConfigBattleBaseChargePower.Value + 0.5);
factory.powerSystem.consumerPool[battleBase.pcId].workEnergyPerTick = (long)(LogisticsConstants.DispenserChargePowerMultiplier * AutoConfigBattleBaseChargePower.Value + 0.5);
// === Per-facility "apply all settings" (also used as auto-config-on-build entry point) ===
@@ -8,6 +8,7 @@ using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using UXAssist.Common;
using UXAssist.Common.GameConstants;
using Object = UnityEngine.Object;
namespace UXAssist.Patches.Logistics;
@@ -181,19 +182,18 @@ internal static class RealtimeLogisticsInfoPanel
private static int _lastPlanetId;
private static int _localStorageMax = 5000;
private static int _remoteStorageMax = 10000;
private static int _localStorageMax = LogisticsConstants.DefaultLocalStorageMax;
private static int _remoteStorageMax = LogisticsConstants.DefaultRemoteStorageMax;
private static int _localStorageExtra;
private static int _remoteStorageExtra;
private static int _localStorageMaxTotal = _localStorageMax;
private static int _remoteStorageMaxTotal = _remoteStorageMax;
private static float _localStoragePixelPerItem = StorageSliderWidth / _localStorageMaxTotal;
private static float _remoteStoragePixelPerItem = StorageSliderWidth / _remoteStorageMaxTotal;
private static float _localStoragePixelPerItem = LogisticsConstants.StorageSliderWidth / _localStorageMaxTotal;
private static float _remoteStoragePixelPerItem = LogisticsConstants.StorageSliderWidth / _remoteStorageMaxTotal;
private static int _storageMaxSlotCount = 5;
private static int _storageMaxSlotCount = LogisticsConstants.DefaultStorageSlotCount;
private const int CarrierSlotCount = 3;
private const float StorageSliderWidth = 70f;
private const float StorageSliderHeight = 5f;
private static bool UpdateStorageMax()
{
@@ -204,8 +204,8 @@ internal static class RealtimeLogisticsInfoPanel
_remoteStorageExtra = history.remoteStationExtraStorage;
_localStorageMaxTotal = _localStorageMax + _localStorageExtra;
_remoteStorageMaxTotal = _remoteStorageMax + _remoteStorageExtra;
_localStoragePixelPerItem = StorageSliderWidth / _localStorageMaxTotal;
_remoteStoragePixelPerItem = StorageSliderWidth / _remoteStorageMaxTotal;
_localStoragePixelPerItem = LogisticsConstants.StorageSliderWidth / _localStorageMaxTotal;
_remoteStoragePixelPerItem = LogisticsConstants.StorageSliderWidth / _remoteStorageMaxTotal;
return true;
}
@@ -258,9 +258,9 @@ internal static class RealtimeLogisticsInfoPanel
internal static void OnDataLoaded()
{
_storageMaxSlotCount = 5;
_localStorageMax = 5000;
_remoteStorageMax = 10000;
_storageMaxSlotCount = LogisticsConstants.DefaultStorageSlotCount;
_localStorageMax = LogisticsConstants.DefaultLocalStorageMax;
_remoteStorageMax = LogisticsConstants.DefaultRemoteStorageMax;
foreach (var model in LDB.models.dataArray)
{
var prefabDesc = model?.prefabDesc;
@@ -361,25 +361,25 @@ internal static class RealtimeLogisticsInfoPanel
var sliderBg = Object.Instantiate(sliderBgPrefab.gameObject, new Vector3(0, 0, 0), Quaternion.identity, _tipPrefab.transform);
sliderBg.name = "sliderBg" + index;
rectTrans = (RectTransform)sliderBg.transform;
rectTrans.sizeDelta = new Vector2(StorageSliderWidth, StorageSliderHeight);
rectTrans.sizeDelta = new Vector2(LogisticsConstants.StorageSliderWidth, LogisticsConstants.StorageSliderHeight);
rectTrans.anchorMax = new Vector2(0f, 1f);
rectTrans.anchorMin = new Vector2(0f, 1f);
rectTrans.pivot = new Vector2(0f, 1f);
rectTrans.anchoredPosition3D = new Vector3(30f, y - 22f, 0f);
rectTrans = (RectTransform)sliderBg.transform.Find("current-fg").transform;
rectTrans.sizeDelta = new Vector2(0f, StorageSliderHeight);
rectTrans.sizeDelta = new Vector2(0f, LogisticsConstants.StorageSliderHeight);
rectTrans.anchorMax = new Vector2(0f, 1f);
rectTrans.anchorMin = new Vector2(0f, 1f);
rectTrans.pivot = new Vector2(0f, 1f);
rectTrans.localPosition = new Vector3(0f, 0f, 0f);
rectTrans = (RectTransform)sliderBg.transform.Find("ordered-fg").transform;
rectTrans.sizeDelta = new Vector2(0f, StorageSliderHeight);
rectTrans.sizeDelta = new Vector2(0f, LogisticsConstants.StorageSliderHeight);
rectTrans.anchorMax = new Vector2(0f, 1f);
rectTrans.anchorMin = new Vector2(0f, 1f);
rectTrans.pivot = new Vector2(0f, 1f);
rectTrans.localPosition = new Vector3(0f, 0f, 0f);
rectTrans = (RectTransform)sliderBg.transform.Find("max-fg").transform;
rectTrans.sizeDelta = new Vector2(StorageSliderWidth, StorageSliderHeight);
rectTrans.sizeDelta = new Vector2(LogisticsConstants.StorageSliderWidth, LogisticsConstants.StorageSliderHeight);
rectTrans.anchorMax = new Vector2(0f, 1f);
rectTrans.anchorMin = new Vector2(0f, 1f);
rectTrans.pivot = new Vector2(0f, 1f);
@@ -934,7 +934,7 @@ internal static class RealtimeLogisticsInfoPanel
{
((RectTransform)_sliderCurrent[i].transform).sizeDelta = new Vector2(
_pixelPerItem * itemCount,
StorageSliderHeight
LogisticsConstants.StorageSliderHeight
);
_sliderCurrent[i].gameObject.SetActive(true);
}
@@ -968,7 +968,7 @@ internal static class RealtimeLogisticsInfoPanel
);
rectTrans.sizeDelta = new Vector2(
_pixelPerItem * itemOrdered + 0.49f,
StorageSliderHeight
LogisticsConstants.StorageSliderHeight
);
break;
case < 0:
@@ -981,7 +981,7 @@ internal static class RealtimeLogisticsInfoPanel
);
rectTrans.sizeDelta = new Vector2(
_pixelPerItem * -itemOrdered + 0.49f,
StorageSliderHeight
LogisticsConstants.StorageSliderHeight
);
break;
}
@@ -997,7 +997,7 @@ internal static class RealtimeLogisticsInfoPanel
if (itemMax > itemLimit) itemMax = itemLimit;
((RectTransform)_sliderMax[i].transform).sizeDelta = new Vector2(
_pixelPerItem * itemMax,
StorageSliderHeight
LogisticsConstants.StorageSliderHeight
);
}
}