Skip to content

Commit

Permalink
aot things
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Oct 17, 2024
1 parent 534010b commit 00edd45
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/StarBreaker.Cli/ChfCommands/ChfProcessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace StarBreaker.Cli;

public static class ChfProcessing
{
private static readonly JsonSerializerOptions opts = new() { WriteIndented = true, Converters = { new JsonStringEnumConverter() } };

public static async Task ProcessCharacter(string chf)
{
Expand All @@ -24,7 +23,8 @@ public static async Task ProcessCharacter(string chf)
var json = Path.ChangeExtension(chf, ".json");
var data = await File.ReadAllBytesAsync(bin);
var character = StarCitizenCharacter.FromBytes(data);
var jsonString = JsonSerializer.Serialize(character, opts);
var jsonString = JsonSerializer.Serialize(character, StarBreakerSerializerContext.Default.StarCitizenCharacter);
await File.WriteAllTextAsync(json, jsonString);
}
}
}

12 changes: 7 additions & 5 deletions src/StarBreaker.Cli/ChfCommands/DownloadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ public async ValueTask ExecuteAsync(IConsole console)

while (true)
{
var response = await http.GetFromJsonAsync<SccRoot>(
$"https://www.star-citizen-characters.com/api/heads?page={page++}&orderBy=latest");
var response = await http.GetFromJsonAsync(
$"https://www.star-citizen-characters.com/api/heads?page={page++}&orderBy=latest",
StarBreakerSerializerContext.Default.SccRoot
);
rowsList.AddRange(response!.body!.rows!);
if (response.body.hasNextPage == false)
break;
}

await console.Output.WriteLineAsync("Downloaded metadata");
await console.Output.WriteLineAsync("Downloading all missing characters...");

foreach (var row in rowsList)
{
try
Expand All @@ -69,9 +71,9 @@ public async ValueTask ExecuteAsync(IConsole console)
await console.Output.WriteLineAsync($"Skipping {row.title}, already downloaded");
continue;
}

await console.Output.WriteLineAsync($"Downloading {row.title}...");

await Task.WhenAll(DownloadFileAsync(http, row.dnaUrl, dnaFile), DownloadFileAsync(http, row.previewUrl, imageFile));

await console.Output.WriteLineAsync($"Downloaded {row.title}");
Expand Down
10 changes: 10 additions & 0 deletions src/StarBreaker.Cli/ChfCommands/StarBreakerSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace StarBreaker.Chf;

[JsonSourceGenerationOptions(WriteIndented = true, UseStringEnumConverter = true)]
[JsonSerializable(typeof(DownloadCommand.SccRoot))]
[JsonSerializable(typeof(DownloadCommand.SccBody))]
[JsonSerializable(typeof(DownloadCommand.SccCharacter))]
[JsonSerializable(typeof(StarCitizenCharacter))]
internal partial class StarBreakerSerializerContext : JsonSerializerContext;
23 changes: 23 additions & 0 deletions src/StarBreaker.P4k/FileProcessing/CryXmlProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using StarBreaker.CryXmlB;

namespace StarBreaker.P4k;

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)
return false;

return CryXml.IsCryXmlB(test);
}

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

public sealed class GenericFileProcessor : IFileProcessor
{
public bool CanProcess(string entryName, Stream stream) => true;

public void ProcessEntry(string outputRootFolder, string entryName, Stream entryStream)
{
var entryPath = Path.Combine(outputRootFolder, entryName);

using var writeStream = new FileStream(entryPath, FileMode.Create, FileAccess.Write, FileShare.None);

entryStream.CopyTo(writeStream);
}
}
29 changes: 29 additions & 0 deletions src/StarBreaker.P4k/FileProcessing/ZipFileProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.IO.Compression;

namespace StarBreaker.P4k;

public sealed class ZipFileProcessor : IFileProcessor
{
public bool CanProcess(string entryName, Stream stream)
{
//todo: optimistic list of extensions?
return entryName.EndsWith(".zip", StringComparison.OrdinalIgnoreCase);
}

public void ProcessEntry(string outputRootFolder, string entryName, Stream stream)
{
var entryPath = Path.Combine(outputRootFolder, Path.ChangeExtension(entryName, "unzipped"));

using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
foreach (var childEntry in archive.Entries)
{
if (childEntry.Length == 0)
continue;

using var childStream = childEntry.Open();

var processor = FileProcessors.GetProcessor(childEntry.FullName, childStream);
processor.ProcessEntry(entryPath, childEntry.FullName, childStream);
}
}
}
10 changes: 5 additions & 5 deletions src/StarBreaker.P4k/P4kFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public P4kFile(string filePath)
_entries = new ZipEntry[eocd64.EntriesOnDisk];

reader.BaseStream.Seek((long)eocd64.CentralDirectoryOffset, SeekOrigin.Begin);

for (var i = 0; i < (int)eocd64.TotalEntries; i++)
{
var header = reader.Read<CentralDirectoryFileHeader>();
Expand Down Expand Up @@ -85,20 +85,20 @@ public P4kFile(string filePath)

if (reader2.Read<ushort>() != 0x5000)
throw new Exception("Invalid extra field id");

var extra0x5000Size = reader2.Read<ushort>();
reader2.Advance(extra0x5000Size - 4);

if (reader2.Read<ushort>() != 0x5002)
throw new Exception("Invalid extra field id");
if (reader2.Read<ushort>() != 6)
throw new Exception("Invalid extra field size");

var isCrypted = reader2.Read<ushort>() == 1;

if (reader2.Read<ushort>() != 0x5003)
throw new Exception("Invalid extra field id");

var extra0x5003Size = reader2.Read<ushort>();
reader2.Advance(extra0x5003Size - 4);

Expand Down Expand Up @@ -126,7 +126,7 @@ public P4kFile(string filePath)
public void Extract(string outputDir, string? filter = null, IProgress<double>? progress = null)
{
var filteredEntries = filter is null ? _entries : _entries.Where(entry => FileSystemName.MatchesSimpleExpression(filter, entry.Name, true)).ToArray();

var numberOfEntries = filteredEntries.Length;
var fivePercent = numberOfEntries / 20;
var processedEntries = 0;
Expand Down
1 change: 1 addition & 0 deletions src/StarBreaker.P4k/StarBreaker.P4k.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

<ItemGroup>
<ProjectReference Include="..\StarBreaker.Common\StarBreaker.Common.csproj" />
<ProjectReference Include="..\StarBreaker.CryXmlB\StarBreaker.CryXmlB.csproj" />
</ItemGroup>
</Project>

0 comments on commit 00edd45

Please sign in to comment.