remove unused mpq lib from filesystem, finish mpq lister and extractor tool

This commit is contained in:
Alex Yatskov 2018-12-15 17:43:13 -08:00
parent 0489ae469e
commit 93af65142b
3 changed files with 113 additions and 39 deletions

View File

@ -1,10 +1,7 @@
package filesystem package filesystem
import ( import (
"bytes"
"io" "io"
"github.com/icza/mpq"
) )
type FileSystem interface { type FileSystem interface {
@ -18,33 +15,16 @@ func New() FileSystem {
} }
type filesystem struct { type filesystem struct {
db *mpq.MPQ
} }
func (fs *filesystem) Mount(root, path string) error { func (fs *filesystem) Mount(root, path string) error {
db, err := mpq.NewFromFile(path)
if err != nil {
return err
}
fs.db = db
return nil return nil
} }
func (fs *filesystem) List() ([]string, error) { func (fs *filesystem) List() ([]string, error) {
data, err := fs.db.FileByName("(listfile)") return nil, nil
if err != nil {
return nil, err
}
return []string{string(data)}, nil
} }
func (fs *filesystem) Open(path string) (io.ReadSeeker, error) { func (fs *filesystem) Open(path string) (io.ReadSeeker, error) {
data, err := fs.db.FileByName(path) return nil, nil
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
} }

View File

@ -29,6 +29,7 @@ import (
type File interface { type File interface {
Read(data []byte) (int, error) Read(data []byte) (int, error)
GetSize() (int, error) GetSize() (int, error)
Close() error
} }
type Archive interface { type Archive interface {
@ -143,6 +144,7 @@ func (a *archive) GetPaths() ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close()
var buff bytes.Buffer var buff bytes.Buffer
if _, err := io.Copy(&buff, f); err != nil { if _, err := io.Copy(&buff, f); err != nil {

View File

@ -2,32 +2,124 @@ package main
import ( import (
"flag" "flag"
"log" "fmt"
"io"
"os" "os"
"path"
"github.com/FooSoft/lazarus/filesystem" "github.com/FooSoft/lazarus/formats/mpq"
"github.com/bmatcuk/doublestar"
) )
func list(mpqPath, filter string) error {
arch, err := mpq.New(mpqPath)
if err != nil {
return err
}
defer arch.Close()
resPaths, err := arch.GetPaths()
if err != nil {
return err
}
for _, resPath := range resPaths {
match, err := doublestar.Match(filter, resPath)
if err != nil {
return err
}
if match {
fmt.Println(resPath)
}
}
return nil
}
func extract(mpqPath, filter, targetDir string) error {
arch, err := mpq.New(mpqPath)
if err != nil {
return err
}
defer arch.Close()
resPaths, err := arch.GetPaths()
if err != nil {
return err
}
for _, resPath := range resPaths {
match, err := doublestar.Match(filter, resPath)
if err != nil {
return err
}
if !match {
continue
}
resFile, err := arch.OpenFile(resPath)
if err != nil {
return err
}
defer resFile.Close()
sysPath := path.Join(targetDir, resPath)
if err := os.MkdirAll(path.Dir(sysPath), 0777); err != nil {
return err
}
sysFile, err := os.Create(sysPath)
if err != nil {
return err
}
defer sysFile.Close()
if _, err := io.Copy(sysFile, resFile); err != nil {
return err
}
}
return nil
}
func main() { func main() {
// var ( var (
// wildcard = flag.String("wildcard", "*.*", "wildcard filter") filter = flag.String("filter", "*.*", "wildcard file filter")
// ) targetDir = flag.String("target", ".", "target directory")
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] command [files]\n", path.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "Parameters:\n\n")
flag.PrintDefaults()
}
flag.Parse() flag.Parse()
if flag.NArg() == 0 { if flag.NArg() < 1 {
flag.PrintDefaults() flag.Usage()
os.Exit(2) os.Exit(2)
} }
fs := filesystem.New() switch flag.Arg(0) {
if err := fs.Mount("", flag.Arg(0)); err != nil { case "list":
log.Fatal(err) for i := 1; i < flag.NArg(); i++ {
if err := list(flag.Arg(i), *filter); err != nil {
fmt.Fprintln(os.Stderr)
os.Exit(1)
} }
paths, err := fs.List()
if err != nil {
log.Fatal(err)
} }
case "extract":
log.Print(paths) for i := 1; i < flag.NArg(); i++ {
if err := extract(flag.Arg(i), *filter, *targetDir); err != nil {
fmt.Fprintln(os.Stderr)
os.Exit(1)
}
}
default:
flag.Usage()
os.Exit(2)
}
} }