Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ModularDefinitionApi
/// <summary>
/// The expected API version. Don't touch this unless you're developing for the Modular Assemblies Framework.
/// </summary>
public const int ApiVersion = 2;
public const int ApiVersion = 3;

/// <summary>
/// Triggered whenever the API is ready - added to by the constructor or manually.
Expand Down Expand Up @@ -163,18 +163,18 @@ public IMyCubeGrid GetAssemblyGrid(int assemblyId)
/// Registers an Action<AssemblyId> triggered on assembly removal.
/// </summary>
/// <param name="action"></param>
public void AddOnAssemblyClose(Action<int> action)
public void RegisterOnAssemblyClose(string definitionName, Action<int> action)
{
_addOnAssemblyClose?.Invoke(action);
_registerOnAssemblyClose?.Invoke(definitionName, action);
}

/// <summary>
/// De-registers an Action(AssemblyId) triggered on assembly removal.
/// </summary>
/// <param name="action"></param>
public void RemoveOnAssemblyClose(Action<int> action)
public void UnregisterOnAssemblyClose(string definitionName, Action<int> action)
{
_removeOnAssemblyClose?.Invoke(action);
_unregisterOnAssemblyClose?.Invoke(definitionName, action);
}

/// <summary>
Expand All @@ -194,9 +194,9 @@ public void RecreateAssembly(int assemblyId)
/// <returns></returns>
public T GetAssemblyProperty<T>(int assemblyId, string propertyName)
{
var value = _getAssemblyProperty(assemblyId, propertyName);
object value = _getAssemblyProperty(assemblyId, propertyName);

return value == null ? default(T) : (T)value;
return value == null ? default(T) : (T) value;
}

/// <summary>
Expand Down Expand Up @@ -283,6 +283,7 @@ public string[] RegisterDefinitions(DefinitionDefs.ModularDefinitionContainer mo
RegisterOnPartAdd(definition.Name, definition.OnPartAdd);
RegisterOnPartRemove(definition.Name, definition.OnPartRemove);
RegisterOnPartDestroy(definition.Name, definition.OnPartDestroy);
RegisterOnAssemblyClose(definition.Name, definition.OnAssemblyClose);

if (validDefinitions.Contains(definition.Name))
definition.OnInit?.Invoke();
Expand Down Expand Up @@ -440,8 +441,8 @@ public void RemoveChatCommand(string command)
private Func<int, IMyCubeBlock[]> _getMemberParts;
private Func<int, IMyCubeBlock> _getBasePart;
private Func<int, IMyCubeGrid> _getAssemblyGrid;
private Action<Action<int>> _addOnAssemblyClose;
private Action<Action<int>> _removeOnAssemblyClose;
private Action<string, Action<int>> _registerOnAssemblyClose;
private Action<string, Action<int>> _unregisterOnAssemblyClose;
private Action<int> _recreateAssembly;
private Func<int, string, object> _getAssemblyProperty;
private Action<int, string, object> _setAssemblyProperty;
Expand Down Expand Up @@ -498,8 +499,8 @@ public bool ApiAssign()
SetApiMethod("GetMemberParts", ref _getMemberParts);
SetApiMethod("GetBasePart", ref _getBasePart);
SetApiMethod("GetAssemblyGrid", ref _getAssemblyGrid);
SetApiMethod("AddOnAssemblyClose", ref _addOnAssemblyClose);
SetApiMethod("RemoveOnAssemblyClose", ref _removeOnAssemblyClose);
SetApiMethod("RegisterOnAssemblyClose", ref _registerOnAssemblyClose);
SetApiMethod("UnregisterOnAssemblyClose", ref _unregisterOnAssemblyClose);
SetApiMethod("RecreateAssembly", ref _recreateAssembly);
SetApiMethod("GetAssemblyProperty", ref _getAssemblyProperty);
SetApiMethod("SetAssemblyProperty", ref _setAssemblyProperty);
Expand Down Expand Up @@ -662,6 +663,14 @@ public class ModularPhysicalDefinition
/// </summary>
public Action<int, IMyCubeBlock, bool> OnPartDestroy { get; set; }

/// <summary>
/// Called when an assembly is closed. Note - OnPartRemove will not be called.
/// <para>
/// Arg1 is PhysicalAssemblyId
/// </para>
/// </summary>
public Action<int> OnAssemblyClose { get; set; }

/// <summary>
/// All allowed SubtypeIds. The mod will likely misbehave if two mods allow the same blocks, so please be cautious.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ internal partial class ModularDefinition
// You can remove this function, and any others if need be.
},

OnAssemblyClose = assemblyId =>
{
SFusionManager.I.FusionSystems.Remove(assemblyId);
},

// The most important block in an assembly. Connection checking starts here.
BaseBlockSubtype = null,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ internal class SFusionManager
{
public static SFusionManager I = new SFusionManager();

private bool _didRegisterAssemblyClose;

private int _ticks;
public ModularDefinition FusionDefinition;
public Dictionary<int, SFusionSystem> FusionSystems = new Dictionary<int, SFusionSystem>();
Expand All @@ -29,12 +27,6 @@ public void Unload()

public void UpdateTick()
{
if (!_didRegisterAssemblyClose && (ModularApi?.IsReady ?? false))
{
ModularApi.AddOnAssemblyClose(assemblyId => FusionSystems.Remove(assemblyId));
_didRegisterAssemblyClose = true;
}

foreach (var fusionSystem in FusionSystems.Values)
fusionSystem.UpdateTick();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ internal partial class ModularDefinition
// Triggers whenever a part is destroyed, simultaneously with OnPartRemove
OnPartDestroy = null,

OnAssemblyClose = assemblyId =>
{
HeatManager.I.RemoveAssembly(assemblyId);
},

// The most important block in an assembly. Connection checking starts here.
BaseBlockSubtype = null,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public void UpdateTick()
HeatRatio = HeatStored / (HeatCapacity + BaseHeatCapacity);
}

public void RemoveAssembly(int assemblyId)
{
_heatSystems.Remove(assemblyId);
}

private void Update15Tick()
{
var gridSize = (Grid.Max - Grid.Min + Vector3I.One) * Grid.GridSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Sandbox.ModAPI;
using VRage.Game.ModAPI;
using VRage.ModAPI;
Expand Down Expand Up @@ -34,6 +35,12 @@ public void UpdateTick()
system.UpdateTick();
}

public void RemoveAssembly(int assemblyId)
{
foreach (var system in _heatSystems.Values)
system.RemoveAssembly(assemblyId);
}

public float GetGridHeatLevel(IMyCubeGrid grid)
{
return _heatSystems.GetValueOrDefault(grid, null)?.HeatRatio ?? -1;
Expand Down
Loading