Skip to content

Commit

Permalink
file processors initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Oct 17, 2024
1 parent 00edd45 commit 067afed
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/StarBreaker.P4k/FileProcessing/CryXmlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ public sealed class CryXmlProcessor : IFileProcessor
{
public bool CanProcess(string entryName, Stream stream)
{
if (stream.Length < 4)
return false;

Span<byte> test = stackalloc byte[4];
if (stream.Read(test) != 4)
var read = stream.Read(test);
stream.Seek(0, SeekOrigin.Current);

if (read != 4)
return false;

return CryXml.IsCryXmlB(test);
}

public void ProcessEntry(string outputRootFolder, string entryName, Stream stream)
public void ProcessEntry(string outputRootFolder, string entryName, Stream entryStream)
{
throw new NotImplementedException();
}
Expand Down
31 changes: 31 additions & 0 deletions src/StarBreaker.P4k/FileProcessing/IFileProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace StarBreaker.P4k;

public interface IFileProcessor
{
bool CanProcess(string entryName, Stream stream);
void ProcessEntry(string outputRootFolder, string entryName, Stream entryStream);
}

public static class FileProcessors
{
private static readonly GenericFileProcessor _genericFileProcessor = new();
public static List<IFileProcessor> Processors { get; }

static FileProcessors()
{
//reflection would be nicer but native aot doesn't support it
Processors =
[
new CryXmlProcessor(),
new ZipFileProcessor(),
];
}

public static IFileProcessor GetProcessor(string entryName, Stream entryStream)
{
if (Processors.FirstOrDefault(p => p.CanProcess(entryName, entryStream)) is { } processor)
return processor;

return _genericFileProcessor;
}
}
6 changes: 3 additions & 3 deletions src/StarBreaker.P4k/FileProcessing/ZipFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public bool CanProcess(string entryName, Stream stream)
return entryName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase);
}

public void ProcessEntry(string outputRootFolder, string entryName, Stream stream)
public void ProcessEntry(string outputRootFolder, string entryName, Stream entryStream)
{
var entryPath = Path.Combine(outputRootFolder, Path.ChangeExtension(entryName, "unzipped"));
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);

using var archive = new ZipArchive(entryStream, ZipArchiveMode.Read, leaveOpen: true);
foreach (var childEntry in archive.Entries)
{
if (childEntry.Length == 0)
Expand Down
8 changes: 8 additions & 0 deletions src/StarBreaker.P4k/P4kFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ public void Extract(string outputDir, string? filter = null, IProgress<double>?
var processedEntries = 0;

progress?.Report(0);

//TODO: Preprocessing step:
// 1. start with the list of total files
// 2. run the following according to the filter:
// 3. find one-shot single file procesors
// 4. find file -> multiple file processors
// 5. find multiple file -> single file unsplit processors - remove from the list so we don't double process
// run it!

Parallel.ForEach(filteredEntries, entry =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/StarBreaker.Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//ExtractChunkFiles.Run();
//ExtractSocPak.Run();
//await GrpcClient.RunAsync();
//TimeP4kExtract.Run();
TimeP4kExtract.Run();
//TimeZipNode.Run();

StringCrc32c.Run();
//StringCrc32c.Run();

0 comments on commit 067afed

Please sign in to comment.