Skip to content

Commit

Permalink
prevent lumina from changing the endianness of a file twice
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAdam committed Nov 28, 2020
1 parent a334213 commit ee0c5d9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
37 changes: 37 additions & 0 deletions src/Lumina/Data/FileOptionsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace Lumina.Data
{
[AttributeUsage( AttributeTargets.Class )]
public class FileOptionsAttribute : Attribute
{
/// <summary>
/// The current cache behaviour
/// </summary>
public FileCacheBehaviour CacheBehaviour { get; }

public FileOptionsAttribute(FileCacheBehaviour cacheBehaviour)
{
CacheBehaviour = cacheBehaviour;
}

/// <summary>
/// Changes the behaviour for file caching
/// </summary>
public enum FileCacheBehaviour
{
/// <summary>
/// The default lumina option is used from <see cref="LuminaOptions"/>
/// </summary>
None,
/// <summary>
/// The file is always cached
/// </summary>
Always,
/// <summary>
/// The file is never cached
/// </summary>
Never
}
}
}
6 changes: 5 additions & 1 deletion src/Lumina/Data/Files/Excel/ExcelDataFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Lumina.Data.Structs.Excel;
using Lumina.Extensions;

Expand All @@ -18,6 +17,11 @@ public ExcelDataFile()
public ExcelDataHeader Header { get; protected set; }

public Dictionary< uint, ExcelDataOffset > RowData { get; protected set; }

/// <summary>
/// Whether the endianness of the underlying data has been swapped so it doesn't happen twice
/// </summary>
public bool SwappedEndianness { get; internal set; }

public override unsafe void LoadFile()
{
Expand Down
46 changes: 25 additions & 21 deletions src/Lumina/Data/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -72,19 +71,19 @@ public T GetFile< T >( string cat, ParsedFilePath path ) where T : FileResource

public T GetFile< T >( byte cat, ParsedFilePath path ) where T : FileResource
{
if( Categories.TryGetValue( cat, out var categories ) )
if( !Categories.TryGetValue( cat, out var categories ) )
{
foreach( var category in categories )
{
var file = category.GetFile< T >( path );
if( file != null )
return file;
}
return null;
}

foreach( var category in categories )
{
return category.GetFile< T >( path );
}

return null;
}

public SqPackFileInfo? GetFileMetadata( string cat, ParsedFilePath path )
{
if( CategoryNameToIdMap.TryGetValue( cat, out var catId ) )
Expand All @@ -97,15 +96,17 @@ public T GetFile< T >( byte cat, ParsedFilePath path ) where T : FileResource

public SqPackFileInfo? GetFileMetadata( byte cat, ParsedFilePath path )
{
if( Categories.TryGetValue( cat, out var categories ) )
if( !Categories.TryGetValue( cat, out var categories ) )
{
return null;
}

foreach( var category in categories )
{
foreach( var category in categories )
var file = category.GetFileMetadata( path );
if( file != null )
{
var file = category.GetFileMetadata( path );
if( file != null )
{
return file;
}
return file;
}
}

Expand All @@ -125,7 +126,10 @@ private void GetExpansionId()
}
catch( FormatException e )
{
Trace.TraceWarning( "failed to parse expansionid, e: {0}", e.Message );
_Lumina.Logger?.Error(
"failed to parse expansionid, value: {Value} e: {ExceptionMessage}",
e.Message
);
}
}

Expand Down Expand Up @@ -174,7 +178,7 @@ private void SetupIndexes()
{
continue;
}

// grab first index from the discovered indexes, this should be index if you have both
// otherwise it _should_ be index2
var file = indexFiles.FirstOrDefault();
Expand All @@ -185,7 +189,7 @@ private void SetupIndexes()

var index = new SqPackIndex( file, _Lumina );

var dat = new Category(
var dat = new Category(
cat.Value,
ExpansionId,
chunk,
Expand Down Expand Up @@ -225,7 +229,7 @@ public static string BuildDatStr( byte cat, int ex, int chunk, Structs.PlatformI
public List< FileInfo > FindIndexes( byte cat, int ex, int chunk )
{
var files = new List< FileInfo >();

foreach( var type in new[] { "index", "index2" } )
{
var index = BuildDatStr( cat, ex, chunk, _Lumina.Options.CurrentPlatform, type );
Expand All @@ -248,7 +252,7 @@ public bool FileExists( string catName, ParsedFilePath path )
{
return false;
}

var categories = Categories[ catId ];

foreach( var cat in categories )
Expand Down
8 changes: 7 additions & 1 deletion src/Lumina/Excel/ExcelModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ExcelModule( Lumina lumina )
/// </remarks>
/// <param name="name">A sheet name</param>
/// <returns>An absolute path to an excel header file</returns>
public string BuildExcelHeaderPath( string name )
public static string BuildExcelHeaderPath( string name )
{
return $"exd/{name}.exh";
}
Expand Down Expand Up @@ -155,6 +155,12 @@ private ExcelSheet< T > CreateNewSheet< T >(
Tuple< Language, ulong > noLangKey
) where T : class, IExcelRow
{
_lumina.Logger?.Debug(
"sheet {SheetName} not in cache - creating new sheet for language {Language}",
name,
language
);

var path = BuildExcelHeaderPath( name );
var headerFile = _lumina.GetFile< ExcelHeaderFile >( path );

Expand Down
9 changes: 9 additions & 0 deletions src/Lumina/Excel/ExcelSheetImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,20 @@ protected void ProcessDataRow( long offset, MemoryStream ms, BinaryWriter bw, Bi
/// Reverses the endianness of a data file on LE machines so the underlying stream can be copied from as is
/// </summary>
/// <param name="file">The file to swap endianness for</param>
// todo: refactor and move into ExceLDataFile
protected void ProcessDataEndianness( ExcelDataFile file )
{
if( !BitConverter.IsLittleEndian )
{
return;
}

if( file.SwappedEndianness )
{
return;
}


var stream = new MemoryStream( file.Data );
var writer = new BinaryWriter( stream );
var reader = new BinaryReader( stream );
Expand Down Expand Up @@ -250,6 +257,8 @@ protected void ProcessDataEndianness( ExcelDataFile file )
ProcessDataRow( offset + 6, stream, writer, reader );
}
}

file.SwappedEndianness = true;
}

/// <summary>
Expand Down

0 comments on commit ee0c5d9

Please sign in to comment.