-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdebug.go
More file actions
384 lines (336 loc) · 9.45 KB
/
Copy pathdebug.go
File metadata and controls
384 lines (336 loc) · 9.45 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"time"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
func moveFile(src, dst string) error {
err := os.Rename(src, dst)
if err == nil {
return nil
}
if _, ok := err.(*os.LinkError); ok {
return copyFile(src, dst)
}
return err
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
if err := out.Close(); err != nil {
return err
}
return os.Remove(src)
}
func (a *App) getPrimaryModsAndGroupMap(allMods []string) ([]string, map[string][]string) {
a.mu.Lock()
deps := a.ProjectData.Dependencies
a.mu.Unlock()
allModsSet := make(map[string]bool)
for _, m := range allMods {
allModsSet[m] = true
}
allDepsInSet := make(map[string]bool)
for _, mod := range allMods {
for _, dep := range deps[mod] {
if allModsSet[dep] {
allDepsInSet[dep] = true
}
}
}
var primaryMods []string
for _, mod := range allMods {
if !allDepsInSet[mod] {
primaryMods = append(primaryMods, mod)
}
}
sort.Strings(primaryMods)
if len(primaryMods) == 0 {
primaryMods = make([]string, len(allMods))
copy(primaryMods, allMods)
sort.Strings(primaryMods)
}
groupMap := make(map[string][]string)
for _, primary := range primaryMods {
group := make(map[string]bool)
group[primary] = true
queue := make([]string, 0)
queue = append(queue, deps[primary]...)
processed := make(map[string]bool)
for len(queue) > 0 {
dep := queue[0]
queue = queue[1:]
if processed[dep] {
continue
}
processed[dep] = true
if allModsSet[dep] {
group[dep] = true
queue = append(queue, deps[dep]...)
}
}
var groupList []string
for m := range group {
groupList = append(groupList, m)
}
sort.Strings(groupList)
groupMap[primary] = groupList
}
return primaryMods, groupMap
}
func (a *App) runDebugScan(modsToTest []string) {
a.mu.Lock()
a.ActiveScan = true
a.ScanCancelled = false
a.CurrentTestGroup = modsToTest
modsDir := a.ProjectData.ModsDir
a.mu.Unlock()
a.emitLog(fmt.Sprintf("Starting debug scan with %d mods...", len(modsToTest)), LogInfo)
a.emitLog("Preparing... (moving mods to temp directory)", LogInfo)
for i, mod := range modsToTest {
if a.getScanCancelled() {
break
}
src := filepath.Join(modsDir, mod)
dst := filepath.Join(a.TempDir, mod)
if _, err := os.Stat(src); err == nil {
if err := moveFile(src, dst); err != nil {
a.emitLog("Failed to move "+mod+": "+err.Error(), LogError)
a.restoreAllMods(modsDir)
a.mu.Lock()
a.ActiveScan = false
a.CurrentTestGroup = nil
a.mu.Unlock()
wailsRuntime.EventsEmit(a.ctx, "debug-failed", nil)
return
}
}
a.emitLog(fmt.Sprintf("Moving... (%d/%d)", i+1, len(modsToTest)), LogProgress)
}
var culpritInfo string
if !a.getScanCancelled() {
a.emitLog("Preparation complete.", LogSuccess)
a.emitLog("Analyzing mod dependencies to form testable groups...", LogInfo)
primaryMods, groupMap := a.getPrimaryModsAndGroupMap(modsToTest)
a.emitLog(fmt.Sprintf("Identified %d primary mods/groups for testing.", len(primaryMods)), LogSuccess)
if len(primaryMods) > 0 {
a.emitLog("Starting debug search to isolate 1 culprit...", LogInfo)
culpritInfo = a.binarySearch(primaryMods, groupMap, 1)
if culpritInfo == "" && !a.getScanCancelled() {
a.emitLog("Unable to isolate a single culprit. Falling back to 2-culprit resolution...", LogWarning)
wailsRuntime.EventsEmit(a.ctx, "test-fallback", "Unable to isolate a single problematic mod/group.\n\nFalling back to identify an interaction between 2.")
culpritInfo = a.binarySearch(primaryMods, groupMap, 2)
}
if culpritInfo == "" && !a.getScanCancelled() {
a.emitLog("Unable to isolate 2 culprits. Falling back to 3-culprit resolution...", LogWarning)
wailsRuntime.EventsEmit(a.ctx, "test-fallback", "Unable to isolate a 2-mod/group interaction.\n\nFalling back to identify an interaction between 3.")
culpritInfo = a.binarySearch(primaryMods, groupMap, 3)
}
} else {
a.emitLog("No primary mods could be identified. Aborting scan.", LogError)
}
}
a.restoreAllMods(modsDir)
a.mu.Lock()
a.ActiveScan = false
a.CurrentTestGroup = nil
a.mu.Unlock()
if a.getScanCancelled() {
a.emitLog("Debug scan cancelled by user.", LogWarning)
} else if culpritInfo != "" {
a.emitLog(fmt.Sprintf("CULPRIT(S) IDENTIFIED: %s", culpritInfo), LogError)
wailsRuntime.EventsEmit(a.ctx, "debug-complete", culpritInfo)
} else {
a.emitLog("No specific culprit(s) identified.", LogWarning)
wailsRuntime.EventsEmit(a.ctx, "debug-failed", nil)
}
a.emitLog("Cleanup complete. Ready for next session.", LogSuccess)
}
func (a *App) restoreAllMods(modsDir string) {
a.mu.Lock()
group := a.CurrentTestGroup
a.mu.Unlock()
allOk := true
for _, mod := range group {
src := filepath.Join(a.TempDir, mod)
dst := filepath.Join(modsDir, mod)
if _, err := os.Stat(src); err == nil {
if err := moveFile(src, dst); err != nil {
allOk = false
a.emitLog(fmt.Sprintf("Failed to restore %s: %v", mod, err), LogError)
}
}
}
if allOk {
os.RemoveAll(a.TempDir)
} else {
a.emitLog(fmt.Sprintf("Some mods could not be restored. Check %s for remaining files.", a.TempDir), LogWarning)
}
}
func (a *App) getScanCancelled() bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.ScanCancelled
}
func (a *App) binarySearch(primaryMods []string, groupMap map[string][]string, targetCount int) string {
current := make([]string, len(primaryMods))
copy(current, primaryMods)
for len(current) > targetCount && !a.getScanCancelled() {
mid := len(current) / 2
groupA := current[:mid]
groupB := current[mid:]
modsAList := flattenGroup(groupA, groupMap)
a.emitLog(fmt.Sprintf("Testing Group A (%d mods from %d primary groups)...", len(modsAList), len(groupA)), LogInfo)
result := a.testGroup(modsAList)
if a.getScanCancelled() {
return ""
}
if result {
a.emitLog("Group A passed - focusing on Group B", LogSuccess)
current = groupB
} else {
a.emitLog("Group A failed - focusing on Group A", LogError)
current = groupA
}
}
if a.getScanCancelled() || len(current) > targetCount {
return ""
}
finalList := flattenGroup(current, groupMap)
a.emitLog(fmt.Sprintf("Final test on suspected culprit(s) (%d mods from %d groups)...", len(finalList), len(current)), LogInfo)
finalResult := a.testGroup(finalList)
if a.getScanCancelled() {
return ""
}
if !finalResult {
sort.Strings(finalList)
return joinStrings(finalList, ", ")
}
a.emitLog("Final test passed. Could not isolate the issue.", LogWarning)
return ""
}
func flattenGroup(primaryMods []string, groupMap map[string][]string) []string {
modSet := make(map[string]bool)
for _, p := range primaryMods {
for _, m := range groupMap[p] {
modSet[m] = true
}
}
var list []string
for m := range modSet {
list = append(list, m)
}
return list
}
func (a *App) testGroup(mods []string) bool {
a.mu.Lock()
modsDir := a.ProjectData.ModsDir
currentGroup := a.CurrentTestGroup
a.mu.Unlock()
for _, item := range readDirNames(modsDir) {
if filepath.Ext(item) != ".jar" {
continue
}
inCurrent := false
for _, m := range currentGroup {
if m == item {
inCurrent = true
break
}
}
if inCurrent {
src := filepath.Join(modsDir, item)
dst := filepath.Join(a.TempDir, item)
if _, err := os.Stat(src); err == nil {
if err := moveFile(src, dst); err != nil {
a.emitLog("Failed to isolate "+item+": "+err.Error(), LogError)
}
}
}
}
for _, mod := range mods {
src := filepath.Join(a.TempDir, mod)
dst := filepath.Join(modsDir, mod)
if _, err := os.Stat(src); err == nil {
if err := moveFile(src, dst); err != nil {
a.emitLog("Failed to restore "+mod+": "+err.Error(), LogError)
}
}
}
return a.waitForTestResult(len(mods))
}
func (a *App) waitForTestResult(modCount int) bool {
if a.AutoLaunchGame {
prismPath, err := findPrismLauncher()
if err != nil {
a.emitLog(fmt.Sprintf("Failed to launch Prism Launcher: %v", err), LogError)
wailsRuntime.EventsEmit(a.ctx, "test-group-prompt", map[string]interface{}{
"count": modCount,
"auto_launch": false,
})
result := <-a.testResultChan
return result
}
instanceName := a.getInstanceName()
cmd := exec.Command(prismPath, "-l", instanceName)
if err := cmd.Start(); err != nil {
a.emitLog(fmt.Sprintf("Failed to launch Prism Launcher: %v", err), LogError)
wailsRuntime.EventsEmit(a.ctx, "test-group-prompt", map[string]interface{}{
"count": modCount,
"auto_launch": false,
})
result := <-a.testResultChan
return result
}
defer func() {
cmd.Process.Kill()
time.Sleep(500 * time.Millisecond)
}()
}
wailsRuntime.EventsEmit(a.ctx, "test-group-prompt", map[string]interface{}{
"count": modCount,
"auto_launch": a.AutoLaunchGame,
})
result := <-a.testResultChan
return result
}
func (a *App) getInstanceName() string {
return filepath.Base(filepath.Dir(filepath.Dir(a.ProjectData.ModsDir)))
}
func readDirNames(dir string) []string {
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var names []string
for _, e := range entries {
names = append(names, e.Name())
}
return names
}
func joinStrings(items []string, sep string) string {
if len(items) == 0 {
return ""
}
result := items[0]
for _, s := range items[1:] {
result += sep + s
}
return result
}