Skip to content

Commit

Permalink
rename archive entries to reflect the official name (WolvenKit#305)
Browse files Browse the repository at this point in the history
* rename archive entries to reflect the official name

* cr2w writing and more verbose unit test
  • Loading branch information
rfuzzo authored Jan 26, 2021
1 parent 6bc4d1f commit 5e8530f
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 187 deletions.
35 changes: 20 additions & 15 deletions CP77.CR2W/Archive/Archive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using WolvenKit.Common.Extensions;
using CP77.CR2W.Types;
using WolvenKit.Common.Oodle;
using Index = CP77Tools.Model.Index;

namespace CP77.CR2W.Archive
{
Expand All @@ -27,8 +28,8 @@ public class Archive : IGameArchive

public Archive()
{
Header = new ArHeader();
Table = new ArTable();
Header = new Header();
Index = new Index();
}

/// <summary>
Expand All @@ -47,12 +48,16 @@ public Archive(string path)
#region properties
public EArchiveType TypeName => EArchiveType.Archive;

public ArHeader Header { get; set; }
public ArTable Table { get; set; }
public Header Header { get; set; }


public Index Index { get; set; }


public string ArchiveAbsolutePath { get; set; }

[JsonIgnore]
public Dictionary<ulong, ArchiveItem> Files => Table?.FileInfo;
public Dictionary<ulong, FileEntry> Files => Index?.FileEntries;

public int FileCount => Files?.Count ?? 0;

Expand All @@ -77,13 +82,13 @@ private void ReadTables()
// using (var vs = mmf.CreateViewStream((long)_header.Tableoffset, (long)_header.Tablesize,
// MemoryMappedFileAccess.Read))
// {
// _table = new ArTable(new BinaryReader(vs), this);
// _table = new Index(new BinaryReader(vs), this);
// }

using var vs = new FileStream(ArchiveAbsolutePath, FileMode.Open, FileAccess.Read);
Header = new ArHeader(new BinaryReader(vs));
vs.Seek((long) Header.Tableoffset, SeekOrigin.Begin);
Table = new ArTable(new BinaryReader(vs), this);
Header = new Header(new BinaryReader(vs));
vs.Seek((long) Header.IndexPosition, SeekOrigin.Begin);
Index = new Index(new BinaryReader(vs), this);
vs.Close();
}

Expand All @@ -105,7 +110,7 @@ public bool CanUncook(ulong hash)
return false;
var archiveItem = Files[hash];
string name = archiveItem.FileName;
var hasBuffers = (archiveItem.LastOffsetTableIdx - archiveItem.FirstOffsetTableIdx) > 1;
var hasBuffers = (archiveItem.SegmentsEnd - archiveItem.SegmentsStart) > 1;

var values = Enum.GetNames(typeof(ECookedFileFormat));
var b = values.Any(e => e == Path.GetExtension(name)[1..]) || hasBuffers ;
Expand All @@ -125,25 +130,25 @@ public bool CanUncook(ulong hash)
if (!Files.ContainsKey(hash)) return (null, null);

var entry = Files[hash];
var startindex = (int)entry.FirstOffsetTableIdx;
var nextindex = (int)entry.LastOffsetTableIdx;
var startindex = (int)entry.SegmentsStart;
var nextindex = (int)entry.SegmentsEnd;

// decompress main file
var file = ExtractFile(this.Table.Offsets[startindex], true);
var file = ExtractFile(this.Index.FileSegments[startindex], true);

// don't decompress buffers
var buffers = new List<byte[]>();
for (int j = startindex + 1; j < nextindex; j++)
{
var offsetentry = this.Table.Offsets[j];
var offsetentry = this.Index.FileSegments[j];
var buffer = ExtractFile(offsetentry, decompressBuffers);
buffers.Add(buffer);
}

return (file, buffers);

// local
byte[] ExtractFile(OffsetEntry offsetentry, bool decompress)
byte[] ExtractFile(FileSegment offsetentry, bool decompress)
{
using var ms = new MemoryStream();
using var bw = new BinaryWriter(ms);
Expand Down
10 changes: 5 additions & 5 deletions CP77.CR2W/Archive/ArchiveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ArchiveManager(DirectoryInfo indir)
_parentDirectoryInfo = indir;

Archives = new Dictionary<string, Archive>();
Files = new Dictionary<ulong, List<ArchiveItem>>();
Files = new Dictionary<ulong, List<FileEntry>>();

// load files
Reload(indir);
Expand All @@ -38,8 +38,8 @@ public ArchiveManager(DirectoryInfo indir)

#region properties
public Dictionary<string, Archive> Archives { get; set; }
public Dictionary<ulong, List<ArchiveItem>> Files { get; }
public Dictionary<string, List<ArchiveItem>> GroupedFiles =>
public Dictionary<ulong, List<FileEntry>> Files { get; }
public Dictionary<string, List<FileEntry>> GroupedFiles =>

Files.Values.GroupBy(
f => f.FirstOrDefault().Extension,
Expand Down Expand Up @@ -90,7 +90,7 @@ private void ReloadFiles()
{
// add file
if (!Files.ContainsKey(hash))
Files.Add(hash, new List<ArchiveItem>());
Files.Add(hash, new List<FileEntry>());
Files[hash].Add(value);

// add extension
Expand Down Expand Up @@ -136,7 +136,7 @@ public override void LoadArchive(string filename, bool ispatch = false)
if (Archives.ContainsKey(filename))
return;
var bundle = new Archive(filename);
foreach (KeyValuePair<ulong, ArchiveItem> item in bundle.Files)
foreach (KeyValuePair<ulong, FileEntry> item in bundle.Files)
{
// add new key if the file isn't already in another bundle
if (!Items.ContainsKey(item.Key.ToString()))
Expand Down
56 changes: 28 additions & 28 deletions CP77.CR2W/Archive/ArchiveItem.cs → CP77.CR2W/Archive/FileEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

namespace CP77.CR2W.Archive
{
public class ArchiveItem : IGameFile
public class FileEntry : IGameFile
{
public ulong NameHash64 { get; set; }
public DateTime DateTime { get; set; }
public uint FileFlags { get; set; }
public uint FirstOffsetTableIdx { get; set; }
public uint LastOffsetTableIdx { get; set; }
public uint FirstImportTableIdx { get; set; }
public uint LastImportTableIdx { get; set; }
public DateTime Timestamp { get; set; }
public uint NumInlineBufferSegments { get; set; }
public uint SegmentsStart { get; set; }
public uint SegmentsEnd { get; set; }
public uint ResourceDependenciesStart { get; set; }
public uint ResourceDependenciesEnd { get; set; }
public byte[] SHA1Hash { get; set; }

public string bytesAsString => BitConverter.ToString(SHA1Hash);
Expand All @@ -35,24 +35,24 @@ public class ArchiveItem : IGameFile

public string CompressionType => throw new NotImplementedException();

public ArchiveItem(BinaryReader br, IGameArchive parent)
public FileEntry(BinaryReader br, IGameArchive parent)
{
Archive = parent;
var mainController = ServiceLocator.Default.ResolveType<IHashService>();

Read(br, mainController);
}

public ArchiveItem(ulong hash, DateTime datetime, uint flags
, uint firstOffsetTableIdx, uint lastOffsetTableIdx, uint firstImportTableIdx, uint lastImportTableIdx, byte[] sha1hash)
public FileEntry(ulong hash, DateTime datetime, uint flags
, uint segmentsStart, uint segmentsEnd, uint resourceDependenciesStart, uint resourceDependenciesEnd, byte[] sha1hash)
{
NameHash64 = hash;
DateTime = datetime;
FileFlags = flags;
FirstImportTableIdx = firstImportTableIdx;
LastImportTableIdx = lastImportTableIdx;
FirstOffsetTableIdx = firstOffsetTableIdx;
LastOffsetTableIdx = lastOffsetTableIdx;
Timestamp = datetime;
NumInlineBufferSegments = flags;
ResourceDependenciesStart = resourceDependenciesStart;
ResourceDependenciesEnd = resourceDependenciesEnd;
SegmentsStart = segmentsStart;
SegmentsEnd = segmentsEnd;
SHA1Hash = sha1hash;
}

Expand All @@ -71,27 +71,27 @@ private void Read(BinaryReader br, IHashService mainController)
_nameStr = _nameStr.Replace('\\', Path.DirectorySeparatorChar);
}

DateTime = DateTime.FromFileTime(br.ReadInt64());
Timestamp = DateTime.FromFileTime(br.ReadInt64());


FileFlags = br.ReadUInt32();
FirstOffsetTableIdx = br.ReadUInt32();
LastOffsetTableIdx = br.ReadUInt32();
FirstImportTableIdx = br.ReadUInt32();
LastImportTableIdx = br.ReadUInt32();
NumInlineBufferSegments = br.ReadUInt32();
SegmentsStart = br.ReadUInt32();
SegmentsEnd = br.ReadUInt32();
ResourceDependenciesStart = br.ReadUInt32();
ResourceDependenciesEnd = br.ReadUInt32();

SHA1Hash = br.ReadBytes(20);
}

public void Write(BinaryWriter bw)
{
bw.Write(NameHash64);
bw.Write(DateTime.ToFileTime());
bw.Write(FileFlags);
bw.Write(FirstOffsetTableIdx);
bw.Write(LastOffsetTableIdx);
bw.Write(FirstImportTableIdx);
bw.Write(LastImportTableIdx);
bw.Write(Timestamp.ToFileTime());
bw.Write(NumInlineBufferSegments);
bw.Write(SegmentsStart);
bw.Write(SegmentsEnd);
bw.Write(ResourceDependenciesStart);
bw.Write(ResourceDependenciesEnd);
bw.Write(SHA1Hash);
}

Expand Down
38 changes: 17 additions & 21 deletions CP77.CR2W/Archive/ArchiveHeader.cs → CP77.CR2W/Archive/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,29 @@ namespace CP77Tools.Model
/// <summary>
///
/// </summary>
public class ArHeader
public class Header
{
public const uint MAGIC = 1380009042;
public const int SIZE = 40;


public uint Magic { get; private set; }
public uint Version { get; private set; }
public ulong Tableoffset { get; set; }
public uint Tablesize { get; set; }
public uint Unk1 { get; private set; }
public uint Unk2 { get; private set; }
public uint Unk3 { get; private set; }
public ulong IndexPosition { get; set; }
public uint IndexSize { get; set; }
public ulong DebugPosition { get; private set; }
public uint DebugSize { get; private set; }
public ulong Filesize { get; set; }

public ArHeader()
public Header()
{
Magic = MAGIC;
Version = 12;
Unk1 = 0;
Unk2 = 0;
Unk3 = 0;
DebugPosition = 0;
DebugSize = 0;
}

public ArHeader(BinaryReader br)
public Header(BinaryReader br)
{
Read(br);
}
Expand All @@ -51,23 +49,21 @@ private void Read(BinaryReader br)
throw new NotImplementedException();

Version = br.ReadUInt32();
Tableoffset = br.ReadUInt64();
Tablesize = br.ReadUInt32();
Unk1 = br.ReadUInt32();
Unk2 = br.ReadUInt32();
Unk3 = br.ReadUInt32();
IndexPosition = br.ReadUInt64();
IndexSize = br.ReadUInt32();
DebugPosition = br.ReadUInt64();
DebugSize = br.ReadUInt32();
Filesize = br.ReadUInt64();
}

public void Write(BinaryWriter bw)
{
bw.Write(MAGIC);
bw.Write(Version);
bw.Write(Tableoffset);
bw.Write(Tablesize);
bw.Write(Unk1);
bw.Write(Unk2);
bw.Write(Unk3);
bw.Write(IndexPosition);
bw.Write(IndexSize);
bw.Write(DebugPosition);
bw.Write(DebugSize);
bw.Write(Filesize);
}
}
Expand Down
Loading

0 comments on commit 5e8530f

Please sign in to comment.