mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-09-22 06:45:11 +00:00
ZArchive support (#75)
* ZArchive support * Fix offset record format * Simplfiy Extensions * Delete unused writers and test data * Rework reader * Fix build
This commit is contained in:
37
SabreTools.Data.Extensions/ZArchiveExtensions.cs
Normal file
37
SabreTools.Data.Extensions/ZArchiveExtensions.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using SabreTools.Data.Models.ZArchive;
|
||||
using SabreTools.Numerics;
|
||||
|
||||
namespace SabreTools.Data.Extensions
|
||||
{
|
||||
public static class ZArchiveExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves the name of the specified node from the NameTable
|
||||
/// </summary>
|
||||
/// <param name="node">Node in the file tree</param>
|
||||
/// <param name="nameTable">ZArchive NameTable</param>
|
||||
/// <returns>UTF-8 string representing node's name</returns>
|
||||
public static string? GetName(this FileDirectoryEntry node, NameTable nameTable)
|
||||
{
|
||||
// Check for a valid offset into the NameTable
|
||||
uint nameOffset = node.NameOffsetAndTypeFlag & Constants.RootNode;
|
||||
if (nameOffset == Constants.RootNode)
|
||||
return null;
|
||||
|
||||
// Get the index into the name table
|
||||
var index = Array.IndexOf(nameTable.NameTableOffsets, nameOffset);
|
||||
if (index < 0)
|
||||
return null;
|
||||
|
||||
// Get the name entry for the requested index
|
||||
var nameEntry = nameTable.NameEntries[index];
|
||||
if (nameEntry is null)
|
||||
return null;
|
||||
|
||||
// Decode name to UTF-8
|
||||
return Encoding.UTF8.GetString(nameEntry.NodeName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user