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

fix: pk3dirs cannot be excluded #9

Merged
merged 3 commits into from
Jun 7, 2024
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
4 changes: 2 additions & 2 deletions Pack3r.Core/IO/AssetSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Pack3r.IO;

public abstract class AssetSource : IDisposable
{
public abstract bool IsPak0 { get; }
public abstract bool IsExcluded { get; }
public abstract string RootPath { get; }
public abstract FileInfo? GetShaderlist();

Expand Down Expand Up @@ -42,7 +42,7 @@ public bool TryHandleAsset(

if (Assets.TryGetValue(relativePath, out IAsset? asset))
{
if (IsPak0)
if (IsExcluded)
{
entry = null;
}
Expand Down
8 changes: 4 additions & 4 deletions Pack3r.Core/IO/DirectoryAssetSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

namespace Pack3r.IO;

public sealed class DirectoryAssetSource(DirectoryInfo directory, IIntegrityChecker checker) : AssetSource(checker)
public sealed class DirectoryAssetSource(DirectoryInfo directory, bool isExcluded, IIntegrityChecker checker) : AssetSource(checker)
{
public DirectoryInfo Directory { get; } = directory;
public override string RootPath => Directory.FullName;
public override bool IsPak0 => false;
public DirectoryInfo Directory => directory;
public override string RootPath => directory.FullName;
public override bool IsExcluded => isExcluded;

public override string ToString() => $"{{ Dir: {Directory.FullName} }}";

Expand Down
6 changes: 3 additions & 3 deletions Pack3r.Core/IO/Pk3AssetSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace Pack3r.IO;

public sealed class Pk3AssetSource(string path, bool isPak0, IIntegrityChecker checker) : AssetSource(checker)
public sealed class Pk3AssetSource(string path, bool isExcluded, IIntegrityChecker checker) : AssetSource(checker)
{
public string ArchivePath { get; } = path;
public override bool IsPak0 { get; } = isPak0;
public string ArchivePath => path;
public override bool IsExcluded { get; } = isExcluded;
public override string RootPath => ArchivePath;

private readonly ZipArchive _archive = ZipFile.OpenRead(path);
Expand Down
31 changes: 12 additions & 19 deletions Pack3r.Core/Models/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ 3. pk3dirs in reverse alphabetical order
unique.Add(GetMapRoot());
yield return new DirectoryInfo(GetMapRoot());

// try to add etmain second in case .map was in a pk3dir
if (unique.Add(ETMain.FullName))
yield return ETMain;

foreach (var pk3dir in ETMain
.EnumerateDirectories("*.pk3dir", SearchOption.TopDirectoryOnly)
.OrderByDescending(dir => dir.Name, StringComparer.OrdinalIgnoreCase))
{
if (IsExcluded(pk3dir) == SourceFilter.Ignored)
continue;

if (unique.Add(pk3dir.FullName))
yield return pk3dir;
}
Expand All @@ -149,15 +153,17 @@ private ImmutableArray<AssetSource> InitAssetSources()

foreach (var dir in AssetDirectories)
{
var dirFilter = IsExcluded(dir);

// never ignore etmain
if (!dir.FullName.EqualsF(ETMain.FullName) && IsExcluded(dir) == SourceFilter.Ignored)
if (dirFilter == SourceFilter.Ignored && !dir.FullName.EqualsF(ETMain.FullName))
{
continue;
}

// is exclude support needed for dirs?
list.Add(new DirectoryAssetSource(dir, _integrityChecker));
list.Add(new DirectoryAssetSource(dir, isExcluded: dirFilter == SourceFilter.Excluded, _integrityChecker));

// TODO: exclude all child pk3s in a pk3dir?
if (_options.LoadPk3s || _options.ExcludeSources.Count > 0)
{
foreach (var file in dir.EnumerateFiles("*.pk3", SearchOption.TopDirectoryOnly))
Expand All @@ -175,14 +181,14 @@ private ImmutableArray<AssetSource> InitAssetSources()

/*
Same ordering as in AssetDirectories, but:
1. pak0 is always first
1. pak0 is always first (and other excluded sources)
2. all other pk3s are always last, in reverse alphabetical order
*/
return list
.OrderBy(s => s switch
{
DirectoryAssetSource d => AssetDirectories.IndexOf(d.Directory),
Pk3AssetSource p => p.IsPak0 ? int.MinValue : int.MaxValue,
Pk3AssetSource p => p.IsExcluded ? int.MinValue : int.MaxValue,
_ => 0,
})
.ThenByDescending(s => IOPath.GetFileNameWithoutExtension(s.RootPath))
Expand All @@ -191,20 +197,7 @@ 1. pak0 is always first

private SourceFilter IsExcluded(FileSystemInfo item)
{
scoped ReadOnlySpan<char> dirOrPk3;

if (item is FileInfo file)
{
dirOrPk3 = IOPath.GetFileName(file.FullName.AsSpan());
}
else if (item is DirectoryInfo dir)
{
dirOrPk3 = IOPath.GetDirectoryName(dir.FullName.AsSpan());
}
else
{
return SourceFilter.Ignored;
}
ReadOnlySpan<char> dirOrPk3 = IOPath.GetFileName(item.FullName.AsSpan());

foreach (var value in _options.IgnoreSources)
{
Expand Down
2 changes: 1 addition & 1 deletion Pack3r.Core/Parsers/ShaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ bool skipPredicate(string name)
return toReturn;
}

if (!a.Source.IsPak0 || !b.Source.IsPak0)
if (!a.Source.IsExcluded || !b.Source.IsExcluded)
{
duplicate.AddOrUpdate(
key: a.Name,
Expand Down
2 changes: 1 addition & 1 deletion Pack3r.Core/Services/AssetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<Map> GetPackingData(CancellationToken cancellationToken)
{
string srcMsg = string.Join(
Environment.NewLine,
map.AssetSources.Select(src => $"\t{src.RootPath}{(src.IsPak0 ? " (not packed)" : "")}"));
map.AssetSources.Select(src => $"\t{src.RootPath}{(src.IsExcluded ? " (not packed)" : "")}"));
logger.System($"Using source(s) for discovery: {Environment.NewLine}{srcMsg}");

foreach (var res in map.Resources)
Expand Down
4 changes: 2 additions & 2 deletions Pack3r.Core/Services/Packager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ bool IsHandledOrExcluded(ReadOnlyMemory<char> relativePath)

foreach (var source in map.AssetSources)
{
if (source.IsPak0 && source.Contains(relativePath))
if (source.IsExcluded && source.Contains(relativePath))
return true;
}

Expand All @@ -179,7 +179,7 @@ void AddCompileFile(string absolutePath)

void AddShaderFile(Shader shader)
{
if (shader.Source is Pk3AssetSource { IsPak0: true })
if (shader.Source is Pk3AssetSource { IsExcluded: true })
return;

if (TryAddFileFromSource(shader.Source, shader.DestinationPath.AsMemory()))
Expand Down
2 changes: 1 addition & 1 deletion Pack3r.Tests/ShaderParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Pack3r.Tests;

public class ShaderParserTests
{
private static DirectoryAssetSource Source => new(new DirectoryInfo("~/"), new NoopChecker());
private static DirectoryAssetSource Source => new(new DirectoryInfo("~/"), false, new NoopChecker());

private static async Task<Shader> ParseSingle(string data, bool includeDevFiles = false)
{
Expand Down