Replaced go/embed with rice again.

This commit is contained in:
Sander Schobers 2021-08-09 13:44:50 +02:00
parent 9991c5a6f0
commit 4ee8fd52d1
6 changed files with 50 additions and 19 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.vscode/launch.json .vscode/launch.json
scripts/build scripts/build
cmd/tins2021/rice-box.go

View File

@ -19,6 +19,7 @@
* [go-colurful](###go-colurful) * [go-colurful](###go-colurful)
* [resize](###resize) * [resize](###resize)
* [testify](###testify) * [testify](###testify)
* [go.rice](###rice)
## Introduction ## Introduction
@ -387,3 +388,28 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
### rice
Copyright (c) 2013, Geert-Johan Riemer
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -16,10 +16,10 @@ type appContext struct {
} }
func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext { func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext {
newAnimatedTexture(ctx, "star", "resources/images/star.png", func() image.Image { newAnimatedTexture(ctx, "star", "images/star.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames)) return tins2021.AnimatePolygon(tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames))
}) })
newAnimatedTexture(ctx, "heart", "resources/images/heart.png", func() image.Image { newAnimatedTexture(ctx, "heart", "images/heart.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames)) return tins2021.AnimatePolygon(tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames))
}) })
@ -31,13 +31,13 @@ func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext {
tins2021.MonsterTypeChaser: "chasing-monster", tins2021.MonsterTypeChaser: "chasing-monster",
}, },
} }
newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeStraight], "resources/images/monster-straight.png", func() image.Image { newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeStraight], "images/monster-straight.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
}) })
newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeRandom], "resources/images/monster-random.png", func() image.Image { newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeRandom], "images/monster-random.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
}) })
newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeChaser], "resources/images/monster-chaser.png", func() image.Image { newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeChaser], "images/monster-chaser.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
}) })
return app return app

View File

@ -58,6 +58,8 @@ func newCredits(app *appContext, ctx ui.Context) *credits {
" - https://github.com/nfnt/resize", "", " - https://github.com/nfnt/resize", "",
"testify: a testing library for Go", "testify: a testing library for Go",
" - https://github.com/stretchr/testify", "", " - https://github.com/stretchr/testify", "",
"rice: a library for embedding files in Go",
" - https://github.com/GeertJohan/go.rice", "",
"", "",
"# THANKS", "# THANKS",
"", "",

View File

@ -1,11 +1,12 @@
package main package main
import ( import (
"embed"
"io" "io"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
rice "github.com/GeertJohan/go.rice"
) )
func copyFile(path string, file fs.File) error { func copyFile(path string, file fs.File) error {
@ -20,19 +21,19 @@ func copyFile(path string, file fs.File) error {
return err return err
} }
func copyBoxToDisk(resources embed.FS) error { func copyBoxToDisk() error {
return fs.WalkDir(resources, "", func(path string, entry fs.DirEntry, err error) error { box := rice.MustFindBox(`resources`)
return box.Walk("", func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if entry.IsDir() { if info.IsDir() {
return nil return nil
} }
src, err := resources.Open(path) src, err := box.Open(path)
if err != nil { if err != nil {
return err return err
} }
defer src.Close() return copyFile(filepath.Join(box.Name(), path), src)
return copyFile(filepath.Join(`resources`, path), src)
}) })
} }

View File

@ -1,20 +1,20 @@
package main package main
import ( import (
"embed"
"flag" "flag"
"image/color" "image/color"
"log" "log"
rice "github.com/GeertJohan/go.rice"
"opslag.de/schobers/geom" "opslag.de/schobers/geom"
"opslag.de/schobers/tins2021" "opslag.de/schobers/tins2021"
"opslag.de/schobers/zntg" "opslag.de/schobers/zntg"
"opslag.de/schobers/zntg/addons/embedres" "opslag.de/schobers/zntg/addons/riceres"
"opslag.de/schobers/zntg/ui" "opslag.de/schobers/zntg/ui"
) )
//go:embed resources/* //go:generate go get -u github.com/GeertJohan/go.rice/rice
var resources embed.FS //go:generate rice embed
func main() { func main() {
err := run() err := run()
@ -24,8 +24,9 @@ func main() {
} }
func openResources() ui.Resources { func openResources() ui.Resources {
embedded := embedres.New(resources) box := rice.MustFindBox(`resources`)
return ui.NewFallbackResources(ui.NewPathResources(nil, `resources`), embedded) embedded := riceres.New(box)
return ui.NewFallbackResources(ui.NewPathResources(nil, box.Name()), embedded)
} }
func run() error { func run() error {
@ -34,7 +35,7 @@ func run() error {
flag.Parse() flag.Parse()
if extract { if extract {
return copyBoxToDisk(resources) return copyBoxToDisk()
} }
res := openResources() res := openResources()