Bugfix: embedded levelpacks from bindata

This commit is contained in:
Noah 2021-12-30 18:39:11 -08:00
parent 3881457300
commit fa5f303dad
2 changed files with 32 additions and 11 deletions

View File

@ -3,8 +3,11 @@ package levelpack
import ( import (
"archive/zip" "archive/zip"
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"runtime" "runtime"
@ -45,17 +48,37 @@ type Level struct {
// LoadFile reads a .levelpack zip file. // LoadFile reads a .levelpack zip file.
func LoadFile(filename string) (LevelPack, error) { func LoadFile(filename string) (LevelPack, error) {
var (
fh io.ReaderAt
filesize int64
)
// Look in embedded bindata.
if data, err := assets.Asset(filename); err == nil {
filesize = int64(len(data))
fh = bytes.NewReader(data)
}
// Try the filesystem.
if fh == nil {
stat, err := os.Stat(filename) stat, err := os.Stat(filename)
if err != nil { if err != nil {
return LevelPack{}, err return LevelPack{}, err
} }
filesize = stat.Size()
fh, err := os.Open(filename) fh, err = os.Open(filename)
if err != nil { if err != nil {
return LevelPack{}, err return LevelPack{}, err
} }
}
reader, err := zip.NewReader(fh, stat.Size()) // No luck?
if fh == nil {
return LevelPack{}, errors.New("no file found")
}
reader, err := zip.NewReader(fh, filesize)
if err != nil { if err != nil {
return LevelPack{}, err return LevelPack{}, err
} }
@ -84,12 +107,13 @@ func LoadAllAvailable() ([]string, map[string]LevelPack, error) {
// Resolve the filename to a definite path on disk. // Resolve the filename to a definite path on disk.
path, err := filesystem.FindFile(filename) path, err := filesystem.FindFile(filename)
if err != nil { if err != nil {
fmt.Errorf("LoadAllAvailable: FindFile(%s): %s", path, err)
return filenames, nil, err return filenames, nil, err
} }
lp, err := LoadFile(path) lp, err := LoadFile(path)
if err != nil { if err != nil {
return filenames, nil, err return filenames, nil, fmt.Errorf("LoadAllAvailable: LoadFile(%s): %s", path, err)
} }
dictionary[filename] = lp dictionary[filename] = lp

View File

@ -42,9 +42,6 @@ func NewLevelPackWindow(config LevelPack) *ui.Window {
log.Error("Couldn't list levelpack files: %s", err) log.Error("Couldn't list levelpack files: %s", err)
} }
log.Error("lpFiles: %+v", lpFiles)
log.Error("packmap: %+v", packmap)
window := ui.NewWindow(title) window := ui.NewWindow(title)
window.SetButtons(ui.CloseButton) window.SetButtons(ui.CloseButton)
window.Configure(ui.Config{ window.Configure(ui.Config{