Skip to content

Commit

Permalink
Bugfixing and New API Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Apr 19, 2024
1 parent 803af2d commit b4ba5e8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 21 deletions.
1 change: 1 addition & 0 deletions Data/Scripts/AssemblyScripts/AssembliesSessionInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected override void UnloadData()
I = null;
watch.Stop();
ModularLog.Log($"Finished unloading in {watch.ElapsedMilliseconds}ms.");
ModularLog.Close();
}

#endregion
Expand Down
44 changes: 34 additions & 10 deletions Data/Scripts/AssemblyScripts/Commands/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@

namespace Modular_Assemblies.Data.Scripts.AssemblyScripts
{
/// <summary>
/// Parses commands from chat and triggers relevant methods.
/// </summary>
public class CommandHandler
{
public static CommandHandler I;

private Dictionary<string, Command> commands = new Dictionary<string, Command>()
private readonly Dictionary<string, Command> _commands = new Dictionary<string, Command>
{
["help"] = new Command("ModularAssemblies", "Displays command help.", message => I.ShowHelp()),
["debug"] = new Command("ModularAssemblies", "Toggles debug draw.", message => AssembliesSessionInit.DebugMode = !AssembliesSessionInit.DebugMode;),
["help"] = new Command(
"Modular Assemblies",
"Displays command help.",
message => I.ShowHelp()),
["debug"] = new Command(
"Modular Assemblies",
"Toggles debug draw.",
message => AssembliesSessionInit.DebugMode = !AssembliesSessionInit.DebugMode),
};

private void ShowHelp()
{
StringBuilder helpBuilder = new StringBuilder();
List<string> modNames = new List<string>();
foreach (var command in commands.Values)
foreach (var command in _commands.Values)
if (!modNames.Contains(command.modName))
modNames.Add(command.modName);

MyAPIGateway.Utilities.ShowMessage("Modular Assemblies Help", "");

foreach (var modName in modNames)
{
foreach (var command in commands)
foreach (var command in _commands)
if (command.Value.modName == modName)
helpBuilder.Append($"\n{{!md {command.Key}}}: " + command.Value.helpText);

Expand All @@ -53,7 +62,10 @@ public static void Init()
public static void Close()
{
if (I != null)
{
MyAPIGateway.Utilities.MessageEnteredSender -= I.Command_MessageEnteredSender;
I._commands.Clear();
}
I = null;
}

Expand All @@ -76,10 +88,10 @@ private void Command_MessageEnteredSender(ulong sender, string messageText, ref
}

// Really basic command handler
if (commands.ContainsKey(parts[0].ToLower()))
commands[parts[0].ToLower()].action.Invoke(parts);
if (_commands.ContainsKey(parts[0].ToLower()))
_commands[parts[0].ToLower()].action.Invoke(parts);
else
MyAPIGateway.Utilities.ShowMessage("Modular Assemblies", $"Unrecognized command \"{messageText}\" ({sender})");
MyAPIGateway.Utilities.ShowMessage("Modular Assemblies", $"Unrecognized command \"{parts[0].ToLower()}\"");
}
catch (Exception ex)
{
Expand All @@ -98,16 +110,28 @@ public static void AddCommand(string command, string helpText, Action<string[]>
if (I == null)
return;

if (I.commands.ContainsKey(command))
if (I._commands.ContainsKey(command))
{
SoftHandle.RaiseException("Attempted to add duplicate command " + command + " from [" + modName + "]", callingType: typeof(CommandHandler));
return;
}

I.commands.Add(command, new Command(modName, helpText, action));
I._commands.Add(command, new Command(modName, helpText, action));
ModularLog.Log($"Registered new chat command \"!{command}\" from [{modName}]");
}

/// <summary>
/// Removes a command from Modular Assemblies' command handler.
/// </summary>
/// <param name="command"></param>
public static void RemoveCommand(string command)
{
if (I == null || command == "help" || command == "debug") // Debug and Help should never be removed.
return;
if (I._commands.Remove(command))
ModularLog.Log($"De-registered chat command \"!{command}\".");
}

private class Command
{
public string modName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.Commands
{
internal static class DebugCommands
/// <summary>
/// Stores methods for commands in CommandHandler.
/// </summary>
internal static class CommandMethods
{

}
Expand Down
28 changes: 21 additions & 7 deletions Data/Scripts/AssemblyScripts/Definitions/ApiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Modular_Assemblies.Data.Scripts.AssemblyScripts.Debug;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.ModAPI;
Expand All @@ -17,16 +18,29 @@ internal ApiDefinitions()
{
ModApiMethods = new Dictionary<string, Delegate>()
{
["GetAllParts"] = new Func<MyEntity[]>(GetAllParts),
["GetAllAssemblies"] = new Func<int[]>(GetAllAssemblies),
["GetMemberParts"] = new Func<int, MyEntity[]>(GetMemberParts),
// Global assembly methods
["GetAllParts"] = new Func<MyEntity[]>(GetAllParts), // Returns a MyEntity array of all CubeBlocks with Assembly parts.
["GetAllAssemblies"] = new Func<int[]>(GetAllAssemblies), // Returns an int array of all Assembly IDs.

// Per-assembly methods
["GetMemberParts"] = new Func<int, MyEntity[]>(GetMemberParts), // Returns a MyEntity array of all CubeBlocks within a given Assembly ID.
["GetAssemblyGrid"] = new Func<int, IMyCubeGrid>(GetAssemblyGrid), // Returns the IMyCubeGrid an assembly ID is contained in.
["AddOnAssemblyClose"] = new Action<Action<int>>(AddOnAssemblyClose), // Registers an action triggered on assembly removal.
["RemoveOnAssemblyClose"] = new Action<Action<int>>(RemoveOnAssemblyClose), // De-registers an action triggered on assembly removal.
// TODO: RecreateAssembly - Destroys assembly and makes all contained blocks queue for search.
// TODO: Replace stupid dumb definition actions with nice fancy API methods.

// Per-part methods
["GetConnectedBlocks"] = new Func<MyEntity, bool, MyEntity[]>(GetConnectedBlocks),
["GetBasePart"] = new Func<int, MyEntity>(GetBasePart),
["IsDebug"] = new Func<bool>(() => AssembliesSessionInit.DebugMode),
["GetContainingAssembly"] = new Func<MyEntity, int>(GetContainingAssembly),
["GetAssemblyGrid"] = new Func<int, IMyCubeGrid>(GetAssemblyGrid),
["AddOnAssemblyClose"] = new Action<Action<int>>(AddOnAssemblyClose),
["RemoveOnAssemblyClose"] = new Action<Action<int>>(RemoveOnAssemblyClose),
// TODO: RecreateConnections - Destroys connections and queues for search.

// Global methods
["IsDebug"] = new Func<bool>(() => AssembliesSessionInit.DebugMode), // Returns whether debug mode is enabled or not.
["LogWriteLine"] = new Action<string>(ModularLog.Log), // Writes a new line in the Modular Assemblies debug log.
["AddChatCommand"] = new Action<string, string, Action<string[]>, string>(CommandHandler.AddCommand), // Registers a command for Modular Assemblies' command handler.
["RemoveChatCommand"] = new Action<string>(CommandHandler.RemoveCommand), // Removes a command from Modular Assemblies' command handler.
};
}

Expand Down
4 changes: 2 additions & 2 deletions Data/Scripts/AssemblyScripts/Definitions/ApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Modular_Assemblies.Data.Scripts.AssemblyScripts.Definitions
internal class ApiHandler : MySessionComponentBase
{
private const long Channel = 8774;
public static readonly Vector2I ModVersion = new Vector2I(0, 0); // Mod version, API version
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;

Expand Down Expand Up @@ -55,7 +55,7 @@ public override void LoadData()
{
ModularLog.Log($"Exception in Api Load: {ex}");
}
ModularLog.Log("ModularDefinitionsAPI inited.");
ModularLog.Log($"ModularDefinitionsAPI v{ModVersion.Y} initialized.");
}


Expand Down
2 changes: 1 addition & 1 deletion Modular-Assemblies.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<Compile Include="Data\Scripts\AssemblyScripts\AssembliesSessionInit.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\AssemblyComponents\AssemblyPart.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\AssemblyComponents\AssemblyPartManager.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Commands\DebugCommands.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Commands\CommandMethods.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Commands\CommandHandler.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Debug\DebugDrawManager.cs" />
<Compile Include="Data\Scripts\AssemblyScripts\Debug\ModularLog.cs" />
Expand Down

0 comments on commit b4ba5e8

Please sign in to comment.