Skip to content

Commit

Permalink
Speed up PartModule lookups (#48)
Browse files Browse the repository at this point in the history
Achieved by using the faster method provided by KSPCF.
  • Loading branch information
siimav authored Oct 19, 2024
1 parent aa2bbe0 commit a470898
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/RealAntennasProject/ModuleRealAntenna.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using KSPCommunityFixes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -173,7 +174,7 @@ public override void OnStart(StartState state)
Events[nameof(AntennaTargetGUI)].active = false;
}

deployableAntenna = part.FindModuleImplementing<ModuleDeployableAntenna>();
deployableAntenna = part.FindModuleImplementingFast<ModuleDeployableAntenna>();

ApplyGameSettings();
SetupUICallbacks();
Expand Down
66 changes: 42 additions & 24 deletions src/RealAntennasProject/RACommNetVessel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Expansions.Serenity.DeployedScience.Runtime;
using Experience.Effects;
using KSPCommunityFixes;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Profiling;

namespace RealAntennas
{
Expand Down Expand Up @@ -124,40 +126,51 @@ private int CountControllingCrew()

private void DetermineControlUnloaded()
{
Profiler.BeginSample("DetermineControlUnloaded");
int numControl = CountControllingCrew();
int index = 0;
foreach (ProtoPartSnapshot protoPartSnapshot in vessel.protoVessel.protoPartSnapshots)
{
index++;
Part part = protoPartSnapshot.partInfo.partPrefab;
foreach (PartModule module in part.Modules)
if (part.FindModuleImplementingFast<CommNet.ModuleProbeControlPoint>() is CommNet.ModuleProbeControlPoint pcp &&
pcp.CanControlUnloaded(protoPartSnapshot.FindModule(pcp, index)))
{
if ((module is CommNet.ModuleProbeControlPoint probeControlPoint) && probeControlPoint.CanControlUnloaded(protoPartSnapshot.FindModule(module, index)))
{
if (numControl >= probeControlPoint.minimumCrew || probeControlPoint.minimumCrew <= 0)
comm.isControlSource = true;
if (probeControlPoint.multiHop)
comm.isControlSourceMultiHop = true;
}
if (numControl >= pcp.minimumCrew || pcp.minimumCrew <= 0)
comm.isControlSource = true;
if (pcp.multiHop)
comm.isControlSourceMultiHop = true;

// Assume that control parts are closer to root than leaf nodes.
// If both fields are true then no point in looking any further.
if (comm.isControlSource && comm.isControlSourceMultiHop)
break;
}
}
Profiler.EndSample();
}

private void DetermineControlLoaded()
{
Profiler.BeginSample("DetermineControlLoaded");
int numControl = CountControllingCrew();
foreach (Part part in vessel.Parts)
{
foreach (PartModule module in part.Modules)
if (part.FindModuleImplementingFast<CommNet.ModuleProbeControlPoint>() is CommNet.ModuleProbeControlPoint pcp &&
pcp.CanControl())
{
if ((module is CommNet.ModuleProbeControlPoint probeControlPoint) && probeControlPoint.CanControl())
{
if (numControl >= probeControlPoint.minimumCrew || probeControlPoint.minimumCrew <= 0)
comm.isControlSource = true;
if (probeControlPoint.multiHop)
comm.isControlSourceMultiHop = true;
}
if (numControl >= pcp.minimumCrew || pcp.minimumCrew <= 0)
comm.isControlSource = true;
if (pcp.multiHop)
comm.isControlSourceMultiHop = true;

// Assume that control parts are closer to root than leaf nodes.
// If both fields are true then no point in looking any further.
if (comm.isControlSource && comm.isControlSourceMultiHop)
break;
}
}
Profiler.EndSample();
}

protected void OnVesselModified(Vessel data)
Expand All @@ -173,14 +186,18 @@ protected List<RealAntenna> DiscoverAntennas()
if (Vessel == null) return antennaList;
if (Vessel.loaded)
{
foreach (ModuleRealAntenna ant in Vessel.FindPartModulesImplementing<ModuleRealAntenna>().ToList())
foreach (Part part in vessel.parts)
{
if (ant.Condition == AntennaCondition.Enabled)
var moduleList = part.FindModulesImplementingReadOnly<ModuleRealAntenna>();
foreach (ModuleRealAntenna ant in moduleList)
{
ant.RAAntenna.ParentNode = Comm;
if (DeployedLoaded(ant.part)) antennaList.Add(ant.RAAntenna);
else inactiveAntennas.Add(ant.RAAntenna);
ValidateAntennaTarget(ant.RAAntenna);
if (ant.Condition == AntennaCondition.Enabled)
{
ant.RAAntenna.ParentNode = Comm;
if (DeployedLoaded(ant.part)) antennaList.Add(ant.RAAntenna);
else inactiveAntennas.Add(ant.RAAntenna);
ValidateAntennaTarget(ant.RAAntenna);
}
}
}
return antennaList;
Expand All @@ -197,7 +214,8 @@ protected List<RealAntenna> DiscoverAntennas()
_enabled = sState == AntennaCondition.Enabled.ToString();

// Doesn't get the correct PartModule if multiple, but the only impact is the name, which defaults to the part anyway.
if (_enabled && part.partInfo.partPrefab.FindModuleImplementing<ModuleRealAntenna>() is ModuleRealAntenna mra && mra.CanCommUnloaded(snap))
if (_enabled && part.partInfo.partPrefab.FindModuleImplementingFast<ModuleRealAntenna>() is ModuleRealAntenna mra &&
mra.CanCommUnloaded(snap))
{
RealAntenna ra = new RealAntennaDigital(part.partPrefab.partInfo.title) { ParentNode = Comm, ParentSnapshot = snap };
ra.LoadFromConfigNode(snap.moduleValues);
Expand Down Expand Up @@ -226,7 +244,7 @@ public static bool DeployedUnloaded(ProtoPartSnapshot part)
return true;
}
public static bool DeployedLoaded(Part part) =>
(part.FindModuleImplementing<ModuleDeployableAntenna>() is ModuleDeployableAntenna mda) ?
(part.FindModuleImplementingFast<ModuleDeployableAntenna>() is ModuleDeployableAntenna mda) ?
mda.deployState == ModuleDeployablePart.DeployState.EXTENDED : true;

private bool IsDeployedScienceCluster(Vessel v) => GetDeployedScienceCluster(v) != null;
Expand Down
1 change: 1 addition & 0 deletions src/RealAntennasProject/RealAntennas.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Reference Include="Assembly-CSharp" />
<Reference Include="Assembly-CSharp-firstpass" />
<Reference Include="ClickThroughBlocker" />
<Reference Include="KSPCommunityFixes" />
<Reference Include="System" />
<Reference Include="Unity.Burst" />
<Reference Include="Unity.Collections" />
Expand Down

0 comments on commit a470898

Please sign in to comment.