Skip to content

Commit

Permalink
feat: Add hook to set Battlefield 2 default profile
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Jan 28, 2023
1 parent b0d6486 commit 211dfd6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ games:
install_path: C:\Games\Battlefield 2
args: ["+fullscreen", "0", "+szx", "1600", "+szy", "900"]
hooks:
- handler: set-default-profile
when: pre-launch
args:
profile: 0010
- handler: purge-server-history
when: pre-launch
22 changes: 21 additions & 1 deletion internal/titles/bf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
bf2ModPoE2 = "poe2"

bf2HookPurgeServerHistory = "purge-server-history"
bf2HookSetDefaultProfile = "set-default-profile"
hookArgProfile = "profile"
)

var Bf2 = domain.GameTitle{
Expand Down Expand Up @@ -148,13 +150,31 @@ var Bf2 = domain.GameTitle{
},
HookHandlers: map[string]game_launcher.HookHandler{
localinternal.HookKillProcess: localinternal.KillProcessHookHandler(true),
bf2HookSetDefaultProfile: bf2SetDefaultProfileHookHandler,
bf2HookPurgeServerHistory: bf2PurgeServerHistoryHookHandler,
},
}

var bf2SetDefaultProfileHookHandler = func(fr game_launcher.FileRepository, u *url.URL, config game_launcher.Config, launchType game_launcher.LaunchType, args map[string]string) error {
profileKey, ok := args[hookArgProfile]
if !ok {
return fmt.Errorf("required argument %s for hook %s is missing", hookArgProfile, bf2HookSetDefaultProfile)
}

h := handler.New(fr)
globalCon, err := h.ReadGlobalConfig(handler.GameBf2)
if err != nil {
return err
}

bf2.SetDefaultProfile(globalCon, profileKey)

return h.WriteConfigFile(globalCon)
}

var bf2PurgeServerHistoryHookHandler = func(fr game_launcher.FileRepository, u *url.URL, config game_launcher.Config, launchType game_launcher.LaunchType, args map[string]string) error {
h := handler.New(fr)
profileKey, ok := args["profile"]
profileKey, ok := args[hookArgProfile]
if !ok {
// Use default profile if none has been configured
var err error
Expand Down
72 changes: 72 additions & 0 deletions internal/titles/bf2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,78 @@ import (
"github.com/cetteup/joinme.click-launcher/pkg/game_launcher"
)

func TestBf2SetDefaultProfileHookHandler(t *testing.T) {
type test struct {
name string
givenArgs map[string]string
expect func(fr *MockFileRepository)
wantErrContains string
}

tests := []test{
{
name: "sets given profile as default profile",
givenArgs: map[string]string{
"profile": "0001",
},
expect: func(fr *MockFileRepository) {
fr.EXPECT().ReadFile(testhelpers.StringContainsMatcher("Battlefield 2\\Profiles\\Global.con")).Return([]byte("GlobalSettings.setDefaultUser \"0002\""), nil)
fr.EXPECT().WriteFile(testhelpers.StringContainsMatcher("Battlefield 2\\Profiles\\Global.con"), []byte("GlobalSettings.setDefaultUser \"0001\"\r\n"), gomock.Any())
},
},
{
name: "errors if profile argument is missing",
givenArgs: map[string]string{},
expect: func(fr *MockFileRepository) {},
wantErrContains: "required argument profile for hook set-default-profile is missing",
},
{
name: "errors if Global.con cannot be read",
givenArgs: map[string]string{
"profile": "0001",
},
expect: func(fr *MockFileRepository) {
fr.EXPECT().ReadFile(testhelpers.StringContainsMatcher("Battlefield 2\\Profiles\\Global.con")).Return(nil, fmt.Errorf("some-read-error"))
},
wantErrContains: "some-read-error",
},
{
name: "errors if Global.con cannot be written",
givenArgs: map[string]string{
"profile": "0001",
},
expect: func(fr *MockFileRepository) {
fr.EXPECT().ReadFile(testhelpers.StringContainsMatcher("Battlefield 2\\Profiles\\Global.con")).Return([]byte("GlobalSettings.setDefaultUser \"0002\""), nil)
fr.EXPECT().WriteFile(testhelpers.StringContainsMatcher("Battlefield 2\\Profiles\\Global.con"), []byte("GlobalSettings.setDefaultUser \"0001\"\r\n"), gomock.Any()).Return(fmt.Errorf("some-write-error"))
},
wantErrContains: "some-write-error",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// GIVEN
ctrl := gomock.NewController(t)
mockRepository := NewMockFileRepository(ctrl)
u := &url.URL{Host: net.JoinHostPort("1.1.1.1", "16567")}
config := game_launcher.Config{}

// EXPECT
tt.expect(mockRepository)

// WHEN
err := bf2SetDefaultProfileHookHandler(mockRepository, u, config, game_launcher.LaunchTypeLaunchAndJoin, tt.givenArgs)

// THEN
if tt.wantErrContains != "" {
assert.ErrorContains(t, err, tt.wantErrContains)
} else {
require.NoError(t, err)
}
})
}
}

func TestBf2PurgeServerHistoryHookHandler(t *testing.T) {
type test struct {
name string
Expand Down

0 comments on commit 211dfd6

Please sign in to comment.