Skip to content

Commit

Permalink
Allow loadacts to take a file
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugOk committed Feb 19, 2024
1 parent b0c9f56 commit 9d39233
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
65 changes: 65 additions & 0 deletions Content.Client/Actions/ActionsSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Linq;
using Content.Shared.Actions;
using Content.Shared.Mapping;
using JetBrains.Annotations;
using Robust.Client.Player;
using Robust.Shared.ContentPack;
Expand Down Expand Up @@ -310,6 +311,70 @@ public void LoadActionAssignments(string path, bool userData)
AssignSlot?.Invoke(assignments);
}

/// <summary>
/// Load actions and their toolbar assignments from a file.
/// DeltaV - Load from an existing yaml stream instead
/// </summary>
public void LoadActionAssignments(YamlStream stream)
{
if (_playerManager.LocalEntity is not { } user)
return;

if (stream.Documents[0].RootNode.ToDataNode() is not SequenceDataNode sequence)
return;

ClearAssignments?.Invoke();

var assignments = new List<SlotAssignment>();
var existingActions = GetClientActions();
var existingActionsList = existingActions.ToList();

foreach (var entry in sequence.Sequence)
{
if (entry is not MappingDataNode map)
continue;

if (!map.TryGet("action", out var actionNode))
continue;

if (!map.TryGet<ValueDataNode>("name", out var nameNode))
continue;

var action = _serialization.Read<BaseActionComponent>(actionNode, notNullableOverride: true);

// Prevent spawning actions multiple times
var existing = existingActionsList.FirstOrNull(a =>
Name(a.Id) == nameNode.Value);

EntityUid actionId;
if (existing == null)
{
actionId = Spawn(null);
AddComp(actionId, action);
_metaData.SetEntityName(actionId, nameNode.Value);
DirtyEntity(actionId);
AddActionDirect(user, actionId);
}
else
{
actionId = existing.Value.Id;
}

if (!map.TryGet("assignments", out var assignmentNode))
continue;

var nodeAssignments = _serialization.Read<List<(byte Hotbar, byte Slot)>>(assignmentNode, notNullableOverride: true);

foreach (var index in nodeAssignments)
{
var assignment = new SlotAssignment(index.Hotbar, index.Slot, actionId);
assignments.Add(assignment);
}
}

AssignSlot?.Invoke(assignments);
}

public record struct SlotAssignment(byte Hotbar, byte Slot, EntityUid ActionId);
}
}
29 changes: 26 additions & 3 deletions Content.Client/Commands/ActionsCommands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.IO;
using Content.Client.Actions;
using Content.Client.Mapping;
using Content.Shared.Administration;
using Robust.Client.UserInterface;
using Robust.Shared.Console;
using YamlDotNet.RepresentationModel;

namespace Content.Client.Commands;

Expand Down Expand Up @@ -34,7 +37,7 @@ public void Execute(IConsoleShell shell, string argStr, string[] args)
}
*/

[AnyCommand]
// [AnyCommand] DeltaV - Disable AnyCommand, require at least admin rank
public sealed class LoadActionsCommand : LocalizedCommands
{
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
Expand All @@ -47,7 +50,7 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteLine(Help);
LoadActs(); // DeltaV - Load from a file dialogue instead
return;
}

Expand All @@ -60,9 +63,29 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)
shell.WriteError(LocalizationManager.GetString($"cmd-{Command}-error"));
}
}

/// <summary>
/// DeltaV - Load actions from a file stream instead
/// </summary>
private static async void LoadActs()
{
var fileMan = IoCManager.Resolve<IFileDialogManager>();
var actMan = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<ActionsSystem>();

var stream = await fileMan.OpenFile(new FileDialogFilters(new FileDialogFilters.Group("yml")));
if (stream is null)
return;

var reader = new StreamReader(stream);
var yamlStream = new YamlStream();
yamlStream.Load(reader);

actMan.LoadActionAssignments(yamlStream);
reader.Close();
}
}

[AnyCommand]
// [AnyCommand] DeltaV - Disable AnyCommand, require at least admin rank
public sealed class LoadMappingActionsCommand : LocalizedCommands
{
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
Expand Down

0 comments on commit 9d39233

Please sign in to comment.