-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlauncher.go
120 lines (99 loc) · 2.37 KB
/
launcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
)
func prepareLaunchPath(basePath, version string) string {
defaultPath := filepath.Join(basePath, version)
if !isFolderInUse(defaultPath) {
return defaultPath
}
for i := 1; ; i++ {
tempFolderName := fmt.Sprintf("%s_%d", version, i)
tempPath := filepath.Join(basePath, tempFolderName)
if _, err := os.Stat(tempPath); err == nil {
if !isFolderInUse(tempPath) {
return tempPath
}
} else {
if err := os.MkdirAll(tempPath, os.ModePerm); err != nil {
fmt.Println("Error creating temporary folder:", err)
return defaultPath
}
if err := copyFolder(defaultPath, tempPath); err != nil {
fmt.Println("Error copying folder for new instance:", err)
return defaultPath
}
return tempPath
}
}
}
func createLockFile(path string, pid int) {
file, err := os.Create(path)
if err != nil {
fmt.Println("Error creating lock file:", err)
return
}
defer file.Close()
_, err = file.WriteString(strconv.Itoa(pid))
if err != nil {
fmt.Println("Error writing to lock file:", err)
}
}
func isFolderInUse(folderPath string) bool {
dcrFiles := []string{"habbo.dcr", "habbo-xl.dcr"}
for _, dcrFile := range dcrFiles {
habboFilePath := filepath.Join(folderPath, dcrFile)
file, err := os.OpenFile(habboFilePath, os.O_RDWR, 0666)
if err != nil {
if os.IsNotExist(err) {
continue
}
return true
}
file.Close()
}
return false
}
func launchApplication(path, habboExe string) {
exePath := filepath.Join(path, habboExe)
cmd := exec.Command(exePath)
err := cmd.Start()
if err != nil {
fmt.Println("Error launching application:", err)
return
}
lockFilePath := filepath.Join(path, "instance.lock")
createLockFile(lockFilePath, cmd.Process.Pid)
}
func copyFolder(src, dest string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
destPath := filepath.Join(dest, relPath)
if info.IsDir() {
return os.MkdirAll(destPath, info.Mode())
}
inputFile, err := os.Open(path)
if err != nil {
return err
}
defer inputFile.Close()
outputFile, err := os.Create(destPath)
if err != nil {
return err
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
return err
})
}