libmcpil/libmcpil.go

83 lines
2.2 KiB
Go

package libmcpil
import (
"strings"
"os/exec"
"os"
)
const Version = "1.0"
// Fetch the options from an executable_path as a map.
// executable_path must be a string with an executable
// that provides --print-available-feature-flags and its
// functionality.
func GetOptionsMap(executable_path string) map[string]bool {
flags_unformatted, _ := exec.Command(executable_path, "--print-available-feature-flags").Output()
flags_newline_split := strings.Split(string(flags_unformatted), "\n")
//fmt.Println(flags_newline_split)
flags_formatted := map[string]bool{}
for i := 0; i <= len(flags_newline_split)-1; i++ {
flag := flags_newline_split[i]
if strings.HasPrefix(flag, "TRUE") {
flags_formatted[string((flag[5:]))] = true
} else if strings.HasPrefix(flag, "FALSE") {
flags_formatted[string(flag[6:])] = false
}
//fmt.Println(flags_formatted) // Debug
}
//fmt.Println(flags_formatted)
return flags_formatted
}
// Same as GetOptionsMap but returns a slice of option
// names instead of options and their values.
func GetOptionsList (executable_path string) []string {
flags_map := GetOptionsMap(executable_path)
flags_slice := []string{}
for key, _ := range flags_map {
flags_slice = append(flags_slice, key)
}
return flags_slice
}
// Launch the game provided the executable path
// Features must be a slice with strings of enabled features.
//
func Launch (
executable_path string,
username string,
render_distance string,
debug bool,
features []string,
benchmark bool,
) *exec.Cmd {
var executable_args []string // Arguments slice
// Set the arguments
if debug{
executable_args = append(executable_args, "--debug")
}
if benchmark {
executable_args = append(executable_args, "--benchmark")
}
cmd := exec.Command(executable_path, executable_args...) // Executable init
cmd.Env = os.Environ() // This is required, without this Reborn won't work.
features_string := strings.Join(features, "|") // Python equievelent of .join
cmd.Env = append(cmd.Env, // Append the enviroment variables
"MCPI_USERNAME="+username,
"MCPI_RENDER_DISTANCE="+render_distance,
"MCPI_FEATURE_FLAGS="+features_string,
)
// Return the Cmd object to let the user do anything they want with it
return cmd
}