-
Notifications
You must be signed in to change notification settings - Fork 1
/
windows.go
206 lines (176 loc) · 4.86 KB
/
windows.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
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
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"unsafe"
"golang.org/x/sys/windows"
)
const (
processCheckInterval = 5 * time.Second
fileReadInterval = 1 * time.Second
)
type ProcessWatcher interface {
WatchProcess(ctx context.Context, gameStatus *bool) error
GetProcessPath(pid uint32) (string, error)
}
type FileWatcher interface {
TailFile(ctx context.Context, filePath string) error
}
type DefaultProcessWatcher struct {
wg sync.WaitGroup
lineStream chan string
}
type DefaultFileWatcher struct {
lineStream chan string
}
func NewProcessWatcher(lineStream chan string) ProcessWatcher {
return &DefaultProcessWatcher{
lineStream: lineStream,
}
}
func NewFileWatcher(lineStream chan string) FileWatcher {
return &DefaultFileWatcher{
lineStream: lineStream,
}
}
func (pw *DefaultProcessWatcher) WatchProcess(ctx context.Context, isProcessRunning *bool) error {
fw := NewFileWatcher(pw.lineStream)
var fileWatcherCancel context.CancelFunc
pw.wg.Add(1)
go func() {
defer pw.wg.Done()
for {
select {
case <-ctx.Done():
if fileWatcherCancel != nil {
fileWatcherCancel()
}
return
default:
clientPath, err := pw.findProcessByName()
if err == nil && !*isProcessRunning {
*isProcessRunning = true
pw.wg.Add(1)
var fileCtx context.Context
fileCtx, fileWatcherCancel = context.WithCancel(ctx)
fmt.Printf("Found PoE Client.txt Path: %s\n", clientPath)
go func(path string) {
defer pw.wg.Done()
defer func() {
*isProcessRunning = false
if fileWatcherCancel != nil {
fileWatcherCancel()
fileWatcherCancel = nil
}
}()
fmt.Println("Starting to tail Client.txt...")
if err := fw.TailFile(fileCtx, path); err != nil {
if err != context.Canceled {
fmt.Printf("Error tailing file: %v\n", err)
}
}
}(clientPath)
} else if err != nil && *isProcessRunning {
fmt.Println("PoE process stopped, waiting for restart...")
if fileWatcherCancel != nil {
fileWatcherCancel()
fileWatcherCancel = nil
}
*isProcessRunning = false
}
time.Sleep(processCheckInterval)
}
}
}()
return nil
}
func (fw *DefaultFileWatcher) TailFile(ctx context.Context, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("open file: %w", err)
}
defer file.Close()
if _, err := file.Seek(0, io.SeekEnd); err != nil {
return fmt.Errorf("seek to end: %w", err)
}
reader := bufio.NewReader(file)
for {
select {
case <-ctx.Done():
fmt.Println("Stopping file tail operation...")
return ctx.Err()
default:
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
select {
case <-ctx.Done():
return ctx.Err()
default:
time.Sleep(fileReadInterval)
continue
}
}
return fmt.Errorf("read file: %w", err)
}
select {
case fw.lineStream <- strings.TrimSpace(line):
case <-ctx.Done():
return ctx.Err()
}
}
}
}
func (pw *DefaultProcessWatcher) findProcessByName() (string, error) {
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return "", fmt.Errorf("create process snapshot: %w", err)
}
defer windows.CloseHandle(snapshot)
var procEntry windows.ProcessEntry32
procEntry.Size = uint32(unsafe.Sizeof(procEntry))
if err := windows.Process32First(snapshot, &procEntry); err != nil {
return "", fmt.Errorf("get first process: %w", err)
}
for {
processName := windows.UTF16ToString(procEntry.ExeFile[:])
// เช็คว่า process name ตรงกับตัวใดตัวหนึ่งใน slice หรือไม่
for _, poeProcessName := range POE2_PROCESS_NAMES {
if strings.EqualFold(processName, poeProcessName) {
path, err := pw.GetProcessPath(procEntry.ProcessID)
if err != nil {
return "", fmt.Errorf("get process path: %w", err)
}
clientFilePath := filepath.Join(filepath.Dir(path), "logs", "Client.txt")
return clientFilePath, nil
}
}
if err := windows.Process32Next(snapshot, &procEntry); err != nil {
if err == windows.ERROR_NO_MORE_FILES {
break
}
return "", fmt.Errorf("iterate processes: %w", err)
}
}
return "", fmt.Errorf("PoE process not found")
}
func (pw *DefaultProcessWatcher) GetProcessPath(pid uint32) (string, error) {
handle, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
return "", fmt.Errorf("open process (PID: %d): %w", pid, err)
}
defer windows.CloseHandle(handle)
var buffer [windows.MAX_PATH]uint16
size := uint32(len(buffer))
if err := windows.QueryFullProcessImageName(handle, 0, &buffer[0], &size); err != nil {
return "", fmt.Errorf("get process image name: %w", err)
}
return windows.UTF16ToString(buffer[:]), nil
}