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

Ziiiiiiiiiiiiiiiiiiiiiiiiiiipatch #1386

Merged
merged 2 commits into from
Oct 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class IndexedZiPatchIndex
public readonly int ExpacVersion;

private readonly List<string> sourceFiles = new();
private readonly List<int> sourceFileLastPtr = new();
private readonly List<long> sourceFileLastPtr = new();
private readonly List<IndexedZiPatchTargetFile> targetFiles = new();
private readonly List<IList<Tuple<int, int>>> sourceFilePartsCache = new();

Expand Down Expand Up @@ -52,7 +52,7 @@ public IndexedZiPatchIndex(BinaryReader reader, bool disposeReader = true)
}

public IList<string> Sources => this.sourceFiles.AsReadOnly();
public int GetSourceLastPtr(int index) => this.sourceFileLastPtr[index];
public long GetSourceLastPtr(int index) => this.sourceFileLastPtr[index];
public IList<IndexedZiPatchTargetFile> Targets => this.targetFiles.AsReadOnly();

public IList<IList<Tuple<int, int>>> SourceParts
Expand Down Expand Up @@ -149,7 +149,7 @@ await Task.Run(() =>
cancellationToken.Value.ThrowIfCancellationRequested();

var block = sqpkFile.CompressedData[i];
var dataOffset = (int)sqpkFile.CompressedDataSourceOffsets[i];
var dataOffset = sqpkFile.CompressedDataSourceOffsets[i];
if (block.IsCompressed)
{
file.Update(new IndexedZiPatchPartLocator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -11,17 +12,14 @@ public class AddDirectoryChunk : ZiPatchChunk

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
var dirNameLen = this.Reader.ReadUInt32BE();

DirName = this.Reader.ReadFixedLengthString(dirNameLen);

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}


public AddDirectoryChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public AddDirectoryChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

public override void ApplyChunk(ZiPatchConfig config)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -13,15 +14,12 @@ public class ApplyFreeSpaceChunk : ZiPatchChunk

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
UnknownFieldA = this.Reader.ReadInt64BE();
UnknownFieldB = this.Reader.ReadInt64BE();

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

public ApplyFreeSpaceChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public ApplyFreeSpaceChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -17,12 +18,11 @@ public enum ApplyOptionKind : uint

public bool OptionValue { get; protected set; }

public ApplyOptionChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public ApplyOptionChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
OptionKind = (ApplyOptionKind)this.Reader.ReadUInt32BE();

// Discarded padding, always 0x0000_0004 as far as observed
Expand All @@ -35,8 +35,6 @@ protected override void ReadChunk()
OptionValue = value;
else
OptionValue = false; // defaults to false if OptionKind isn't valid

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

public override void ApplyChunk(ZiPatchConfig config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using Serilog;
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -11,17 +12,14 @@ public class DeleteDirectoryChunk : ZiPatchChunk

public string DirName { get; protected set; }

public DeleteDirectoryChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public DeleteDirectoryChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
var dirNameLen = this.Reader.ReadUInt32BE();

DirName = this.Reader.ReadFixedLengthString(dirNameLen);

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

public override void ApplyChunk(ZiPatchConfig config)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -8,12 +9,10 @@ public class EndOfFileChunk : ZiPatchChunk

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
}

public EndOfFileChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public EndOfFileChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -24,11 +25,11 @@ public class FileHeaderChunk : ZiPatchChunk
public uint SqpkHeaderCommands { get; protected set; }
public uint SqpkFileCommands { get; protected set; }

public FileHeaderChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public FileHeaderChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;
using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);

Version = (byte)(this.Reader.ReadUInt32() >> 16);
PatchType = this.Reader.ReadFixedLengthString(4u);
Expand All @@ -51,7 +52,6 @@ protected override void ReadChunk()

// 0xB8 of unknown data for V3, 0x08 of 0x00 for V2
// ... Probably irrelevant.
this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

public override string ToString()
Expand Down
14 changes: 7 additions & 7 deletions src/XIVLauncher.Common/Patching/ZiPatch/Chunk/SqpkChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using XIVLauncher.Common.Patching.Util;
using XIVLauncher.Common.Patching.ZiPatch.Chunk.SqpkCommand;
using XIVLauncher.Common.Patching.ZiPatch.Util;

namespace XIVLauncher.Common.Patching.ZiPatch.Chunk
{
Expand All @@ -12,8 +13,9 @@ public abstract class SqpkChunk : ZiPatchChunk
public static string Command { get; protected set; }


private static readonly Dictionary<string, Func<ChecksumBinaryReader, int, int, SqpkChunk>> CommandTypes =
new Dictionary<string, Func<ChecksumBinaryReader, int, int, SqpkChunk>> {
private static readonly Dictionary<string, Func<ChecksumBinaryReader, long, long, SqpkChunk>> CommandTypes =
new()
{
{ SqpkAddData.Command, (reader, offset, size) => new SqpkAddData(reader, offset, size) },
{ SqpkDeleteData.Command, (reader, offset, size) => new SqpkDeleteData(reader, offset, size) },
{ SqpkHeader.Command, (reader, offset, size) => new SqpkHeader(reader, offset, size) },
Expand All @@ -24,7 +26,7 @@ public abstract class SqpkChunk : ZiPatchChunk
{ SqpkPatchInfo.Command, (reader, offset, size) => new SqpkPatchInfo(reader, offset, size) }
};

public static ZiPatchChunk GetCommand(ChecksumBinaryReader reader, int offset, int size)
public static ZiPatchChunk GetCommand(ChecksumBinaryReader reader, long offset, long size)
{
try
{
Expand All @@ -50,12 +52,10 @@ public static ZiPatchChunk GetCommand(ChecksumBinaryReader reader, int offset, i

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
}

protected SqpkChunk(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size)
protected SqpkChunk(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size)
{ }

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ class SqpkAddData : SqpkChunk


public SqpackDatFile TargetFile { get; protected set; }
public int BlockOffset { get; protected set; }
public int BlockNumber { get; protected set; }
public int BlockDeleteNumber { get; protected set; }
public long BlockOffset { get; protected set; }
public long BlockNumber { get; protected set; }
public long BlockDeleteNumber { get; protected set; }

public byte[] BlockData { get; protected set; }
public long BlockDataSourceOffset { get; protected set; }


public SqpkAddData(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public SqpkAddData(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
this.Reader.ReadBytes(3); // Alignment

TargetFile = new SqpackDatFile(this.Reader);

BlockOffset = this.Reader.ReadInt32BE() << 7;
BlockNumber = this.Reader.ReadInt32BE() << 7;
BlockDeleteNumber = this.Reader.ReadInt32BE() << 7;
BlockOffset = (long)this.Reader.ReadUInt32BE() << 7;
BlockNumber = (long)this.Reader.ReadUInt32BE() << 7;
BlockDeleteNumber = (long)this.Reader.ReadUInt32BE() << 7;

BlockDataSourceOffset = Offset + this.Reader.BaseStream.Position;
BlockData = this.Reader.ReadBytes((int)BlockNumber);

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
BlockData = this.Reader.ReadBytes(checked((int)BlockNumber));
}

public override void ApplyChunk(ZiPatchConfig config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ class SqpkDeleteData : SqpkChunk


public SqpackDatFile TargetFile { get; protected set; }
public int BlockOffset { get; protected set; }
public int BlockNumber { get; protected set; }
public long BlockOffset { get; protected set; }
public long BlockNumber { get; protected set; }


public SqpkDeleteData(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public SqpkDeleteData(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
this.Reader.ReadBytes(3); // Alignment

TargetFile = new SqpackDatFile(this.Reader);

BlockOffset = this.Reader.ReadInt32BE() << 7;
BlockNumber = this.Reader.ReadInt32BE();
BlockOffset = (long)this.Reader.ReadUInt32BE() << 7;
BlockNumber = this.Reader.ReadUInt32BE();

this.Reader.ReadUInt32(); // Reserved

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

public override void ApplyChunk(ZiPatchConfig config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ class SqpkExpandData : SqpkChunk


public SqpackDatFile TargetFile { get; protected set; }
public int BlockOffset { get; protected set; }
public int BlockNumber { get; protected set; }
public long BlockOffset { get; protected set; }
public long BlockNumber { get; protected set; }


public SqpkExpandData(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public SqpkExpandData(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
this.Reader.ReadBytes(3);

TargetFile = new SqpackDatFile(this.Reader);

BlockOffset = this.Reader.ReadInt32BE() << 7;
BlockNumber = this.Reader.ReadInt32BE();
BlockOffset = (long)this.Reader.ReadUInt32BE() << 7;
BlockNumber = (long)this.Reader.ReadUInt32BE();

this.Reader.ReadUInt32(); // Reserved

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

public override void ApplyChunk(ZiPatchConfig config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ public enum OperationKind : byte

public OperationKind Operation { get; protected set; }
public long FileOffset { get; protected set; }
public ulong FileSize { get; protected set; }
public long FileSize { get; protected set; }
public ushort ExpansionId { get; protected set; }
public SqexFile TargetFile { get; protected set; }

public List<long> CompressedDataSourceOffsets { get; protected set; }
public List<SqpkCompressedBlock> CompressedData { get; protected set; }

public SqpkFile(ChecksumBinaryReader reader, int offset, int size) : base(reader, offset, size) {}
public SqpkFile(ChecksumBinaryReader reader, long offset, long size) : base(reader, offset, size) {}

protected override void ReadChunk()
{
var start = this.Reader.BaseStream.Position;

using var advanceAfter = new AdvanceOnDispose(this.Reader, Size);
Operation = (OperationKind)this.Reader.ReadByte();
this.Reader.ReadBytes(2); // Alignment

FileOffset = this.Reader.ReadInt64BE();
FileSize = this.Reader.ReadUInt64BE();
FileSize = this.Reader.ReadInt64BE();

var pathLen = this.Reader.ReadUInt32BE();

Expand All @@ -53,15 +52,13 @@ protected override void ReadChunk()
CompressedDataSourceOffsets = new();
CompressedData = new List<SqpkCompressedBlock>();

while (Size - this.Reader.BaseStream.Position + start > 0)
while (advanceAfter.NumBytesRemaining > 0)
{
CompressedDataSourceOffsets.Add(Offset + this.Reader.BaseStream.Position);
CompressedData.Add(new SqpkCompressedBlock(this.Reader));
CompressedDataSourceOffsets[CompressedDataSourceOffsets.Count - 1] += CompressedData[CompressedData.Count - 1].HeaderSize;
}
}

this.Reader.ReadBytes(Size - (int)(this.Reader.BaseStream.Position - start));
}

private static bool RemoveAllFilter(string filePath) =>
Expand Down
Loading