2018-12-15 02:13:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2018-12-16 01:43:13 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-12-15 02:13:19 +00:00
|
|
|
"os"
|
2018-12-16 01:43:13 +00:00
|
|
|
"path"
|
2018-12-15 02:13:19 +00:00
|
|
|
|
2018-12-16 01:43:13 +00:00
|
|
|
"github.com/FooSoft/lazarus/formats/mpq"
|
|
|
|
"github.com/bmatcuk/doublestar"
|
2018-12-15 02:13:19 +00:00
|
|
|
)
|
|
|
|
|
2018-12-16 01:43:13 +00:00
|
|
|
func list(mpqPath, filter string) error {
|
2018-12-16 04:48:20 +00:00
|
|
|
arch, err := mpq.NewFromFile(mpqPath)
|
2018-12-16 01:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer arch.Close()
|
2018-12-15 02:13:19 +00:00
|
|
|
|
2018-12-16 04:48:20 +00:00
|
|
|
for _, resPath := range arch.GetPaths() {
|
2018-12-16 01:43:13 +00:00
|
|
|
match, err := doublestar.Match(filter, resPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if match {
|
|
|
|
fmt.Println(resPath)
|
|
|
|
}
|
2018-12-15 02:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 01:43:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-16 04:48:20 +00:00
|
|
|
func extract(mpqPath, filter, targetDir string) error {
|
|
|
|
arch, err := mpq.NewFromFile(mpqPath)
|
2018-12-16 01:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer arch.Close()
|
|
|
|
|
2018-12-16 04:48:20 +00:00
|
|
|
for _, resPath := range arch.GetPaths() {
|
2018-12-16 01:43:13 +00:00
|
|
|
match, err := doublestar.Match(filter, resPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-16 01:57:37 +00:00
|
|
|
fmt.Println(resPath)
|
|
|
|
|
2018-12-16 01:43:13 +00:00
|
|
|
resFile, err := arch.OpenFile(resPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resFile.Close()
|
|
|
|
|
2018-12-16 04:48:20 +00:00
|
|
|
sysPath := path.Join(targetDir, resPath)
|
2018-12-16 01:43:13 +00:00
|
|
|
if err := os.MkdirAll(path.Dir(sysPath), 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sysFile, err := os.Create(sysPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(sysFile, resFile); err != nil {
|
2018-12-16 02:00:49 +00:00
|
|
|
sysFile.Close()
|
2018-12-16 01:43:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-12-16 02:00:49 +00:00
|
|
|
|
|
|
|
sysFile.Close()
|
2018-12-15 02:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 01:43:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
2018-12-16 01:56:42 +00:00
|
|
|
filter = flag.String("filter", "**", "wildcard file filter")
|
2018-12-16 01:43:13 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
if flag.NArg() < 1 {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch flag.Arg(0) {
|
|
|
|
case "list":
|
|
|
|
for i := 1; i < flag.NArg(); i++ {
|
|
|
|
if err := list(flag.Arg(i), *filter); err != nil {
|
2018-12-16 01:56:42 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-12-16 01:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "extract":
|
|
|
|
for i := 1; i < flag.NArg(); i++ {
|
2018-12-16 04:48:20 +00:00
|
|
|
if err := extract(flag.Arg(i), *filter, *targetDir); err != nil {
|
2018-12-16 01:56:42 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-12-16 01:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2018-12-15 02:13:19 +00:00
|
|
|
}
|