Skip to content

Commit

Permalink
steamworks: add ISteamFriends.GetPersonaName method (hajimehoshi#4)
Browse files Browse the repository at this point in the history
This method is useful to find out the current player's
display name.
  • Loading branch information
quasilyte authored Aug 9, 2023
1 parent 018554c commit ee1a819
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
7 changes: 7 additions & 0 deletions steamworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ type ISteamUtils interface {
ShowFloatingGamepadTextInput(keyboardMode EFloatingGamepadTextInputMode, textFieldXPosition, textFieldYPosition, textFieldWidth, textFieldHeight int32) bool
}

type ISteamFriends interface {
GetPersonaName() string
}

const (
flatAPI_RestartAppIfNecessary = "SteamAPI_RestartAppIfNecessary"
flatAPI_Init = "SteamAPI_Init"
Expand All @@ -89,6 +93,9 @@ const (
flatAPI_ISteamApps_GetAppInstallDir = "SteamAPI_ISteamApps_GetAppInstallDir"
flatAPI_ISteamApps_GetCurrentGameLanguage = "SteamAPI_ISteamApps_GetCurrentGameLanguage"

flagAPI_SteamFriends = "SteamAPI_SteamFriends_v017"
flatAPI_ISteamFriends_GetPersonaName = "SteamAPI_ISteamFriends_GetPersonaName"

flatAPI_SteamInput = "SteamAPI_SteamInput_v006"
flatAPI_ISteamInput_GetConnectedControllers = "SteamAPI_ISteamInput_GetConnectedControllers"
flatAPI_ISteamInput_GetInputTypeForHandle = "SteamAPI_ISteamInput_GetInputTypeForHandle"
Expand Down
18 changes: 18 additions & 0 deletions steamworks_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ func (s steamApps) GetCurrentGameLanguage() string {
return C.GoString(C.uintptrToChar(C.uintptr_t(v)))
}

func SteamFriends() ISteamFriends {
v, err := theLib.call(funcType_Ptr, flagAPI_SteamFriends)
if err != nil {
panic(err)
}
return steamFriends(v)
}

type steamFriends C.uintptr_t

func (s steamFriends) GetPersonaName() string {
v, err := theLib.call(funcType_Ptr_Ptr, flatAPI_ISteamFriends_GetPersonaName, uintptr(s))
if err != nil {
panic(err)
}
return C.GoString(C.uintptrToChar(C.uintptr_t(v)))
}

func SteamInput() ISteamInput {
v, err := theLib.call(funcType_Ptr, flatAPI_SteamInput)
if err != nil {
Expand Down
39 changes: 30 additions & 9 deletions steamworks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import (

const is32Bit = unsafe.Sizeof(int(0)) == 4

func cStringToGoString(v uintptr, sizeHint int) string {
bs := make([]byte, 0, sizeHint)
for i := int32(0); ; i++ {
b := *(*byte)(unsafe.Pointer(v))
v += unsafe.Sizeof(byte(0))
if b == 0 {
break
}
bs = append(bs, b)
}
return string(bs)
}

type dll struct {
d *windows.LazyDLL
procs map[string]*windows.LazyProc
Expand Down Expand Up @@ -104,17 +117,25 @@ func (s steamApps) GetCurrentGameLanguage() string {
if err != nil {
panic(err)
}
return cStringToGoString(v, 256)
}

bs := make([]byte, 0, 256)
for i := int32(0); ; i++ {
b := *(*byte)(unsafe.Pointer(v))
v += unsafe.Sizeof(byte(0))
if b == 0 {
break
}
bs = append(bs, b)
func SteamFriends() ISteamFriends {
v, err := theDLL.call(flagAPI_SteamFriends)
if err != nil {
panic(err)
}
return string(bs)
return steamFriends(v)
}

type steamFriends uintptr

func (s steamFriends) GetPersonaName() string {
v, err := theDLL.call(flatAPI_ISteamFriends_GetPersonaName, uintptr(s))
if err != nil {
panic(err)
}
return cStringToGoString(v, 64)
}

func SteamInput() ISteamInput {
Expand Down

0 comments on commit ee1a819

Please sign in to comment.