Skip to content

Commit

Permalink
Fixed issue with magic respawning assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Apr 19, 2024
1 parent 0eabbd0 commit 2ab1133
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Data/Scripts/AssemblyScripts/AssembliesSessionInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage.Game.Components;
using VRage.Utils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ public void PartRemoved(bool notifyMods = true)
if (Block.Integrity <= 0)
DefinitionHandler.I.SendOnPartDestroy(AssemblyDefinition.Name, MemberAssembly?.AssemblyId ?? -1, Block.FatBlock.EntityId, IsBaseBlock);
}

AssemblyPartManager.I.QueueConnectionCheck(this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage.Game.Components;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
Expand Down Expand Up @@ -86,7 +86,10 @@ public void UpdateAfterSimulation()

if (AssembliesSessionInit.DebugMode)
{
MyAPIGateway.Utilities.ShowNotification($"Assemblies: {AllPhysicalAssemblies.Count} | Parts: {AllAssemblyParts.Count}", 1000 / 60);
int partCount = 0;
foreach (var definition in AllAssemblyParts)
partCount += definition.Value.Count;
MyAPIGateway.Utilities.ShowNotification($"Assemblies: {AllPhysicalAssemblies.Count} | Parts: {partCount}", 1000 / 60);
MyAPIGateway.Utilities.ShowNotification($"Definitions: {DefinitionHandler.I.ModularDefinitions.Count}", 1000 / 60);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage.Game.ModAPI;
using VRageMath;

Expand Down Expand Up @@ -33,7 +34,6 @@ public void Update()
foreach (var conPart in part.ConnectedParts)
DebugDrawManager.AddLine(DebugDrawManager.GridToGlobal(part.Block.Position, part.Block.CubeGrid), DebugDrawManager.GridToGlobal(conPart.Block.Position, part.Block.CubeGrid), color, 0f);
}
//DebugDrawManager.AddGPS($"ASM {AssemblyId} Parts: {ComponentParts.Count}", ComponentParts[0].Block.FatBlock.GetPosition(), 1/60f);
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public void Close()
AssemblyPartManager.I.AllPhysicalAssemblies.Remove(AssemblyId);
}



