diff --git a/.gitignore b/.gitignore index bccebf6..b6e9c13 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ libraries/** natives/** temp/ /m1mc +/m1mc.zip diff --git a/README.md b/README.md index f516368..b34a994 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ So I made this program to make Minecraft run natively with [HMCL](https://github 1. Go to [releases page](https://github.com/Dreamail/M1MC/releases), download `M1MC.zip` under "Assets". 2. Unzip M1MC.zip, open M1MC folder, right click "m1mc" with Alt key, click Copy "m1mc" as Pathname. 3. Open HMCL and click Settings, paste into "Wrapper command" field. -4. Custom "Native Library Path" with `M1MC/natives`. +4. Custom "Native Library Path" with any path, because M1MC will modify it when game start and HMCL won't work if you don't use custom native lib path. 5. Done! Enjoy your play! ## How to Build 1. Clone this repository. 2. Run command `go build -o m1mc main.go`. -3. Download [LWJGL macOS arm64](https://www.lwjgl.org/customize) and [`java-objc-bridge`](https://mvnrepository.com/artifact/ca.weblite/java-objc-bridge) and unzip necessary jars into `libraries` folder, native libraries into `natives`. +3. ~~Download [LWJGL macOS arm64](https://www.lwjgl.org/customize) and [`java-objc-bridge`](https://mvnrepository.com/artifact/ca.weblite/java-objc-bridge) and unzip necessary jars into `libraries` folder, native libraries into `natives`.~~ No longer need! M1MC will download libraries when game start at first time. diff --git a/main.go b/main.go index ab8efed..b7cae9c 100644 --- a/main.go +++ b/main.go @@ -19,41 +19,50 @@ var wg sync.WaitGroup func main() { args := os.Args - proPath, _ := filepath.Abs(args[0]) - basePath := filepath.Dir(proPath) + err := os.Mkdir(os.Getenv("INST_MC_DIR")+"/m1mc", 0770) + if err != nil { + if !strings.Contains(err.Error(), "file exists") { + log.Fatal(err) + } + } + workDir := os.Getenv("INST_MC_DIR") + "/m1mc" pwd, err := os.Getwd() if err != nil { log.Fatal(err) } - os.Chdir(basePath) + os.Chdir(workDir) getLibs() isEnv := os.Getenv("CLASSPATH") != "" + classPathStr := "" classPathIndex := 0 + nativeIndex := 0 - nClassPath := make([]string, 0) - filepath.Walk(basePath+"/libraries/", func(path string, info fs.FileInfo, err error) error { - if strings.Contains(info.Name(), "lwjgl") || strings.Contains(info.Name(), "java-objc-bridge") { - nClassPath = append(nClassPath, path) + for i, arg := range args { + if arg == "-cp" { + classPathIndex = i + 1 + classPathStr = args[i+1] } - return err - }) - - classPathStr := "" + if strings.Contains(arg, "-Djava.library.path") { + nativeIndex = i + } + if (classPathIndex != 0 && nativeIndex != 0) || (isEnv && nativeIndex != 0) { + break + } + } if isEnv { classPathStr = os.Getenv("CLASSPATH") - } else { - for i, arg := range args { - if arg == "-cp" { - classPathIndex = i + 1 - classPathStr = args[i+1] - break - } - } } + nClassPath := make([]string, 0) + filepath.Walk(workDir+"/libraries/", func(path string, info fs.FileInfo, err error) error { + if strings.Contains(info.Name(), "lwjgl") || strings.Contains(info.Name(), "java-objc-bridge") { + nClassPath = append(nClassPath, path) + } + return err + }) for _, v := range strings.Split(classPathStr, ":") { if !strings.Contains(v, "lwjgl") && !strings.Contains(v, "java-objc-bridge") { nClassPath = append(nClassPath, v) @@ -62,6 +71,8 @@ func main() { os.Chdir(pwd) args[classPathIndex] = strings.Join(nClassPath, ":") + args[nativeIndex] = "-Djava.library.path=" + workDir + "/natives" + println(strings.Join(args, " ")) cmd := exec.Command(args[1], args[2:]...) if isEnv { cmd.Env = append(os.Environ(), "CLASSPATH=") @@ -69,17 +80,6 @@ func main() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - //println(strings.Join(nClassPath, ":")) - - /*var stdout bytes.Buffer - - err := cmd.Run() - println(string(stdout.Bytes())) - if err != nil { - println(err.Error()) - return - }*/ - cmd.Run() } @@ -109,13 +109,6 @@ func getLibs() { lwjglVersion := "" ObjCVersion := "" - /*client := http.Client{ - CheckRedirect: func(r *http.Request, via []*http.Request) error { - r.URL.Opaque = r.URL.Path - return nil - }, - }*/ - for _, v := range metaUrls { resp, err := http.Get(v) if err != nil {