1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2026-03-23 03:43:26 +08:00
Files
DSP_Mods/CheatEnabler/Functions/PlayerFunctions.cs
Soar Qin f69a90d452 Fix potential bugs in UXAssist and CheatEnabler
UXAssist:
- PlanetFunctions: fix infinite loop in constructStats cleanup (i++ -> i--)
- PlanetFunctions: skip entityPool slot 0 (sentinel) in DismantleAll foreach
- PlanetFunctions: fix belt buffer walk infinite loop when buffer[j]==250,
  add cargoPool bounds check
- FactoryPatch: fix UnfixProto guard condition (< 3 -> < PowerPoleIds.Length)
- FactoryPatch: fix Array.Resize(index*2) -> (index+1)*2 to handle index==0
- LogisticsPatch: fix UpdateStorageMax early-exit to check both local and remote
- LogisticsPatch: fix storage max scan to use Math.Max instead of last-write
- LogisticsPatch: add divide-by-zero guard in station storage ratio calculation
- LogisticsPatch: fix station entry right-click delegates using per-instance
  dictionary instead of shared static array to prevent unsubscribe mismatch
- LogisticsPatch: add null checks for GameObject.Find results in InitGUI
- GamePatch: log exception in previously empty catch block
- GameLogic: invoke event handlers individually with try/catch so one failing
  subscriber does not abort subsequent ones
- DysonSpherePatch/LogisticsPatch/PersistPatch/PlayerPatch: replace
  matcher.Labels = null with matcher.Labels = [] (5 sites)
- UIConfigWindow: remove duplicate I18N.Add for 'Outgoing integration count'
- UIConfigWindow: remove two orphaned y += 36f spacing increments

CheatEnabler:
- GamePatch: add null check for GameMain.gameScenario in AbnormalDisabler.OnEnable
- FactoryPatch: add null check for GameMain.mainPlayer in TakeTailItemsPatch
  and GetItemCountPatch
- FactoryPatch: add null check for GameMain.mainPlayer in
  ConstructionSystem_GameTick_Prefix
- FactoryPatch: fix _portalFrom.Remove(beltId) -> Remove(v) (wrong key)
- FactoryPatch: add null guard for GameMain.data before iterating factories
  in WindTurbinesPowerGlobalCoverage
- DysonSphereFunctions: fix shell pool rebuild loop writing all shells to
  slot 1 (id=1); now uses j+1 per iteration
- DysonSphereFunctions: replace GameMain.gameScenario.NotifyOnPlanDysonShell()
  with null-conditional call (?.) at 4 sites
- DysonSpherePatch: fix SkipAbsorbPatch/QuickAbsorbPatch sharing _instantAbsorb:
  OnDisable now recalculates the flag instead of unconditionally clearing it
- PlayerFunctions: add null check for GameMain.galaxy in TeleportToOuterSpace
- UIConfigWindow: add null guard in UpdateButtons before accessing button fields
- PlanetFunctions: add colliderId bounds check in BuryAllVeins
2026-03-16 19:52:14 +08:00

