From 093cef549cc81c6d3eb5d82be573c84cb7e1bd9b Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Thu, 14 May 2020 14:43:13 +0200 Subject: [PATCH] Added extract command (extracts resources to the current working directory). --- cmd/tins2020/extract.go | 37 +++++++++++++++++++++++++++++++++++++ cmd/tins2020/tins2020.go | 21 +++++++++++++++------ resources.go | 4 ++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 cmd/tins2020/extract.go diff --git a/cmd/tins2020/extract.go b/cmd/tins2020/extract.go new file mode 100644 index 0000000..3f049d9 --- /dev/null +++ b/cmd/tins2020/extract.go @@ -0,0 +1,37 @@ +package main + +import ( + "io" + "os" + "path/filepath" + + rice "github.com/GeertJohan/go.rice" +) + +func copyFile(path string, file *rice.File) error { + dir := filepath.Dir(path) + os.MkdirAll(dir, 0777) + dst, err := os.Create(path) + if err != nil { + return err + } + defer dst.Close() + _, err = io.Copy(dst, file) + return err +} + +func copyBoxToDisk(box *rice.Box) error { + return box.Walk("", func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + src, err := box.Open(path) + if err != nil { + return err + } + return copyFile(filepath.Join(box.Name(), path), src) + }) +} diff --git a/cmd/tins2020/tins2020.go b/cmd/tins2020/tins2020.go index 392f4f8..1d5cccd 100644 --- a/cmd/tins2020/tins2020.go +++ b/cmd/tins2020/tins2020.go @@ -1,6 +1,7 @@ package main import ( + "flag" "log" rice "github.com/GeertJohan/go.rice" @@ -26,6 +27,20 @@ func logSDLVersion() { } func run() error { + var extract bool + flag.BoolVar(&extract, "extract-resources", false, "extracts all resources to the current working directory") + flag.Parse() + + ctx, err := tins2020.NewContext(rice.MustFindBox("res")) + if err != nil { + return err + } + defer ctx.Destroy() + + if extract { + return copyBoxToDisk(ctx.Resources.Box()) + } + if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil { return err } @@ -38,12 +53,6 @@ func run() error { } defer ttf.Quit() - ctx, err := tins2020.NewContext(rice.MustFindBox("res")) - if err != nil { - return err - } - defer ctx.Destroy() - if ctx.Settings.Window.Location == nil { ctx.Settings.Window.Location = tins2020.PtPtr(sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED) } diff --git a/resources.go b/resources.go index 54a54a4..fd4f330 100644 --- a/resources.go +++ b/resources.go @@ -13,6 +13,10 @@ type Resources struct { copy vfs.CopyDir } +func (r *Resources) Box() *rice.Box { + return r.box +} + func (r *Resources) Destroy() { r.copy.Destroy() }