Created
March 21, 2023 06:38
-
-
Save TotallyNotAHaxxer/dcb89e0a5cc2e285f6926abc3b1edebc to your computer and use it in GitHub Desktop.
Example using user input to create a console for modules and other sets of systems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
type ScriptSettings struct { | |
PlayerAmmunition int64 | |
PlayerHealth int64 | |
} | |
var s ScriptSettings | |
func SetPlayerAmmunition(ammunition int64) bool { | |
s.PlayerAmmunition = ammunition | |
fmt.Println("[+] Player ammo set : ", s.PlayerAmmunition) | |
return true | |
} | |
func SetPlayerHealth(health int64) bool { | |
s.PlayerHealth = health | |
fmt.Println("[+] Player health set : ", s.PlayerHealth) | |
return true | |
} | |
var Modwhat = map[string]func(int64) bool{ | |
"health": SetPlayerHealth, | |
"ammunition": SetPlayerAmmunition, | |
} | |
func GetHealth() string { | |
return "[+] Player health is at -> " + fmt.Sprint(s.PlayerHealth) | |
} | |
func GetAmmo() string { | |
return "[+] Player ammo is at -> " + fmt.Sprint(s.PlayerAmmunition) | |
} | |
var ViewWhat = map[string]func() string{ | |
"health": GetHealth, | |
"ammo": GetAmmo, | |
} | |
// Modules or commands that initate the modules modification | |
var ScriptModule = map[string]bool{ | |
"set": true, | |
} | |
var ViewModule = map[string]bool{ | |
"view": true, | |
} | |
// Start anonymous function | |
// give player default data | |
func init() { | |
s.PlayerAmmunition = 50 // set ammo to 50 | |
s.PlayerHealth = 88 // Set health to 88 | |
} | |
func main() { | |
// Start console | |
fmt.Println("[+] Hello there! Welcome to a base example of code changing") | |
fmt.Println("[*] ------------------------------------------------------- [*]") | |
fmt.Println("[+] Player health -> ", s.PlayerHealth) // Call to output players health | |
fmt.Println("[+] Player Ammo -> ", s.PlayerAmmunition) // Call to output players ammunition | |
fmt.Println("[*] ------------------------------------------------------- [*]") | |
var input string | |
IN := bufio.NewReader(os.Stdin) // Read from Operating System STANDARD INPUT | |
fmt.Print("Command > ") // Command prompt | |
for { | |
input, _ = IN.ReadString('\n') // Tell it to read input until a new line is started | |
input = strings.Replace(input, "\n", "", -1) // Replace the input state | |
// check if the payload is empty | |
if len(input) == 0 || len(input) != -1 { | |
strindex := strings.Index(input, " ") // Index the input for the space because the second space will be the next module | |
if strindex == -1 { | |
continue // if the index is less than one | |
} | |
if _, ok := ScriptModule[input[0:strindex]]; ok { | |
splitter := strings.Split(input[strindex:], "=") | |
if splitter[0] != "" && splitter[1] != "" { | |
mod := Modwhat[strings.TrimSpace(splitter[0])] | |
if mod != nil { | |
i, _ := strconv.ParseInt(splitter[1], 0, 64) | |
mod(i) | |
} | |
} | |
} | |
if _, ok := ViewModule[input[0:strindex]]; ok { | |
splitter := strings.Split(input[strindex:], " ") | |
if splitter[1] != "" { | |
viewport := ViewWhat[splitter[1]] | |
if viewport != nil { | |
fmt.Println(viewport()) | |
} | |
} | |
} | |
} else { | |
fmt.Println("[-] No input detected....try again") | |
} | |
fmt.Print("Command > ") // Command prompt | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment