-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (93 loc) · 2.69 KB
/
Copy pathmain.go
File metadata and controls
117 lines (93 loc) · 2.69 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"time"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/Hefero/D2R-AutoPotion-Go/cmd/config"
"github.com/Hefero/D2R-AutoPotion-Go/cmd/lifewatcher"
"github.com/Hefero/D2R-AutoPotion-Go/pkg/memory"
"github.com/faiface/beep"
)
func main() {
a := app.New()
w := a.NewWindow("Hefero Diablo 2 Ressurrected AutoPotion")
var manager = lifewatcher.Manager{}
manager.Timer = time.Now()
var XP = lifewatcher.ExperienceCalc{}
errL := config.Load()
if errL != nil {
log.Fatalf("Error loading configuration file config.yaml: %s", errL.Error())
}
audioBufferL, err := lifewatcher.InitAudio("cmd/lifewatcher/assets/life.wav")
audioBufferM, err := lifewatcher.InitAudio("cmd/lifewatcher/assets/mana.wav")
audioBufferR, err := lifewatcher.InitAudio("cmd/lifewatcher/assets/rejuv.wav")
ctx := contextWithSigterm(context.Background())
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
var cmd *exec.Cmd
cmd = exec.Command(path + "\\gui.exe")
hello := widget.NewLabel("Diablo 2 Ressurrected AutoPotion")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Start", func() {
process, err := memory.NewProcess()
if err != nil {
fmt.Printf("error starting process: player needs to be inside a running game\n")
fmt.Print("\033[A")
}
if err == nil {
gr := memory.NewGameReader(process)
watcher := lifewatcher.NewWatcher(gr)
if cmd.Process == nil {
cmd = exec.Command(path + "\\gui.exe")
cmd.Start()
}
if cmd.Process != nil {
cmd.Process.Kill()
cmd = exec.Command(path + "\\gui.exe")
cmd.Start()
}
go StartWatcher(*watcher, ctx, &manager, &XP, audioBufferL, audioBufferM, audioBufferR, path)
}
}),
widget.NewButton("Reset", func() {
lifewatcher.ResetXPCalc(&XP)
}),
))
w.ShowAndRun()
defer func() {
fmt.Println("\ncleanup")
cmd.Process.Kill()
}()
}
func StartWatcher(watcher lifewatcher.Watcher, ctx context.Context, manager *lifewatcher.Manager, XP *lifewatcher.ExperienceCalc, audioBufferL *beep.Buffer, audioBufferM *beep.Buffer, audioBufferR *beep.Buffer, path string) {
ticker := time.NewTicker(time.Nanosecond * 1)
for range ticker.C {
watcher.Start(ctx, manager, XP, audioBufferL, audioBufferM, audioBufferR)
}
}
func updateLabel(label *widget.Label, text string) {
label.SetText(text)
}
func contextWithSigterm(ctx context.Context) context.Context {
ctxWithCancel, cancel := context.WithCancel(ctx)
go func() {
defer cancel()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt)
select {
case <-signalCh:
case <-ctx.Done():
}
}()
return ctxWithCancel
}