-
Notifications
You must be signed in to change notification settings - Fork 4
/
export_js.go
155 lines (129 loc) · 3.6 KB
/
export_js.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
// +build js
package main
import (
"archive/zip"
"bytes"
"compress/flate"
"fmt"
"io"
"io/ioutil"
"net/http"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/therecipe/qt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
)
var zipCache = make(map[string][]byte)
func copyWithProgress(w io.Writer, r io.Reader, callback func(off int64)) error {
tee := io.TeeReader(r, w)
buf := make([]byte, bytes.MinRead*10)
off := int64(0)
for count := 0; ; count++ {
n, err := tee.Read(buf)
off += int64(n)
if count%100 == 0 {
callback(off)
}
if err == io.EOF {
break
}
if err != nil {
return err
}
}
callback(off)
return nil
}
func qtsched() {
core.QCoreApplication_ProcessEvents(core.QEventLoop__AllEvents)
runtime.Gosched()
time.Sleep(1 * time.Millisecond)
}
func patchZip(t string, pay []byte, b *widgets.QPushButton) {
b.SetDisabled(true)
defer func() {
b.SetEnabled(true)
b.SetStyleSheet("")
}()
if _, ok := zipCache[t]; !ok {
b.SetText("(1/3) Downloading " + t + ".zip")
qtsched()
resp, err := http.Get(t + ".zip")
if err != nil {
b.SetText(fmt.Sprint("Failed to download zip: " + err.Error()))
return
}
buf := new(bytes.Buffer)
copyWithProgress(buf, resp.Body, func(off int64) {
b.SetText(fmt.Sprintf("(1/3) Downloading %v%%", off/(resp.ContentLength/100)))
qtsched()
})
resp.Body.Close()
zipCache[t] = buf.Bytes()
}
b.SetText("(2/3) Preparing " + t + ".zip")
qtsched()
wb := new(bytes.Buffer)
r, _ := zip.NewReader(bytes.NewReader(zipCache[t]), int64(len(zipCache[t])))
w := zip.NewWriter(wb)
w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) { return flate.NewWriter(out, flate.NoCompression) })
for i, f := range r.File {
fr, _ := f.Open()
fw, _ := w.CreateHeader(&f.FileHeader)
if strings.HasPrefix(filepath.Base(f.Name), "entry") ||
filepath.Base(f.Name) == "go.wasm" || filepath.Base(f.Name) == "go.js" {
data, _ := ioutil.ReadAll(fr)
m := "MAIN"
if false { //TODO: support bootloader patching ?
m = "BOOT"
}
start := bytes.Index(data, []byte("_STARTOF"+m+"_"))
if start != -1 {
off := start
lendiff := (maxPayloadLen - (startIndex + endIndex)) - len(pay)
if lendiff < 0 {
println("FAILED")
return
}
b.SetText("(2/3) Patching binary for " + t)
qtsched()
copy(data[off+startIndex:off+startIndex+len(pay)+lendiff], append(pay, bytes.Repeat([]byte{0}, lendiff)...))
b.SetText("(2/3) Packing binary for " + t)
qtsched()
copyWithProgress(fw, bytes.NewReader(data), func(off int64) {
b.SetText(fmt.Sprintf("(3/3) Packing %v/%v %v%%", i, len(r.File), off/(int64(len(data))/100)))
qtsched()
})
}
} else {
b.SetText(fmt.Sprintf("(3/3) Packing %v/%v", i, len(r.File)))
percent := (float64(i) / (float64(len(r.File)) / float64(100)) / float64(100))
b.SetStyleSheet(fmt.Sprintf("background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop: 0 #1bb77b, stop: %v #1bb77b, stop: %v rgba(0, 0, 0, 0), stop: 1 white)", percent, percent+0.001))
qtsched()
io.Copy(fw, fr)
}
fr.Close()
}
w.Close()
b.SetText("(3/3) Preparing download of " + t + ".zip")
qtsched()
//ta := qt.TypedArrayOf(wb.Bytes()) //TODO: wasm support
qt.Global.Call("saveAs", qt.Global.Get("Blob").New([]interface{}{wb.Bytes()}, map[string]interface{}{"type": "application/octet-stream"}), t+".zip")
//ta.Release() //TODO: wasm support
switch t {
case "windows":
t = "Windows"
case "linux":
t = "Linux"
case "darwin":
t = "macOS"
case "windowsxp":
t = "WindowsXP"
case "wasm":
t = "WASM"
}
b.SetText("Deploy for " + t)
}