forked from Make-Tarkov-Great-Again/MTGA-GO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodloader.go
179 lines (143 loc) · 4.76 KB
/
modloader.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"MT-GO/data"
"MT-GO/tools"
"fmt"
"github.com/goccy/go-json"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
MTGOUserMods = "%s\"MT-GO/mods/%s\""
//MTGO_SERVER = "\"MT-GO/server\""
ModNameMod = "%s.Mod()"
BundlesToLoad = "var bundlesToLoad = []string{%s,\n}"
BundlesToLoadLoop = "for _, path := range bundlesToLoad {\n\t\tformattedPath := strings.Replace(path, \"\\\\\\\\\", \"\\\\\", -1)\n\t\tdata.AddModBundleDirPath(formattedPath)\n\t}"
)
func main() {
// Get the path of the "mods" folder in the same directory as the executable.
wd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
mods := filepath.Join(wd, "mods")
log.Println("Mod directory:", mods)
if !tools.FileExist(mods) {
if err := tools.CreateDirectory(mods); err != nil {
log.Fatalln(err)
}
}
// List all subdirectories in the "mods" folder.
modSubDirs, err := tools.GetDirectoriesFrom(mods)
if err != nil {
log.Fatalln("Error listing subdirectories in the 'mods' folder:", err)
}
// Create an array to store the mod imports and function calls.
imports := []string{"\"fmt\"", "\"time\""}
calls := make([]string, 0)
variables := make([]string, 0)
if len(modSubDirs) != 0 {
var bundleLoader bool
var modConfig *data.ModInfo
bundlesToLoad := make([]string, 0)
for name := range modSubDirs {
log.Println("Checking directory:", name)
// Check if there's a "mod-info.json" file in the subdirectory.
ModInfoPath := filepath.Join(mods, name, "mod-info.json")
if !tools.FileExist(ModInfoPath) {
log.Println("Did not find 'mod-info.json' in:", name, ", continuing...")
continue
}
// Read and parse the mod-info.json file.
input := tools.GetJSONRawMessage(ModInfoPath)
if err := json.Unmarshal(input, &modConfig); err != nil {
fmt.Printf("Error parsing mod-info.json in %s: %v\n", name, err)
continue
}
// Construct the mod import and function call with alias.
dir := filepath.Join(mods, name)
if tools.FileExist(filepath.Join(dir, "bundles")) {
if !bundleLoader {
imports = append(imports, "\"MT-GO/data\"", "\"strings\"")
calls = append(calls, BundlesToLoadLoop)
bundleLoader = true
}
//TODO: See if we can make this better because golly-fuckin-gee
bundleName := filepath.Join(dir, "bundles")
fixed := "\n\t\"" + strings.Replace(bundleName, "\\", "\\\\", -1) + "\""
bundlesToLoad = append(bundlesToLoad, fixed)
log.Println()
}
var modImport string
var modCall string
if modConfig.PackageAlias == modConfig.PackageName {
modImport = fmt.Sprintf(MTGOUserMods, "", modConfig.PackageName)
modCall = fmt.Sprintf(ModNameMod, modConfig.PackageName)
} else {
modImport = fmt.Sprintf(MTGOUserMods, modConfig.PackageAlias, modConfig.PackageName)
modCall = fmt.Sprintf(ModNameMod, modConfig.PackageAlias)
}
imports = append(imports, modImport)
calls = append(calls, modCall)
}
if len(bundlesToLoad) != 0 {
bundles := strings.Join(bundlesToLoad, ",")
bundlesVariable := fmt.Sprintf(BundlesToLoad, bundles)
variables = append(variables, bundlesVariable)
}
}
// Update the "mods.go" file.
modFile := filepath.Join(mods, "mods.go")
log.Println("Updating 'mods.go' file:", modFile)
if err := updateModsFile(modFile, imports, variables, calls); err != nil {
log.Fatalln("Error updating mods.go:", err)
}
exeDir, err := os.Executable()
if err != nil {
log.Fatalln("Error getting the executable path:", err)
}
// Start the main Go instance in a new cmd instance.
mainGoFile := filepath.Join(filepath.Dir(exeDir), "backend.go")
log.Println("Starting main Go instance:", mainGoFile)
cmd := exec.Command("cmd", "/c", "start", "cmd", "/k", "go run backend.go")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run the new cmd instance to start the main Go program.
if err := cmd.Run(); err != nil {
log.Println("Error starting the main Go instance:", err)
return
}
// Close the updater executable.
log.Println("Updater finished.")
os.Exit(1)
}
func updateModsFile(filePath string, imports []string, variables []string, calls []string) error {
// Create the new content with updated imports and function calls.
newContent := []byte(fmt.Sprintf(
`package mods
//TODO: DO NOT DELETE OR MANUALLY EDIT THIS FILE
// This file is automatically generated for mod functionality
import (
%s
)
%s
func Init() {
startTime := time.Now()
%s
endTime := time.Now()
fmt.Printf("\n[MOD LOADER : COMPLETE] in %%s\n", endTime.Sub(startTime))
}`,
strings.Join(imports, "\n\t"),
strings.Join(variables, "\n"),
strings.Join(calls, "\n\t"),
))
// Write the updated content to "mods.go" or create the file if it doesn't exist.
err := os.WriteFile(filePath, newContent, os.ModePerm)
if err != nil {
return err
}
return nil
}