Skip to content

Skin Manager #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/mc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mc
import (
"github.com/MakeNowJust/heredoc"
"github.com/mworzala/mc/cmd/mc/modrinth"
"github.com/mworzala/mc/cmd/mc/skin"

"github.com/mworzala/mc/cmd/mc/profile"

Expand Down Expand Up @@ -36,6 +37,7 @@ func NewRootCmd(app *cli.App) *cobra.Command {
cmd.AddCommand(account.NewAccountCmd(app))
cmd.AddCommand(java.NewJavaCmd(app))
cmd.AddCommand(profile.NewProfileCmd(app))
cmd.AddCommand(skin.NewSkinCmd(app))
cmd.AddCommand(newLaunchCmd(app))
cmd.AddCommand(newInstallCmd(app))
cmd.AddCommand(modrinth.NewModrinthCmd(app))
Expand Down
137 changes: 137 additions & 0 deletions cmd/mc/skin/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package skin

import (
"context"
"errors"
"fmt"
"os"
"os/signal"

"github.com/google/uuid"
"github.com/mworzala/mc/internal/pkg/cli"
"github.com/mworzala/mc/internal/pkg/mojang"
"github.com/spf13/cobra"
)

type addSkinOpts struct {
app *cli.App

account string
variant string
cape string
name string
apply bool
}

var (
ErrInvalidVariant = errors.New("invalid variant")

validationMap = map[string]bool{"classic": true, "slim": true, "": true}
)

func newAddCmd(app *cli.App, account string) *cobra.Command {
var o addSkinOpts

cmd := &cobra.Command{
Use: "add",
Short: "Add a skin to your list",
Args: func(cmd *cobra.Command, args []string) error {
o.app = app
return o.validateArgs(cmd, args)
},
RunE: func(_ *cobra.Command, args []string) error {
o.app = app
return o.execute(args)
},
}

o.account = account

cmd.Flags().StringVar(&o.variant, "variant", "", "Skin variant [classic/slim] (defaults to classic)")
cmd.Flags().StringVar(&o.cape, "cape", "", "Cape name, 'none' to remove")
cmd.Flags().BoolVar(&o.apply, "apply", false, "Apply the skin")
cmd.Flags().BoolVar(&o.apply, "set", false, "Apply the skin")

cmd.Flags().FlagUsages()

return cmd
}

func (o *addSkinOpts) validateArgs(cmd *cobra.Command, args []string) (err error) {
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}

if !validationMap[o.variant] {
return ErrInvalidVariant
}

return nil
}

func (o *addSkinOpts) execute(args []string) error {
if len(args) > 1 {
o.name = args[1]
}

if o.name == "" {
o.name = uuid.New().String()
}

if o.account == "" {
o.account = o.app.AccountManager().GetDefault()
}

token, err := o.app.AccountManager().GetAccountToken(o.account)
if err != nil {
return err
}

client := mojang.NewProfileClient(o.app.Build.Version, token)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

info, err := client.ProfileInformation(ctx)
if err != nil {
return err
}

if o.cape == "" {
for _, cape := range info.Capes {
if cape.State == "ACTIVE" {
o.cape = cape.ID
}
}
} else if o.cape != "none" {
for _, cape := range info.Capes {
if cape.Alias == o.cape {
o.cape = cape.ID
}
}
}

skinData := args[0]

skin, err := o.app.SkinManager().CreateSkin(ctx, client, o.name, o.variant, skinData, o.cape)
if err != nil {
return err
}

if o.apply {

err = o.app.SkinManager().ApplySkin(ctx, client, skin)
if err != nil {
return err
}
if !o.app.Config.NonInteractive {
fmt.Printf("skin %s applied", skin.Name)
}
}

if !o.app.Config.NonInteractive {
fmt.Printf("skin %s with cape %s was added to the list", skin.Name, skin.Cape)
}

return o.app.SkinManager().Save()

}
80 changes: 80 additions & 0 deletions cmd/mc/skin/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package skin

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/mworzala/mc/internal/pkg/cli"
"github.com/mworzala/mc/internal/pkg/mojang"
"github.com/spf13/cobra"
)

type applySkinOpts struct {
app *cli.App

account string
}

func newApplyCmd(app *cli.App, account string) *cobra.Command {
var o applySkinOpts

cmd := &cobra.Command{
Use: "apply",
Short: "Apply a saved skin",
Aliases: []string{"set"},
Args: func(cmd *cobra.Command, args []string) error {
o.app = app
return o.validateArgs(cmd, args)
},
RunE: func(_ *cobra.Command, args []string) error {
o.app = app
return o.execute(args)
},
}

o.account = account

return cmd
}

func (o *applySkinOpts) validateArgs(cmd *cobra.Command, args []string) (err error) {
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}

return nil
}

func (o *applySkinOpts) execute(args []string) error {
skinName := args[0]

if o.account == "" {
o.account = o.app.AccountManager().GetDefault()
}

token, err := o.app.AccountManager().GetAccountToken(o.account)
if err != nil {
return err
}

skin, err := o.app.SkinManager().GetSkin(skinName)
if err != nil {
return err
}

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

client := mojang.NewProfileClient(o.app.Build.Version, token)

err = o.app.SkinManager().ApplySkin(ctx, client, skin)
if err != nil {
return err
}
if !o.app.Config.NonInteractive {
fmt.Printf("skin %s applied", skin.Name)
}
return nil
}
41 changes: 41 additions & 0 deletions cmd/mc/skin/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package skin

import (
"github.com/mworzala/mc/internal/pkg/cli"
appModel "github.com/mworzala/mc/internal/pkg/cli/model"
"github.com/spf13/cobra"
)

type listSkinsOpts struct {
app *cli.App
}

func newListCmd(app *cli.App) *cobra.Command {
var o listSkinsOpts

cmd := &cobra.Command{
Use: "list",
Short: "List saved skins",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
o.app = app
return o.listSkins()
},
}

return cmd
}

func (o *listSkinsOpts) listSkins() error {
skinManager := o.app.SkinManager()

var result appModel.SkinList
for _, skin := range skinManager.Skins() {
result = append(result, &appModel.Skin{
Name: skin.Name,
Modified: skin.AddedDate,
})
}

return o.app.Present(result)
}
23 changes: 23 additions & 0 deletions cmd/mc/skin/skin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package skin

import (
"github.com/mworzala/mc/internal/pkg/cli"
"github.com/spf13/cobra"
)

func NewSkinCmd(app *cli.App) *cobra.Command {
cmd := &cobra.Command{
Use: "skin",
Short: "Manage Minecraft skins and capes",
}

var account string

cmd.Flags().StringVar(&account, "account", "", "Account to use")

cmd.AddCommand(newListCmd(app))
cmd.AddCommand(newAddCmd(app, account))
cmd.AddCommand(newApplyCmd(app, account))

return cmd
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.2
require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/atotto/clipboard v0.1.4
github.com/google/uuid v1.6.0
github.com/gosuri/uitable v0.0.4
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cobra v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
Expand Down
14 changes: 14 additions & 0 deletions internal/pkg/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/mworzala/mc/internal/pkg/java"
"github.com/mworzala/mc/internal/pkg/platform"
"github.com/mworzala/mc/internal/pkg/profile"
"github.com/mworzala/mc/internal/pkg/skin"
"github.com/spf13/viper"
)

Expand All @@ -34,6 +35,7 @@ type App struct {
versionManager *game.VersionManager
profileManager profile.Manager
gameManager game.Manager
skinManager skin.Manager
}

func NewApp(build BuildInfo) *App {
Expand Down Expand Up @@ -133,3 +135,15 @@ func (a *App) GameManager() game.Manager {

return a.gameManager
}

func (a *App) SkinManager() skin.Manager {
if a.skinManager == nil {
var err error
a.skinManager, err = skin.NewManager(a.ConfigDir)
if err != nil {
a.Fatal(err)
}
}

return a.skinManager
}
28 changes: 28 additions & 0 deletions internal/pkg/cli/model/skin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package model

import (
"fmt"
"time"

"github.com/gosuri/uitable"
)

type Skin struct {
Name string
Modified time.Time
}

func (i *Skin) String() string {
return fmt.Sprintf("%s\t%s", i.Name, i.Modified)
}

type SkinList []*Skin

func (l SkinList) String() string {
table := uitable.New()
table.AddRow("NAME", "MODIFIED")
for _, skin := range l {
table.AddRow(skin.Name, skin.Modified)
}
return table.String()
}
Loading
Loading