Skip to content

Commit

Permalink
Merge pull request #49 from StarCoreSE/yard-logging
Browse files Browse the repository at this point in the history
more attempted logging and anti locking
  • Loading branch information
InvalidArgument3 authored Nov 16, 2024
2 parents e28ca3a + f0eb61d commit ab27e7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
16 changes: 5 additions & 11 deletions ShipyardsRevival/Data/Scripts/ShipyardMod/Communication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ private static void HandleYardCommand(byte[] data)
long yardId = BitConverter.ToInt64(data, 0);
var type = (ShipyardType)data.Last();

// Prevent duplicate processing
if (!_processingYards.Add(yardId))
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Yard {yardId} already being processed, skipping duplicate command");
Expand All @@ -721,32 +720,27 @@ private static void HandleYardCommand(byte[] data)

try
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Received {type} command for yard {yardId}");

foreach (ShipyardItem yard in ProcessShipyardDetection.ShipyardsList)
{
if (yard.EntityId != yardId)
continue;

Logging.Instance.WriteDebug($"[HandleYardCommand] Found yard {yardId}, current type: {yard.YardType}");
if (!yard.CanProcessCommand(type))
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Yard {yardId} cannot process command at this time");
return;
}

if (type == ShipyardType.Disabled || type == ShipyardType.Invalid)
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Disabling yard {yardId}");
yard.Disable();
}
else
{
Logging.Instance.WriteDebug($"[HandleYardCommand] Initializing yard {yardId} to type {type}");
yard.Init(type);
}

break;
}
}
finally
{
// Always remove from processing set when done
_processingYards.Remove(yardId);
}
}
Expand Down
32 changes: 24 additions & 8 deletions ShipyardsRevival/Data/Scripts/ShipyardMod/ShipyardItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ public void Init(ShipyardType yardType)
Logging.Instance.WriteLine($"[ShipyardItem.Init] Starting initialization for yard {EntityId} to type {yardType}");
Logging.Instance.WriteDebug($"YardItem.Init: {yardType}");

// Clear existing state first
TargetBlocks.Clear();
ProxDict.Clear();

// Clear any active processing
foreach (var entry in BlocksToProcess)
{
for (int i = 0; i < entry.Value.Length; i++)
Expand All @@ -109,20 +107,17 @@ public void Init(ShipyardType yardType)
}
}

// Clear old grids
foreach (var grid in YardGrids)
{
if (grid != null)
{
((MyCubeGrid)grid).OnGridSplit -= OnGridSplit;
}
}
YardGrids.Clear();

// Set new state
YardGrids.Clear();
YardType = yardType;

// Register new grids
foreach (IMyCubeGrid grid in ContainsGrids.Where(g => g != null && !g.MarkedForClose))
{
((MyCubeGrid)grid).OnGridSplit += OnGridSplit;
Expand All @@ -132,7 +127,6 @@ public void Init(ShipyardType yardType)
ContainsGrids.Clear();
IntersectsGrids.Clear();

// Enable tools
Utilities.Invoke(() =>
{
foreach (IMyCubeBlock tool in Tools)
Expand All @@ -149,7 +143,6 @@ public void Init(ShipyardType yardType)
});

Logging.Instance.WriteDebug($"[ShipyardItem.Init] Initialized with {YardGrids.Count} grids");

Communication.SendYardState(this);
}
finally
Expand Down Expand Up @@ -186,6 +179,29 @@ public void Disable(bool broadcast = true, string reason = null)
}
}

public bool CanProcessCommand(ShipyardType newType)
{
if (_isDisabling || _isOperating)
{
Logging.Instance.WriteDebug($"[ShipyardItem.CanProcessCommand] Yard {EntityId} is busy (Disabling: {_isDisabling}, Operating: {_isOperating})");
return false;
}

if (Tools.Any(t => t.Closed || t.MarkedForClose))
{
Logging.Instance.WriteDebug($"[ShipyardItem.CanProcessCommand] Yard {EntityId} has closed tools");
return false;
}

if (YardEntity.Closed || YardEntity.MarkedForClose)
{
Logging.Instance.WriteDebug($"[ShipyardItem.CanProcessCommand] Yard {EntityId} entity is closed");
return false;
}

return true;
}

public void ProcessDisable()
{
if (!_shouldDisable.Item1)
Expand Down

0 comments on commit ab27e7d

Please sign in to comment.