Skip to content

Commit

Permalink
access files via hash
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAdam committed Apr 5, 2021
1 parent 935f7fa commit 0edd570
Showing 1 changed file with 61 additions and 20 deletions.
81 changes: 61 additions & 20 deletions src/Lumina/Data/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -58,7 +58,7 @@ internal Category(
DatFiles[ id ] = new SqPack( fileInfo, _gameData );
}
}

// postprocess indexes into one hashlist
IndexHashTableEntries = new Dictionary< ulong, IndexHashTableEntry >();

Expand All @@ -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;
Expand All @@ -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 );
Expand All @@ -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 );
Expand Down

0 comments on commit 0edd570

Please sign in to comment.