191 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Linq;
using UXAssist.Common;
namespace CheatEnabler.Functions;
public static class PlayerFunctions
{
public static void Init()
{
I18N.Add("ClearAllMetadataConsumptionDetails",
"""
Metadata consumption records of all gamesaves are about to be cleared.
You will gain following metadata back:
""",
"""
""");
I18N.Add("ClearCurrentMetadataConsumptionDetails",
"""
Metadata consumption records of current gamesave are about to be cleared.
You will gain following metadata back:
""",
"""
""");
I18N.Add("NoMetadataConsumptionRecord",
"No metadata consumption records found.",
"未找到元数据消耗记录。");
}
public static void TeleportToOuterSpace()
{
var maxSqrDistance = 0.0;
var starPosition = VectorLF3.zero;
if (GameMain.galaxy == null) return;
foreach (var star in GameMain.galaxy.stars)
{
var sqrDistance = star.position.sqrMagnitude;
if (sqrDistance > maxSqrDistance)
{
maxSqrDistance = sqrDistance;
starPosition = star.position;
}
}
if (starPosition == VectorLF3.zero) return;
var distance = Math.Sqrt(maxSqrDistance);
GameMain.mainPlayer.controller.actionSail.StartFastTravelToUPosition((starPosition + starPosition.normalized * 50) * GalaxyData.LY);
}
public static void TeleportToSelectedAstronomical()
{
var starmap = UIRoot.instance?.uiGame?.starmap;
if (starmap == null) return;
if (starmap.focusPlanet != null)
{
GameMain.mainPlayer.controller.actionSail.StartFastTravelToPlanet(starmap.focusPlanet.planet);
return;
}
var targetUPos = VectorLF3.zero;
if (starmap.focusStar != null)
{
var star = starmap.focusStar.star;
targetUPos = star.uPosition + VectorLF3.unit_x * star.physicsRadius;
}
else if (starmap.focusHive != null)
{
var hive = starmap.focusHive.hive;
var id = hive.hiveAstroId - 1000000;
if (id > 0 && id < starmap.spaceSector.astros.Length)
{
ref var astro = ref starmap.spaceSector.astros[id];
targetUPos = astro.uPos + VectorLF3.unit_x * astro.uRadius;
}
}
GameMain.mainPlayer.controller.actionSail.StartFastTravelToUPosition(targetUPos);
}
private static void PurgePropertySystem(PropertySystem propertySystem)
{
var propertyDatas = propertySystem.propertyDatas;
for (var i = 0; i < propertyDatas.Count;)
{
if (propertyDatas[i].totalProduction.Any(idcnt => idcnt.count > 0) || propertyDatas[i].totalConsumption.Any(idcnt => idcnt.count > 0))
{
i++;
}
else
{
propertyDatas.RemoveAt(i);
}
}
}
public static void RemoveAllMetadataConsumptions()
{
var propertySystem = DSPGame.propertySystem;
if (propertySystem == null) return;
PurgePropertySystem(propertySystem);
var itemCnt = new int[6];
foreach (var cons in propertySystem.propertyDatas.SelectMany(data => data.totalConsumption.Where(cons => cons.id is >= 6001 and <= 6006)))
{
itemCnt[cons.id - 6001] += cons.count;
}
if (itemCnt.All(cnt => cnt == 0))
{
UIMessageBox.Show("Remove all metadata consumption records".Translate(), "NoMetadataConsumptionRecord".Translate(), "OK".Translate(), UIMessageBox.INFO);
return;
}
var msg = "ClearAllMetadataConsumptionDetails".Translate();
for (var i = 0; i < 6; i++)
{
if (itemCnt[i] > 0)
{
msg += $"\n {LDB.items.Select(i + 6001).propertyName} x{itemCnt[i]}";
}
}
UIMessageBox.Show("Remove all metadata consumption records".Translate(), msg, "取消".Translate(), "确定".Translate(), UIMessageBox.QUESTION, null, () =>
{
foreach (var data in propertySystem.propertyDatas)
{
for (var i = 0; i < data.totalConsumption.Count; i++)
{
if (data.totalConsumption[i].count == 0) continue;
var id = data.totalConsumption[i].id;
data.totalConsumption[i] = new IDCNT(id, 0);
}
}
PurgePropertySystem(propertySystem);
propertySystem.SaveToFile();
});
}
public static void RemoveCurrentMetadataConsumptions()
{
var propertySystem = DSPGame.propertySystem;
if (propertySystem == null) return;
PurgePropertySystem(propertySystem);
var itemCnt = new int[6];
var seedKey = DSPGame.GameDesc.seedKey64;
var clusterPropertyData = propertySystem.propertyDatas.FirstOrDefault(cpd => cpd.seedKey == seedKey);
if (clusterPropertyData == null)
{
UIMessageBox.Show("Remove metadata consumption record in current game".Translate(), "NoMetadataConsumptionRecord".Translate(), "OK".Translate(), UIMessageBox.INFO);
return;
}
var currentGamePropertyData = GameMain.data.history.propertyData;
foreach (var cons in currentGamePropertyData.totalConsumption.Where(cons => cons.id is >= 6001 and <= 6006))
{
itemCnt[cons.id - 6001] += cons.count;
}
if (itemCnt.All(cnt => cnt == 0))
{
UIMessageBox.Show("Remove metadata consumption record in current game".Translate(), "NoMetadataConsumptionRecord".Translate(), "OK".Translate(), UIMessageBox.INFO);
return;
}
var msg = "ClearCurrentMetadataConsumptionDetails".Translate();
for (var i = 0; i < 6; i++)
{
if (itemCnt[i] > 0)
{
msg += $"\n {LDB.items.Select(i + 6001).propertyName} x{itemCnt[i]}";
}
}
UIMessageBox.Show("Remove metadata consumption record in current game".Translate(), msg, "取消".Translate(), "确定".Translate(), UIMessageBox.QUESTION, null, () =>
{
for (var i = 0; i < clusterPropertyData.totalConsumption.Count; i++)
{
if (clusterPropertyData.totalConsumption[i].count == 0) continue;
var id = clusterPropertyData.totalConsumption[i].id;
if (id < 6001 || id > 6006) continue;
var currentGameCount = itemCnt[id - 6001];
if (currentGameCount == 0) continue;
var totalCount = clusterPropertyData.totalConsumption[i].count;
clusterPropertyData.totalConsumption[i] = new IDCNT(id, totalCount > currentGameCount ? totalCount - currentGameCount : 0);
}
PurgePropertySystem(propertySystem);
propertySystem.SaveToFile();
});
}
public static void ClearMetadataBanAchievements()
{
GameMain.history.hasUsedPropertyBanAchievement = false;
}
}