Skip to content

Commit

Permalink
fix: platform specific code in launch logic (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Sep 16, 2023
1 parent 40a7ccd commit 8baea7c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/atotto/clipboard v0.1.4
github.com/gosuri/uitable v0.0.4
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
Expand All @@ -27,7 +28,6 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
Expand Down
116 changes: 87 additions & 29 deletions internal/pkg/game/launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
"os"
"os/exec"
"path"
"reflect"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/mworzala/mc/internal/pkg/platform"

"github.com/mworzala/mc/internal/pkg/game/rule"

"github.com/mworzala/mc/internal/pkg/account"
Expand Down Expand Up @@ -51,15 +55,15 @@ func LaunchProfile(dataDir string, p *profile.Profile, acc *account.Account, acc
libPath := path.Join(librariesPath, lib.Downloads.Artifact.Path)
classpath.WriteString(libPath)
} else if lib.Url != "" { // Direct maven library
parts := strings.Split(lib.Name, ":")
parts := strings.Split(lib.Name, platform.ClasspathSeparator())
groupId := parts[0]
artifactName := parts[1]
version := parts[2]

artifactPath := fmt.Sprintf("%s/%s/%s/%s-%s.jar", strings.ReplaceAll(groupId, ".", "/"), artifactName, version, artifactName, version)
classpath.WriteString(path.Join(librariesPath, artifactPath))
}
classpath.WriteString(":")
classpath.WriteString(platform.ClasspathSeparator())
}

if spec.InheritsFrom != "" {
Expand Down Expand Up @@ -102,25 +106,51 @@ func LaunchProfile(dataDir string, p *profile.Profile, acc *account.Account, acc
}

var args []string
args = append(args, "-XstartOnFirstThread")

for _, arg := range spec.Arguments.JVM {
if s, ok := arg.(string); ok {
args = append(args, replaceVars(s))
} else if m, ok := arg.(map[string]interface{}); ok {
_ = m
//value := m["value"]
//if s, ok := value.(string); ok {
// args = append(args, replaceVars(s))
//} else if a, ok := value.([]interface{}); ok {
// for _, v := range a {
// if s, ok := v.(string); ok {
// args = append(args, replaceVars(s))
// }
// }
//} else {
// panic(fmt.Sprintf("unknown type: %T", value))
//}
var ruleDef []*rule.Rule

md, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: func(from, to reflect.Type, value interface{}) (interface{}, error) {
if from.Kind() == reflect.String && to == reflect.TypeOf(rule.Allow) {
if value.(string) == "allow" {
return rule.Allow, nil
}
return rule.Deny, nil
}
return value, nil
},
Result: &ruleDef,
})
if err != nil {
panic("todo")
}
if err := md.Decode(m["rules"]); err != nil {
panic(fmt.Errorf("invalid rule: %w", err)) //todo better error handling. Should print about this and add an option to ignore unknown rules
}

if rules.Eval(ruleDef) == rule.Deny {
continue
}

// Add the rules
switch value := m["value"].(type) {
case string:
args = append(args, replaceVars(value))
case []interface{}:
for _, v := range value {
if s, ok := v.(string); ok {
args = append(args, replaceVars(s))
} else {
panic(fmt.Sprintf("unknown inner value type: %T", v))
}
}
default:
panic(fmt.Sprintf("unknown value type: %T", value))
}
} else {
panic("unknown arg type")
}
Expand All @@ -132,19 +162,47 @@ func LaunchProfile(dataDir string, p *profile.Profile, acc *account.Account, acc
if s, ok := arg.(string); ok {
args = append(args, replaceVars(s))
} else if m, ok := arg.(map[string]interface{}); ok {
_ = m
//value := m["value"]
//if s, ok := value.(string); ok {
// args = append(args, replaceVars(s))
//} else if a, ok := value.([]interface{}); ok {
// for _, v := range a {
// if s, ok := v.(string); ok {
// args = append(args, replaceVars(s))
// }
// }
//} else {
// panic(fmt.Sprintf("unknown type: %T", value))
//}
//todo duplicated above
var ruleDef []*rule.Rule

md, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: func(from, to reflect.Type, value interface{}) (interface{}, error) {
if from.Kind() == reflect.String && to == reflect.TypeOf(rule.Allow) {
if value.(string) == "allow" {
return rule.Allow, nil
}
return rule.Deny, nil
}
return value, nil
},
Result: &ruleDef,
})
if err != nil {
panic("todo")
}
if err := md.Decode(m["rules"]); err != nil {
panic(fmt.Errorf("invalid rule: %w", err)) //todo better error handling. Should print about this and add an option to ignore unknown rules
}

if rules.Eval(ruleDef) == rule.Deny {
continue
}

// Add the rules
switch value := m["value"].(type) {
case string:
args = append(args, replaceVars(value))
case []interface{}:
for _, v := range value {
if s, ok := v.(string); ok {
args = append(args, replaceVars(s))
} else {
panic(fmt.Sprintf("unknown inner value type: %T", v))
}
}
default:
panic(fmt.Sprintf("unknown value type: %T", value))
}
} else {
panic("unknown arg type")
}
Expand Down
7 changes: 7 additions & 0 deletions internal/pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ func OpenUrl(url string) error {
func WriteToClipboard(text string) error {
return clipboard.WriteAll(text)
}

func ClasspathSeparator() string {
if runtime.GOOS == "windows" {
return ";"
}
return ":"
}

0 comments on commit 8baea7c

Please sign in to comment.