mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-20 03:16:26 -05:00
77c358ed67
* d2loader refactor - export sources - asset and source both have a Path method - make asset and source implement fmt.Stringer, which just calls Path method - Loader.AddSource now returns the source and an error * Update loader.go
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package filesystem
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset/types"
|
|
)
|
|
|
|
// static check that Source implements AssetSource
|
|
var _ asset.Source = &Source{}
|
|
|
|
// Source represents an asset source which is a normal directory on the host file system
|
|
type Source struct {
|
|
Root string
|
|
}
|
|
|
|
// Type returns the type of this asset source
|
|
func (s *Source) Type() types.SourceType {
|
|
return types.AssetSourceFileSystem
|
|
}
|
|
|
|
// Open opens a file with the given sub-path within the Root dir of the file system source
|
|
func (s *Source) Open(subPath string) (asset.Asset, error) {
|
|
file, err := os.Open(s.fullPath(subPath))
|
|
|
|
if err == nil {
|
|
a := &Asset{
|
|
assetType: types.Ext2AssetType(filepath.Ext(subPath)),
|
|
source: s,
|
|
path: subPath,
|
|
file: file,
|
|
}
|
|
|
|
return a, nil
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
func (s *Source) fullPath(subPath string) string {
|
|
return filepath.Clean(filepath.Join(s.Root, subPath))
|
|
}
|
|
|
|
// String returns the Root dir of this file system source
|
|
func (s *Source) Path() string {
|
|
return s.Root
|
|
}
|
|
|
|
// String returns the path
|
|
func (s *Source) String() string {
|
|
return s.Path()
|
|
}
|