Skip to content

Instantly share code, notes, and snippets.

@CryZe
Created April 17, 2023 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CryZe/1b3fec94287cb82ea4bb9cc77de6b7d7 to your computer and use it in GitHub Desktop.
Save CryZe/1b3fec94287cb82ea4bb9cc77de6b7d7 to your computer and use it in GitHub Desktop.
Bindings for LiveSplit One's Auto Splitting Runtime for TinyGo
package asr
import "time"
type TimerState = uint32
type ProcessId = uint64
type Address = uint64
func decodeString(text string) (*byte, uint) {
return decodeSlice(([]byte)(text))
}
func decodeSlice(buf []byte) (*byte, uint) {
var bufPtr *byte
bufLen := uint(len(buf))
if bufLen == 0 {
bufPtr = nil
} else {
bufPtr = &buf[0]
}
return bufPtr, bufLen
}
// Gets the state that the timer currently is in.
//
//export timer_get_state
func TimerGetState() TimerState
// Starts the timer.
//
//export timer_start
func TimerStart()
// Splits the current segment.
//
//export timer_split
func TimerSplit()
// Resets the timer.
//
//export timer_reset
func TimerReset()
// Sets a custom key value pair. This may be arbitrary information that
// the auto splitter wants to provide for visualization.
//
//export timer_set_variable
func timer_set_variable(
key_ptr *byte,
key_len uint,
value_ptr *byte,
value_len uint,
)
// Sets a custom key value pair. This may be arbitrary information that
// the auto splitter wants to provide for visualization.
func TimerSetVariable(key string, value string) {
keyPtr, keyLen := decodeString(key)
valuePtr, valueLen := decodeString(value)
timer_set_variable(keyPtr, keyLen, valuePtr, valueLen)
}
// Sets the game time.
//
//export timer_set_game_time
func timer_set_game_time(secs int64, nanos int32)
// Sets the game time.
func TimerSetGameTime(time time.Duration) {
nanos := time.Nanoseconds()
secs := nanos / 1_000_000_000
subsec_nanos := nanos % 1_000_000_000
timer_set_game_time(secs, int32(subsec_nanos))
}
// Pauses the game time. This does not pause the timer, only the
// automatic flow of time for the game time.
//
//export timer_pause_game_time
func TimerPauseGameTime()
// Resumes the game time. This does not resume the timer, only the
// automatic flow of time for the game time.
//
//export timer_resume_game_time
func TimerResumeGameTime()
// Attaches to a process based on its name.
//
//export process_attach
func process_attach(name_ptr *byte, name_len uint) ProcessId
// Attaches to a process based on its name.
func ProcessAttach(name string) ProcessId {
namePtr, nameLen := decodeString(name)
return process_attach(namePtr, nameLen)
}
// Detaches from a process.
//
//export process_detach
func ProcessDetach(process ProcessId)
// Checks whether is a process is still open. You should detach from a
// process and stop using it if this returns `false`.
//
//export process_is_open
func ProcessIsOpen(process ProcessId) bool
// Reads memory from a process at the address given. This will write
// the memory to the buffer given. Returns `false` if this fails.
//
//export process_read
func process_read(
process ProcessId,
address Address,
buf_ptr *byte,
buf_len uint,
) bool
// Reads memory from a process at the address given. This will write
// the memory to the buffer given. Returns `false` if this fails.
func ProcessRead(
process ProcessId,
address Address,
buf []byte,
) bool {
bufPtr, bufLen := decodeSlice(buf)
return process_read(process, address, bufPtr, bufLen)
}
// Gets the address of a module in a process.
//
//export process_get_module_address
func process_get_module_address(
process ProcessId,
name_ptr *byte,
name_len uint,
) Address
// Gets the address of a module in a process.
func ProcessGetModuleAddress(process ProcessId, name string) Address {
namePtr, nameLen := decodeString(name)
return process_get_module_address(process, namePtr, nameLen)
}
// Gets the size of a module in a process.
//
//export process_get_module_size
func process_get_module_size(
process ProcessId,
name_ptr *byte,
name_len uint,
) uint64
// Gets the size of a module in a process.
func ProcessGetModuleSize(process ProcessId, name string) uint64 {
namePtr, nameLen := decodeString(name)
return process_get_module_size(process, namePtr, nameLen)
}
// Sets the tick rate of the runtime. This influences the amount of
// times the `update` function is called per second.
//
//export runtime_set_tick_rate
func RuntimeSetTickRate(ticks_per_second float64)
// Prints a log message for debugging purposes.
//
//export runtime_print_message
func runtime_print_message(text_ptr *byte, text_len uint)
// Prints a log message for debugging purposes.
func RuntimePrintMessage(text string) {
textPtr, textLen := decodeString(text)
runtime_print_message(textPtr, textLen)
}
// Stores the name of the operating system that the runtime is running
// on in the buffer given. Returns `false` if the buffer is too small.
// After this call, no matter whether it was successful or not, the
// `buf_len_ptr` will be set to the required buffer size. The name is
// guaranteed to be valid UTF-8 and is not nul-terminated.
// Example values: `windows`, `linux`, `macos`
//
//export runtime_get_os
func runtime_get_os(buf_ptr *byte, buf_len_ptr *uint) bool
// Returns the name of the operating system that the runtime is running on.
// Example values: `windows`, `linux`, `macos`
func RuntimeGetOS() string {
originalBufLen := uint(0)
bufLenPtr := &originalBufLen
runtime_get_os(nil, bufLenPtr)
buf := make([]byte, originalBufLen)
bufPtr, bufLen := decodeSlice(buf)
bufLenPtr = &bufLen
if runtime_get_os(bufPtr, bufLenPtr) {
return string(buf[0:bufLen])
} else {
return ""
}
}
// Stores the name of the architecture that the runtime is running on
// in the buffer given. Returns `false` if the buffer is too small.
// After this call, no matter whether it was successful or not, the
// `buf_len_ptr` will be set to the required buffer size. The name is
// guaranteed to be valid UTF-8 and is not nul-terminated.
// Example values: `x86`, `x86_64`, `arm`, `aarch64`
//
//export runtime_get_arch
func runtime_get_arch(buf_ptr *byte, buf_len_ptr *uint) bool
// Returns the name of the architecture that the runtime is running on. Example
// values: `x86`, `x86_64`, `arm`, `aarch64`
func RuntimeGetArch() string {
originalBufLen := uint(0)
bufLenPtr := &originalBufLen
runtime_get_arch(nil, bufLenPtr)
buf := make([]byte, originalBufLen)
bufPtr, bufLen := decodeSlice(buf)
bufLenPtr = &bufLen
if runtime_get_arch(bufPtr, bufLenPtr) {
return string(buf[0:bufLen])
} else {
return ""
}
}
// Adds a new setting that the user can modify. This will return either
// the specified default value or the value that the user has set.
//
//export user_settings_add_bool
func user_settings_add_bool(
key_ptr *byte,
key_len uint,
description_ptr *byte,
description_len uint,
default_value bool,
) bool
// Adds a new setting that the user can modify. This will return either
// the specified default value or the value that the user has set.
func UserSettingsAddBool(key string, description string, defaultValue bool) bool {
keyPtr, keyLen := decodeString(key)
descriptionPtr, descriptionLen := decodeString(description)
return user_settings_add_bool(
keyPtr, keyLen,
descriptionPtr, descriptionLen,
defaultValue,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment