bedrocktool/cmd/bedrocktool/main.go

224 lines
5.2 KiB
Go
Raw Normal View History

2022-02-12 22:27:49 +00:00
package main
import (
"bufio"
"context"
2022-02-12 22:27:49 +00:00
"flag"
"fmt"
"io"
2022-02-12 22:27:49 +00:00
"os"
"os/signal"
"runtime/debug"
"syscall"
2023-04-02 12:20:50 +00:00
"time"
2022-02-12 22:27:49 +00:00
"github.com/bedrock-tool/bedrocktool/locale"
2023-05-04 14:00:51 +00:00
"github.com/bedrock-tool/bedrocktool/ui/messages"
2022-09-04 14:53:21 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
2023-02-12 21:22:44 +00:00
"gopkg.in/square/go-jose.v2/json"
2022-09-04 14:53:21 +00:00
_ "github.com/bedrock-tool/bedrocktool/subcommands"
_ "github.com/bedrock-tool/bedrocktool/subcommands/skins"
_ "github.com/bedrock-tool/bedrocktool/subcommands/world"
_ "github.com/bedrock-tool/bedrocktool/ui"
2022-09-04 14:26:32 +00:00
"github.com/google/subcommands"
2022-09-04 00:24:58 +00:00
"github.com/sirupsen/logrus"
2022-02-12 22:27:49 +00:00
)
2023-03-05 21:50:58 +00:00
type CLI struct {
utils.BaseUI
2023-03-05 21:50:58 +00:00
}
func (c *CLI) Init() bool {
utils.SetCurrentUI(c)
utils.Auth.LoginWithMicrosoftCallback = func(r io.Reader) {
io.Copy(os.Stdout, r)
}
return true
2023-03-05 21:50:58 +00:00
}
/*
var m = &worlds.Map{}
func (c *CLI) Message(data interface{}) messages.MessageResponse {
switch me := data.(type) {
case messages.CanShowImages:
return messages.MessageResponse{Ok: true}
case messages.UpdateMap:
m.Update(&me)
}
return messages.MessageResponse{
Ok: false,
Data: nil,
}
}
*/
2023-03-08 11:46:16 +00:00
func (c *CLI) Start(ctx context.Context, cancel context.CancelFunc) error {
2023-03-05 21:50:58 +00:00
flag.Parse()
subcommands.Execute(ctx)
2023-04-02 12:20:50 +00:00
time.Sleep(50 * time.Millisecond)
cancel()
time.Sleep(50 * time.Millisecond)
2023-03-05 21:50:58 +00:00
return nil
}
2022-02-27 10:08:19 +00:00
func main() {
2023-03-30 11:48:03 +00:00
/*
cf, _ := os.Create("cpu.pprof")
err := pprof.StartCPUProfile(cf)
if err != nil {
logrus.Error(err)
}
defer pprof.StopCPUProfile()
*/
2022-09-06 13:24:08 +00:00
defer func() {
if err := recover(); err != nil {
logrus.Errorf(locale.Loc("fatal_error", nil))
2022-09-06 13:24:08 +00:00
println("")
println("--COPY FROM HERE--")
logrus.Infof("Version: %s", utils.Version)
logrus.Infof("Cmdline: %s", os.Args)
logrus.Errorf("Error: %s", err)
println("stacktrace from panic: \n" + string(debug.Stack()))
2022-09-06 13:24:08 +00:00
println("--END COPY HERE--")
println("")
println(locale.Loc("report_issue", nil))
2023-03-05 21:50:58 +00:00
if utils.Options.ExtraDebug {
2023-01-27 12:07:53 +00:00
println(locale.Loc("used_extra_debug_report", nil))
}
2023-03-05 21:50:58 +00:00
if utils.Options.IsInteractive {
2022-09-08 08:39:46 +00:00
input := bufio.NewScanner(os.Stdin)
input.Scan()
}
2022-09-06 13:24:08 +00:00
os.Exit(1)
}
}()
2022-09-04 00:24:58 +00:00
logrus.SetLevel(logrus.DebugLevel)
2022-09-05 21:57:11 +00:00
if utils.Version != "" {
logrus.Infof(locale.Loc("bedrocktool_version", locale.Strmap{"Version": utils.Version}))
2022-09-05 21:57:11 +00:00
}
ctx, cancel := context.WithCancel(context.Background())
2023-02-13 00:32:27 +00:00
flag.StringVar(&utils.RealmsEnv, "realms-env", "", "realms env")
2023-03-05 21:50:58 +00:00
flag.BoolVar(&utils.Options.Debug, "debug", false, locale.Loc("debug_mode", nil))
flag.BoolVar(&utils.Options.Preload, "preload", false, locale.Loc("preload_packs", nil))
flag.BoolVar(&utils.Options.ExtraDebug, "extra-debug", false, locale.Loc("extra_debug", nil))
2023-03-06 01:03:31 +00:00
flag.StringVar(&utils.Options.PathCustomUserData, "userdata", "", locale.Loc("custom_user_data", nil))
2023-01-23 18:18:01 +00:00
flag.String("lang", "", "lang")
2023-04-20 22:55:52 +00:00
flag.BoolVar(&utils.Options.Capture, "capture", false, "Capture Packet log")
2022-09-05 21:57:11 +00:00
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.ImportantFlag("debug")
2023-04-20 22:55:52 +00:00
subcommands.ImportantFlag("capture")
subcommands.ImportantFlag("preload")
2022-08-15 00:29:01 +00:00
subcommands.HelpCommand()
2023-03-06 14:50:36 +00:00
var ui utils.UI
2023-03-05 21:50:58 +00:00
if len(os.Args) < 2 {
2023-03-06 14:50:36 +00:00
ui = utils.MakeGui()
2023-03-05 21:50:58 +00:00
utils.Options.IsInteractive = true
} else {
ui = &CLI{}
}
2023-05-04 14:00:51 +00:00
utils.CurrentUI = ui
if utils.Version != "" {
go func() {
newVersion, err := utils.Updater.UpdateAvailable()
if err != nil {
logrus.Error(err)
}
if newVersion != "" {
logrus.Infof(locale.Loc("update_available", locale.Strmap{"Version": newVersion}))
utils.UpdateAvailable = newVersion
ui.Message(messages.UpdateAvailable{Version: newVersion})
}
}()
}
2022-08-11 20:22:39 +00:00
// exit cleanup
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
println("cancelling")
cancel()
}()
2022-02-20 11:39:02 +00:00
if !ui.Init() {
logrus.Error("Failed to init UI!")
return
}
2023-03-30 11:54:11 +00:00
err := ui.Start(ctx, cancel)
2023-03-08 11:46:16 +00:00
cancel()
if err != nil {
logrus.Error(err)
2022-09-08 08:39:46 +00:00
}
2022-07-29 16:12:06 +00:00
}
type TransCMD struct {
auth bool
}
func (*TransCMD) Name() string { return "trans" }
func (*TransCMD) Synopsis() string { return "" }
func (c *TransCMD) SetFlags(f *flag.FlagSet) {
f.BoolVar(&c.auth, "auth", false, locale.Loc("should_login_xbox", nil))
}
func (c *TransCMD) Execute(_ context.Context, ui utils.UI) error {
const (
2023-01-29 21:20:13 +00:00
BlackFg = "\033[30m"
2023-02-08 11:00:36 +00:00
Bold = "\033[1m"
Blue = "\033[46m"
Pink = "\033[45m"
White = "\033[47m"
Reset = "\033[0m"
)
if c.auth {
utils.Auth.GetTokenSource()
}
2023-01-29 21:20:13 +00:00
fmt.Println(BlackFg + Bold + Blue + " Trans " + Pink + " Rights " + White + " Are " + Pink + " Human " + Blue + " Rights " + Reset)
return nil
}
2022-09-04 13:24:55 +00:00
2023-02-12 21:22:44 +00:00
type CreateCustomDataCMD struct {
path string
}
func (*CreateCustomDataCMD) Name() string { return "create-customdata" }
func (*CreateCustomDataCMD) Synopsis() string { return "" }
func (c *CreateCustomDataCMD) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.path, "path", "customdata.json", "where to save")
}
func (c *CreateCustomDataCMD) Execute(_ context.Context, ui utils.UI) error {
2023-02-12 21:22:44 +00:00
var data utils.CustomClientData
fio, err := os.Create(c.path)
if err == nil {
defer fio.Close()
var bdata []byte
bdata, err = json.MarshalIndent(&data, "", "\t")
fio.Write(bdata)
}
if err != nil {
return err
2023-02-12 21:22:44 +00:00
}
return nil
2023-02-12 21:22:44 +00:00
}
func init() {
2022-09-04 14:26:32 +00:00
utils.RegisterCommand(&TransCMD{})
2023-02-12 21:22:44 +00:00
utils.RegisterCommand(&CreateCustomDataCMD{})
}