public void MergeWith(PhysicalAssembly assembly)
{
Expand Down
2 changes: 1 addition & 1 deletion Data/Scripts/AssemblyScripts/Commands/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Definitions;

namespace Modular_Assemblies.Data.Scripts.AssemblyScripts
Expand Down
7 changes: 5 additions & 2 deletions Data/Scripts/AssemblyScripts/DebugUtils/ModularLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
using System.Threading.Tasks;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Definitions;

namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug
namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils
{
/// <summary>
/// Standard logging class. Outputs to %AppData%\Roaming\Space Engineers\Storage\
/// </summary>
public class ModularLog
{
readonly TextWriter _writer;
private readonly TextWriter _writer;
private static ModularLog I;

public static void Log(string message)
{
I._Log(message);
}

public static void LogException(Exception ex, Type callingType, string prefix = "")
{
I._LogException(ex, callingType, prefix);
Expand All @@ -34,6 +35,7 @@ public static void Init()

private ModularLog()
{
MyAPIGateway.Utilities.DeleteFileInGlobalStorage("ModularAssemblies.log");
_writer = MyAPIGateway.Utilities.WriteFileInGlobalStorage("ModularAssemblies.log"); // Only creating one debug.log to avoid clutter. Might change in the future.
_writer.WriteLine($"Modular Assemblies v{ApiHandler.ModVersion.X} - Debug Log\n===========================================\n");
_writer.Flush();
Expand All @@ -54,6 +56,7 @@ private void _Log(string message)
_writer.WriteLine($"{DateTime.UtcNow:HH:mm:ss}: {message}");
_writer.Flush();
}

private void _LogException(Exception ex, Type callingType, string prefix = "")
{
if (ex == null)
Expand Down
2 changes: 1 addition & 1 deletion Data/Scripts/AssemblyScripts/DebugUtils/SoftHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug
namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils
{
public class SoftHandle
{
Expand Down
12 changes: 10 additions & 2 deletions Data/Scripts/AssemblyScripts/Definitions/ApiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.ModAPI;
Expand Down Expand Up @@ -118,7 +118,10 @@ private void RecreateAssembly(int assemblyId)
return;

foreach (var part in assembly.ComponentParts)
{
part.PartRemoved();
AssemblyPartManager.I.QueueConnectionCheck(part);
}
}

#endregion
Expand Down Expand Up @@ -173,7 +176,12 @@ private void RecreateConnections(IMyCubeBlock block, string definitionName)
if (definition == null)
return;

AssemblyPartManager.I.AllAssemblyParts[definition].GetValueOrDefault(block.SlimBlock, null)?.PartRemoved();
AssemblyPart part = AssemblyPartManager.I.AllAssemblyParts[definition].GetValueOrDefault(block.SlimBlock, null);
if (part != null)
{
part.PartRemoved();
AssemblyPartManager.I.QueueConnectionCheck(part);
}
}

#endregion
Expand Down
14 changes: 7 additions & 7 deletions Data/Scripts/AssemblyScripts/Definitions/ApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage;
using VRage.Game.Components;
using VRage.Utils;
using VRageMath;

namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.Definitions
{
[MySessionComponentDescriptor(MyUpdateOrder.NoUpdate, Priority = 0)]
internal class ApiHandler : MySessionComponentBase
internal class ApiHandler
{
private const long Channel = 8774;
public static readonly Vector2I ModVersion = new Vector2I(0, 1); // Mod version, API version
private readonly IReadOnlyDictionary<string, Delegate> _apiDefinitions = new ApiDefinitions().ModApiMethods;
private MyTuple<Vector2I, IReadOnlyDictionary<string, Delegate>> _endpointTuple;
private readonly IReadOnlyDictionary<string, Delegate> _apiDefinitions;
private readonly MyTuple<Vector2I, IReadOnlyDictionary<string, Delegate>> _endpointTuple;

/// <summary>
/// Is the API ready?
Expand All @@ -40,8 +39,9 @@ private void HandleMessage(object o)
/// <summary>
/// Registers for API requests and updates any pre-existing clients.
/// </summary>
public override void LoadData()
public ApiHandler()
{
_apiDefinitions = new ApiDefinitions().ModApiMethods;
_endpointTuple = new MyTuple<Vector2I, IReadOnlyDictionary<string, Delegate>>(ModVersion, _apiDefinitions);

MyAPIGateway.Utilities.RegisterMessageHandler(Channel, HandleMessage);
Expand All @@ -62,7 +62,7 @@ public override void LoadData()
/// <summary>
/// Unloads all API endpoints and detaches events.
/// </summary>
protected override void UnloadData()
public void Unload()
{
MyAPIGateway.Utilities.UnregisterMessageHandler(Channel, HandleMessage);

Expand Down
10 changes: 6 additions & 4 deletions Data/Scripts/AssemblyScripts/Definitions/DefinitionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage;
using VRage.Game.Components;
using VRage.Profiler;
Expand All @@ -20,17 +20,18 @@ namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.Definitions
internal class DefinitionHandler
{
public static DefinitionHandler I;
const int DefinitionMessageId = 8772;
const int InboundMessageId = 8773;
const int OutboundMessageId = 8771;

internal ApiHandler ApiHandler;

public Dictionary<string, ModularDefinition> ModularDefinitionsMap = new Dictionary<string, ModularDefinition>();
public ICollection<ModularDefinition> ModularDefinitions => ModularDefinitionsMap.Values;

public void Init()
{
I = this;

ApiHandler = new ApiHandler();
ModularLog.Log("DefinitionHandler loading...");
MyAPIGateway.Session.OnSessionReady += CheckValidDefinitions;
}
Expand All @@ -40,6 +41,7 @@ public void Unload()
I = null;
ModularLog.Log("DefinitionHandler closing...");
MyAPIGateway.Session.OnSessionReady -= CheckValidDefinitions;
ApiHandler.Unload();
}

/// <summary>
Expand Down Expand Up @@ -184,7 +186,7 @@ public void SendOnPartDestroy(string DefinitionName, int PhysicalAssemblyId, lon
IsBaseBlock
);

SendFunc(new FunctionCall()
SendFunc(new FunctionCall
{
ActionId = FunctionCall.ActionType.OnPartDestroy,
DefinitionName = DefinitionName,
Expand Down
2 changes: 1 addition & 1 deletion Data/Scripts/AssemblyScripts/ModularDefiniton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.DebugUtils;
using VRage.Game.ModAPI;
using VRage.Utils;
using VRageMath;
Expand Down

0 comments on commit 2ab1133

Please sign in to comment.