Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audio filepath completion helper #4968

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ END TEMPLATE-->

### New features

*None yet*
* Add a CompletionHelper for audio filepaths that handles server packaging.

### Bugfixes

Expand Down
8 changes: 8 additions & 0 deletions Robust.Shared/Collections/ValueList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,12 @@ public void EnsureLength(int newCount)
region.Clear();
Count = newCount;
}

public void AddRange(IEnumerable<T> select)
{
foreach (var result in select)
{
Add(result);
}
}
}
24 changes: 24 additions & 0 deletions Robust.Shared/Console/CompletionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.Collections;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
Expand All @@ -23,6 +25,28 @@ public static class CompletionHelper
public static IEnumerable<CompletionOption> Booleans => new[]
{ new CompletionOption(bool.FalseString), new CompletionOption(bool.TrueString) };

/// <summary>
/// Special-cased file handler for audio that accounts for serverside completion.
/// </summary>
public static IEnumerable<CompletionOption> AudioFilePath(string arg, IPrototypeManager protoManager,
metalgearsloth marked this conversation as resolved.
Show resolved Hide resolved
IResourceManager res)
{
var results = new ValueList<CompletionOption>();

foreach (var proto in protoManager.EnumeratePrototypes<AudioMetadataPrototype>())
{
if (!proto.ID.StartsWith(arg))
continue;

results.Add(new CompletionOption(proto.ID));
}

results.AddRange(ContentFilePath(arg, res));
results.Sort();

return results;
}

public static IEnumerable<CompletionOption> ContentFilePath(string arg, IResourceManager res)
{
var curPath = arg;
Expand Down
8 changes: 7 additions & 1 deletion Robust.Shared/Console/CompletionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static CompletionOption[] ConvertOptions(IEnumerable<string> stringOpts)
/// <summary>
/// Possible option to tab-complete in a <see cref="CompletionResult"/>.
/// </summary>
public record struct CompletionOption(string Value, string? Hint = null, CompletionOptionFlags Flags = default)
public record struct CompletionOption(string Value, string? Hint = null, CompletionOptionFlags Flags = default) : IComparable<CompletionOption>
{
/// <summary>
/// The value that will be filled in if completed.
Expand All @@ -54,6 +54,12 @@ public record struct CompletionOption(string Value, string? Hint = null, Complet
/// Flags that control how this completion is used.
/// </summary>
public CompletionOptionFlags Flags { get; set; } = Flags;

public int CompareTo(CompletionOption other)
{
var valueComparison = string.Compare(Value, other.Value, StringComparison.CurrentCultureIgnoreCase);
return valueComparison;
}
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions Robust.Shared/ContentPack/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,16 @@ public bool TryContentFileRead(string path, [NotNullWhen(true)] out Stream? file
/// <inheritdoc />
public bool TryContentFileRead(ResPath? path, [NotNullWhen(true)] out Stream? fileStream)
{
fileStream = null;

if (path == null)
{
throw new ArgumentNullException(nameof(path));
return false;
}

if (!path.Value.IsRooted)
{
throw new ArgumentException($"Path '{path}' must be rooted", nameof(path));
return false;
metalgearsloth marked this conversation as resolved.
Show resolved Hide resolved
}
#if DEBUG
if (!IsPathValid(path.Value))
Expand Down
Loading