diff --git a/config.example.yaml b/config.example.yaml index 3c05686..2dd42d4 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -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 \ No newline at end of file diff --git a/internal/titles/bf2.go b/internal/titles/bf2.go index ccffafb..ad223f9 100644 --- a/internal/titles/bf2.go +++ b/internal/titles/bf2.go @@ -23,6 +23,8 @@ const ( bf2ModPoE2 = "poe2" bf2HookPurgeServerHistory = "purge-server-history" + bf2HookSetDefaultProfile = "set-default-profile" + hookArgProfile = "profile" ) var Bf2 = domain.GameTitle{ @@ -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 diff --git a/internal/titles/bf2_test.go b/internal/titles/bf2_test.go index 521b5f9..92ce1ea 100644 --- a/internal/titles/bf2_test.go +++ b/internal/titles/bf2_test.go @@ -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