This repository has been archived by the owner on Jan 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
425 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
jump*.png | ||
debugger | ||
youjumpijump-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"image/png" | ||
"log" | ||
"math" | ||
"os" | ||
"os/exec" | ||
"runtime/debug" | ||
"strconv" | ||
"time" | ||
|
||
jump "github.com/faceair/youjumpijump" | ||
) | ||
|
||
func main() { | ||
defer func() { | ||
jump.Debugger() | ||
if e := recover(); e != nil { | ||
log.Printf("%s: %s", e, debug.Stack()) | ||
fmt.Print("the program has crashed, press any key to exit") | ||
var c string | ||
fmt.Scanln(&c) | ||
} | ||
}() | ||
|
||
var ratio float64 | ||
fmt.Print("input jump ratio (recommend 2.04):") | ||
_, err := fmt.Scanln(&ratio) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("now jump ratio is %f", ratio) | ||
|
||
for { | ||
jump.Debugger() | ||
|
||
_, err := exec.Command("/system/bin/screencap", "-p", "jump.png").Output() | ||
if err != nil { | ||
panic(err) | ||
} | ||
infile, err := os.Open("jump.png") | ||
if err != nil { | ||
panic(err) | ||
} | ||
src, err := png.Decode(infile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
start, end := jump.Find(src) | ||
if start == nil { | ||
log.Print("can't find the starting point,please export the debugger directory") | ||
break | ||
} else if end == nil { | ||
log.Print("can't find the end point,please export the debugger directory") | ||
break | ||
} | ||
|
||
ms := int(math.Pow(math.Pow(float64(start[0]-end[0]), 2)+math.Pow(float64(start[1]-end[1]), 2), 0.5) * ratio) | ||
log.Printf("from:%v to:%v press:%vms", start, end, ms) | ||
|
||
_, err = exec.Command("/system/bin/sh", "/system/bin/input", "swipe", "320", "410", "320", "410", strconv.Itoa(ms)).Output() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
infile.Close() | ||
time.Sleep(time.Millisecond * 1500) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"image/png" | ||
"log" | ||
"math" | ||
"os" | ||
"runtime/debug" | ||
"time" | ||
|
||
jump "github.com/faceair/youjumpijump" | ||
) | ||
|
||
type ScreenshotRes struct { | ||
Value string `json:"value"` | ||
SessionID string `json:"sessionId"` | ||
Status int `json:"status"` | ||
} | ||
|
||
func main() { | ||
defer func() { | ||
jump.Debugger() | ||
if e := recover(); e != nil { | ||
log.Printf("%s: %s", e, debug.Stack()) | ||
fmt.Print("程序已崩溃,按任意键退出") | ||
var c string | ||
fmt.Scanln(&c) | ||
} | ||
}() | ||
|
||
var ip string | ||
fmt.Print("请输入 WebDriverAgentRunner 监听的 IP 和端口 (例如 192.168.9.94:8100):") | ||
_, err := fmt.Scanln(&ip) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var ratio float64 | ||
fmt.Print("请输入跳跃系数(推荐值 2,可适当调整):") | ||
_, err = fmt.Scanln(&ratio) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("现在跳跃系数是 %f", ratio) | ||
|
||
r := jump.NewRequest() | ||
|
||
for { | ||
jump.Debugger() | ||
|
||
_, body, err := r.Get(fmt.Sprintf("http://%s/screenshot", ip)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
scr := new(ScreenshotRes) | ||
err = json.Unmarshal(body, scr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
pngValue, err := base64.StdEncoding.DecodeString(scr.Value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
src, err := png.Decode(bytes.NewReader(pngValue)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
f, _ := os.OpenFile("jump.png", os.O_WRONLY|os.O_CREATE, 0600) | ||
png.Encode(f, src) | ||
f.Close() | ||
|
||
start, end := jump.Find(src) | ||
if start == nil { | ||
log.Print("找不到起点,请把 debugger 目录打包发给开发者检查问题。") | ||
break | ||
} else if end == nil { | ||
log.Print("找不到落脚点,请把 debugger 目录打包发给开发者检查问题。") | ||
break | ||
} | ||
|
||
ms := float64(math.Pow(math.Pow(float64(start[0]-end[0]), 2)+math.Pow(float64(start[1]-end[1]), 2), 0.5) * ratio) | ||
log.Printf("from:%v to:%v press:%vms", start, end, ms) | ||
|
||
_, _, err = r.PostJSON(fmt.Sprintf("http://%s/session/%s/wda/touchAndHold", ip, scr.SessionID), map[string]interface{}{ | ||
"x": 200, | ||
"y": 200, | ||
"duration": ms / 1000, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
time.Sleep(time.Millisecond * 1500) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package jump | ||
|
||
import ( | ||
"image" | ||
"image/color" | ||
"image/png" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"math" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/nfnt/resize" | ||
) | ||
|
||
var basePath string | ||
|
||
func init() { | ||
ex, err := os.Executable() | ||
if err != nil { | ||
panic(err) | ||
} | ||
basePath = filepath.Dir(ex) | ||
|
||
if ok, _ := Exists(basePath + "/debugger"); !ok { | ||
os.MkdirAll(basePath+"/debugger", os.ModePerm) | ||
} | ||
|
||
os.Remove(basePath + "/debugger/debug.log") | ||
logFile, _ := os.OpenFile(basePath+"/debugger/debug.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) | ||
log.SetOutput(io.MultiWriter(os.Stdout, logFile)) | ||
} | ||
|
||
func Debugger() { | ||
if ok, _ := Exists(basePath + "/jump.png"); ok { | ||
os.Rename(basePath+"/jump.png", basePath+"/debugger/"+strconv.Itoa(TimeStamp())+".png") | ||
|
||
files, err := ioutil.ReadDir(basePath + "/debugger/") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, f := range files { | ||
fname := f.Name() | ||
ext := filepath.Ext(fname) | ||
name := fname[0 : len(fname)-len(ext)] | ||
if ts, err := strconv.Atoi(name); err == nil { | ||
if TimeStamp()-ts > 10 { | ||
os.Remove(basePath + "/debugger/" + fname) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func Exists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return true, err | ||
} | ||
|
||
func TimeStamp() int { | ||
return int(time.Now().UnixNano() / int64(time.Second)) | ||
} | ||
|
||
func getRGB(m color.Model, c color.Color) [3]int { | ||
if m == color.RGBAModel { | ||
return [3]int{int(c.(color.RGBA).R), int(c.(color.RGBA).G), int(c.(color.RGBA).B)} | ||
} else if m == color.RGBA64Model { | ||
return [3]int{int(c.(color.RGBA64).R), int(c.(color.RGBA64).G), int(c.(color.RGBA64).B)} | ||
} else if m == color.NRGBAModel { | ||
return [3]int{int(c.(color.NRGBA).R), int(c.(color.NRGBA).G), int(c.(color.NRGBA).B)} | ||
} else if m == color.NRGBA64Model { | ||
return [3]int{int(c.(color.NRGBA64).R), int(c.(color.NRGBA64).G), int(c.(color.NRGBA64).B)} | ||
} | ||
return [3]int{0, 0, 0} | ||
} | ||
|
||
func colorSimilar(a, b [3]int, distance float64) bool { | ||
return (math.Abs(float64(a[0]-b[0])) < distance) && (math.Abs(float64(a[1]-b[1])) < distance) && (math.Abs(float64(a[2]-b[2])) < distance) | ||
} | ||
|
||
func Find(src image.Image) ([]int, []int) { | ||
src = resize.Resize(720, 0, src, resize.Lanczos3) | ||
f, _ := os.OpenFile("jump.720.png", os.O_WRONLY|os.O_CREATE, 0600) | ||
png.Encode(f, src) | ||
f.Close() | ||
|
||
bounds := src.Bounds() | ||
w, h := bounds.Max.X, bounds.Max.Y | ||
|
||
jumpCubeColor := [3]int{54, 52, 92} | ||
points := [][]int{} | ||
for y := 0; y < h; y++ { | ||
line := 0 | ||
for x := 0; x < w; x++ { | ||
c := src.At(x, y) | ||
if colorSimilar(getRGB(src.ColorModel(), c), jumpCubeColor, 20) { | ||
line++ | ||
} else { | ||
if y > 300 && x-line > 10 && line > 30 { | ||
points = append(points, []int{x - line/2, y, line}) | ||
} | ||
line = 0 | ||
} | ||
} | ||
} | ||
jumpCube := []int{0, 0, 0} | ||
for _, point := range points { | ||
if point[2] > jumpCube[2] { | ||
jumpCube = point | ||
} | ||
} | ||
jumpCube = []int{jumpCube[0], jumpCube[1]} | ||
if jumpCube[0] == 0 { | ||
return nil, nil | ||
} | ||
|
||
possible := [][]int{} | ||
for y := 0; y < h; y++ { | ||
line := 0 | ||
bgColor := getRGB(src.ColorModel(), src.At(w-10, y)) | ||
for x := 0; x < w; x++ { | ||
c := src.At(x, y) | ||
if !colorSimilar(getRGB(src.ColorModel(), c), bgColor, 5) { | ||
line++ | ||
} else { | ||
if y > 300 && x-line > 10 && line > 35 && ((x-line/2) < (jumpCube[0]-20) || (x-line/2) > (jumpCube[0]+20)) { | ||
possible = append(possible, []int{x - line/2, y, line, x}) | ||
} | ||
line = 0 | ||
} | ||
} | ||
} | ||
if len(possible) == 0 { | ||
return jumpCube, nil | ||
} | ||
target := possible[0] | ||
for _, point := range possible { | ||
if point[3] > target[3] && point[1]-target[1] <= 5 { | ||
target = point | ||
} | ||
} | ||
target = []int{target[0], target[1]} | ||
|
||
return jumpCube, target | ||
} |
Oops, something went wrong.