Skip to content

Commit

Permalink
Merge commits from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ppebb committed Sep 17, 2024
2 parents d90d82d + 3a55fdd commit 6973b3e
Show file tree
Hide file tree
Showing 20 changed files with 1,734 additions and 360 deletions.
35 changes: 35 additions & 0 deletions common/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ package common

import (
"encoding/base64"
"errors"
"strings"
)

type GameSpyBase64Encoding int

const (
GameSpyBase64EncodingDefault = iota // 0
GameSpyBase64EncodingAlternate // 1
GameSpyBase64EncodingURLSafe // 2
)

var Base64DwcEncoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-").WithPadding('*')
Expand All @@ -20,6 +30,31 @@ func Base32Encode(value uint64) string {
return encoded
}

func DecodeGameSpyBase64(gameSpyBase64 string, gameSpyBase64Encoding GameSpyBase64Encoding) ([]byte, error) {
base64String, err := GameSpyBase64ToBase64(gameSpyBase64, gameSpyBase64Encoding)
if err != nil {
return nil, err
}

return base64.StdEncoding.DecodeString(base64String)
}

func GameSpyBase64ToBase64(gameSpyBase64 string, gameSpyBase64Encoding GameSpyBase64Encoding) (string, error) {
switch gameSpyBase64Encoding {
case GameSpyBase64EncodingDefault:
return gameSpyBase64, nil

case GameSpyBase64EncodingAlternate:
return strings.NewReplacer("[", "+", "]", "/", "_", "=").Replace(gameSpyBase64), nil

case GameSpyBase64EncodingURLSafe:
return strings.NewReplacer("-", "+", "_", "/" /*, "=", "="*/).Replace(gameSpyBase64), nil

default:
return "", errors.New("invalid GameSpy Base64 encoding specified")
}
}

func reverse(s string) string {
rns := []rune(s)
for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 {
Expand Down
22 changes: 22 additions & 0 deletions common/game_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var (
)

func GetGameInfoByID(gameId int) *GameInfo {
ReadGameList()

mutex.Lock()
defer mutex.Unlock()

Expand All @@ -37,6 +39,8 @@ func GetGameInfoByID(gameId int) *GameInfo {
}

func GetGameInfoByName(name string) *GameInfo {
ReadGameList()

mutex.Lock()
defer mutex.Unlock()

Expand All @@ -47,6 +51,24 @@ func GetGameInfoByName(name string) *GameInfo {
return nil
}

func GetGameID(name string) int {
info := GetGameInfoByName(name)
if info != nil {
return info.GameID
}

return -1
}

func GetGameIDOrPanic(name string) int {
id := GetGameID(name)
if id == -1 {
panic("Game not found: " + name)
}

return id
}

func ReadGameList() {
mutex.Lock()
defer mutex.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion common/gamespy_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ParseGameSpyMessage(msg string) ([]GameSpyCommand, error) {
func CreateGameSpyMessage(command GameSpyCommand) string {
query := ""
for k, v := range command.OtherValues {
query += fmt.Sprintf(`\%s\%s`, strings.Replace(k, `\`, ``, -1), strings.Replace(v, `\`, ``, -1))
query += fmt.Sprintf(`\%s\%s`, k, v)
}

if command.Command != "" {
Expand Down
Loading

0 comments on commit 6973b3e

Please sign in to comment.