wfc-server/common/friend_code.go
mkwcat bdce41eaa4
More server list and general changes
- Server browser (Matchmaking) now returns a server list (although hardcoded)
- GPCM now keeps track of an actual thread-safe session
- Some int types were changed to reflect the actual size
- Improvements made to logging in many places
2023-10-22 21:22:44 -04:00

33 lines
687 B
Go

package common
import (
"crypto/md5"
"encoding/binary"
"fmt"
)
func CalcFriendCode(pid uint32, gameId string) uint64 {
if pid == 0 {
return 0
}
buffer := make([]byte, 8)
binary.LittleEndian.PutUint32(buffer, pid)
buffer[4] = gameId[3]
buffer[5] = gameId[2]
buffer[6] = gameId[1]
buffer[7] = gameId[0]
digest := md5.Sum(buffer)
return uint64(pid) | (uint64(digest[0]&0xfe) << 31)
}
func CalcFriendCodeString(pid uint32, gameId string) string {
return GetRawFriendCodeString(CalcFriendCode(pid, gameId))
}
func GetRawFriendCodeString(fc uint64) string {
s := fmt.Sprintf("%012d", fc)
return s[len(s)-12:len(s)-8] + "-" + s[len(s)-8:len(s)-4] + "-" + s[len(s)-4:]
}