Skip to content

Commit

Permalink
noxtools: Use decomp command from noxscript package.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Feb 9, 2024
1 parent d8e570c commit 18d8ab2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 59 deletions.
55 changes: 2 additions & 53 deletions cmd/noxtools/noxscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"go/format"
"go/token"
"io"
"log"
"os"
Expand All @@ -15,6 +13,7 @@ import (
"github.com/spf13/cobra"

"github.com/noxworld-dev/noxscript/ns/asm"
nscmd "github.com/noxworld-dev/noxscript/ns/v3/cmd"
"github.com/noxworld-dev/noxscript/ns/v3/noxast"

"github.com/noxworld-dev/opennox-lib/maps"
Expand Down Expand Up @@ -73,7 +72,7 @@ func init() {
cmdDecompNoOpt := cmdDecomp.Flags().Bool("noopt", false, "do not optimize the code")
cmdDecompPkg := cmdDecomp.Flags().String("pkg", "", "package name for the script")
cmdDecomp.RunE = func(cmd *cobra.Command, args []string) error {
return cmdNSDecomp(cmd, args, &noxast.Config{
return nscmd.Decomp(args, &noxast.Config{
Package: *cmdDecompPkg,
DoNotOptimize: *cmdDecompNoOpt,
DoNotFold: *cmdDecompNoFold,
Expand Down Expand Up @@ -291,53 +290,3 @@ func cmdNSDisasm(cmd *cobra.Command, args []string) error {
}
return last
}

func cmdNSDecomp(cmd *cobra.Command, args []string, c *noxast.Config) error {
if len(args) != 1 && len(args) != 2 {
return errors.New("expected one or two argument")
}
fname := args[0]
if c.Package == "" {
base := filepath.Base(fname)
base = strings.TrimSuffix(base, filepath.Ext(base))
if base != "" && !strings.ContainsAny(base, " ") {
c.Package = strings.ToLower(base)
}
}
var out io.Writer = os.Stdout
if len(args) == 2 {
f, err := os.Create(args[1])
if err != nil {
return err
}
defer f.Close()
out = f
}
f, err := os.Open(fname)
if err != nil {
return err
}
defer f.Close()

var raw []byte
if filepath.Ext(fname) == ".map" {
s, err := maps.ReadScript(f)
if err != nil {
return err
}
raw = s.Data
} else {
raw, err = io.ReadAll(f)
if err != nil {
return err
}
}
_ = f.Close()

scr, err := asm.ReadScript(bytes.NewReader(raw))
if err != nil {
return err
}
astf := noxast.Translate(scr, c)
return format.Node(out, token.NewFileSet(), astf)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/noxworld-dev/noxcrypt v0.0.0-20230831140413-02623e75408e
github.com/noxworld-dev/noxscript/eud/v171 v171.3.1
github.com/noxworld-dev/noxscript/ns v1.0.2
github.com/noxworld-dev/noxscript/ns/v3 v3.4.2
github.com/noxworld-dev/noxscript/ns/v4 v4.14.0
github.com/noxworld-dev/noxscript/ns/v3 v3.4.4
github.com/noxworld-dev/noxscript/ns/v4 v4.18.0
github.com/shoenig/test v1.7.0
github.com/spf13/cobra v1.7.0
github.com/traefik/yaegi v0.15.2-0.20230926222205-da27c4fbc25f
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ github.com/noxworld-dev/noxscript/eud/v171 v171.3.1 h1:KRugWrwLY54qiRucyJn3iqvWr
github.com/noxworld-dev/noxscript/eud/v171 v171.3.1/go.mod h1:Aigh/4MqsXvkPY/2JXXaNZghGK8mScpsip3Kdz6EUxE=
github.com/noxworld-dev/noxscript/ns v1.0.2 h1:PMpKqRi3fEhwAfBM+PZVTopT6o2vL6AmCDQH8zV5QWU=
github.com/noxworld-dev/noxscript/ns v1.0.2/go.mod h1:DobGfGPUycvh7D6hB5mW5gbL1QiRsw7QQiVPJz4wYZ0=
github.com/noxworld-dev/noxscript/ns/v3 v3.4.2 h1:cvwGW7Cnx7nIurippYXiG17ft3brxjOtW3TWIiLsZ2s=
github.com/noxworld-dev/noxscript/ns/v3 v3.4.2/go.mod h1:oouTgZPYQ/MeLLkpQsYwU7n+oIwfXB4DryLU0UhSj98=
github.com/noxworld-dev/noxscript/ns/v4 v4.14.0 h1:43GYbBgVfVjqVyPrOBwxsNYf25ZGNF08aIXEslsu6/U=
github.com/noxworld-dev/noxscript/ns/v4 v4.14.0/go.mod h1:l8sd8BvVo6LjzYjzAr7V/nbBkp0B/UMQRWbVTq0U67A=
github.com/noxworld-dev/noxscript/ns/v3 v3.4.4 h1:xCq/s+bP8FmmdPq2tHeziSkyhgw4UVfRU9f/f3WV3Oc=
github.com/noxworld-dev/noxscript/ns/v3 v3.4.4/go.mod h1:CM2cLjU/ZxYVVvG5WwP21nYM7H4g7bg68PQwZaOXX+E=
github.com/noxworld-dev/noxscript/ns/v4 v4.18.0 h1:C9nmyV/pyVl2JQlHgcM6UV/CvOaDotRQNsdIv4z//fU=
github.com/noxworld-dev/noxscript/ns/v4 v4.18.0/go.mod h1:5EClTEElcEsAKZBAW4lp+23N0s6X+P/832hJT0iSIOA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down

0 comments on commit 18d8ab2

Please sign in to comment.