Пример #1
0
func (p *Plugin) apiAudioSetTarget(l *lua.State) int {
	if l.GetTop() == 0 {
		p.instance.Client.VoiceTarget = nil
		return 0
	}

	voiceTarget, ok := luar.LuaToGo(l, nil, 1).(*gumble.VoiceTarget)
	if !ok {
		l.PushBoolean(false)
		return 1
	}
	p.instance.Client.Send(voiceTarget)
	p.instance.Client.VoiceTarget = voiceTarget
	return 0
}
Пример #2
0
// LoadConfig parses the Lua file pointed by 'config' and stores it to options.
func LoadConfig(config string, options interface{}) {
	L, err := MakeSandbox(log.Println)
	defer L.Close()

	err = L.DoFile(config)
	if err != nil {
		log.Fatalf("Error loading config: %s", err)
	}

	L.GetGlobal("_G")
	r := luar.LuaToGo(L, reflect.TypeOf(options), -1)
	L.Pop(1)

	v := reflect.ValueOf(options)
	v.Elem().Set(reflect.ValueOf(r).Elem())
}
Пример #3
0
func ExampleMakeChan() {
	L1 := luar.Init()
	defer L1.Close()
	L2 := luar.Init()
	defer L2.Close()

	luar.MakeChan(L1)
	L1.SetGlobal("c")
	L1.GetGlobal("c")
	c := luar.LuaToGo(L1, nil, -1)

	luar.Register(L2, "", luar.Map{
		"c":     c,
		"Print": fmt.Println,
	})

	const code1 = `
c.send(17)
`

	const code2 = `
v = c.recv()
Print(v)
`

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		err := L1.DoString(code1)
		if err != nil {
			fmt.Println(err)
		}
		wg.Done()
	}()

	err := L2.DoString(code2)
	if err != nil {
		fmt.Println(err)
	}
	wg.Wait()
	// Output:
	// 17
}
Пример #4
0
// 'exist' is optional.
func run(L *lua.State, registryIndex string, code string, input *inputInfo, output *outputInfo, exist *inputInfo) error {
	// Restore the sandbox.
	err := L.DoString(luaRestoreSandbox)
	if err != nil {
		log.Fatal("Cannot load function to restore sandbox", err)
	}
	L.PushString(registryWhitelist)
	L.GetTable(lua.LUA_REGISTRYINDEX)
	err = L.Call(1, 0)
	if err != nil {
		log.Fatal("Failed to restore sandbox", err)
	}

	goToLua(L, "input", *input)
	goToLua(L, "output", *output)

	if exist != nil {
		goToLua(L, "existinfo", *exist)
	}

	// Shortcut (mostly for prescript and postscript).
	L.GetGlobal("input")
	L.GetField(-1, "tags")
	L.SetGlobal("i")
	L.Pop(1)
	L.GetGlobal("output")
	L.GetField(-1, "tags")
	L.SetGlobal("o")
	L.Pop(1)

	// Call the compiled script.
	L.PushString(registryIndex)
	L.GetTable(lua.LUA_REGISTRYINDEX)
	L.PushString(code)
	if L.IsTable(-2) {
		L.GetTable(-2)
		if L.IsFunction(-1) {
			err := L.Call(0, 0)
			if err != nil {
				L.SetTop(0)
				return fmt.Errorf("%s", err)
			}
		} else {
			L.Pop(1)
		}
	} else {
		L.Pop(1)
	}
	L.Pop(1)

	// Allow tags to be numbers for convenience.
	outputNumbersToStrings(L)

	L.GetGlobal("output")
	r := luar.LuaToGo(L, reflect.TypeOf(*output), -1)
	L.Pop(1)

	*output = r.(outputInfo)

	return nil
}