Skip to content

Commit

Permalink
Merge pull request #130 from inada-s/accept-gdxsv-prefixed-version
Browse files Browse the repository at this point in the history
support gdxsv- prefixed flycast version
  • Loading branch information
inada-s authored Jun 17, 2021
2 parents 48ebdf6 + 439981c commit 507627b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 17 deletions.
41 changes: 24 additions & 17 deletions gdxsv/lbs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import (
"encoding/json"
"fmt"
"go.uber.org/zap"
"golang.org/x/mod/semver"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
"golang.org/x/text/width"
"hash/fnv"
"io/ioutil"
"math"
"sort"
"strconv"
"strings"

"golang.org/x/mod/semver"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
"golang.org/x/text/width"
)

type LbsHandler func(*LbsPeer, *LbsMessage)
Expand Down Expand Up @@ -251,6 +250,17 @@ func sendUserList(p *LbsPeer) {
p.SendMessage(n)
}

func isOldFlycastVersion(userVersion string) bool {
if strings.HasPrefix(userVersion, "gdxsv-") {
// New version-string have the prefix.
userVersion = "v" + strings.TrimPrefix(userVersion, "gdxsv-")
}
if semver.Compare(userVersion, requiredFlycastVersion) < 0 {
return true
}
return false
}

var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) {
loginType := m.Reader().Read8()

Expand All @@ -274,13 +284,12 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) {

switch loginType {
case 0:
if p.PlatformInfo["flycast"] != "" {
if semver.Compare(p.PlatformInfo["flycast"], requiredFlycastVersion) < 0 {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>PLEASE UPDATE Flycast<END>").Msg())
return
}
if p.PlatformInfo["flycast"] != "" && isOldFlycastVersion(p.PlatformInfo["flycast"]) {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>PLEASE UPDATE Flycast<END>").Msg())
return
}

if p.LoginKey != "" {
// Check login key is banned
if p.app.IsBannedAccount(p.LoginKey) {
Expand Down Expand Up @@ -313,12 +322,10 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>UNSUPPORTED LOGIN TYPE<END>").Msg())
case 2:
if p.PlatformInfo["flycast"] != "" {
if semver.Compare(p.PlatformInfo["flycast"], requiredFlycastVersion) < 0 {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>PLEASE UPDATE Flycast<END>").Msg())
return
}
if p.PlatformInfo["flycast"] != "" && isOldFlycastVersion(p.PlatformInfo["flycast"]) {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>PLEASE UPDATE Flycast<END>").Msg())
return
}

// Go to account registration flow.
Expand Down
57 changes: 57 additions & 0 deletions gdxsv/lbs_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import "testing"

func Test_isOldFlycastVersion(t *testing.T) {
type args struct {
userVersion string
}
tests := []struct {
name string
requiredVersion string
args args
want bool
}{
{
name: "same version ok",
requiredVersion: "v0.7.0",
args: args{userVersion: "v0.7.0"},
want: false,
},
{
name: "old version ng",
requiredVersion: "v0.7.0",
args: args{userVersion: "v0.6.9"},
want: true,
},
{
name: "new version ok",
requiredVersion: "v0.7.0",
args: args{userVersion: "v9.9.9"},
want: false,
},
{
name: "gdxsv prefix version ok",
requiredVersion: "v0.7.0",
args: args{userVersion: "gdxsv-0.7.0"},
want: false,
},
{
name: "gdxsv prefix version ng",
requiredVersion: "v0.7.0",
args: args{userVersion: "gdxsv-0.6.9"},
want: true,
},
}

backup := requiredFlycastVersion
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
requiredFlycastVersion = tt.requiredVersion
if got := isOldFlycastVersion(tt.args.userVersion); got != tt.want {
t.Errorf("isOldFlycastVersion() = %v, want %v", got, tt.want)
}
})
}
requiredFlycastVersion = backup
}

0 comments on commit 507627b

Please sign in to comment.