Skip to content

Commit

Permalink
fix: pk3dirs cannot be excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
ovska committed Jun 6, 2024
1 parent 3d26450 commit 9203842
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
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 isPak0, 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 => isPak0;

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
19 changes: 13 additions & 6 deletions Pack3r.Core/Models/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ 3. pk3dirs in reverse alphabetical order
.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 +152,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, isPak0: 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 +180,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 @@ -199,7 +204,9 @@ private SourceFilter IsExcluded(FileSystemInfo item)
}
else if (item is DirectoryInfo dir)
{
dirOrPk3 = IOPath.GetDirectoryName(dir.FullName.AsSpan());
dirOrPk3 = dir.FullName.GetExtension().IsEmpty
? IOPath.GetDirectoryName(dir.FullName.AsSpan())
: IOPath.GetFileName(dir.FullName.AsSpan());
}
else
{
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

0 comments on commit 9203842

Please sign in to comment.