Skip to content

Commit

Permalink
cleanup p4k reading
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Jun 22, 2024
1 parent 621e619 commit 20b52e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion StarBreaker.P4k/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static string ReadStringCustom(this BinaryReader br, int length)
if (br.Read(span) != length)
throw new Exception("Failed to read from stream");

return Encoding.UTF8.GetString(span);
return Encoding.ASCII.GetString(span);
}

public static List<ZipExtraField> ReadExtraFields(this BinaryReader br, ushort length)
Expand Down
8 changes: 1 addition & 7 deletions StarBreaker.P4k/P4kFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class P4kFile
public P4kFile(string filePath)
{
p4kPath = filePath;
using var _stream = new FileStream(p4kPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 16);
using var _stream = new FileStream(p4kPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 1024);
using var reader = new BinaryReader(_stream, Encoding.UTF8, true);

var eocdLocation = reader.Locate(EOCDRecord.Magic);
Expand Down Expand Up @@ -50,8 +50,6 @@ public P4kFile(string filePath)
ulong localHeaderOffset = header.LocalFileHeaderOffset;
ulong diskNumberStart = header.DiskNumberStart;

var beforeExtraField = reader.BaseStream.Position;

if (reader.ReadUInt16() != 1)
throw new Exception();

Expand Down Expand Up @@ -85,10 +83,6 @@ public P4kFile(string filePath)
var extra0x5003Size = reader.ReadUInt16();
reader.BaseStream.Seek(extra0x5003Size - 4, SeekOrigin.Current);

var size = reader.BaseStream.Position - beforeExtraField;
if (size != header.ExtraFieldLength)
throw new Exception("Extra field size mismatch");

var fileComment = reader.ReadStringCustom(header.FileCommentLength);

_entries[i] = new ZipEntry(fileName, fileComment, compressedSize, uncompressedSize, header.CompressionMethod, isCrypted, localHeaderOffset, header.LastModifiedTime, header.LastModifiedDate);
Expand Down
12 changes: 11 additions & 1 deletion StarBreaker.Profile/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
using System.Diagnostics;
using StarBreaker.CryXmlB;
using StarBreaker.P4k;

namespace StarBreaker.Profile;

public static class Program
{
public static void Main(string[] args)
{
const string dest = "D:\\xml-parsed";
const string dest = @"D:\xml-parsed";
const string p4k = @"C:\Program Files\Roberts Space Industries\StarCitizen\LIVE\Data.p4k";
var sw1 = Stopwatch.StartNew();

var p4kf = new P4kFile(p4k);
var elapsed1 = sw1.ElapsedMilliseconds;
Console.WriteLine($"P4kFile ctor: {elapsed1}ms");
return;


var xmlFiles = Directory.GetFiles("D:\\out", "*.xml", SearchOption.AllDirectories);
var sw = Stopwatch.StartNew();
Parallel.ForEach(xmlFiles, file =>
Expand Down

0 comments on commit 20b52e8

Please sign in to comment.