Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3ee9c0f44 | ||
|
|
31140b5df4 | ||
|
|
3857f5983f | ||
|
|
88f50218a4 |
@@ -1,4 +1,32 @@
|
|||||||
# BeamNG SDK
|
# BeamNG SDK
|
||||||
Simple SDK to interact with BeamNG.drive OutGauge data.
|
Simple SDK to interact with BeamNG.drive OutGauge data.
|
||||||
There's some features missing listed in a comment in the
|
|
||||||
`outgauge.go` file. I will implement them as necessary.
|
## Development
|
||||||
|
### Performance
|
||||||
|
`go test -bench=BenchmarkReadData -benchmem -memprofile=mem.pprof`
|
||||||
|
replace the function to be tested
|
||||||
|
|
||||||
|
Use `go tool pprof` to analyze the results
|
||||||
|
|
||||||
|
#### Results
|
||||||
|
```bash
|
||||||
|
# Previous footprint
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: github.com/ESilva15/gobngsdk
|
||||||
|
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
|
||||||
|
BenchmarkReadData-16 320931 4058 ns/op 100 B/op 2 allocs/op
|
||||||
|
PASS
|
||||||
|
ok github.com/ESilva15/gobngsdk 1.345s
|
||||||
|
|
||||||
|
# New footprint
|
||||||
|
goos: linux
|
||||||
|
goarch: amd64
|
||||||
|
pkg: github.com/ESilva15/gobngsdk
|
||||||
|
cpu: AMD Ryzen 7 5800X3D 8-Core Processor
|
||||||
|
BenchmarkReadData-16 362514 3188 ns/op 4 B/op 1 allocs/op
|
||||||
|
PASS
|
||||||
|
ok github.com/ESilva15/gobngsdk 1.194s
|
||||||
|
|
||||||
|
# Pretty good enough. I can finally go be productive instead of "procrastinating" here
|
||||||
|
```
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,11 +18,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BeamNGSDK struct {
|
type BeamNGSDK struct {
|
||||||
Addr *net.UDPAddr
|
Addr *net.UDPAddr
|
||||||
Conn *net.UDPConn
|
Conn *net.UDPConn
|
||||||
Buffer []byte
|
Buffer []byte
|
||||||
Data *Outgauge
|
Data Outgauge
|
||||||
DataDict map[string]any
|
// Internal data
|
||||||
|
reader bytes.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) {
|
func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) {
|
||||||
@@ -40,6 +42,32 @@ func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error
|
|||||||
return conn, addr, nil
|
return conn, addr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseData will parse the bytes read from the socket into the Outgauge struct
|
||||||
|
func (sdk *BeamNGSDK) parseData() error {
|
||||||
|
sdk.Data.Time = binary.LittleEndian.Uint32(sdk.Buffer[0:4])
|
||||||
|
copy(sdk.Data.Car[:], sdk.Buffer[4:8])
|
||||||
|
sdk.Data.Flags = binary.LittleEndian.Uint16(sdk.Buffer[8:10])
|
||||||
|
sdk.Data.Gear = int8(sdk.Buffer[10])
|
||||||
|
sdk.Data.Plid = int8(sdk.Buffer[11])
|
||||||
|
sdk.Data.Speed = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[12:16]))
|
||||||
|
sdk.Data.RPM = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[16:20]))
|
||||||
|
sdk.Data.Turbo = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[20:24]))
|
||||||
|
sdk.Data.EngTemp = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[24:28]))
|
||||||
|
sdk.Data.Fuel = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[28:32]))
|
||||||
|
sdk.Data.OilPressure = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[32:36]))
|
||||||
|
sdk.Data.OilTemp = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[36:40]))
|
||||||
|
sdk.Data.DashLights = binary.LittleEndian.Uint32(sdk.Buffer[40:44])
|
||||||
|
sdk.Data.ShowLights = binary.LittleEndian.Uint32(sdk.Buffer[44:48])
|
||||||
|
sdk.Data.Throttle = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[48:52]))
|
||||||
|
sdk.Data.Brake = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[52:56]))
|
||||||
|
sdk.Data.Throttle = math.Float32frombits(binary.LittleEndian.Uint32(sdk.Buffer[56:60]))
|
||||||
|
copy(sdk.Data.Display1[:], sdk.Buffer[60:76])
|
||||||
|
copy(sdk.Data.Display2[:], sdk.Buffer[76:92])
|
||||||
|
sdk.Data.ID = int32(binary.LittleEndian.Uint32(sdk.Buffer[92:96]))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ReadData will read new data from the UDP server
|
// ReadData will read new data from the UDP server
|
||||||
func (sdk *BeamNGSDK) ReadData() error {
|
func (sdk *BeamNGSDK) ReadData() error {
|
||||||
// Receive data from the socket
|
// Receive data from the socket
|
||||||
@@ -50,25 +78,19 @@ func (sdk *BeamNGSDK) ReadData() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if enough data was received to fill our struct
|
// Check if enough data was received to fill our struct
|
||||||
if n < binary.Size(Outgauge{}) {
|
if n < outgaugeSize {
|
||||||
fmt.Println("Received packet too small for Outgauge struct")
|
fmt.Println("Received packet too small for Outgauge struct")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the binary data into the struct
|
// Read the binary data into the struct
|
||||||
// NOTE: create a reader for this struct and then reset it with the data from here
|
sdk.reader.Reset(sdk.Buffer[:n])
|
||||||
// instead of creating this one everytime
|
err = sdk.parseData()
|
||||||
reader := bytes.NewReader(sdk.Buffer[:n])
|
if err != nil {
|
||||||
if err := binary.Read(reader, binary.LittleEndian, sdk.Data); err != nil {
|
|
||||||
fmt.Println("Error decoding UDP packet:", err)
|
fmt.Println("Error decoding UDP packet:", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the local map
|
|
||||||
// NOTE: maybe stop doing this here and make the user request this when he
|
|
||||||
// explicitly wants it
|
|
||||||
sdk.DataDict = sdk.Data.ToMap()
|
|
||||||
|
|
||||||
// this means there's new data
|
// this means there's new data
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -91,8 +113,6 @@ func Init(ip string, port int) (BeamNGSDK, error) {
|
|||||||
|
|
||||||
// Initiate the data variables
|
// Initiate the data variables
|
||||||
sdk.Buffer = make([]byte, 1024)
|
sdk.Buffer = make([]byte, 1024)
|
||||||
sdk.Data = &Outgauge{}
|
|
||||||
sdk.DataDict = sdk.Data.ToMap()
|
|
||||||
|
|
||||||
return sdk, nil
|
return sdk, nil
|
||||||
}
|
}
|
||||||
@@ -233,7 +253,7 @@ func (sdk *BeamNGSDK) HasRightIndicatorLight() bool {
|
|||||||
return sdk.Data.DashLights&DL_SIGNAL_R != 0
|
return sdk.Data.DashLights&DL_SIGNAL_R != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnyIndicator returns:
|
// HasAnyIndicatorLight returns:
|
||||||
// true if its available
|
// true if its available
|
||||||
// false if its unavailable
|
// false if its unavailable
|
||||||
func (sdk *BeamNGSDK) HasAnyIndicatorLight() bool {
|
func (sdk *BeamNGSDK) HasAnyIndicatorLight() bool {
|
||||||
@@ -262,3 +282,60 @@ func (sdk *BeamNGSDK) HasABSLight() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DashLights - functions to check if a given dash light is provided [END]
|
// DashLights - functions to check if a given dash light is provided [END]
|
||||||
|
|
||||||
|
// Flags - functions to check if a given flag is ON [START]
|
||||||
|
|
||||||
|
// HasTurbo returns:
|
||||||
|
// true if the car has a turbo
|
||||||
|
// false if it doesn't
|
||||||
|
func (sdk *BeamNGSDK) HasTurbo() bool {
|
||||||
|
return sdk.Data.Flags&OG_TURBO != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrefersKm returns:
|
||||||
|
// true if true
|
||||||
|
// false if false
|
||||||
|
// wow
|
||||||
|
func (sdk *BeamNGSDK) PrefersKm() bool {
|
||||||
|
return sdk.Data.Flags&OG_KM != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrefersBAR returns:
|
||||||
|
// true if true
|
||||||
|
// false if false
|
||||||
|
// wow
|
||||||
|
func (sdk *BeamNGSDK) PrefersBAR() bool {
|
||||||
|
return sdk.Data.Flags&OG_BAR != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flags - functions to check if a given flag is ON [END]
|
||||||
|
|
||||||
|
// Data Retrieval [START]
|
||||||
|
|
||||||
|
// ToMap creates a map with the data in the Outgauge struct
|
||||||
|
func (sdk *BeamNGSDK) ToMap() map[string]any {
|
||||||
|
return map[string]any{
|
||||||
|
"Time": sdk.Data.Time, // time in milliseconds (to check order)
|
||||||
|
"Car": sdk.Data.Car, // Car name
|
||||||
|
"Flags": sdk.Data.Flags, // Info (see OG_x below)
|
||||||
|
"Gear": sdk.Data.Gear, // Reverse:0, Neutral:1, First:2...
|
||||||
|
"Plid": sdk.Data.Plid, // Unique ID of viewed player (0 = none)
|
||||||
|
"Speed": sdk.Data.Speed, // M/S
|
||||||
|
"RPM": sdk.Data.RPM, // RPM
|
||||||
|
"Turbo": sdk.Data.Turbo, // BAR
|
||||||
|
"EngTemp": sdk.Data.EngTemp, // C
|
||||||
|
"Fuel": sdk.Data.Fuel, // 0 to 1
|
||||||
|
"OilPressure": sdk.Data.OilPressure, // BAR
|
||||||
|
"OilTemp": sdk.Data.OilTemp, // C
|
||||||
|
"DashLights": sdk.Data.DashLights, // Dash lights available (see DL_x below)
|
||||||
|
"ShowLights": sdk.Data.ShowLights, // Dash lights currently switched on
|
||||||
|
"Throttle": sdk.Data.Throttle, // 0 to 1
|
||||||
|
"Brake": sdk.Data.Brake, // 0 to 1
|
||||||
|
"Clutch": sdk.Data.Clutch, // 0 to 1
|
||||||
|
"Display1": sdk.Data.Display1, // Usually Fuel
|
||||||
|
"Display2": sdk.Data.Display2, // Usually Settings
|
||||||
|
"ID": sdk.Data.ID, // optional - only if OutGauge ID is specified
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data Retrieval [END]
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package bngsdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkReadData(b *testing.B) {
|
||||||
|
// Spin up an UDP server
|
||||||
|
sdk, err := Init("127.0.0.1", 0)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("Failed to initialize SDK: %v", err)
|
||||||
|
}
|
||||||
|
defer sdk.Close()
|
||||||
|
|
||||||
|
// Retrieve the actual assigned UDP address
|
||||||
|
localAddr := sdk.Conn.LocalAddr().(*net.UDPAddr)
|
||||||
|
|
||||||
|
// Start a client to stream data
|
||||||
|
clientConn, err := net.DialUDP("udp", nil, localAddr)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("Failed to dial local UDP socket: %v", err)
|
||||||
|
}
|
||||||
|
defer clientConn.Close()
|
||||||
|
|
||||||
|
// Pre serialize some data
|
||||||
|
dummyOutgauge := Outgauge{
|
||||||
|
Time: 424242,
|
||||||
|
Car: [4]byte{'P', 'E', 'R', 'F'},
|
||||||
|
Speed: 45.2,
|
||||||
|
RPM: 3500.0,
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := binary.Write(&buf, binary.LittleEndian, dummyOutgauge); err != nil {
|
||||||
|
b.Fatalf("Failed to serialize dummy struct: %v", err)
|
||||||
|
}
|
||||||
|
packetBytes := buf.Bytes()
|
||||||
|
|
||||||
|
// 4. Reset the timer to isolate the setup allocations from the loop metrics
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs() // Tracks memory allocations per operation (B/op and allocs/op)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
// Feed a packet into the network buffer right before reading it
|
||||||
|
_, err := clientConn.Write(packetBytes)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("Failed to write to UDP socket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the target function
|
||||||
|
err = sdk.ReadData()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatalf("ReadData failed at iteration %d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-26
@@ -1,5 +1,9 @@
|
|||||||
package bngsdk
|
package bngsdk
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
|
var outgaugeSize = binary.Size(Outgauge{})
|
||||||
|
|
||||||
// More documentation at https://go.beamng.com/protocols.
|
// More documentation at https://go.beamng.com/protocols.
|
||||||
// Or at BeamNG/lua/vehicle/protocols/outgauge.lua
|
// Or at BeamNG/lua/vehicle/protocols/outgauge.lua
|
||||||
// OG_x buts for flags
|
// OG_x buts for flags
|
||||||
@@ -50,29 +54,3 @@ type Outgauge struct {
|
|||||||
Display2 [16]byte // Usually Settings
|
Display2 [16]byte // Usually Settings
|
||||||
ID int32 // optional - only if OutGauge ID is specified
|
ID int32 // optional - only if OutGauge ID is specified
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToMap creates a map with the data in the Outgauge struct
|
|
||||||
func (o *Outgauge) ToMap() map[string]any {
|
|
||||||
return map[string]any{
|
|
||||||
"Time": o.Time, // time in milliseconds (to check order)
|
|
||||||
"Car": o.Car, // Car name
|
|
||||||
"Flags": o.Flags, // Info (see OG_x below)
|
|
||||||
"Gear": o.Gear, // Reverse:0, Neutral:1, First:2...
|
|
||||||
"Plid": o.Plid, // Unique ID of viewed player (0 = none)
|
|
||||||
"Speed": o.Speed, // M/S
|
|
||||||
"RPM": o.RPM, // RPM
|
|
||||||
"Turbo": o.Turbo, // BAR
|
|
||||||
"EngTemp": o.EngTemp, // C
|
|
||||||
"Fuel": o.Fuel, // 0 to 1
|
|
||||||
"OilPressure": o.OilPressure, // BAR
|
|
||||||
"OilTemp": o.OilTemp, // C
|
|
||||||
"DashLights": o.DashLights, // Dash lights available (see DL_x below)
|
|
||||||
"ShowLights": o.ShowLights, // Dash lights currently switched on
|
|
||||||
"Throttle": o.Throttle, // 0 to 1
|
|
||||||
"Brake": o.Brake, // 0 to 1
|
|
||||||
"Clutch": o.Clutch, // 0 to 1
|
|
||||||
"Display1": o.Display1, // Usually Fuel
|
|
||||||
"Display2": o.Display2, // Usually Settings
|
|
||||||
"ID": o.ID, // optional - only if OutGauge ID is specified
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user