Skip to content

Commit

Permalink
Fix for other platforms and avoid cgo dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
corfe83 committed Oct 28, 2023
1 parent dd10fa6 commit 666913f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion steamworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type ISteamUtils interface {

type ISteamFriends interface {
GetPersonaName() string
SetRichPresence(string, string)
SetRichPresence(string, string) bool
}

const (
Expand Down
14 changes: 14 additions & 0 deletions steamworks_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ func (s steamFriends) GetPersonaName() string {
return C.GoString(C.uintptrToChar(C.uintptr_t(v)))
}

func (s steamFriends) SetRichPresence(key, value string) bool {
ckey := append([]byte(key), 0)
defer runtime.KeepAlive(ckey)
cvalue := append([]byte(value), 0)
defer runtime.KeepAlive(cvalue)

v, err := theLib.call(funcType_Bool_Ptr_Ptr_Ptr, flatAPI_ISteamFriends_SetRichPresence, uintptr(s), uintptr(unsafe.Pointer(ckey), uintptr(unsafe.Pointer(cvalue)))
if err != nil {
panic(err)
}

return byte(v) != 0
}

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

package steamworks

// #include <stdlib.h>
import "C"

import (
"runtime"
"unsafe"
Expand Down Expand Up @@ -145,18 +142,17 @@ func (s steamFriends) GetPersonaName() string {
return cStringToGoString(v, 64)
}

func (s steamFriends) SetRichPresence(key, value string) {
keyString := C.CString(key)
valueString := C.CString(value)
defer func() {
C.free(unsafe.Pointer(keyString))
C.free(unsafe.Pointer(valueString))
}()
func (s steamFriends) SetRichPresence(key, value string) bool {
ckey := append([]byte(key), 0)
defer runtime.KeepAlive(ckey)
cvalue := append([]byte(value), 0)
defer runtime.KeepAlive(cvalue)

_, err := theDLL.call(flatAPI_ISteamFriends_SetRichPresence, uintptr(s), uintptr(unsafe.Pointer(keyString)), uintptr(unsafe.Pointer(valueString)))
v, err := theDLL.call(flatAPI_ISteamFriends_SetRichPresence, uintptr(s), uintptr(unsafe.Pointer(&ckey[0])), uintptr(unsafe.Pointer(&cvalue[0])))
if err != nil {
panic(err)
}
return byte(v) != 0
}

func SteamInput() ISteamInput {
Expand Down

0 comments on commit 666913f

Please sign in to comment.