Skip to content

Commit

Permalink
Add support for using embedded file systems. (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: sailormoon <sailormoon@users.noreply.github.com>
  • Loading branch information
sailormoon and sailormoon authored Dec 13, 2021
1 parent b702af3 commit 8f6d20a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ trigger:

steps:
- name: lint
image: golang:1.13
image: golang:1.16
pull: always
commands:
- go vet -all .
- go get -u github.com/mgechev/revive
- revive -config .revive.toml -exclude=./vendor/... ./...

- name: test
image: golang:1.13
image: golang:1.16
pull: always
commands:
- go test -v .
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/lafriks/go-tiled

go 1.13
go 1.16

require (
github.com/disintegration/imaging v1.6.2
Expand Down
12 changes: 4 additions & 8 deletions tiled.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package tiled
import (
"encoding/xml"
"io"
"net/http"
"io/fs"
"os"
"path/filepath"
)
Expand All @@ -47,12 +47,12 @@ type Loader struct {
// resources it may reference.
//
// A nil FileSystem uses the local file system.
FileSystem http.FileSystem
FileSystem fs.FS
}

// open opens the given file using the Loader's FileSystem, or uses os.Open
// if l or l.FileSystem is nil.
func (l *Loader) open(name string) (http.File, error) {
func (l *Loader) open(name string) (fs.File, error) {
if l == nil || l.FileSystem == nil {
return os.Open(name)
}
Expand Down Expand Up @@ -83,10 +83,6 @@ func (l *Loader) LoadFromFile(fileName string) (*Map, error) {
}
defer f.Close()

dir, err := filepath.Abs(filepath.Dir(fileName))
if err != nil {
return nil, err
}

dir := filepath.Dir(fileName)
return l.LoadFromReader(dir, f)
}
50 changes: 48 additions & 2 deletions tiled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ package tiled

import (
"bytes"
"embed"
"encoding/xml"
"image/color"
"net/http"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -208,7 +209,7 @@ type testFileSystem struct {
AttemptedOpen []string
}

func (t *testFileSystem) Open(filename string) (http.File, error) {
func (t *testFileSystem) Open(filename string) (fs.File, error) {
t.AttemptedOpen = append(t.AttemptedOpen, filename)
if filepath.Base(filename) == "loader.tmx" {
return os.Open(filepath.Join(GetAssetsDirectory(), "loader.tmx"))
Expand All @@ -233,6 +234,51 @@ func TestLoader(t *testing.T) {
assert.Equal(t, []string{mapFile, filepath.Join(GetAssetsDirectory(), "..", "README.md")}, fs.AttemptedOpen)
}

//go:embed assets/**
var assetsFS embed.FS

func TestEmbeddedLoader(t *testing.T) {
loader := &Loader{
FileSystem: assetsFS,
}
tcs := []struct {
name string
load func() (*Map, error)
}{
{
name: "LoadFromReader",
load: func() (*Map, error) {
file, err := assetsFS.Open("assets/test2.tmx")
if err != nil {
return nil, err
}
return loader.LoadFromReader("assets", file)
},
},
{
name: "LoadFromFile",
load: func() (*Map, error) {
return loader.LoadFromFile("assets/test2.tmx")
},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
m, err := tc.load()
assert.NoError(t, err)

assert.Equal(t, m.Version, "1.2")
assert.Equal(t, m.TiledVersion, "1.2.3")

assert.Len(t, m.Tilesets, 1)
tileset := m.Tilesets[0]
assert.Equal(t, tileset.Version, "1.2")
assert.Equal(t, tileset.TiledVersion, "1.2.3")
})
}
}

func TestParseHexColor(t *testing.T) {
type test struct {
name string
Expand Down

0 comments on commit 8f6d20a

Please sign in to comment.