-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
363 lines (298 loc) · 8.31 KB
/
main.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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/color"
"image/draw"
"io/fs"
"log"
_ "net/http/pprof"
"os"
"os/exec"
"strconv"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"golang.design/x/clipboard"
"github.com/fsnotify/fsnotify"
"fyne.io/fyne/v2/widget"
"github.com/go-vgo/robotgo"
"github.com/vcaesar/bitmap"
"gopkg.in/ini.v1"
)
type Direction uint8
type MousePos struct {
X int
Y int
}
type Action struct {
Id int `json:"id"`
Name string `json:"name,omitempty"`
Action string `json:"action"`
Data string `json:"data"`
True int `json:"true"`
False int `json:"false"`
Repeat int `json:"repeat"`
Delay int `json:"delay"`
Actions []Action `json:"actions"`
Click bool `json:"click"`
TrueClick bool `json:"trueClick"`
ImgTolerance float64 `json:"tolerance,omitempty"`
}
var defaultDelayValue int
var defaultImgToleranceValue float64
var DEFAULT_DELAY_ENTRY = "100"
var DEFAULT_IMG_TOLERANCE_ENTRY = "0.2"
var moveMouseSmoothProcess *exec.Cmd
func runMoveMouseSmooth(x int, y int, low float64, high float64) {
cmd := exec.Command("./MoveMouseSmooth.exe", strconv.Itoa(x), strconv.Itoa(y), floatToString(low), floatToString(high))
moveMouseSmoothProcess = cmd
err := cmd.Run()
if err != nil {
fmt.Println(err)
return
}
cmd.Wait()
}
func (action *Action) UnmarshalJSON(data []byte) error {
type ActionAlias Action
actionAlias := &ActionAlias{
Delay: defaultDelayValue,
ImgTolerance: defaultImgToleranceValue,
}
err := json.Unmarshal(data, actionAlias)
if err != nil {
return err
}
*action = Action(*actionAlias)
return nil
}
func parseInt(str string) int {
num, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0
}
return int(num)
}
func parseFloat(str string) float64 {
num, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0
}
return num
}
func floatToString(x float64) string {
return strconv.FormatFloat(x, 'f', -1, 64)
}
func getCurrentRefreshRate() (int64, error) {
cmd := exec.Command("wmic", "PATH", "Win32_videocontroller", "get", "currentrefreshrate")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
return strconv.ParseInt(strings.Split(out.String(), "\n")[1], 10, 64)
}
func runScreenClip() {
cmd := exec.Command("explorer", "ms-screenclip:")
err := cmd.Run()
if err == nil {
log.Fatal(err)
}
}
func getImgFromClipboard() image.Image {
imageByte := clipboard.Read(clipboard.FmtImage)
img, _, err := image.Decode(bytes.NewReader(imageByte))
if err != nil {
log.Fatalln(err)
}
// Get the bounds of the image
bounds := img.Bounds()
// Create a new image with the first 3 rows removed
newImg := image.NewRGBA(image.Rect(0, 0, bounds.Dx()-3, bounds.Dy()))
// Copy the pixels, excluding the first 3 rows
draw.Draw(newImg, newImg.Bounds(), img, image.Pt(3, 0), draw.Src)
return newImg
// robotgo.SavePng(img, imagePathName+".png")
}
func findImageFromScreen(imageBitMap robotgo.CBitmap, tolerance float64) (int, int) {
currentScreen := robotgo.CaptureScreen(allScreenBound...)
defer robotgo.FreeBitmap(currentScreen)
return bitmap.Find(imageBitMap, currentScreen, tolerance)
}
func findImageFromImage(img robotgo.CBitmap, imgToFind robotgo.CBitmap, tolerance float64) (int, int) {
return bitmap.Find(imgToFind, img, tolerance)
}
// func setContentToText(c fyne.Canvas) {
// black := color.NRGBA{R: 0, G: 180, B: 0, A: 255}
// text := canvas.NewText("Text", black)
// canvas.
// text.TextStyle.Bold = true
// c.SetContent(text)
// }
func getDirList(dir string) []fs.DirEntry {
entries, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
return entries
}
func getScriptList() []string {
entries := getDirList("./scripts")
var entriesName []string
for _, e := range entries {
// fmt.Println(e.Type())
if e.Type().IsDir() {
entriesName = append(entriesName, e.Name())
}
}
return entriesName
}
func watchScriptsFolderChange(scriptSelect *widget.Select) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err)
}
err = watcher.Add("./scripts")
if err != nil {
fmt.Println(err)
}
for {
select {
// case _ = <-watcher.Events:
case <-watcher.Events:
// fmt.Println("event:", event)
// if event.Op&fsnotify.Write == fsnotify.Write {
// fmt.Println("modified file:", event.Name)
// }
scriptSelect.Options = getScriptList()
case err := <-watcher.Errors:
fmt.Println("error:", err)
}
}
}
// var AllRunButtons []*widget.Button
// func newRunButton(label string, tapped func()) *widget.Button {
// newButton := widget.NewButton(label, tapped)
// AllRunButtons = append(AllRunButtons, newButton)
// return newButton
// }
// func disableAllRunButton() {
// for _, runButton := range AllRunButtons {
// runButton.Disable()
// }
// }
// func enableAllRunButton() {
// for _, runButton := range AllRunButtons {
// runButton.Enable()
// }
// }
// var green = color.NRGBA{R: 0, G: 180, B: 0, A: 255}
var black = color.NRGBA{R: 0, G: 0, B: 0, A: 255}
var selectedScript string
// var newSelectedScript string
const configPath = "./config.ini"
var mainWindow fyne.Window
var mainApp fyne.App
var cfgGlobal *ini.File
func findAllScreenBounds() []int {
var monitors [][]int
screenIndex := 0
for {
sx, sy, sw, sh := robotgo.GetDisplayBounds(screenIndex)
if sw != 0 && sh != 0 {
monitors = append(monitors, []int{sx, sy, sw, sh})
screenIndex++
} else {
break
}
}
minX := 0
minY := 0
maxTotalW := 0
maxTotalH := 0
for _, monitor := range monitors {
if minX > monitor[0] {
minX = monitor[0]
}
if minY > monitor[1] {
minY = monitor[1]
}
if maxTotalW < monitor[2] {
maxTotalW = monitor[0] + monitor[2]
}
if maxTotalH < monitor[3] {
maxTotalH = monitor[1] + monitor[3]
}
}
// negative coordinates (screen on top left of main screen)
maxTotalW -= minX
maxTotalH -= minY
return []int{minX, minY, maxTotalW, maxTotalH}
}
var allScreenBound = findAllScreenBounds()
func main() {
// go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
// }()
fmt.Println(allScreenBound)
cfg, err := ini.Load(configPath)
if err != nil {
cfg = ini.Empty()
cfgGlobal = cfg
err := cfg.SaveTo(configPath)
if err != nil {
// Handle error
fmt.Println("Error saving INI file:", err)
return
}
}
cfgGlobal = cfg
DEFAULT_DELAY_ENTRY = cfg.Section("setting").Key("DefaultDelay").String()
DEFAULT_IMG_TOLERANCE_ENTRY = cfg.Section("setting").Key("DefaultImageTolerance").String()
selectedScript = cfg.Section("setting").Key("SelectedScript").String()
mainApp = app.NewWithID("com.vietanht.autobot")
// mainApp.Preferences().SetBool("Boolean", true)
mainWindow = mainApp.NewWindow("Auto bot")
mainWindow.SetMaster()
windowWidth, err := cfg.Section("setting").Key("WindowWidth").Int()
if err != nil {
windowWidth = 600
}
windowHeight, err := cfg.Section("setting").Key("WindowHeight").Int()
if err != nil {
windowHeight = 400
}
mainWindow.Resize(fyne.Size{Width: float32(windowWidth), Height: float32(windowHeight)})
mainApp.Settings().SetTheme(theme.LightTheme())
tabs := container.NewAppTabs(
container.NewTabItem("Play", makeMainContainer()),
container.NewTabItem("Record Simple", makeSimpleScriptContainer()),
container.NewTabItem("Script Editor", makeScriptEditorContainer()),
)
tabs.SetTabLocation(container.TabLocationLeading)
mainWindow.SetContent(tabs)
currentposwindow := createCurrentMousePosWindow(mainApp)
currentposwindow.Show()
mainWindow.Show()
mainApp.Run()
defer func() {
// cfgGlobal.Section("setting").Key("DefaultDelay").SetValue(scriptIntervalEntry.Text)
// cfgGlobal.Section("setting").Key("DefaultImageTolerance").SetValue(imageToleranceEntry.Text)
cfgGlobal.Section("setting").Key("WindowWidth").SetValue(strconv.FormatFloat(float64(mainWindow.Canvas().Size().Width), 'f', -1, 32))
cfgGlobal.Section("setting").Key("WindowHeight").SetValue(strconv.FormatFloat(float64(mainWindow.Canvas().Size().Height), 'f', -1, 32))
cfgGlobal.Section("setting").Key("SelectedScript").SetValue(selectedScript)
// fmt.Println(mainWindow.Canvas().Size())
err := cfgGlobal.SaveTo(configPath)
if err != nil {
// Handle error
fmt.Println("Error saving INI file:", err)
return
}
}()
}