Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 61 additions & 27 deletions src/RemoteTech/Network/NetworkState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal class NetworkState : IDisposable
/// </summary>
JobHandle dependentHandle;

bool disposed;

ISatellite[] satellites;

// The settings snapshot this state was built from; reused by point-to-point
Expand Down Expand Up @@ -78,32 +80,32 @@ public void Dispose()

public JobHandle Dispose(JobHandle deps)
{
var handle = new NetworkStateDisposeJob { state = new ObjectHandle<NetworkState>(this) }
.Schedule(JobHandle.CombineDependencies(deps, this.handle, dependentHandle));
return handle;
}
if (disposed)
return deps;
disposed = true;

void DisposeInternal()
{
satmap.Dispose();
nodes.Dispose();
edges.Dispose();
connected.Dispose();
return new NetworkStateDisposeJob
{
satmap = satmap,
nodes = nodes,
edges = edges,
connected = connected,

scoreGs.Dispose();
parentGs.Dispose();
originGs.Dispose();
scoreGs = scoreGs,
parentGs = parentGs,
originGs = originGs,

scoreCs.Dispose();
parentCs.Dispose();
originCs.Dispose();
scoreCs = scoreCs,
parentCs = parentCs,
originCs = originCs,

fingerprints.Dispose();
changedNodes.Dispose();
fingerprints = fingerprints,
changedNodes = changedNodes,

bodies.Dispose();
coneCandidates.Dispose();
markCandidates.Dispose();
bodies = bodies,
coneCandidates = coneCandidates,
markCandidates = markCandidates,
}.Schedule(JobHandle.CombineDependencies(deps, this.handle, dependentHandle));
}

void AddDependentHandle(JobHandle handle)
Expand Down Expand Up @@ -177,7 +179,7 @@ public static NetworkState ScheduleNetworkUpdate(NetworkManager manager, Network
NetworkUpdate.VisibilityState visibility = new()
{
ActiveVessel = FlightGlobals.ActiveVessel?.id ?? default,
TargetVessel = FlightGlobals.fetch?.VesselTarget?.GetVessel().id ?? default,
TargetVessel = FlightGlobals.fetch?.VesselTarget?.GetVessel()?.id ?? default,
filter = MapViewFiltering.Instance.IsNotNullOrDestroyed()
? MapViewFiltering.vesselTypeFilter
: null
Expand Down Expand Up @@ -824,16 +826,48 @@ private bool TryGetEdge(int a, int b, out NetworkEdge edge)
return false;
}



[BurstCompile]
struct NetworkStateDisposeJob : IJob
{
public ObjectHandle<NetworkState> state;
public NativeHashMap<Guid, int> satmap;
[DeallocateOnJobCompletion]
public NativeArray<JobNode> nodes;
[DeallocateOnJobCompletion]
public NativeArray<NetworkEdge> edges;
public NativeBitArray connected;

[DeallocateOnJobCompletion]
public NativeArray<double> scoreGs;
[DeallocateOnJobCompletion]
public NativeArray<int> parentGs;
[DeallocateOnJobCompletion]
public NativeArray<int> originGs;

[DeallocateOnJobCompletion]
public NativeArray<double> scoreCs;
[DeallocateOnJobCompletion]
public NativeArray<int> parentCs;
[DeallocateOnJobCompletion]
public NativeArray<int> originCs;

public NativeList<Hash128> fingerprints;
public NativeList<int> changedNodes;

[DeallocateOnJobCompletion]
public NativeArray<JobBody> bodies;
public NativeList<ConeCandidate> coneCandidates;
public NativeList<SatelliteMarkCandidate> markCandidates;

public void Execute()
{
using var guard = state;
state.Target.DisposeInternal();
satmap.Dispose();
connected.Dispose();

fingerprints.Dispose();
changedNodes.Dispose();

coneCandidates.Dispose();
markCandidates.Dispose();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/RemoteTech/RTSpaceCentre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void OnDestroy()

_optionWindow = null;

if (LauncherButton != null)
if (LauncherButton != null && ApplicationLauncher.Instance != null)
{
ApplicationLauncher.Instance.RemoveModApplication(LauncherButton);
}
Expand Down
3 changes: 2 additions & 1 deletion src/RemoteTech/UI/FocusOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public void Dispose()
}

// Remove button on destroy
KSP.UI.Screens.ApplicationLauncher.Instance.RemoveModApplication(mButton);
if (mButton != null && KSP.UI.Screens.ApplicationLauncher.Instance != null)
KSP.UI.Screens.ApplicationLauncher.Instance.RemoveModApplication(mButton);
RemoveTrackingListeners();
}

Expand Down
Loading