fix(uxassist): limit quick miner diagnostics to Debug builds

This commit is contained in:
soarqin committed 2026-10-31 15:35:59 +08:00
1 parent 3580e9623f
commit 6263775dc1
4 files changed
+174 -27

No files matched your search

+4
View File
@@ -205,8 +205,10 @@ public static class I18NKeys
public const string QuickMinerFailed = "Miner placement stopped; check the log before retrying";
public const string QuickMinerVeinsChanged = "Veins changed during miner layout search";
public const string QuickMinerResult = "Placed {0} Mining Machines, {1} total vein coverage, {2} buried nodes";
#if DEBUG
public const string QuickMinerDebugSaved = "Miner layout debug data: {0}";
public const string QuickMinerDebugFailed = "Could not save miner layout debug data";
#endif
public const string MaximumCountToBuild = "Maximum count to build";
public const string Max = "max";
public const string StopEjectorsWhenAvailableNodesAreAllFilledUp = "Stop ejectors when available nodes are all filled up";
@@ -501,8 +503,10 @@ You can set 2 shortcut keys in Settings panel:
I18N.Add(QuickMinerFailed, "Miner placement stopped. Check the UXAssist log before retrying.", "采矿机铺设已停止,请查看 UXAssist 日志后重试。");
I18N.Add(QuickMinerVeinsChanged, "The veins changed during calculation. Click the button again to recalculate the layout.", "计算期间矿点状态已变化,请再次点击按钮重新计算布局。");
I18N.Add(QuickMinerResult, "Placed {0} Mining Machines; total vein coverage: {1} (shared nodes counted per miner); buried nodes: {2}.", "已铺设 {0} 台采矿机,总覆盖 {1} 个矿点(各台分别计数),掩埋 {2} 个矿点。");
#if DEBUG
I18N.Add(QuickMinerDebugSaved, "Debug data: {0}", "调试数据:{0}");
I18N.Add(QuickMinerDebugFailed, "Could not save debug data. Check the UXAssist log before retrying.", "调试数据保存失败,请查看 UXAssist 日志后重试。");
#endif
I18N.Add(MaximumCountToBuild, "Maximum count to build", "最大建造数量");
I18N.Add(Max, "max", "最大");
I18N.Add(StopEjectorsWhenAvailableNodesAreAllFilledUp, "Stop ejectors when available nodes are all filled up", "可用节点全部造完时停止弹射");
+166 -24
View File
@@ -2,11 +2,15 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
#if DEBUG
using System.IO;
#endif
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#if DEBUG
using BepInEx;
#endif
using BepInEx.Configuration;
using UnityEngine;
using UXAssist.Common;
@@ -99,12 +103,13 @@ public static class QuickMinerBuild
private static void Fail(Exception exception)
{
UXAssist.Logger.LogError($"Quick miner placement failed: {exception}");
#if DEBUG
_job?.RecordFailure(exception);
#endif
Stop();
UIRealtimeTip.Popup(I18NKeys.QuickMinerFailed.Translate());
}
// Completion, failure and cancellation all finish the job, which records its debug data.
private static void Stop()
{
var job = _job;
@@ -120,7 +125,9 @@ public static class QuickMinerBuild
private static void ShowResult(string message)
{
#if DEBUG
UXAssist.Logger.LogInfo($"Quick miner result: {message.Replace("\n", " | ")}");
#endif
UIRealtimeTip.Popup(message);
foreach (var tip in UIRoot.instance.uiGame.generalTips.realtimeTips)
{
@@ -160,7 +167,10 @@ public static class QuickMinerBuild
private sealed class Node
{
public int Id;
public Vector3 Position, OriginalPosition;
public Vector3 Position;
#if DEBUG
public Vector3 OriginalPosition;
#endif
public ColliderData Collider;
public bool Target, Visible, CanBury;
public EVeinType Type;
@@ -187,10 +197,12 @@ public static class QuickMinerBuild
private readonly ColliderData _body;
private readonly float _footprintRadius, _halfDiagonal;
private readonly bool _capsule, _allowVeinReform;
#if DEBUG
private readonly MinerPlacementDebug.Snapshot _debug = new();
private readonly List<MinerPlacementDebug.Phase> _phases = [];
private readonly List<MinerPlacementDebug.Placement> _placements = [];
private readonly Stopwatch _timer = Stopwatch.StartNew();
#endif
private readonly CancellationTokenSource _cancellation = new();
private readonly int _workers = MinerPlacementSolver.WorkerCount;
private IEnumerator _work;
@@ -232,6 +244,7 @@ public static class QuickMinerBuild
// Native clearance checks measure from the encompassing build collider.
_halfDiagonal = Mathf.Sqrt(desc.buildCollider.ext.x * desc.buildCollider.ext.x + desc.buildCollider.ext.z * desc.buildCollider.ext.z);
_capsule = planet.id == planet.galaxy.birthPlanetId && GameMain.history.SpaceCapsuleExist();
#if DEBUG
_debug.version = PluginInfo.PLUGIN_VERSION;
_debug.gameVersion = GameConfig.gameVersion.ToString();
_debug.startedUtc = DateTime.UtcNow.ToString("O");
@@ -248,15 +261,18 @@ public static class QuickMinerBuild
_debug.buildColliders = desc.buildColliders.Select(MinerPlacementDebug.Collider.Capture).ToArray();
_debug.minerColliders = _nativeObstacles.Select(MinerPlacementDebug.Collider.Capture).ToArray();
_debug.outcome = "cancelled";
#endif
}
public bool Step() => (_work ??= Run()).MoveNext();
#if DEBUG
public void RecordFailure(Exception exception)
{
_debug.outcome = "failed";
_debug.error = exception.ToString();
}
#endif
public void Finish()
{
@@ -272,15 +288,20 @@ public static class QuickMinerBuild
_cancellation.Dispose();
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
(_work as IDisposable)?.Dispose();
#if DEBUG
var location = WriteDebug();
if (_result != null) ShowResult(_result + "\n" + location);
if (_result != null) _result += "\n" + location;
#endif
if (_result != null) ShowResult(_result);
}
private IEnumerator Run()
{
if (!FindNodes())
{
#if DEBUG
_debug.outcome = "no-veins";
#endif
_result = I18NKeys.QuickMinerNoVeins.Translate();
yield break;
}
@@ -300,23 +321,43 @@ public static class QuickMinerBuild
}
}
var solver = new MinerPlacementSolver(_candidates, _nodes.Count, _allowVeinReform);
var work = Search(solver, "coarse", 10);
#if DEBUG
var work = Search(solver, 10, "coarse");
#else
var work = Search(solver, 10);
#endif
while (work.MoveNext()) yield return null;
work = Expand(solver.Best, 0.35f, 3f);
while (work.MoveNext()) yield return null;
work = Search(solver, "refine-position", 6);
#if DEBUG
work = Search(solver, 6, "refine-position");
#else
work = Search(solver, 6);
#endif
while (work.MoveNext()) yield return null;
work = Expand(solver.Best, 0.12f, 1f);
while (work.MoveNext()) yield return null;
work = Search(solver, "refine-angle", 4);
#if DEBUG
work = Search(solver, 4, "refine-angle");
#else
work = Search(solver, 4);
#endif
while (work.MoveNext()) yield return null;
#if DEBUG
_debug.outcome = "completed";
#endif
Commit(solver.Best);
}
private IEnumerator Search(MinerPlacementSolver solver, string phase, int passes)
#if DEBUG
private IEnumerator Search(MinerPlacementSolver solver, int passes, string phase)
#else
private IEnumerator Search(MinerPlacementSolver solver, int passes)
#endif
{
#if DEBUG
var timer = Stopwatch.StartNew();
#endif
if (_workers > 0)
{
_search = solver.SearchAsync(passes, _cancellation.Token, _workers);
@@ -329,14 +370,17 @@ public static class QuickMinerBuild
var work = solver.Search(passes);
while (work.MoveNext()) yield return null;
}
#if DEBUG
_phases.Add(new MinerPlacementDebug.Phase
{
name = phase, candidateCount = _candidates.Count, passes = passes, score = solver.BestScore,
searchMilliseconds = timer.ElapsedMilliseconds, elapsedMilliseconds = _timer.ElapsedMilliseconds,
selected = solver.Best.Select(candidate => _candidates.IndexOf(candidate)).ToArray()
});
#endif
}
#if DEBUG
private string WriteDebug()
{
try
@@ -370,6 +414,7 @@ public static class QuickMinerBuild
return I18NKeys.QuickMinerDebugFailed.Translate();
}
}
#endif
private bool FindNodes()
{
@@ -388,7 +433,9 @@ public static class QuickMinerBuild
}
if (group == 0) return false;
_group = group;
#if DEBUG
_debug.veinGroup = group;
#endif
var count = 0;
for (var i = 1; i < _factory.veinCursor; i++)
{
@@ -440,11 +487,15 @@ public static class QuickMinerBuild
_nodes.Add(new Node
{
Id = i, Target = vein.groupIndex == group, Type = vein.type,
Position = vein.pos, OriginalPosition = vein.pos, Collider = _physics.GetColliderData(vein.colliderId),
Position = vein.pos, Collider = _physics.GetColliderData(vein.colliderId),
#if DEBUG
OriginalPosition = vein.pos,
#endif
Visible = vein.pos.magnitude >= _planet.realRadius - 40f,
CanBury = _allowVeinReform && vein.groupIndex == group && vein.minerCount == 0 && !reserved.Contains(i)
});
}
#if DEBUG
var existing = new List<MinerPlacementDebug.ExistingMiner>();
for (var i = 1; i < _factory.entityCursor; i++)
{
@@ -470,6 +521,7 @@ public static class QuickMinerBuild
});
}
_debug.existingMiners = existing.ToArray();
#endif
return _nodes.Count > 0;
}
@@ -595,13 +647,26 @@ public static class QuickMinerBuild
private void TryCandidate(float x, float z, float yaw)
{
#if DEBUG
_debug.rejections.attempted++;
#endif
if (x != _probeX || z != _probeZ) Probe(x, z);
var pos = _probe;
if (!FactoryPatch.RemoveBuildRangeLimitEnabled.Value &&
(pos - _player.position).sqrMagnitude > _player.mecha.buildArea * _player.mecha.buildArea)
{ _debug.rejections.range++; return; }
if (_probeRestricted) { _debug.rejections.clearance++; return; }
{
#if DEBUG
_debug.rejections.range++;
#endif
return;
}
if (_probeRestricted)
{
#if DEBUG
_debug.rejections.clearance++;
#endif
return;
}
var rotation = Maths.SphericalRotation(pos, yaw);
var pose = new Pose(pos, rotation);
_covered.Clear();
@@ -609,10 +674,22 @@ public static class QuickMinerBuild
{
var node = _nodes[i];
if (!MinerComponent.IsTargetVeinInRange(node.Position, pose, _item.prefabDesc)) continue;
if (node.Type != _type) { _debug.rejections.mixedType++; return; }
if (node.Type != _type)
{
#if DEBUG
_debug.rejections.mixedType++;
#endif
return;
}
if (node.Target) _covered.Add(i);
}
if (_covered.Count < MinerPlacementSolver.MinimumCoverage) { _debug.rejections.noCoverage++; return; }
if (_covered.Count < MinerPlacementSolver.MinimumCoverage)
{
#if DEBUG
_debug.rejections.noCoverage++;
#endif
return;
}
for (var i = 0; i < _buildBoxes.Length; i++)
_buildBoxes[i] = new Box(_item.prefabDesc.buildColliders[i].BindToObject(0, 0, EObjectType.None, pos, rotation));
_buried?.Clear();
@@ -620,17 +697,41 @@ public static class QuickMinerBuild
{
var node = _nodes[i];
if (!Hits(node.Collider)) continue;
if (!node.CanBury) { _debug.rejections.veinCollision++; return; }
if (!node.CanBury)
{
#if DEBUG
_debug.rejections.veinCollision++;
#endif
return;
}
_buried.Add(i);
}
if (_allowVeinReform)
{
foreach (var index in _buried) _covered.Remove(index);
if (_covered.Count < MinerPlacementSolver.MinimumCoverage) { _debug.rejections.noCoverage++; return; }
if (_covered.Count < MinerPlacementSolver.MinimumCoverage)
{
#if DEBUG
_debug.rejections.noCoverage++;
#endif
return;
}
}
foreach (var obstacle in _nearObstacles)
if (Hits(obstacle)) { _debug.rejections.buildingCollision++; return; }
if (!HasGround(pos, rotation)) { _debug.rejections.ground++; return; }
{
if (!Hits(obstacle)) continue;
#if DEBUG
_debug.rejections.buildingCollision++;
#endif
return;
}
if (!HasGround(pos, rotation))
{
#if DEBUG
_debug.rejections.ground++;
#endif
return;
}
for (var i = 0; i < _nativeObstacles.Length; i++)
_obstacleBoxes[i] = new Box(_nativeObstacles[i].BindToObject(0, 0, EObjectType.None, pos, rotation));
_candidates.Add(new Candidate
@@ -717,17 +818,25 @@ public static class QuickMinerBuild
{
if (layout.Count == 0)
{
#if DEBUG
_debug.outcome = "no-placement";
#endif
_result = I18NKeys.QuickMinerNoPlacement.Translate();
return;
}
if (!NodesUnchanged(layout))
{
#if DEBUG
_debug.outcome = "veins-changed";
#endif
_result = I18NKeys.QuickMinerVeinsChanged.Translate();
return;
}
#if DEBUG
var placed = new List<(Candidate Candidate, int Prebuild, MinerPlacementDebug.Placement Debug)>();
#else
var placed = new List<(Candidate Candidate, int Prebuild)>();
#endif
var plannedBurial = _allowVeinReform
? layout.SelectMany(candidate => candidate.Buried).Select(index => _nodes[index].Id).ToHashSet() : null;
var hidden = _allowVeinReform ? new Dictionary<int, Vector3>() : null;
@@ -746,11 +855,13 @@ public static class QuickMinerBuild
validator.buildPreviews.Add(preview);
foreach (var candidate in layout)
{
#if DEBUG
var debug = new MinerPlacementDebug.Placement
{
candidate = _candidates.IndexOf(candidate), outcome = "pending", nativeCondition = "not-checked", nativeVeins = [], plannedVeins = [], finalVeins = []
};
_placements.Add(debug);
#endif
// No yield between burial, native validation, item consumption, and creation.
if (_allowVeinReform)
{
@@ -765,7 +876,11 @@ public static class QuickMinerBuild
}
// Validation needs current Unity colliders; GPU buffers are synced once at the end.
_physics.RefreshColliders();
if (!Validate(validator, preview, candidate, plannedBurial, debug)) continue;
#if DEBUG
if (!Validate(validator, preview, candidate, plannedBurial, out var plannedVeins, debug)) continue;
#else
if (!Validate(validator, preview, candidate, plannedBurial, out var plannedVeins)) continue;
#endif
var itemId = ItemIds.MiningMachine;
var count = 1;
if (_player.inhandItemId == itemId && _player.inhandItemCount > 0) _player.UseHandItems(1, out _);
@@ -778,14 +893,20 @@ public static class QuickMinerBuild
itemRequired = count == 1 ? 0 : 1,
filterId = _factory.veinPool[_nodes[candidate.Covered[0]].Id].productId
};
prebuild.InitParametersArray(debug.plannedVeins.Length);
Array.Copy(debug.plannedVeins, prebuild.parameters, debug.plannedVeins.Length);
prebuild.InitParametersArray(plannedVeins.Length);
Array.Copy(plannedVeins, prebuild.parameters, plannedVeins.Length);
var id = _factory.AddPrebuildDataWithComponents(prebuild);
#if DEBUG
debug.prebuildId = id;
debug.itemRequired = prebuild.itemRequired;
debug.outcome = id > 0 ? "placed" : "prebuild-failed";
#endif
if (id == 0 && count == 1) _player.TryAddItemToPackage(ItemIds.MiningMachine, 1, 0, true);
#if DEBUG
if (id > 0) placed.Add((candidate, id, debug));
#else
if (id > 0) placed.Add((candidate, id));
#endif
}
}
finally
@@ -814,15 +935,19 @@ public static class QuickMinerBuild
ref var prebuild = ref _factory.prebuildPool[entry.Prebuild];
prebuild.InitParametersArray(ids.Count);
if (ids.Count > 0) ids.CopyTo(prebuild.parameters);
#if DEBUG
entry.Debug.finalVeins = ids.ToArray();
#endif
coverage += ids.Count;
}
if (_allowVeinReform) RefreshVeins(buriedCount > 0 ? 1 : 0);
}
#if DEBUG
_debug.placedCount = placed.Count;
_debug.totalCoverage = coverage;
_debug.buriedCount = buriedCount;
if (placed.Count == 0) _debug.outcome = "no-placement";
#endif
_result = placed.Count == 0 ? I18NKeys.QuickMinerNoPlacement.Translate() :
string.Format(I18NKeys.QuickMinerResult.Translate(), placed.Count, coverage, buriedCount);
}
@@ -848,9 +973,16 @@ public static class QuickMinerBuild
}
// CheckBuildConditions repeats the search's land-point tests, so ground needs no separate check.
private bool Validate(BuildTool_Click validator, BuildPreview preview, Candidate candidate, HashSet<int> plannedBurial, MinerPlacementDebug.Placement debug)
#if DEBUG
private bool Validate(BuildTool_Click validator, BuildPreview preview, Candidate candidate, HashSet<int> plannedBurial, out int[] plannedVeins, MinerPlacementDebug.Placement debug)
#else
private bool Validate(BuildTool_Click validator, BuildPreview preview, Candidate candidate, HashSet<int> plannedBurial, out int[] plannedVeins)
#endif
{
plannedVeins = [];
#if DEBUG
debug.outcome = "native-rejected";
#endif
preview.ResetAll();
preview.item = _item;
preview.desc = _item.prefabDesc;
@@ -885,16 +1017,26 @@ public static class QuickMinerBuild
}
Physics.SyncTransforms();
var valid = validator.CheckBuildConditions();
#if DEBUG
debug.nativeCondition = preview.condition.ToString();
debug.coverObjectId = preview.coverObjId;
debug.nativeVeins = preview.parameters?.Take(preview.paramCount).ToArray() ?? [];
#endif
if (!valid || preview.condition != EBuildCondition.Ok || preview.coverObjId != 0) return false;
// Native selection includes nearby groups of the same type. Keep only this
// group and exclude all planned burials before enforcing the minimum.
debug.plannedVeins = debug.nativeVeins.Where(id => _factory.veinPool[id].groupIndex == _group &&
(!_allowVeinReform || !plannedBurial.Contains(id))).ToArray();
if (debug.plannedVeins.Length < MinerPlacementSolver.MinimumCoverage)
{ debug.outcome = "below-minimum-coverage"; return false; }
plannedVeins = preview.parameters?.Take(preview.paramCount).Where(id => _factory.veinPool[id].groupIndex == _group &&
(!_allowVeinReform || !plannedBurial.Contains(id))).ToArray() ?? [];
#if DEBUG
debug.plannedVeins = plannedVeins;
#endif
if (plannedVeins.Length < MinerPlacementSolver.MinimumCoverage)
{
#if DEBUG
debug.outcome = "below-minimum-coverage";
#endif
return false;
}
for (var i = 0; i < preview.paramCount; i++)
{
ref var vein = ref _factory.veinPool[preview.parameters[i]];
+2 -2
View File
@@ -93,7 +93,7 @@
* Reuses collision checks and runs independent layout searches on up to 4 background workers, capped at half the available logical processors. Systems with one logical processor keep the search on the main thread with a frame budget. Native terrain checks and placement stay on the main thread; worker scheduling preserves the same layout as sequential search.
* With `Allow restoring/burying veins` enabled, may bury unused nodes when that improves total coverage. Nodes already assigned to miners or miner prebuilds are protected. Ground, water, existing buildings, construction range, and the game's other placement clearances still apply.
* Click again during calculation to cancel. Changing planets or leaving the game cancels automatically. Construction drones finish the prebuilds; belts and power supply are not placed.
* While tuning this feature, each calculation writes a timestamped JSON snapshot to `BepInEx/debug/UXAssist/QuickMiner/` in the active game profile, including the restoration/burial option, nodes, native colliders, accepted candidates, search phases and their search times, logical processor and worker counts, existing miners, and native placement results. The completion tip stays visible for about 8 seconds; the log also records the result summary and full file path. Replay and compare sequential/parallel results without loading a save using `powershell -File tools/tests/Test-QuickMinerPlacement.ps1 -Snapshot <file.json>` from the repository root. Replay fails if total coverage falls below the recorded result; add `-MinimumTotalCoverage <count>` to require a higher total.
* Debug builds capture a timestamped JSON snapshot for each calculation and export it to `BepInEx/debug/UXAssist/QuickMiner/` in the active game profile, including the restoration/burial option, nodes, native colliders, accepted candidates, search phases and their search times, logical processor and worker counts, existing miners, and native placement results. Only Debug builds log the result summary and full file path; Release builds exclude this debug functionality. The completion tip stays visible for about 8 seconds in both builds. Replay and compare sequential/parallel results without loading a save using `powershell -File tools/tests/Test-QuickMinerPlacement.ps1 -Snapshot <file.json>` from the repository root. Replay fails if total coverage falls below the recorded result; add `-MinimumTotalCoverage <count>` to require a higher total.
* Belt signals for buy out dark fog items automatically
* 6 belt signals are added to the signal panel, which can be used to buy out dark fog items automatically.
* Generated items are stacked in 4 items.
@@ -298,7 +298,7 @@
* 复用碰撞检查结果,使用最多 4 个后台线程并行搜索独立布局,线程数不超过可用逻辑线程数的一半。单逻辑线程系统继续在主线程按每帧预算计算。原生地形检查和实际铺设保留在主线程;并行调度与顺序搜索生成相同布局。
* 开启「允许恢复/掩埋矿物」后,允许掩埋未被使用的矿点来提高总覆盖数;保护已有矿机及待建造矿机使用的矿点。仍需满足地面、水面、现有建筑、建造范围及游戏的其他放置间距要求。
* 计算期间再次点击可取消,切换行星或退出游戏自动取消。待建造对象由建设机器人完成,不自动铺设传送带和电力设施。
* 功能调试期间,每次计算都会在当前游戏配置目录的 `BepInEx/debug/UXAssist/QuickMiner/` 下写入带时间戳的 JSON 文件,记录恢复/掩埋选项、矿点、原生碰撞体、有效候选、搜索阶段及其搜索耗时、逻辑线程数和工作线程数、现有矿机和原生铺设校验结果。完成提示保留约 8 秒,日志也会记录结果摘要和文件的完整路径。在仓库根目录运行 `powershell -File tools/tests/Test-QuickMinerPlacement.ps1 -Snapshot <file.json>`,即可离线重放并比较顺序与并行搜索结果,无需读取存档。总覆盖数低于记录结果时,重放检查会失败;可追加 `-MinimumTotalCoverage <数量>`,要求达到更高的总覆盖数。
* 仅 Debug 版本会为每次计算采集带时间戳的 JSON 快照,并导出到当前游戏配置目录的 `BepInEx/debug/UXAssist/QuickMiner/`,记录恢复/掩埋选项、矿点、原生碰撞体、有效候选、搜索阶段及其搜索耗时、逻辑线程数和工作线程数、现有矿机和原生铺设校验结果。结果摘要和文件完整路径的日志也仅在 Debug 版本中输出;Release 版本不编译这些调试功能。两个版本的完成提示均保留约 8 秒。在仓库根目录运行 `powershell -File tools/tests/Test-QuickMinerPlacement.ps1 -Snapshot <file.json>`,即可离线重放并比较顺序与并行搜索结果,无需读取存档。总覆盖数低于记录结果时,重放检查会失败;可追加 `-MinimumTotalCoverage <数量>`,要求达到更高的总覆盖数。
* 用于自动购买黑雾物品的传送带信号
* 在信号面板上添加了6个传送带信号,可以用于自动购买黑雾道具。
* 生成的物品堆叠数为4。
+2 -1
View File
@@ -11,7 +11,8 @@
<ItemGroup>
<Compile Remove="tools/**/*.cs" />
<Reference Include="System.Runtime.Serialization" />
<Compile Remove="Functions/Factory/MinerPlacementDebug.cs" Condition="'$(Configuration)' != 'Debug'" />
<Reference Include="System.Runtime.Serialization" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="DysonSphereProgram.Modding.CommonAPI" Version="1.*" />
<PackageReference Include="DysonSphereProgram.Modding.DSPModSave" Version="1.*" />
</ItemGroup>