-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
KodularStarter.v
119 lines (103 loc) · 2.9 KB
/
KodularStarter.v
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
import binary
import os
import vweb
const (
version = 2
companion_pkg_name = 'io.makeroid.companion'
port = 8004
adb_path = binary.extract_exe()
)
struct App {
pub mut:
vweb vweb.Context
}
fn main() {
C.atexit(kill_adb)
print_info()
vweb.run<App>(port)
}
pub fn (mut app App) init_once() {
println('Hit Ctrl+C to quit.')
println('- '.repeat(22))
println('[INFO] Turn USB debugging on from the Developer Options on your phone')
}
pub fn (mut app App) init() {
}
fn (mut app App) set_cors_headers() {
app.vweb.add_header('Access-Control-Allow-Origin', '*')
app.vweb.add_header('Access-Control-Allow-Headers', 'origin, content-type')
}
pub fn (mut app App) index() vweb.Result {
app.set_cors_headers()
return app.vweb.text('Hello VVorld :V')
}
['/ping']
pub fn (mut app App) ping() vweb.Result {
app.set_cors_headers()
return app.vweb.json('{"status":"OK","version":$version}')
}
['/utest']
pub fn (mut app App) utest() vweb.Result {
app.set_cors_headers()
println('[INFO] Testing...')
device := get_device() or {
println('[ERROR] Test failed!')
return app.vweb.json('{"status":"NO","version":$version}')
}
return app.vweb.json('{"status":"OK","version":$version,"device":"$device"}')
}
['/ucheck']
pub fn (mut app App) ucheck() vweb.Result {
app.set_cors_headers()
device := get_device() or { return app.vweb.json('{"status":"NO","version":$version}') }
return app.vweb.json('{"status":"OK","version":$version,"device":"$device"}')
}
['/reset']
pub fn (mut app App) reset() vweb.Result {
app.set_cors_headers()
println('[INFO] Resetting...')
kill_adb()
return app.vweb.json('{"status":"OK","version":$version}')
}
['/replstart/:deviceid']
pub fn (mut app App) replstart(deviceid string) vweb.Result {
print('>> Starting companion app on device [$deviceid] (Keep your phone connected via USB)')
start_companion(deviceid)
return app.vweb.text('')
}
fn get_device() ?string {
result := os.exec('$adb_path devices') or {
println('[ERROR] Failed to retrieve connected devices!')
return none
}
lines := result.output.split_into_lines()
for line in lines {
if line.starts_with('*') || line.starts_with('emulator') || 'offline' in line {
continue
}
if line.ends_with('device') {
device_id := line.all_before('\t')
println('[INFO] Device found = $device_id')
return device_id
}
}
println('[ERROR] No devices connected!')
return none
}
fn start_companion(deviceid string) {
os.system('$adb_path -s $deviceid forward tcp:8001 tcp:8001')
os.system('$adb_path -s $deviceid shell am start -a android.intent.action.VIEW -n $companion_pkg_name/.Screen1 --ez rundirect true')
}
fn kill_adb() {
os.exec('$adb_path kill-server') or {
println('[ERROR] Failed to kill adb!')
return
}
println('[INFO] Killed adb...')
}
fn print_info() {
println('Kodular Starter v$version')
println('OS: $os.user_os()')
println('ADB path: $adb_path')
println('- '.repeat(22))
}