Skip to content

Commit

Permalink
auto modify native library path
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamail committed Jun 5, 2022
1 parent b39c92d commit fa40ae0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ libraries/**
natives/**
temp/
/m1mc
/m1mc.zip
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
67 changes: 30 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -62,24 +71,15 @@ 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=")
}
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()
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fa40ae0

Please sign in to comment.