From 1a7c164690107985059ac0576877bee9247ebeb9 Mon Sep 17 00:00:00 2001 From: cfunder Date: Sun, 25 Sep 2022 20:40:21 -0400 Subject: [PATCH] Library --- .gitignore | 16 +++++++++++ docs/legal.md | 25 ---------------- launcher.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 9 ------ 4 files changed, 96 insertions(+), 34 deletions(-) create mode 100644 .gitignore create mode 100644 launcher.go delete mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..656a0a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + diff --git a/docs/legal.md b/docs/legal.md index 85290eb..f2dc0eb 100644 --- a/docs/legal.md +++ b/docs/legal.md @@ -1,31 +1,6 @@ # Legal When including `libmcpilauncher` in your project, you must include these license notices, additionally with the `libmcpilauncher` license notice at the root of the project: -### subprocess license -``` -MIT License - -Copyright (c) 2018 Christopher Simpkins - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -``` - ### Golang Standart Library licenses ``` The Go source code and supporting files in this directory diff --git a/launcher.go b/launcher.go new file mode 100644 index 0000000..ed22fa5 --- /dev/null +++ b/launcher.go @@ -0,0 +1,80 @@ +package launcher +import ( + "strings" + "fmt" + "os/exec" + "os" +) + +// 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 +} diff --git a/main.go b/main.go deleted file mode 100644 index c956c5c..0000000 --- a/main.go +++ /dev/null @@ -1,9 +0,0 @@ -package libmcpilauncher - -import ( - subprocess "gopkg.in/go-rillas/subprocess.v1" -) - -func GetOptionsMap(excutable_path string) map[string]bool { - -}