/
c.go
241 lines (199 loc) · 4.37 KB
/
c.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
package main
/*
#include <stdlib.h>
#include <debugapi.h>
*/
import "C"
import (
"bytes"
"container/list"
"context"
"os"
"sync"
"time"
"unsafe"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/fsnotify/fsnotify"
)
var (
funcQueue = &strQueue{l: list.New(), mtx: new(sync.Mutex)}
currentAlloc *C.char
lastError *C.char
appCtx context.Context
appCtxCancel func()
funcMtx = new(sync.Mutex)
activeCnf *config
)
type strQueue struct {
l *list.List
mtx *sync.Mutex
}
func (q *strQueue) Enq(v string) {
q.mtx.Lock()
defer q.mtx.Unlock()
q.l.PushBack(v)
}
func (q *strQueue) Deq() (string, bool) {
q.mtx.Lock()
defer q.mtx.Unlock()
if q.l.Len() == 0 {
return "", false
}
e := q.l.Front()
q.l.Remove(e)
return e.Value.(string), true
}
func debugLog(str string) {
debug := C.CString(str)
defer C.free(unsafe.Pointer(debug))
C.OutputDebugStringA(debug)
}
func enqueueEvent(fnc string) {
app.logger.Debug("setCurrentFunc", zap.String("func", fnc))
funcQueue.Enq(fnc)
}
func cSetStr(ppStorage **C.char, val string) *C.char {
if *ppStorage != nil {
C.free(unsafe.Pointer(*ppStorage))
}
*ppStorage = C.CString(val)
return *ppStorage
}
func cError(err error) *C.char {
return cSetStr(&lastError, err.Error())
}
func cErrorS(err string) *C.char {
return cSetStr(&lastError, err)
}
type multiLogger []logger
func (m *multiLogger) Printf(format string, args ...interface{}) {
for _, l := range *m {
l.Printf(format, args...)
}
}
type loggerFn func(format string, args ...interface{})
func (l loggerFn) Printf(format string, args ...interface{}) {
l(format, args...)
}
type logger interface {
Printf(format string, args ...interface{})
}
type outputDebugStringAWriter struct {
back bytes.Buffer
}
func (w *outputDebugStringAWriter) Write(data []byte) (int, error) {
n, err := w.back.Write(data)
idx := bytes.IndexByte(w.back.Bytes(), '\n')
for idx != -1 {
debugLog(w.back.String()[:idx])
rem := w.back.Bytes()[idx+1:]
w.back.Reset()
w.back.Write(rem)
idx = bytes.IndexByte(w.back.Bytes(), '\n')
}
return n, err
}
type App struct {
logger *zap.Logger
configMtx sync.Mutex
}
var app = &App{}
func (a *App) ReplaceConfig(c config) {
a.configMtx.Lock()
defer a.configMtx.Unlock()
if activeCnf == nil {
return
}
*activeCnf = c
}
func (a *App) GetConfig() config {
a.configMtx.Lock()
defer a.configMtx.Unlock()
return *activeCnf
}
func loadConfig() error {
cnf, err := readConfigJson()
if err != nil {
return err
}
app.ReplaceConfig(cnf)
return nil
}
//export cInitIntegration
func cInitIntegration() *C.char {
funcMtx.Lock()
defer funcMtx.Unlock()
if appCtxCancel != nil {
return cErrorS("already running")
}
logFile, err := os.OpenFile("System/twitch-integration.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0640)
out := zapcore.AddSync(&outputDebugStringAWriter{})
if err == nil {
out = zap.CombineWriteSyncers(logFile, out)
}
zapCore := zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
out,
zap.NewAtomicLevelAt(zap.DebugLevel))
logger := zap.New(zapCore)
defer logger.Sync()
app.logger = logger
enqueueEvent("INIT")
cnf, err := readConfigJson()
if err != nil {
return cError(err)
}
activeCnf = &cnf
appCtx, appCtxCancel = context.WithCancel(context.Background())
go func() {
if logFile != nil {
defer logFile.Close()
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Warn("failed to setup fsnotify to support config hot-reloading")
} else {
defer watcher.Close()
watchForConfigChanges(appCtx, watcher, logger)
}
for {
if err := handleEventPipe(appCtx, app, logger); err != nil {
logger.Error("failed to run", zap.Error(err))
}
<-time.After(time.Second * 5)
}
}()
enqueueEvent("START")
return cErrorS("")
}
//export cShutdownIntegration
func cShutdownIntegration() *C.char {
funcMtx.Lock()
defer funcMtx.Unlock()
if appCtxCancel == nil {
return cErrorS("not running")
}
appCtxCancel()
appCtxCancel = nil
appCtx = nil
activeCnf = nil
return cErrorS("")
}
//export cHandleEvents
func cHandleEvents() *C.char {
funcMtx.Lock()
defer funcMtx.Unlock()
if appCtx == nil {
return cErrorS("not running")
}
cnf := activeCnf
if cnf == nil {
return cErrorS("no config")
}
fn, ok := funcQueue.Deq()
if ok {
app.logger.Info("func dequeued", zap.String("func", fn))
}
return cSetStr(¤tAlloc, fn)
}