From 8809fb2c22203955e1e0c9799a10af16962cf8e5 Mon Sep 17 00:00:00 2001 From: dk Date: Sat, 4 Jul 2020 21:00:00 -0700 Subject: [PATCH] Made abstractiong for the file manager (#542) I realized that the file manager is actually managing file access to the archives, but it's doing so through the archive manager. I've renamed it, it's now called ArchivedFileManager because that is really what it is. --- d2common/d2interface/archive_manager.go | 4 +--- d2common/d2interface/archived_file_manager.go | 10 ++++++++++ d2common/d2interface/cache.go | 6 ++++++ d2core/d2asset/archive_manager.go | 5 ----- ...le_manager.go => archived_file_manager.go} | 19 +++++++++++++++---- d2core/d2asset/asset_manager.go | 2 +- d2core/d2asset/d2asset.go | 18 +++++++++--------- 7 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 d2common/d2interface/archived_file_manager.go rename d2core/d2asset/{file_manager.go => archived_file_manager.go} (76%) diff --git a/d2common/d2interface/archive_manager.go b/d2common/d2interface/archive_manager.go index ece980cd..95b64edb 100644 --- a/d2common/d2interface/archive_manager.go +++ b/d2common/d2interface/archive_manager.go @@ -2,11 +2,9 @@ package d2interface // ArchiveManager manages loading files from archives type ArchiveManager interface { + Cacher LoadArchiveForFile(filePath string) (Archive, error) FileExistsInArchive(filePath string) (bool, error) LoadArchive(archivePath string) (Archive, error) CacheArchiveEntries() error - SetVerbose(verbose bool) - ClearCache() - GetCache() Cache } diff --git a/d2common/d2interface/archived_file_manager.go b/d2common/d2interface/archived_file_manager.go new file mode 100644 index 00000000..4c430779 --- /dev/null +++ b/d2common/d2interface/archived_file_manager.go @@ -0,0 +1,10 @@ +package d2interface + +// ArchivedFileManager manages file access to the archives being managed +// by the ArchiveManager +type ArchivedFileManager interface { + Cacher + LoadFileStream(filePath string) (ArchiveDataStream, error) + LoadFile(filePath string) ([]byte, error) + FileExists(filePath string) (bool, error) +} diff --git a/d2common/d2interface/cache.go b/d2common/d2interface/cache.go index e480f41f..b2a16a16 100644 --- a/d2common/d2interface/cache.go +++ b/d2common/d2interface/cache.go @@ -9,3 +9,9 @@ type Cache interface { Retrieve(key string) (interface{}, bool) Clear() } + +// Cacher is something that has a cache +type Cacher interface { + ClearCache() + GetCache() Cache +} diff --git a/d2core/d2asset/archive_manager.go b/d2core/d2asset/archive_manager.go index f74feb79..78631e70 100644 --- a/d2core/d2asset/archive_manager.go +++ b/d2core/d2asset/archive_manager.go @@ -108,11 +108,6 @@ func (am *archiveManager) CacheArchiveEntries() error { return nil } -// SetVerbose enables/disables verbose printing for the archive manager -func (am *archiveManager) SetVerbose(verbose bool) { - am.cache.SetVerbose(verbose) -} - // ClearCache clears the archive manager cache func (am *archiveManager) ClearCache() { am.cache.Clear() diff --git a/d2core/d2asset/file_manager.go b/d2core/d2asset/archived_file_manager.go similarity index 76% rename from d2core/d2asset/file_manager.go rename to d2core/d2asset/archived_file_manager.go index 87a986ca..9b3593c5 100644 --- a/d2core/d2asset/file_manager.go +++ b/d2core/d2asset/archived_file_manager.go @@ -19,7 +19,7 @@ type fileManager struct { } func createFileManager(config d2interface.Configuration, - archiveManager d2interface.ArchiveManager) *fileManager { + archiveManager d2interface.ArchiveManager) d2interface.ArchivedFileManager { return &fileManager{ d2common.CreateCache(fileBudget), archiveManager, @@ -27,7 +27,8 @@ func createFileManager(config d2interface.Configuration, } } -func (fm *fileManager) loadFileStream(filePath string) (d2interface.ArchiveDataStream, error) { +// LoadFileStream loads a file as a stream automatically from an archive +func (fm *fileManager) LoadFileStream(filePath string) (d2interface.ArchiveDataStream, error) { filePath = fm.fixupFilePath(filePath) archive, err := fm.archiveManager.LoadArchiveForFile(filePath) @@ -38,7 +39,8 @@ func (fm *fileManager) loadFileStream(filePath string) (d2interface.ArchiveDataS return archive.ReadFileStream(filePath) } -func (fm *fileManager) loadFile(filePath string) ([]byte, error) { +// LoadFile loads a file automatically from a managed archive +func (fm *fileManager) LoadFile(filePath string) ([]byte, error) { filePath = fm.fixupFilePath(filePath) if value, found := fm.cache.Retrieve(filePath); found { return value.([]byte), nil @@ -61,11 +63,20 @@ func (fm *fileManager) loadFile(filePath string) ([]byte, error) { return data, nil } -func (fm *fileManager) fileExists(filePath string) (bool, error) { +// FileExists checks if a file exists in an archive +func (fm *fileManager) FileExists(filePath string) (bool, error) { filePath = fm.fixupFilePath(filePath) return fm.archiveManager.FileExistsInArchive(filePath) } +func (fm *fileManager) ClearCache() { + fm.cache.Clear() +} + +func (fm *fileManager) GetCache() d2interface.Cache { + return fm.cache +} + func (fm *fileManager) fixupFilePath(filePath string) string { filePath = fm.removeLocaleTokens(filePath) filePath = strings.ToLower(filePath) diff --git a/d2core/d2asset/asset_manager.go b/d2core/d2asset/asset_manager.go index d4640b18..b846248a 100644 --- a/d2core/d2asset/asset_manager.go +++ b/d2core/d2asset/asset_manager.go @@ -9,7 +9,7 @@ import ( type assetManager struct { archiveManager d2interface.ArchiveManager - fileManager *fileManager + archivedFileManager d2interface.ArchivedFileManager paletteManager *paletteManager paletteTransformManager *paletteTransformManager animationManager *animationManager diff --git a/d2core/d2asset/d2asset.go b/d2core/d2asset/d2asset.go index c5ac00a3..f0fb12c8 100644 --- a/d2core/d2asset/d2asset.go +++ b/d2core/d2asset/d2asset.go @@ -17,7 +17,7 @@ func Initialize(renderer d2interface.Renderer, var ( config = d2config.Get() archiveManager = createArchiveManager(config) - fileManager = createFileManager(config, archiveManager) + archivedFileManager = createFileManager(config, archiveManager) paletteManager = createPaletteManager() paletteTransformManager = createPaletteTransformManager() animationManager = createAnimationManager(renderer) @@ -26,7 +26,7 @@ func Initialize(renderer d2interface.Renderer, singleton = &assetManager{ archiveManager, - fileManager, + archivedFileManager, paletteManager, paletteTransformManager, animationManager, @@ -40,8 +40,8 @@ func Initialize(renderer d2interface.Renderer, term.OutputInfof("asset manager verbose logging disabled") } - archiveManager.SetVerbose(verbose) - fileManager.cache.SetVerbose(verbose) + archiveManager.GetCache().SetVerbose(verbose) + archivedFileManager.GetCache().SetVerbose(verbose) paletteManager.cache.SetVerbose(verbose) paletteTransformManager.cache.SetVerbose(verbose) animationManager.cache.SetVerbose(verbose) @@ -57,7 +57,7 @@ func Initialize(renderer d2interface.Renderer, } term.OutputInfof("archive cache: %f", cacheStatistics(archiveManager.GetCache())) - term.OutputInfof("file cache: %f", cacheStatistics(fileManager.cache)) + term.OutputInfof("file cache: %f", cacheStatistics(archivedFileManager.GetCache())) term.OutputInfof("palette cache: %f", cacheStatistics(paletteManager.cache)) term.OutputInfof("palette transform cache: %f", cacheStatistics(paletteTransformManager.cache)) term.OutputInfof("animation cache: %f", cacheStatistics(animationManager.cache)) @@ -68,7 +68,7 @@ func Initialize(renderer d2interface.Renderer, if err := term.BindAction("assetclear", "clear asset manager cache", func() { archiveManager.ClearCache() - fileManager.cache.Clear() + archivedFileManager.GetCache().Clear() paletteManager.cache.Clear() paletteTransformManager.cache.Clear() animationManager.cache.Clear() @@ -82,7 +82,7 @@ func Initialize(renderer d2interface.Renderer, // LoadFileStream streams an MPQ file from a source file path func LoadFileStream(filePath string) (d2interface.ArchiveDataStream, error) { - data, err := singleton.fileManager.loadFileStream(filePath) + data, err := singleton.archivedFileManager.LoadFileStream(filePath) if err != nil { log.Printf("error loading file stream %s (%v)", filePath, err.Error()) } @@ -92,7 +92,7 @@ func LoadFileStream(filePath string) (d2interface.ArchiveDataStream, error) { // LoadFile loads an entire file from a source file path as a []byte func LoadFile(filePath string) ([]byte, error) { - data, err := singleton.fileManager.loadFile(filePath) + data, err := singleton.archivedFileManager.LoadFile(filePath) if err != nil { log.Printf("error loading file %s (%v)", filePath, err.Error()) } @@ -102,7 +102,7 @@ func LoadFile(filePath string) ([]byte, error) { // FileExists checks if a file exists on the underlying file system at the given file path. func FileExists(filePath string) (bool, error) { - return singleton.fileManager.fileExists(filePath) + return singleton.archivedFileManager.FileExists(filePath) } // LoadAnimation loads an animation by its resource path and its palette path