Skip to content

Commit

Permalink
Combine DefinitionDefs and DefinitionApi into one file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Apr 24, 2024
1 parent 6f5b39b commit 175843f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 88 deletions.
75 changes: 0 additions & 75 deletions Data/Scripts/AssemblyScripts/Definitions/DefinitionDefs.cs

This file was deleted.

12 changes: 6 additions & 6 deletions Data/Scripts/AssemblyScripts/Definitions/DefinitionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public string[] RegisterDefinitions(byte[] serialized)

try
{
var definitionSet = MyAPIGateway.Utilities.SerializeFromBinary<DefinitionContainer>(serialized);
var definitionSet = MyAPIGateway.Utilities.SerializeFromBinary<ModularDefinitionContainer>(serialized);
return RegisterDefinitions(definitionSet);
}
catch (Exception ex)
Expand All @@ -72,22 +72,22 @@ public string[] RegisterDefinitions(byte[] serialized)
/// <summary>
/// Registers a deserialized definition.
/// </summary>
/// <param name="definitionSet"></param>
/// <param name="modularDefinitionSet"></param>
/// <returns></returns>
public string[] RegisterDefinitions(DefinitionContainer definitionSet)
public string[] RegisterDefinitions(ModularDefinitionContainer modularDefinitionSet)
{
try
{
if (definitionSet == null)
if (modularDefinitionSet == null)
{
ModularLog.Log("Invalid definition container!");
return Array.Empty<string>();
}

ModularLog.Log($"Received {definitionSet.PhysicalDefs.Length} definitions.");
ModularLog.Log($"Received {modularDefinitionSet.PhysicalDefs.Length} definitions.");
var newValidDefinitions = new List<string>();

foreach (var def in definitionSet.PhysicalDefs)
foreach (var def in modularDefinitionSet.PhysicalDefs)
{
var modDef = ModularDefinition.Load(def);
if (modDef == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ProtoBuf;
using Sandbox.ModAPI;
using VRage;
using VRage.Game.ModAPI;
Expand Down Expand Up @@ -210,14 +211,14 @@ public void RecreateConnections(IMyCubeBlock blockPart, string definition)
/// <summary>
/// Registers a set of definitions with Modular Assemblies Framework.
/// </summary>
/// <param name="definitionContainer"></param>
/// <param name="modularDefinitionContainer"></param>
/// <returns></returns>
public string[] RegisterDefinitions(DefinitionDefs.DefinitionContainer definitionContainer)
public string[] RegisterDefinitions(DefinitionDefs.ModularDefinitionContainer modularDefinitionContainer)
{
var validDefinitions =
_registerDefinitions?.Invoke(MyAPIGateway.Utilities.SerializeToBinary(definitionContainer));
_registerDefinitions?.Invoke(MyAPIGateway.Utilities.SerializeToBinary(modularDefinitionContainer));

foreach (var definition in definitionContainer.PhysicalDefs)
foreach (var definition in modularDefinitionContainer.PhysicalDefs)
{
RegisterOnPartAdd(definition.Name, definition.OnPartAdd);
RegisterOnPartRemove(definition.Name, definition.OnPartRemove);
Expand Down Expand Up @@ -541,4 +542,77 @@ private void HandleMessage(object obj)

#endregion
}

public class DefinitionDefs
{
/// <summary>
/// Stores and serialized an array of definitions.
/// </summary>
[ProtoContract]
public class ModularDefinitionContainer
{
[ProtoMember(1)] internal ModularPhysicalDefinition[] PhysicalDefs;
}

/// <summary>
/// Class representing a Modular Assemblies definition.
/// </summary>
[ProtoContract]
public class ModularPhysicalDefinition
{
/// <summary>
/// The name of this definition. Must be unique!
/// </summary>
[ProtoMember(1)]
public string Name { get; set; }

/// <summary>
/// Triggered whenever the definition is first loaded.
/// </summary>
public Action OnInit { get; set; }

/// <summary>
/// Called when a valid part is placed.
/// <para>
/// Arg1 is PhysicalAssemblyId, Arg2 is BlockEntity, Arg3 is IsBaseBlock
/// </para>
/// </summary>
public Action<int, IMyCubeBlock, bool> OnPartAdd { get; set; }

/// <summary>
/// Called when a valid part is removed.
/// <para>
/// Arg1 is PhysicalAssemblyId, Arg2 is BlockEntity, Arg3 is IsBaseBlock
/// </para>
/// </summary>
public Action<int, IMyCubeBlock, bool> OnPartRemove { get; set; }

/// <summary>
/// Called when a component part is destroyed. Note - OnPartRemove is called simultaneously.
/// <para>
/// Arg1 is PhysicalAssemblyId, Arg2 is BlockEntity, Arg3 is IsBaseBlock
/// </para>
/// </summary>
public Action<int, IMyCubeBlock, bool> OnPartDestroy { get; set; }

/// <summary>
/// All allowed SubtypeIds. The mod will likely misbehave if two mods allow the same blocks, so please be cautious.
/// </summary>
[ProtoMember(2)]
public string[] AllowedBlocks { get; set; }

/// <summary>
/// Allowed connection directions. Measured in blocks. If an allowed SubtypeId is not included here, connections are
/// allowed on all sides. If the connection type whitelist is empty, all allowed subtypes may connect on that side.
/// </summary>
[ProtoMember(3)]
public Dictionary<string, Dictionary<Vector3I, string[]>> AllowedConnections { get; set; }

/// <summary>
/// The primary block of a PhysicalAssembly. Make sure this is an AssemblyCore block OR null.
/// </summary>
[ProtoMember(4)]
public string BaseBlock { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion Data/Scripts/AssemblyScripts/ModularDefiniton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ModularDefinition
public Action<int, IMyCubeBlock, bool> OnPartRemove;


public static ModularDefinition Load(PhysicalDefinition definition)
public static ModularDefinition Load(ModularPhysicalDefinition definition)
{
var def = new ModularDefinition
{
Expand Down
3 changes: 1 addition & 2 deletions Modular-Assemblies.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@
<Compile Include="Data\Scripts\AssemblyScripts\DebugUtils\SoftHandle.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Definitions\ApiDefinitions.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Definitions\ApiHandler.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Definitions\DefinitionApi.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Definitions\DefinitionDefs.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Definitions\ModularDefinitionApi.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Definitions\DefinitionHandler.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\ModularDefiniton.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\AssemblyComponents\PhysicalAssembly.cs" />
Expand Down

0 comments on commit 175843f

Please sign in to comment.