From 11b66395b540eae388abe895081e7a26b30d2eb5 Mon Sep 17 00:00:00 2001 From: kayomn Date: Thu, 22 Dec 2022 00:34:41 +0000 Subject: [PATCH] Add command for registering data paths of installed games --- main.go | 35 ++++++++++++++++++++++++++++------- manager.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 3709c3c..108c0a4 100644 --- a/main.go +++ b/main.go @@ -27,9 +27,7 @@ var commands = []Command{ IsVarargs: true, Action: func(requiredArguments []string, providedArguments []string) (string, error) { - var argumentCount = len(providedArguments) - - if argumentCount < len(requiredArguments) { + if len(providedArguments) < len(requiredArguments) { return "", fmt.Errorf("expected %s folowed by at least one %ss", requiredArguments[0], requiredArguments[1]) } @@ -56,6 +54,29 @@ var commands = []Command{ }, }, + { + Name: "register", + Description: "Registers the data directory path of game for management under the given name", + Arguments: []string{"data path", "game name"}, + IsVarargs: false, + + Action: func(requiredArguments []string, providedArguments []string) (string, error) { + if len(providedArguments) < len(requiredArguments) { + return "", fmt.Errorf("expected %s folowed by at least one %ss", + requiredArguments[0], requiredArguments[1]) + } + + var dataPath = providedArguments[0] + var gameName = providedArguments[1] + + if registerGameError := RegisterGame(gameName, dataPath); registerGameError != nil { + return "", registerGameError + } + + return fmt.Sprintf("Registered %s at %s", gameName, dataPath), nil + }, + }, + { Name: "remove", Description: "Remove one or more mods from modman", @@ -169,13 +190,13 @@ var commands = []Command{ Arguments: []string{"game name", "mod name"}, IsVarargs: true, - Action: func(requiredArguments []string, arguments []string) (string, error) { - if len(arguments) != len(requiredArguments) { + Action: func(requiredArguments []string, providedArguments []string) (string, error) { + if len(providedArguments) != len(requiredArguments) { return "", fmt.Errorf("expected %s followed by one or more %ss", requiredArguments[0], requiredArguments[1]) } - var game, gameOpenError = OpenGame(arguments[0]) + var game, gameOpenError = OpenGame(providedArguments[0]) if gameOpenError != nil { return "", gameOpenError @@ -191,7 +212,7 @@ var commands = []Command{ return "", cleanError } - for _, modName := range arguments[1:] { + for _, modName := range providedArguments[1:] { if deployError := game.DeployMod(modName); deployError != nil { return "", deployError } diff --git a/manager.go b/manager.go index d87f951..e9c0622 100644 --- a/manager.go +++ b/manager.go @@ -461,6 +461,60 @@ func (game *Game) RemoveMods(names []string) error { return nil } +func RegisterGame(name string, dataPath string) error { + var configPath, configPathError = os.UserConfigDir() + + if configPathError != nil { + return configPathError + } + + var gamesPath = filepath.Join(configPath, "modman", "games.ini") + + var gamesEntries = []ini.Entry{{ + Section: name, + Key: "path", + Value: dataPath, + }} + + if gamesFile, gamesFileError := os.Open(gamesPath); gamesFileError == nil { + var gamesParser = ini.NewParser(gamesFile) + + for entry := gamesParser.Parse(); !(gamesParser.IsEnd()); entry = gamesParser.Parse() { + gamesEntries = append(gamesEntries, entry) + } + + if err := gamesFile.Close(); err != nil { + return err + } + + if err := gamesParser.Err(); err != nil { + return err + } + } else if !(os.IsNotExist(gamesFileError)) { + return gamesFileError + } + + if gamesFile, gamesFileError := os.Create(gamesPath); gamesFileError == nil { + var writeError = ini.Write(gamesFile, gamesEntries) + + if err := gamesFile.Sync(); err != nil { + return err + } + + if err := gamesFile.Close(); err != nil { + return err + } + + if writeError != nil { + return writeError + } + } else { + return gamesFileError + } + + return nil +} + func (game *Game) RenameMod(modName string, newName string) error { if _, exists := game.Mods[modName]; !(exists) { return fmt.Errorf("no mod with that name exists")