Implement most unimplemented functions

This commit is contained in:
kayomn 2022-12-03 17:56:36 +00:00
parent e44f196236
commit 21b999e6eb
2 changed files with 415 additions and 61 deletions

186
main.go
View File

@ -1,118 +1,178 @@
package main package main
import ( import (
"archive/zip" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings"
) )
type Command struct { type Command struct {
Name string Name string
Description string Description string
Action func([]string) Result Action func([]string) (string, error)
Arguments []string Arguments []string
IsVarargs bool IsVarargs bool
} }
func Failed(message string) Result {
return Result{
HasFailed: true,
Message: message,
}
}
func Ok(message string) Result {
return Result{
HasFailed: false,
Message: message,
}
}
type Result struct {
Message string
HasFailed bool
}
var commands = []Command{ var commands = []Command{
{ {
Name: "install", Name: "install",
Description: "Install one or more mod archives into modman", Description: "Install one or more mod archives into modman",
Arguments: []string{"archive path"}, Arguments: []string{"game name", "archive path"},
IsVarargs: true, IsVarargs: true,
Action: func(arguments []string) Result { Action: func(arguments []string) (string, error) {
var processed = 0 var argumentCount = len(arguments)
for i := range arguments { if argumentCount == 0 {
var archivePath = arguments[i] return "", fmt.Errorf("expected game name")
if strings.HasSuffix(archivePath, ".zip") {
var reader, err = zip.OpenReader(archivePath)
if err == nil {
defer reader.Close()
processed += 1
}
continue
}
} }
return Ok(fmt.Sprint(processed, "of", len(arguments), "installed")) var modsToInstall = arguments[1:]
var installedMods = []string{}
if gameError := WithGame(arguments[0], func(game *Game) error {
var installed, installError = game.InstallMods(modsToInstall)
if installError != nil {
return installError
}
installedMods = installed
return nil
}); gameError != nil {
return "", gameError
}
return fmt.Sprint(len(installedMods), " of ", len(modsToInstall), " installed"), nil
}, },
}, },
{ {
Name: "remove", Name: "remove",
Description: "Remove one or more mods from modman", Description: "Remove one or more mods from modman",
Arguments: []string{"mod name"}, Arguments: []string{"game name", "mod name"},
IsVarargs: true, IsVarargs: true,
Action: func(arguments []string) Result { Action: func(arguments []string) (string, error) {
// TODO: Implement. var argumentCount = len(arguments)
return Failed("Not implemented")
if argumentCount == 0 {
return "", fmt.Errorf("expected game name")
}
var modsToRemove = arguments[1:]
var removedMods = []string{}
if gameError := WithGame(arguments[0], func(game *Game) error {
var removed, removeError = game.RemoveMods(modsToRemove)
if removeError != nil {
return removeError
}
removedMods = removed
return nil
}); gameError != nil {
return "", gameError
}
return fmt.Sprint(len(removedMods), " of ", len(removedMods), " removed"), nil
}, },
}, },
{ {
Name: "rename", Name: "rename",
Description: "Rename a mod within modman", Description: "Rename a mod within modman",
Arguments: []string{"mod name", "new name"}, Arguments: []string{"game name", "mod name", "new name"},
IsVarargs: false, IsVarargs: false,
Action: func(arguments []string) Result { Action: func(arguments []string) (string, error) {
// TODO: Implement. if len(arguments) != 3 {
return Failed("Not implemented") return "", fmt.Errorf("expected game name followed by mod name and new name")
}
if gameError := WithGame(arguments[0], func(game *Game) error {
if removeError := game.RenameMod(arguments[1], arguments[2]); removeError != nil {
return removeError
}
return nil
}); gameError != nil {
return "", gameError
}
return "", nil
}, },
}, },
{ {
Name: "list", Name: "manifest",
Description: "List all installed mods", Description: "Retrieve a manifest of all installed mods",
Arguments: []string{}, Arguments: []string{"game name", "format"},
IsVarargs: false, IsVarargs: false,
Action: func(arguments []string) Result { Action: func(arguments []string) (string, error) {
// TODO: Implement. if len(arguments) != 2 {
return Failed("Not implemented") return "", fmt.Errorf("expected game name followed by format")
}
var modManifest = ""
if gameError := WithGame(arguments[0], func(game *Game) error {
var format = arguments[1]
var formatter, formatterExists = formatters[format]
if !(formatterExists) {
return fmt.Errorf("unsupported format: `%s`", format)
}
var formattedManifest, formatError = formatter(game.Mods)
if formatError != nil {
return formatError
}
// TODO: Reconsider if always casting formatted data to string for output is a good
// idea.
modManifest = string(formattedManifest)
return nil
}); gameError != nil {
return "", gameError
}
return modManifest, nil
}, },
}, },
{ {
Name: "deploy", Name: "deploy",
Description: "Deploy all installed and enabled mods", Description: "Deploy all installed and enabled mods",
Arguments: []string{}, Arguments: []string{"game name"},
IsVarargs: false, IsVarargs: false,
Action: func(arguments []string) Result { Action: func(arguments []string) (string, error) {
// TODO: Implement. // TODO: Implement.
return Failed("Not implemented") return "", fmt.Errorf("not implemented")
}, },
}, },
} }
var formatters = map[string]func(any) ([]byte, error){
"json": func(data any) ([]byte, error) {
var marshalledJson, marshalError = json.Marshal(data)
if marshalError != nil {
return nil, marshalError
}
return marshalledJson, nil
},
}
func main() { func main() {
var argCount = len(os.Args) var argCount = len(os.Args)
@ -145,16 +205,20 @@ func main() {
var command = commands[i] var command = commands[i]
if command.Name == commandName { if command.Name == commandName {
var result = command.Action(os.Args[2:]) var response, actionError = command.Action(os.Args[2:])
if result.HasFailed { if actionError != nil {
fmt.Fprint(os.Stderr, result.Message) fmt.Fprintln(os.Stderr, actionError.Error())
os.Exit(1) os.Exit(1)
} }
fmt.Print(result.Message) if len(response) != 0 {
fmt.Println(response)
}
return return
} }
} }
fmt.Fprintf(os.Stderr, "unknown command: `%s`\n", commandName)
} }

