Skip to content

Commit

Permalink
Refactor + modularize entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jchv committed Jun 23, 2024
1 parent ec606e3 commit 4311f21
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 346 deletions.
96 changes: 2 additions & 94 deletions cmd/pang/main.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/google/subcommands"
"github.com/pangbox/pangfiles/crypto/pyxtea"
"github.com/pangbox/pangfiles/pak"
"github.com/pangbox/pangfiles/version"
)

var xteaKeys = []pyxtea.Key{
pyxtea.KeyUS,
pyxtea.KeyJP,
pyxtea.KeyTH,
pyxtea.KeyEU,
pyxtea.KeyID,
pyxtea.KeyKR,
}

var regionToKey = map[string]pyxtea.Key{
"us": pyxtea.KeyUS,
"jp": pyxtea.KeyJP,
"th": pyxtea.KeyTH,
"eu": pyxtea.KeyEU,
"id": pyxtea.KeyID,
"kr": pyxtea.KeyKR,
}

var keyToRegion = map[pyxtea.Key]string{
pyxtea.KeyUS: "us",
pyxtea.KeyJP: "jp",
pyxtea.KeyTH: "th",
pyxtea.KeyEU: "eu",
pyxtea.KeyID: "id",
pyxtea.KeyKR: "kr",
}

func getRegionKey(regionCode string) pyxtea.Key {
key, ok := regionToKey[regionCode]
if !ok {
log.Fatalf("Invalid region %q (valid regions: us, jp, th, eu, id, kr)", regionCode)
}
return key
}

func getKeyRegion(key pyxtea.Key) string {
region, ok := keyToRegion[key]
if !ok {
panic("programming error: unexpected key")
}
return region
}

func getPakKey(region string, patterns []string) pyxtea.Key {
if region == "" {
log.Println("Auto-detecting pak region (use -region to improve startup delay.)")
key := pak.MustDetectRegion(patterns, xteaKeys)
log.Printf("Detected pak region as %s.", strings.ToUpper(getKeyRegion(key)))
return key
}
return getRegionKey(region)
}

type versionCmd struct{}

func (versionCmd) Name() string { return "version" }
func (versionCmd) Synopsis() string { return "Prints version information to stdout." }
func (v versionCmd) Usage() string { return fmt.Sprintf("%s:\n %s\n", v.Name(), v.Synopsis()) }
func (versionCmd) SetFlags(*flag.FlagSet) {}
func (versionCmd) Execute(context.Context, *flag.FlagSet, ...interface{}) subcommands.ExitStatus {
versionStr := "v" + version.Release
if version.GitCommit != "" {
versionStr += "+" + version.GitCommit
}
fmt.Println(versionStr)
return subcommands.ExitSuccess
}
import "github.com/pangbox/pangfiles/internal/app/pang"

func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(&versionCmd{}, "")
subcommands.Register(&cmdPakMount{}, "paks")
subcommands.Register(&cmdPakExtract{}, "paks")
subcommands.Register(&cmdUpdateListServe{}, "updatelists")
subcommands.Register(&cmdUpdateListEncrypt{}, "updatelists")
subcommands.Register(&cmdUpdateListDecrypt{}, "updatelists")

flag.Parse()
ctx := context.Background()
os.Exit(int(subcommands.Execute(ctx)))
pang.Main()
}
13 changes: 0 additions & 13 deletions cmd/pang/shellstub.go

This file was deleted.

168 changes: 0 additions & 168 deletions cmd/pang/updatelist.go

This file was deleted.

29 changes: 29 additions & 0 deletions internal/app/pang/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pang

import (
"context"
"flag"
"os"

"github.com/google/subcommands"
"github.com/pangbox/pangfiles/internal/cli/commands/pak"
"github.com/pangbox/pangfiles/internal/cli/commands/updatelist"
"github.com/pangbox/pangfiles/internal/cli/commands/updatelistsrv"
"github.com/pangbox/pangfiles/internal/cli/commands/version"
)

func Main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.FlagsCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
subcommands.Register(version.Command(), "")
subcommands.Register(pak.ExtractCommand(), "paks")
subcommands.Register(pak.MountCommand(), "paks")
subcommands.Register(updatelist.EncryptCommand(), "updatelists")
subcommands.Register(updatelist.DecryptCommand(), "updatelists")
subcommands.Register(updatelistsrv.ServeCommand(), "updatelists")

flag.Parse()
ctx := context.Background()
os.Exit(int(subcommands.Execute(ctx)))
}
72 changes: 72 additions & 0 deletions internal/cli/commands/pak/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pak

import (
"context"
"flag"
"log"
"os"

"github.com/google/subcommands"
"github.com/pangbox/pangfiles/internal/region"
"github.com/pangbox/pangfiles/pak"
)

type cmdPakExtract struct {
out string
region string
flat bool
}

func (*cmdPakExtract) Name() string { return "pak-extract" }
func (*cmdPakExtract) Synopsis() string { return "extracts a set of pak files" }
func (*cmdPakExtract) Usage() string {
return `pak-extract [-flat] [-region <code>] [-o <output directory>] <pak files>:
Extracts a set of pak files into a directory.
This will treat the set of pak files as a single incremental archive.
`
}

func (p *cmdPakExtract) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.out, "o", "", "destination to extract to")
f.BoolVar(&p.flat, "flat", false, "flatten the hierarchy (not implemented yet)")
f.StringVar(&p.region, "region", "", "region to use (us, jp, th, eu, id, kr)")
}

func (p *cmdPakExtract) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if f.NArg() < 1 {
log.Println("Not enough arguments. Specify a pak or set of paks to extract.")
return subcommands.ExitUsageError
}

if p.out != "" {
if err := os.MkdirAll(p.out, 0o775); err != nil {
log.Printf("Warning: couldn't make output dir: %v", err)
}
}

fs, err := pak.LoadPaks(region.PakKey(p.region, f.Args()), f.Args())
if err != nil {
log.Printf("Loading pak files: %v", err)
return subcommands.ExitFailure
}

if p.flat {
if err = fs.ExtractFlat(p.out); err != nil {
log.Printf("Extracting pak files: %v", err)
return subcommands.ExitFailure
}
} else {
if err = fs.Extract(p.out); err != nil {
log.Printf("Extracting pak files: %v", err)
return subcommands.ExitFailure
}
}

return subcommands.ExitSuccess
}

func ExtractCommand() subcommands.Command {
return &cmdPakExtract{}
}
Loading

0 comments on commit 4311f21

Please sign in to comment.