Skip to content
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
14 changes: 7 additions & 7 deletions samples/VersionDatabaseCreator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@
// It takes a couple of minutes to scan TWD:DE with a stock Ryzen 5 2600.
var areSymbolsHashed = false;
var msv = MetaStreamVersion.Mbin;
ArchiveVersion ttarchVersion = 0;
TTArchiveVersion ttarchVersion = 0;
await Parallel.ForEachAsync(archivePaths, async (filePath, _) =>
{
Console.WriteLine($"Start reading {filePath}.");

try
{
using ArchiveBase archive = Toolkit.Instance.LoadArchive(filePath, blowfishKey, false);
using Archive archive = Toolkit.Instance.LoadArchive(filePath, blowfishKey);
ttarchVersion = archive.Info.Version;

if (archive.FileEntries.Length > 0)
if (archive.Entries.Values.Any())
{
MemoryStream firstFile = archive.ExtractFile(archive.FileEntries[0].Name);
Stream firstFile = archive.OpenResource(archive.Entries[0].Name);

if (Toolkit.IsMetaFile(archive.FileEntries[0].Name))
if (Toolkit.IsMetaFile(archive.Entries[0].Name))
{
MetaStreamConfiguration config = new MetaStreamReader(firstFile).Configuration;

Expand All @@ -77,11 +77,11 @@ await Parallel.ForEachAsync(archivePaths, async (filePath, _) =>
}
}

foreach (TelltaleFileEntry entry in archive.FileEntries)
foreach (ResourceEntry entry in archive.Entries.Values)
{
// Console.WriteLine($"Reading {entry.Name}");

using MemoryStream file = archive.ExtractFile(entry.Name);
using Stream file = archive.OpenResource(entry.Name);

if (!Toolkit.IsMetaFile(file))
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/TelltaleToolKit/GamesDatabase/GameProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public sealed class GameProfile
/// <summary>
/// Gets the version of the TTARCH archive format used by the game.
/// </summary>
/// <seealso cref="ArchiveVersion"/>
public ArchiveVersion TtarchVersion { get; set; }
/// <seealso cref="TTArchiveVersion"/>
public TTArchiveVersion TtarchVersion { get; set; }

/// <summary>
/// Gets the Lua scripting engine version used by the game.
Expand Down
16 changes: 8 additions & 8 deletions src/TelltaleToolKit/Resource/ArchiveProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace TelltaleToolKit.Resource;
/// Wraps a single Telltale archive (.ttarch or .ttarch2) as an <see cref="IFileProvider"/>.
/// </summary>
/// <remarks>
/// The underlying <see cref="ArchiveBase"/> is loaded through the workspace so that the
/// The underlying <see cref="Archive"/> is loaded through the workspace so that the
/// workspace's <c>ArchiveLoaded</c> event fires and blowfish decryption is applied
/// consistently. Dispose this provider when the archive is no longer needed to release
/// the file handle held by the archive.
/// </remarks>
public sealed class ArchiveProvider : IFileProvider, IDisposable
{
private readonly ArchiveBase _archive;
private readonly Archive _archive;

/// <summary>
/// Loads the archive at <paramref name="path"/> using the blowfish key embedded in
Expand Down Expand Up @@ -45,18 +45,18 @@ public ArchiveProvider(string path, Workspace workspace)
public string Path { get; }

/// <summary>
/// Disposes the underlying <see cref="ArchiveBase"/>, releasing any held file handles.
/// Disposes the underlying <see cref="Archive"/>, releasing any held file handles.
/// </summary>
public void Dispose() => _archive.Dispose();

/// <inheritdoc/>
public Stream? ExtractFile(ulong crc64) => _archive.ExtractFile(crc64);
public Stream? ExtractFile(ulong crc64) => _archive.OpenResource(crc64);

/// <inheritdoc/>
public bool ContainsFile(ulong crc64) => _archive.ContainsFile(crc64);
public bool ContainsFile(ulong crc64) => _archive.Entries.ContainsKey(crc64);

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(ulong crc64) => _archive.FindEntry(crc64);
public ResourceEntry? GetFileEntry(ulong crc64) => _archive.FindResource(crc64);

/// <inheritdoc/>
public Stream? ExtractFile(string fileName)
Expand All @@ -67,9 +67,9 @@ public bool ContainsFile(string fileName)
=> ContainsFile(Crc64.Compute(fileName));

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(string fileName)
public ResourceEntry? GetFileEntry(string fileName)
=> GetFileEntry(Crc64.Compute(fileName));

/// <inheritdoc/>
public IEnumerable<TelltaleFileEntry> GetAllEntries() => _archive.FileEntries;
public IEnumerable<ResourceEntry> GetAllEntries() => _archive.Entries.Values;
}
6 changes: 3 additions & 3 deletions src/TelltaleToolKit/Resource/FolderProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public bool ContainsFile(ulong crc64)
=> _providers.Any(p => p.ContainsFile(crc64));

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(ulong crc64)
public ResourceEntry? GetFileEntry(ulong crc64)
{
foreach (var provider in _providers)
{
Expand All @@ -123,11 +123,11 @@ public bool ContainsFile(string fileName)
=> ContainsFile(Crc64.Compute(fileName));

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(string fileName)
public ResourceEntry? GetFileEntry(string fileName)
=> GetFileEntry(Crc64.Compute(fileName));

/// <inheritdoc/>
public IEnumerable<TelltaleFileEntry> GetAllEntries()
public IEnumerable<ResourceEntry> GetAllEntries()
=> _providers.SelectMany(p => p.GetAllEntries());

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/TelltaleToolKit/Resource/IFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public interface IFileProvider
bool ContainsFile(ulong crc64);

/// <summary>
/// Returns the <see cref="TelltaleFileEntry"/> for the file whose CRC-64 matches
/// Returns the <see cref="ResourceEntry"/> for the file whose CRC-64 matches
/// <paramref name="crc64"/>, or <see langword="null"/> if not found.
/// </summary>
/// <param name="crc64">Pre-computed CRC-64 of the filename.</param>
TelltaleFileEntry? GetFileEntry(ulong crc64);
ResourceEntry? GetFileEntry(ulong crc64);

/// <summary>
/// Opens a read-only stream for the file with the given name.
Expand All @@ -62,10 +62,10 @@ public interface IFileProvider
/// <param name="fileName">
/// The bare filename (e.g. <c>obj_clementine.d3dtx</c>), not a full path.
/// </param>
TelltaleFileEntry? GetFileEntry(string fileName);
ResourceEntry? GetFileEntry(string fileName);

/// <summary>
/// Enumerates every <see cref="TelltaleFileEntry"/> accessible through this provider,
/// Enumerates every <see cref="ResourceEntry"/> accessible through this provider,
/// without duplicates.
/// </summary>
/// <remarks>
Expand All @@ -75,5 +75,5 @@ public interface IFileProvider
/// child providers' entries.
/// </remarks>
/// <returns>An enumerable of file entries; never <see langword="null"/>.</returns>
public IEnumerable<TelltaleFileEntry> GetAllEntries();
public IEnumerable<ResourceEntry> GetAllEntries();
}
14 changes: 7 additions & 7 deletions src/TelltaleToolKit/Resource/LooseFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public bool ContainsFile(ulong crc64)
=> crc64 == Crc64;

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(ulong crc64)
public ResourceEntry? GetFileEntry(ulong crc64)
{
if (crc64 != Crc64) return null;

return new TelltaleFileEntry
return new ResourceEntry
{
Crc64 = crc64, Name = Name, FileOffset = 0, FileSize = (int)Size,
NameCrc = crc64, Name = Name, Offset = 0, Size = (uint)Size,
};
}

Expand All @@ -69,15 +69,15 @@ public bool ContainsFile(string fileName)
=> IsMatch(fileName);

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(string fileName)
public ResourceEntry? GetFileEntry(string fileName)
=> IsMatch(fileName) ? GetFileEntry(Crc64) : null;

/// <inheritdoc/>
public IEnumerable<TelltaleFileEntry> GetAllEntries()
public IEnumerable<ResourceEntry> GetAllEntries()
{
yield return new TelltaleFileEntry
yield return new ResourceEntry
{
Crc64 = Crc64, Name = Name, FileOffset = 0, FileSize = (int)Size,
NameCrc = Crc64, Name = Name, Offset = 0, Size = (uint)Size,
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/TelltaleToolKit/Resource/ResourceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public bool ContainsFile(ulong crc64)

/// <inheritdoc/>
/// <remarks>Returns <see langword="null"/> immediately when the context is disabled.</remarks>
public TelltaleFileEntry? GetFileEntry(ulong crc64)
public ResourceEntry? GetFileEntry(ulong crc64)
{
if (!IsEnabled) return null;

foreach (IFileProvider provider in _providers)
{
TelltaleFileEntry? entry = provider.GetFileEntry(crc64);
ResourceEntry? entry = provider.GetFileEntry(crc64);
if (entry != null) return entry;
}

Expand All @@ -144,10 +144,10 @@ public bool ContainsFile(string fileName)
=> ContainsFile(Crc64.Compute(fileName));

/// <inheritdoc/>
public TelltaleFileEntry? GetFileEntry(string fileName)
public ResourceEntry? GetFileEntry(string fileName)
=> GetFileEntry(Crc64.Compute(fileName));

/// <inheritdoc/>
public IEnumerable<TelltaleFileEntry> GetAllEntries()
public IEnumerable<ResourceEntry> GetAllEntries()
=> _providers.SelectMany(p => p.GetAllEntries());
}
15 changes: 9 additions & 6 deletions src/TelltaleToolKit/Serialization/Binary/MetaStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public override void SerializeMetaHeader()
// Read the number of serialized classes
uint metaClassNum = this.ReadUInt32();

if (metaClassNum >= 1000)
throw new InvalidDataException("Too many classes in the meta stream.");

for (var i = 0; i < metaClassNum; i++)
{
uint size = this.ReadUInt32();
Expand Down Expand Up @@ -94,10 +97,10 @@ public override void SerializeMetaHeader()
long currentPosition = Reader.BaseStream.Position;
if (Configuration.Version is MetaStreamVersion.Msv6 or MetaStreamVersion.Msv5)
{
Sections[(int)SectionType.Main].Stream = new Substream(UnderlyingStream, currentPosition, mainSize);
Sections[(int)SectionType.Main].Stream = new SubStream(UnderlyingStream, currentPosition, mainSize);
Sections[(int)SectionType.Debug].Stream =
new Substream(UnderlyingStream, currentPosition + mainSize, debugSize);
Sections[(int)SectionType.Async].Stream = new Substream(
new SubStream(UnderlyingStream, currentPosition + mainSize, debugSize);
Sections[(int)SectionType.Async].Stream = new SubStream(
UnderlyingStream,
currentPosition + mainSize + debugSize,
asyncSize
Expand All @@ -106,13 +109,13 @@ public override void SerializeMetaHeader()
else
{
// For older versions, the entire stream is the main section
Sections[(int)SectionType.Main].Stream = new Substream(
Sections[(int)SectionType.Main].Stream = new SubStream(
UnderlyingStream,
currentPosition,
UnderlyingStream.Length - currentPosition
);
Sections[(int)SectionType.Debug].Stream = new Substream(UnderlyingStream, 0, 0);
Sections[(int)SectionType.Async].Stream = new Substream(UnderlyingStream, 0, 0);
Sections[(int)SectionType.Debug].Stream = new SubStream(UnderlyingStream, 0, 0);
Sections[(int)SectionType.Async].Stream = new SubStream(UnderlyingStream, 0, 0);
}

InitSerializer();
Expand Down
4 changes: 2 additions & 2 deletions src/TelltaleToolKit/Serialization/Binary/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public static class StreamExtensions
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
/// <returns>A substream.</returns>
public static Substream Substream(this Stream stream, long offset, long length) =>
new Substream(stream, offset, length);
public static SubStream Substream(this Stream stream, long offset, long length) =>
new SubStream(stream, offset, length);
}
4 changes: 2 additions & 2 deletions src/TelltaleToolKit/Serialization/Binary/Substream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/// <summary>
/// Represents a substream of an underlying <see cref="Stream" />.
/// </summary>
public class Substream : Stream
public class SubStream : Stream
{
/// <summary>
/// Creates a new substream instance using the specified underlying stream at the specified offset with the specified length.
/// </summary>
/// <param name="stream">The underlying stream.</param>
/// <param name="offset">The offset.</param>
/// <param name="length">The length.</param>
public Substream(Stream stream, long offset, long length)
public SubStream(Stream stream, long offset, long length)
{
if (stream == null)
{
Expand Down
Loading
Loading