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
12 changes: 12 additions & 0 deletions Tests/Player/ColorizationCatalogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public void VariantsHaveStableIdsAndKeepPaletteAnimationsTogether()
Assert.That(assets.Single(asset => asset.Id == "game/third/pin2dmd.pac").RequiresKey, Is.True);
}
[Test]
public void GamesWhoseKeyCantNameAFolderHaveNoColorizations()
{
// a key with a version, like game/1.0: no library folder is theirs, nothing throws
Asset("game/1.0/game.crz");
var game = new GameIdentity("example", "game/1.0");
Assert.That(ColorizationCatalog.IsFolderName(game.Key), Is.False);
Assert.That(ColorizationCatalog.Read(_root, game.Key), Is.Empty);
Assert.That(ColorizationCatalog.Select(_root, Path.Combine(_root, "Table.vpe"), game, "auto", false), Is.Null);
Assert.That(ColorizationCatalog.Read(_root, ".."), Is.Empty);
Assert.Throws<ArgumentException>(() => ColorizationCatalog.Stage(new ColorizationAsset("x", "x", Asset("game/game.crz")), Path.Combine(_root, "cache"), game.Key, out _));
}
[Test]
public void AmbiguousAutomaticAndMissingManualChoicesNeverFallBack()
{
// Variants in subfolders aren't what DmdDevice loads, so automatic selection refuses to guess.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ internal sealed class ColorizationAsset
}
internal static class ColorizationCatalog
{
/// <summary>Whether a game key can be the name of its folder in a colorization library.</summary>
internal static bool IsFolderName(string game) => !string.IsNullOrWhiteSpace(game) && game == Path.GetFileName(game) && game != "." && game != ".."
&& game.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;

internal static IReadOnlyList<ColorizationAsset> Read(string root, string game)
{
if (string.IsNullOrWhiteSpace(root) || string.IsNullOrWhiteSpace(game)) return Array.Empty<ColorizationAsset>();
if (game != Path.GetFileName(game) || game == "." || game == "..") throw new ArgumentException("Invalid game key.");
// a game whose key can't name a folder, like one with a version (game/1.0), has no colorizations
if (string.IsNullOrWhiteSpace(root) || !IsFolderName(game)) return Array.Empty<ColorizationAsset>();
root = Path.GetFullPath(root);
string directory = Path.Combine(root, game);
if (!Directory.Exists(directory)) return Array.Empty<ColorizationAsset>();
Expand Down Expand Up @@ -107,7 +111,7 @@ internal static ColorizationAsset Select(string root, string game, string select
/// <summary>LibDmd accepts a game folder. An owned cache presents exactly the selected files, leaving the library unchanged.</summary>
internal static IDisposable Stage(ColorizationAsset asset, string cache, string game, out string root)
{
if (game != Path.GetFileName(game) || game == "." || game == "..") throw new ArgumentException("Invalid game key.");
if (!IsFolderName(game)) throw new ArgumentException("Invalid game key.");
root = Path.GetFullPath(Path.Combine(cache, Guid.NewGuid().ToString("N")));
string owned = root;
if (!owned.StartsWith(Path.GetFullPath(cache) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("Invalid cache location.");
Expand Down
Loading