Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
slotthhy committed Apr 5, 2021
2 parents 34635ba + 2894ca4 commit be90399
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 30 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
10 changes: 0 additions & 10 deletions src/Lumina/Excel/LazyRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ public T Value
/// </summary>
public ExcelRow RawRow => Value;

/// <summary>
/// Checks whether something has loaded successfully.
/// </summary>
/// <remarks>
/// If something fails to load, this will still be false regardless.
/// </remarks>
[Obsolete( "Use IsValueCreated instead - HasValue is too ambiguous" )]
public bool HasValue => _value != null;


/// <summary>
/// Checks whether something has loaded successfully.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/Lumina/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Lumina.Data;
using Lumina.Data.Structs;
using Lumina.Excel;
Expand Down Expand Up @@ -49,6 +50,11 @@ public class GameData
public FileHandleManager FileHandleManager { get; private set; }

internal ILogger Logger { get; private set; }

/// <summary>
/// Provides access to the current <see cref="GameData"/> object that was invoked on this thread (if any).
/// </summary>
public static ThreadLocal< GameData > CurrentContext { get; private set; }

/// <summary>
/// Constructs a new Lumina object allowing access to game data.
Expand Down Expand Up @@ -151,6 +157,8 @@ public FileResource GetFile( string path )
/// <returns>Returns the requested file if found, null if not</returns>
public T GetFile< T >( string path ) where T : FileResource
{
SetCurrentContext();

var parsed = ParseFilePath( path );
if( parsed == null )
{
Expand All @@ -174,6 +182,8 @@ public T GetFile< T >( string path ) where T : FileResource
/// <exception cref="FileNotFoundException">The path given doesn't point to an existing file</exception>
public T GetFileFromDisk< T >( string path ) where T : FileResource
{
SetCurrentContext();

if( !File.Exists( path ) )
{
throw new FileNotFoundException( "the file at the specified path doesn't exist" );
Expand Down Expand Up @@ -288,5 +298,15 @@ public void ProcessFileHandleQueue()
{
FileHandleManager.ProcessQueue();
}

internal void SetCurrentContext()
{
SetCurrentContext( this );
}

internal static void SetCurrentContext( GameData gameData )
{
CurrentContext = new(() => gameData);
}
}
}

0 comments on commit be90399

Please sign in to comment.