Exemple #1
0
func LuaEncodeTable(w io.Writer, L *lua.State, index int) error {
	L.PushNil()
	for L.Next(index-1) != 0 {
		binary.Write(w, binary.LittleEndian, byte(1))
		err := LuaEncodeValue(w, L, -2)
		if err != nil {
			return err
		}
		err = LuaEncodeValue(w, L, -1)
		if err != nil {
			return err
		}
		L.Pop(1)
	}
	return binary.Write(w, binary.LittleEndian, byte(0))
}
Exemple #2
0
func LuaStringifyParam(L *lua.State, index int) string {
	if L.IsTable(index) {
		str := "table <not implemented> {"
		return str
		first := true
		L.PushNil()
		for L.Next(index-1) != 0 {
			if !first {
				str += ", "
			}
			first = false
			str += fmt.Sprintf("(%s) -> (%s)", LuaStringifyParam(L, -2), LuaStringifyParam(L, -1))
			L.Pop(1)
		}
		return str + "}"
	}
	if L.IsBoolean(index) {
		if L.ToBoolean(index) {
			return "true"
		}
		return "false"
	}
	return L.ToString(index)
}
Exemple #3
0
func LuaCheckParamsOk(L *lua.State, name string, params ...LuaType) bool {
	fmt.Sprintf("%s(")
	n := L.GetTop()
	if n != len(params) {
		LuaDoError(L, fmt.Sprintf("Got %d parameters to %s.", n, luaMakeSigniature(name, params)))
		return false
	}
	for i := -n; i < 0; i++ {
		ok := false
		switch params[i+n] {
		case LuaInteger:
			ok = L.IsNumber(i)
		case LuaFloat:
			ok = L.IsNumber(i)
		case LuaBoolean:
			ok = L.IsBoolean(i)
		case LuaString:
			ok = L.IsString(i)
		case LuaEntity:
			if L.IsTable(i) {
				L.PushNil()
				for L.Next(i-1) != 0 {
					if L.ToString(-2) == "type" && L.ToString(-1) == "Entity" {
						ok = true
					}
					L.Pop(1)
				}
			}
		case LuaPoint:
			if L.IsTable(i) {
				var x, y bool
				L.PushNil()
				for L.Next(i-1) != 0 {
					if L.ToString(-2) == "X" {
						x = true
					}
					if L.ToString(-2) == "Y" {
						y = true
					}
					L.Pop(1)
				}
				ok = x && y
			}
		case LuaRoom:
			if L.IsTable(i) {
				var floor, room, door bool
				L.PushNil()
				for L.Next(i-1) != 0 {
					switch L.ToString(-2) {
					case "floor":
						floor = true
					case "room":
						room = true
					case "door":
						door = true
					}
					L.Pop(1)
				}
				ok = floor && room && !door
			}
		case LuaDoor:
			if L.IsTable(i) {
				var floor, room, door bool
				L.PushNil()
				for L.Next(i-1) != 0 {
					switch L.ToString(-2) {
					case "floor":
						floor = true
					case "room":
						room = true
					case "door":
						door = true
					}
					L.Pop(1)
				}
				ok = floor && room && door
			}
		case LuaSpawnPoint:
			if L.IsTable(i) {
				L.PushNil()
				for L.Next(i-1) != 0 {
					if L.ToString(-2) == "type" && L.ToString(-1) == "SpawnPoint" {
						ok = true
					}
					L.Pop(1)
				}
			}
		case LuaArray:
			// Make sure that all of the indices 1..length are there, and no others.
			check := make(map[int]int)
			if L.IsTable(i) {
				L.PushNil()
				for L.Next(i-1) != 0 {
					if L.IsNumber(-2) {
						check[L.ToInteger(-2)]++
					} else {
						break
					}
					L.Pop(1)
				}
			}
			count := 0
			for i := 1; i <= len(check); i++ {
				if _, ok := check[i]; ok {
					count++
				}
			}
			ok = (count == len(check))
		case LuaTable:
			ok = L.IsTable(i)
		case LuaAnything:
			ok = true
		}
		if !ok {
			LuaDoError(L, fmt.Sprintf("Unexpected parameters to %s.", luaMakeSigniature(name, params)))
			return false
		}
	}
	return true
}