From ecc1b7f8637ec047e2f417c5af66e553f3ed6270 Mon Sep 17 00:00:00 2001 From: Catherine Gilbert Date: Sun, 5 May 2024 16:11:38 -0400 Subject: [PATCH 1/2] Added option to skip caching when enumerating rows --- src/Lumina/Excel/ExcelSheet.cs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Lumina/Excel/ExcelSheet.cs b/src/Lumina/Excel/ExcelSheet.cs index cb7f11dd..521ceec8 100644 --- a/src/Lumina/Excel/ExcelSheet.cs +++ b/src/Lumina/Excel/ExcelSheet.cs @@ -82,7 +82,7 @@ private T ReadSubRowObj( RowParser parser, uint rowId, uint subRowId ) return obj; } - public IEnumerator< T > GetEnumerator() + public IEnumerator< T > GetEnumerator( bool skipCaching ) { ExcelDataFile file = null!; RowParser parser = null!; @@ -111,7 +111,11 @@ public IEnumerator< T > GetEnumerator() } var obj = ReadSubRowObj( parser, rowPtr.RowId, i ); - _rowCache.TryAdd( cacheKey, obj ); + if( !skipCaching ) + { + _rowCache.TryAdd( cacheKey, obj ); + } + yield return obj; } } @@ -125,15 +129,24 @@ public IEnumerator< T > GetEnumerator() } var obj = ReadRowObj( parser, rowPtr.RowId ); - _rowCache.TryAdd( cacheKey, obj ); + if( !skipCaching ) + { + _rowCache.TryAdd( cacheKey, obj ); + } + yield return obj; } } } - + + public IEnumerator< T > GetEnumerator() + { + return GetEnumerator( false ); + } + IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator(); + return GetEnumerator( false ); } } } \ No newline at end of file From 185e9895e69217730fe2aea62b99ca16b653d8af Mon Sep 17 00:00:00 2001 From: Catherine Gilbert Date: Mon, 6 May 2024 19:05:15 -0400 Subject: [PATCH 2/2] Control caching via the NoCache class --- src/Lumina/Excel/ExcelSheet.cs | 22 ++++++++++------------ src/Lumina/Excel/NoCache.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/Lumina/Excel/NoCache.cs diff --git a/src/Lumina/Excel/ExcelSheet.cs b/src/Lumina/Excel/ExcelSheet.cs index 521ceec8..9d90d9ed 100644 --- a/src/Lumina/Excel/ExcelSheet.cs +++ b/src/Lumina/Excel/ExcelSheet.cs @@ -55,9 +55,12 @@ public ExcelSheet( ExcelHeaderFile headerFile, string name, Language requestedLa { rowObj.PopulateData( parser, GameData, RequestedLanguage ); } - - _rowCache[ cacheKey ] = rowObj; - + + if( !NoCache.IsEnabled ) + { + _rowCache[ cacheKey ] = rowObj; + } + return rowObj; } @@ -82,7 +85,7 @@ private T ReadSubRowObj( RowParser parser, uint rowId, uint subRowId ) return obj; } - public IEnumerator< T > GetEnumerator( bool skipCaching ) + public IEnumerator< T > GetEnumerator() { ExcelDataFile file = null!; RowParser parser = null!; @@ -111,7 +114,7 @@ public IEnumerator< T > GetEnumerator( bool skipCaching ) } var obj = ReadSubRowObj( parser, rowPtr.RowId, i ); - if( !skipCaching ) + if( !NoCache.IsEnabled ) { _rowCache.TryAdd( cacheKey, obj ); } @@ -129,7 +132,7 @@ public IEnumerator< T > GetEnumerator( bool skipCaching ) } var obj = ReadRowObj( parser, rowPtr.RowId ); - if( !skipCaching ) + if( !NoCache.IsEnabled ) { _rowCache.TryAdd( cacheKey, obj ); } @@ -139,14 +142,9 @@ public IEnumerator< T > GetEnumerator( bool skipCaching ) } } - public IEnumerator< T > GetEnumerator() - { - return GetEnumerator( false ); - } - IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator( false ); + return GetEnumerator(); } } } \ No newline at end of file diff --git a/src/Lumina/Excel/NoCache.cs b/src/Lumina/Excel/NoCache.cs new file mode 100644 index 00000000..ad63e6f7 --- /dev/null +++ b/src/Lumina/Excel/NoCache.cs @@ -0,0 +1,29 @@ +using System; + +namespace Lumina.Excel +{ + /// + /// Class that allows to skip the caching of Excel rows when they are read. + /// + public sealed class NoCache : IDisposable + { + [field: ThreadStatic] + internal static bool IsEnabled { get; private set; } + + /// + /// Disables the caching of Excel rows when they are read. + /// + public NoCache() + { + IsEnabled = true; + } + + /// + /// Re-enables the caching of Excel rows when they are read. + /// + public void Dispose() + { + IsEnabled = false; + } + } +} \ No newline at end of file