mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-18 02:16:23 -05:00
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.
This commit is contained in:
parent
0a72ccaf16
commit
8809fb2c22
@ -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
|
||||
}
|
||||
|
10
d2common/d2interface/archived_file_manager.go
Normal file
10
d2common/d2interface/archived_file_manager.go
Normal file
@ -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)
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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)
|
@ -9,7 +9,7 @@ import (
|
||||
|
||||
type assetManager struct {
|
||||
archiveManager d2interface.ArchiveManager
|
||||
fileManager *fileManager
|
||||
archivedFileManager d2interface.ArchivedFileManager
|
||||
paletteManager *paletteManager
|
||||
paletteTransformManager *paletteTransformManager
|
||||
animationManager *animationManager
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user