From 0edd570e0240a3025531ac1da7e07e4232cd4899 Mon Sep 17 00:00:00 2001 From: Adam <893184+NotAdam@users.noreply.github.com> Date: Mon, 5 Apr 2021 14:07:38 +1000 Subject: [PATCH] access files via hash --- src/Lumina/Data/Category.cs | 81 ++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/src/Lumina/Data/Category.cs b/src/Lumina/Data/Category.cs index 4bb7427d..3327a0aa 100644 --- a/src/Lumina/Data/Category.cs +++ b/src/Lumina/Data/Category.cs @@ -19,7 +19,7 @@ public class Category public PlatformId Platform { get; } public SqPackIndex Index { get; } - + public Dictionary< UInt64, IndexHashTableEntry > IndexHashTableEntries { get; set; } public Dictionary< uint, Index2HashTableEntry > Index2HashTableEntries { get; set; } @@ -58,7 +58,7 @@ internal Category( DatFiles[ id ] = new SqPack( fileInfo, _gameData ); } } - + // postprocess indexes into one hashlist IndexHashTableEntries = new Dictionary< ulong, IndexHashTableEntry >(); @@ -82,25 +82,28 @@ public bool FileExists( uint hash ) return Index2HashTableEntries.ContainsKey( hash ); } - private bool TryGetFileDatOffset( ParsedFilePath path, out byte dataFileId, out long offset ) + public bool TryGetFileDatOffset( ulong hash, out byte dataFileId, out long offset ) { - if( !Index.IsIndex2 ) + if( IndexHashTableEntries.TryGetValue( hash, out var hashTableEntry ) ) { - if( IndexHashTableEntries.TryGetValue( path.IndexHash, out var hashTableEntry ) ) - { - dataFileId = hashTableEntry.DataFileId; - offset = hashTableEntry.Offset; - return true; - } + dataFileId = hashTableEntry.DataFileId; + offset = hashTableEntry.Offset; + return true; } - else + + dataFileId = 0; + offset = 0; + + return false; + } + + public bool TryGetFileDatOffset( uint hash, out byte dataFileId, out long offset ) + { + if( Index2HashTableEntries.TryGetValue( hash, out var hashTableEntry ) ) { - if( Index2HashTableEntries.TryGetValue( path.Index2Hash, out var hashTableEntry2 ) ) - { - dataFileId = hashTableEntry2.DataFileId; - offset = hashTableEntry2.Offset; - return true; - } + dataFileId = hashTableEntry.DataFileId; + offset = hashTableEntry.Offset; + return true; } dataFileId = 0; @@ -109,6 +112,38 @@ private bool TryGetFileDatOffset( ParsedFilePath path, out byte dataFileId, out return false; } + public bool TryGetFileDatOffset( ParsedFilePath path, out byte dataFileId, out long offset ) + { + if( !Index.IsIndex2 ) + { + return TryGetFileDatOffset( path.IndexHash, out dataFileId, out offset ); + } + + return TryGetFileDatOffset( path.Index2Hash, out dataFileId, out offset ); + } + + public T GetFile< T >( ulong hash ) where T : FileResource + { + var status = TryGetFileDatOffset( hash, out var dataFileId, out var offset ); + if( !status ) + { + return null; + } + + return GetFile< T >( dataFileId, offset ); + } + + public T GetFile< T >( uint hash ) where T : FileResource + { + var status = TryGetFileDatOffset( hash, out var dataFileId, out var offset ); + if( !status ) + { + return null; + } + + return GetFile< T >( dataFileId, offset ); + } + public T GetFile< T >( ParsedFilePath path ) where T : FileResource { var status = TryGetFileDatOffset( path, out var dataFileId, out var offset ); @@ -117,15 +152,21 @@ public T GetFile< T >( ParsedFilePath path ) where T : FileResource return null; } - // get dat - var dat = DatFiles[ dataFileId ]; - var file = dat.ReadFile< T >( offset ); + var file = GetFile< T >( dataFileId, offset ); file.FilePath = path; return file; } + public T GetFile< T >( byte dataFileId, long offset ) where T : FileResource + { + var dat = DatFiles[ dataFileId ]; + var file = dat.ReadFile< T >( offset ); + + return file; + } + public SqPackFileInfo? GetFileMetadata( ParsedFilePath path ) { var status = TryGetFileDatOffset( path, out var dataFileId, out var offset );