290
manager.go Normal file
View File

@ -0,0 +1,290 @@
package main
import (
"archive/zip"
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type App struct {
ConfigDirPath string
}
type Game struct {
ID string
Mods map[string]Mod
HasUpdated bool
}
func (game *Game) InstallMods(archivePaths []string) ([]string, error) {
var processed = 0
for i := range archivePaths {
var archivePath = archivePaths[i]
var suffixIndex = strings.LastIndex(archivePath, ".")
if suffixIndex < 0 {
return nil, fmt.Errorf("cannot determine file type of `%s`", archivePath)
}
var extension = archivePath[suffixIndex+1:]
var extractor = extractors[extension]
if extractor == nil {
return nil, fmt.Errorf("unsupported file type `%s`", extension)
}
var gamePath, pathError = game.Path()
if pathError != nil {
return nil, pathError
}
var modName = filepath.Base(archivePath[:suffixIndex])
if extractError := extractor(archivePath, filepath.Join(
gamePath, "mods", modName)); extractError != nil {
return nil, extractError
}
game.Mods[modName] = Mod{
IsEnabled: false,
}
game.HasUpdated = true
processed += 1
}
return archivePaths[0:processed], nil
}
type Mod struct {
IsEnabled bool
}
func WithGame(gameName string, action func(*Game) error) error {
var supportedGames = []string{"fallout4", "falloutnv", "skyrim"}
for i := range supportedGames {
var supportedGame = supportedGames[i]
if gameName == supportedGame {
var game = Game{
ID: supportedGame,
Mods: make(map[string]Mod),
HasUpdated: false,
}
var gamePath, pathError = game.Path()
if pathError != nil {
return pathError
}
var manifestPath = filepath.Join(gamePath, "mods.csv")
// Load manifest from disk.
{
var manifestFile, openError = os.Open(manifestPath)
if openError == nil {
defer manifestFile.Close()
var manifestReader = csv.NewReader(manifestFile)
var recordValues, recordError = manifestReader.Read()
for recordValues != nil {
if recordError != nil {
return recordError
}
if len(recordValues) < 2 {
return fmt.Errorf("could not read mod manifest data - may be corrupt")
}
var status = recordValues[0]
var name = recordValues[1]
game.Mods[name] = Mod{
IsEnabled: status == "*",
}
recordValues, recordError = manifestReader.Read()
}
}
}
if actionError := action(&game); actionError != nil {
return actionError
}
// Save manifest back to disk.
if game.HasUpdated {
var manifestFile, openError = os.OpenFile(manifestPath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if openError != nil {
return openError
}
defer manifestFile.Close()
var manifestWriter = csv.NewWriter(manifestFile)
for name, mod := range game.Mods {
var status = ""
if mod.IsEnabled {
status = "*"
}
manifestWriter.Write([]string{status, name})
}
manifestWriter.Flush()
}
return nil
}
}
return fmt.Errorf("%s: game not supported", gameName)
}
func (game Game) Path() (string, error) {
var configDirPath, configError = os.UserConfigDir()
if configError != nil {
return "", configError
}
return filepath.Join(configDirPath, "modman", game.ID), nil
}
func (game *Game) RemoveMods(modNames []string) ([]string, error) {
var gamePath, pathError = game.Path()
if pathError != nil {
return nil, pathError
}
var processed = 0
for i := range modNames {
var modName = modNames[i]
if removeError := os.RemoveAll(filepath.Join(gamePath, modName)); removeError != nil {
return nil, removeError
}
delete(game.Mods, modName)
game.HasUpdated = true
processed += 1
}
return modNames[0:processed], nil
}
func (game *Game) RenameMod(modName string, newName string) error {
var gamePath, pathError = game.Path()
if pathError != nil {
return pathError
}
if _, exists := game.Mods[modName]; !(exists) {
return fmt.Errorf("no mod with that name exists")
}
if _, exists := game.Mods[newName]; exists {
return fmt.Errorf("a mod with the new name already exists")
}
if renameError := os.Rename(filepath.Join(gamePath, modName),
filepath.Join(gamePath, newName)); renameError != nil {
return renameError
}
game.Mods[newName] = game.Mods[modName]
delete(game.Mods, modName)
game.HasUpdated = true
return nil
}
var extractors = map[string]func(string, string) error{
"zip": func(archivePath string, destinationPath string) error {
var zipReader, openReaderError = zip.OpenReader(archivePath)
if openReaderError != nil {
return openReaderError
}
defer func() {
if closeError := zipReader.Close(); closeError != nil {
panic(closeError.Error())
}
}()
if mkdirError := os.MkdirAll(destinationPath, 0755); mkdirError != nil {
return mkdirError
}
for i := range zipReader.File {
var file = zipReader.File[i]
var fileReader, fileOpenError = file.Open()
if fileOpenError != nil {
return fileOpenError
}
defer func() {
if closeError := fileReader.Close(); closeError != nil {
panic(closeError.Error())
}
}()
var path = filepath.Join(destinationPath, file.Name)
if file.FileInfo().IsDir() {
if mkdirError := os.MkdirAll(path, file.Mode()); mkdirError != nil {
return mkdirError
}
} else {
if mkdirError := os.MkdirAll(filepath.Dir(path), file.Mode()); mkdirError != nil {
return mkdirError
}
var extractedFile, fileOpenError = os.OpenFile(path,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if fileOpenError != nil {
return fileOpenError
}
defer func() {
if fileOpenError := extractedFile.Close(); fileOpenError != nil {
panic(fileOpenError)
}
}()
var _, copyError = io.Copy(extractedFile, fileReader)
if copyError != nil {
return copyError
}
}
}
return nil
},
}