This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpiepan.go
83 lines (72 loc) · 1.9 KB
/
piepan.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
package piepan // import "layeh.com/piepan"
import (
"fmt"
"os"
"sync"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
"layeh.com/gumble/gumble"
)
type State struct {
Client *gumble.Client
LState *lua.LState
table *lua.LTable
AudioCommand string
streamMu sync.Mutex
stream *audioStream
mu sync.Mutex
listeners map[string][]lua.LValue
}
func New(args []string) *State {
l := lua.NewState()
state := &State{
LState: l,
listeners: make(map[string][]lua.LValue),
}
t := l.NewTable()
t.RawSetString("On", luar.New(l, state.apiOn))
t.RawSetString("Disconnect", luar.New(l, state.apiDisconnect))
state.table = t
l.SetGlobal("piepan", t)
{
s := l.NewTable()
s.RawSetString("New", luar.New(l, state.apiAudioNew))
s.RawSetString("IsPlaying", luar.New(l, state.apiAudioIsPlaying))
s.RawSetString("Current", luar.New(l, state.apiAudioCurrent))
s.RawSetString("NewTarget", luar.New(l, state.apiAudioNewTarget))
s.RawSetString("SetTarget", luar.New(l, state.apiAudioSetTarget))
s.RawSetString("Bitrate", luar.New(l, state.apiAudioBitrate))
s.RawSetString("SetBitrate", luar.New(l, state.apiAudioSetBitrate))
t.RawSetString("Audio", s)
}
{
s := l.NewTable()
s.RawSetString("New", luar.New(l, state.apiTimerNew))
t.RawSetString("Timer", s)
}
{
s := l.NewTable()
s.RawSetString("New", luar.New(l, state.apiProcessNew))
t.RawSetString("Process", s)
}
{
t.RawSetString("Args", luar.New(l, args))
}
return state
}
func (s *State) LoadFile(filename string) error {
return s.LState.DoFile(filename)
}
func (s *State) callValue(callback lua.LValue, args ...interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
s.LState.Push(callback)
for _, arg := range args {
s.LState.Push(luar.New(s.LState, arg))
}
s.LState.PCall(len(args), 0, s.LState.NewFunction(func(L *lua.LState) int {
fmt.Fprintf(os.Stderr, "%s\n", L.CheckString(1))
return 0
}))
s.LState.SetTop(0)
}