-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
378 lines (325 loc) · 11.4 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// +build !js wasm
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"unsafe"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/widgets"
)
func main() {
isDev = len(mainPayload) == maxPayloadLen || wasDev
if !parsedFlags && !isInsideBrowser && !isTesting {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
isDevRef := flag.Bool("edit", len(mainPayload) == maxPayloadLen, "enable dev console")
saveBoot := flagAsBoolOrString("save-boot", "save the boot payload to boot.js or the path specified")
loadBoot := flagAsBoolOrString("load-boot", "load the boot payload from boot.js or the path specified")
patchBoot := flagAsBoolOrString("patch-boot", "patch the boot payload from boot.js or the path specified")
resetBoot := flag.Bool("reset-boot", false, "resets the boot payload to a safe default")
defaultBoot := flag.Bool("default-boot", false, "start the application with the default boot payload")
skipBootRef := flag.Bool("skip-boot", false, "skip the boot payload and load the main payload instead (works only with *.js files atm)")
savePayload := flagAsBoolOrString("save-payload", "save the payload to payload.js, payload.qml or the path specified")
loadPayload := flagAsBoolOrString("load-payload", "load the payload from payload.js, payload.qml or the path specified")
patchPayload := flagAsBoolOrString("patch-payload", "patch the payload from payload.js, payload.qml or the path specified")
watchBoot := flagAsBoolOrString("watch-boot", "watch boot.js or the path specified for changes")
watchPayload := flagAsBoolOrString("watch-payload", "watch payload.js, payload.qml or the path specified for changes")
errHelp := flag.CommandLine.Parse(os.Args[1:])
if errHelp != nil {
if runtime.GOOS == "windows" {
flag.PrintDefaults()
widgets.NewQApplication(len(os.Args), os.Args)
bb := new(bytes.Buffer)
flag.CommandLine.SetOutput(bb)
flag.PrintDefaults()
help := bb.String()
help = strings.Replace(help, "\n \t", "\n", -1)
help = strings.Replace(help, " -", "\n-", -1)
help = fmt.Sprintf("Usage of %s:\n", flag.CommandLine.Name()) + help
if errHelp != flag.ErrHelp {
help = errHelp.Error() + "\n\n" + strings.Repeat("-", 43) + "\n\n" + help
}
widgets.QMessageBox_Information(nil, "OK", help, widgets.QMessageBox__Ok, 0)
}
os.Exit(2)
}
isDev = *isDevRef
skipBoot = *skipBootRef
if flag.NArg() > 0 {
fp := flag.Arg(0)
out, err := ioutil.ReadFile(fp)
if err != nil {
println("failed to load the main payload " + err.Error())
} else {
softMainPayload = string(out)
if strings.HasPrefix(softMainPayload, "#") {
softMainPayload = "//" + softMainPayload
os.Args = append(os.Args, "-watch-payload")
watchPayload = &fp
}
}
}
if hasFlag("save-boot") {
if len(bootPayload) != maxPayloadLen {
out, err := unpackPayload(bootPayload)
if err != nil {
println("failed to decode the boot payload", err.Error())
} else {
fp := filepath.Join(filepath.Dir(os.Args[0]), "boot.js")
if f := *saveBoot; f != "" {
fp = f
}
err := ioutil.WriteFile(fp, out, 0644)
if err != nil {
println("failed to save the boot payload " + err.Error())
}
}
} else {
println("no boot payload found to be saved")
}
}
if hasFlag("load-boot") || hasFlag("patch-boot") || hasFlag("watch-boot") {
fp := filepath.Join(filepath.Dir(os.Args[0]), "boot.js")
if f := *loadBoot; f != "" {
fp = f
} else if f = *patchBoot; f != "" {
fp = f
} else if f = *watchBoot; f != "" {
fp = f
}
out, err := ioutil.ReadFile(fp)
if err != nil {
println("failed to load the boot payload " + err.Error())
} else {
softBootPayload = string(out)
if hasFlag("patch-boot") {
patchSelf(softBootPayload, true, true)
}
}
}
if *defaultBoot || *resetBoot {
if file := NewQFile(":/qml/main.js"); file.Open(core.QIODevice__ReadOnly) {
softBootPayload = file.ReadAll().ConstData()
file.Close()
file.DestroyQFile()
if *resetBoot {
patchSelf(softBootPayload, true, true)
}
}
}
if hasFlag("save-payload") {
if len(mainPayload) != maxPayloadLen {
out, err := unpackPayload(mainPayload)
if err != nil {
println("failed to decode the main payload", err.Error())
} else {
end := "js"
if bytes.Contains(out, []byte("import QtQuick")) {
end = "qml"
}
fp := filepath.Join(filepath.Dir(os.Args[0]), "payload."+end)
if f := *savePayload; f != "" {
fp = f
}
if !bytes.HasPrefix(out, []byte("//#!")) {
abs, _ := filepath.Abs(os.Args[0])
out = append([]byte("#!"+abs+"\n"), out...)
}
err := ioutil.WriteFile(fp, out, 0755)
if err != nil {
println("failed to save the main payload " + err.Error())
}
}
} else {
println("no main payload found to be saved")
}
}
if hasFlag("load-payload") || hasFlag("patch-payload") || hasFlag("watch-payload") {
end := "js"
if _, err := os.Stat(filepath.Join(filepath.Dir(os.Args[0]), "payload."+end)); err != nil {
end = "qml"
}
fp := filepath.Join(filepath.Dir(os.Args[0]), "payload."+end)
if f := *loadPayload; f != "" {
fp = f
} else if f = *patchPayload; f != "" {
fp = f
} else if f = *watchPayload; f != "" {
fp = f
}
out, err := ioutil.ReadFile(fp)
if err != nil {
println("failed to load the main payload " + err.Error())
} else {
softMainPayload = string(out)
if strings.HasPrefix(softMainPayload, "#") {
softMainPayload = "//" + softMainPayload
}
if hasFlag("patch-payload") {
patchSelf(softMainPayload, true, false)
}
}
}
if !isDev && !(hasFlag("skip-boot") && skipBoot == false) {
//TODO: support non bootloaded qml applications
var payload []byte
if softMainPayload != "" {
payload = []byte(softMainPayload)
} else {
out, _ := unpackPayload(mainPayload)
payload = out
}
skipBoot = !(bytes.Contains(payload, []byte("mport QtQuick")) && !bytes.Contains(payload, []byte("includes(\"mport QtQuick\")")))
}
if skipBoot {
if softMainPayload != "" {
softBootPayload = softMainPayload
} else {
out, _ := unpackPayload(mainPayload)
softBootPayload = string(out)
}
}
if hasFlag("watch-boot") {
watchBootFunc(*watchBoot)
}
if hasFlag("watch-payload") {
watchPayloadFunc(*watchPayload)
}
if hasFlag("save-boot") || hasFlag("save-payload") ||
hasFlag("patch-boot") || hasFlag("patch-payload") ||
*resetBoot {
return
}
}
parsedFlags = true
if isDev {
wasDev = true
}
//
if !isTesting {
app = widgets.NewQApplication(len(os.Args), os.Args)
}
mainThreadHelperInstance = NewMainThreadHelper(app)
engine := qml.NewQJSEngine()
engine.InstallExtensions(qml.QJSEngine__AllExtensions, engine.GlobalObject())
engine.GlobalObject().SetProperty("isMain", engine.NewGoType(!isInsideBrowser))
engine.GlobalObject().SetProperty("bootPayload", engine.NewGoType(""))
engine.GlobalObject().SetProperty("bootPayloadDirty", engine.NewGoType(""))
engine.GlobalObject().SetProperty("mainPayload", engine.NewGoType(""))
engine.GlobalObject().SetProperty("isDev", engine.NewGoType(isDev))
engine.GlobalObject().SetProperty("isInsideBrowser", engine.NewGoType(isInsideBrowser))
engine.GlobalObject().SetProperty("isBrowserJSEngine", engine.NewGoType(isBrowserJSEngine))
engine.GlobalObject().SetProperty("isWinXP", engine.NewGoType(isWinXP))
engine.GlobalObject().SetProperty("isDeployed", engine.NewGoType(len(mainPayload) != maxPayloadLen || softMainPayload != ""))
engine.GlobalObject().SetProperty("recursionCounter", engine.NewGoType(0))
engine.GlobalObject().SetProperty("isTesting", engine.NewGoType(isTesting))
engine.GlobalObject().SetProperty("isTestingVisible", engine.NewGoType(isTestingVisible))
engine.NewGoType("qt.runSync", runSync)
engine.NewGoType("qt.runAsync", runAsync)
engine.NewGoType("qt.setInterval", setInterval)
engine.NewGoType("qt.inspect", inspect)
engine.NewGoType("qt.sleep", sleep)
engine.NewGoType("main.restart", restart)
engine.NewGoType("main.watchBoot", watchBootFunc)
engine.NewGoType("main.watchPayload", watchPayloadFunc)
engine.NewGoType("main.updateEngineReference", updateEngineReference)
engine.NewGoType("main.deploy", deploy)
engine.NewGoType("main.patchSelf", patchSelf)
if len(mainPayload) != maxPayloadLen || softMainPayload != "" {
if softMainPayload != "" {
engine.GlobalObject().SetProperty("mainPayload", engine.NewGoType(softMainPayload))
} else {
out, _ := unpackPayload(mainPayload)
engine.GlobalObject().SetProperty("mainPayload", engine.NewGoType(string(out)))
}
}
var window *widgets.QMainWindow
if file := NewQFile(":/qml/main.js"); file.Open(core.QIODevice__ReadOnly) {
var e *qml.QJSValue
if len(bootPayload) != maxPayloadLen || softBootPayload != "" {
if softBootPayload != "" {
engine.GlobalObject().SetProperty("bootPayload", engine.NewGoType(softBootPayload))
e = engine.Evaluate(softBootPayload, "main.js", 0)
} else {
out, _ := unpackPayload(bootPayload)
engine.GlobalObject().SetProperty("bootPayload", engine.NewGoType(string(out)))
e = engine.Evaluate(string(out), "main.js", 0)
}
} else {
e = engine.Evaluate(file.ReadAll().ConstData(), "main.js", 0)
}
if e.IsError() {
if isInsideBrowser {
println("QJSEngine error:", e.ToString())
} else {
widgets.QMessageBox_Critical(nil, "OK", e.ToString()+"\nFalling back to the default boot payload", widgets.QMessageBox__Ok, 0)
}
if softBootPayloadBackup != "" {
engine.GlobalObject().SetProperty("bootPayload", engine.NewGoType(softBootPayloadBackup))
engine.GlobalObject().SetProperty("bootPayloadDirty", engine.NewGoType(softBootPayload))
softBootPayload = ""
e = engine.Evaluate(softBootPayloadBackup, "main.js", 0)
} else {
engine.GlobalObject().SetProperty("bootPayload", engine.NewGoType(""))
engine.GlobalObject().SetProperty("bootPayloadDirty", engine.NewGoType(softBootPayload))
softBootPayload = ""
e = engine.Evaluate(file.ReadAll().ConstData(), "main.js", 0)
}
} else {
window = widgets.NewQMainWindowFromPointer(unsafe.Pointer(uintptr(e.ToVariant().ToULongLong(nil))))
}
file.Close()
file.DestroyQFile()
}
if lastWindowGeo != nil {
window.SetGeometry2(lastWindowGeo[0], lastWindowGeo[1], lastWindowGeo[2], lastWindowGeo[3])
}
window.ConnectMoveEvent(func(event *gui.QMoveEvent) {
window.MoveEventDefault(event)
lastWindowGeo = []int{window.Geometry().X(), window.Geometry().Y(), window.Geometry().Width(), window.Geometry().Height()}
})
window.ConnectResizeEvent(func(event *gui.QResizeEvent) {
window.ResizeEventDefault(event)
lastWindowGeo = []int{window.Geometry().X(), window.Geometry().Y(), window.Geometry().Width(), window.Geometry().Height()}
})
if skipBoot {
window.Show()
}
if isTesting {
for {
select {
case <-restartChan:
goto broke
default:
core.QCoreApplication_ProcessEvents(core.QEventLoop__AllEvents)
}
}
broke:
} else {
var doRestart bool
go func() {
<-restartChan
doRestart = true
app.Exit(0)
}()
ret := widgets.QApplication_Exec()
if !doRestart {
os.Exit(ret)
}
}
window.DestroyQMainWindow()
engine.CollectGarbage()
engine.DestroyQJSEngine()
if !isTesting {
app.DestroyQApplication()
runtime.GC()
main()
}
}