2022-12-03 18:56:36 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-21 00:28:30 +01:00
|
|
|
"archive/zip"
|
2022-12-05 14:13:58 +01:00
|
|
|
"bufio"
|
2022-12-03 18:56:36 +01:00
|
|
|
"fmt"
|
2022-12-05 14:13:58 +01:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2022-12-03 18:56:36 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
"sauce.pizzawednes.day/kayomn/ini-gusher"
|
|
|
|
)
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func (game *Game) Clean() error {
|
2022-12-21 00:28:30 +01:00
|
|
|
// Clean up currently deployed files first.
|
|
|
|
for _, filePath := range game.DeployedFilePaths {
|
2022-12-23 15:58:59 +01:00
|
|
|
if removeError := os.Remove(filepath.Join(
|
|
|
|
game.Path, filePath)); (removeError != nil) && (!(os.IsNotExist(removeError))) {
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return removeError
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
game.DeployedFilePaths = game.DeployedFilePaths[:0]
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
var overwriteDirPath = filepath.Join(cachePath(), game.Name, "overwritten")
|
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
// Then restore all files overwritten by previously deployment.
|
|
|
|
for _, filePath := range game.OverwrittenFilePaths {
|
2022-12-23 15:58:59 +01:00
|
|
|
if renameError := os.Rename(filepath.Join(overwriteDirPath, filePath),
|
2022-12-21 00:28:30 +01:00
|
|
|
filepath.Join(game.Path, filePath)); renameError != nil {
|
|
|
|
|
|
|
|
return renameError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
game.OverwrittenFilePaths = game.OverwrittenFilePaths[:0]
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return game.saveDeployment()
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func (game *Game) Deploy(names []string) error {
|
2022-12-23 16:49:29 +01:00
|
|
|
if cleanError := game.Clean(); cleanError != nil {
|
|
|
|
return cleanError
|
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
for _, name := range names {
|
|
|
|
var mod, exists = game.Mods[name]
|
2022-12-22 00:32:30 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if !(exists) {
|
|
|
|
return fmt.Errorf("mod does not exist: %s", name)
|
2022-12-22 00:32:30 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
var installPath = fmt.Sprintf("%s.%s",
|
|
|
|
filepath.Join(configPath(), game.Name, name), mod.Format)
|
2022-12-22 00:32:30 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
switch mod.Format {
|
|
|
|
case "zip":
|
|
|
|
var zipReadCloser, openError = zip.OpenReader(installPath)
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if openError != nil {
|
|
|
|
return openError
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
defer func() {
|
|
|
|
if closeError := zipReadCloser.Close(); closeError != nil {
|
|
|
|
// Zip read closer will not have any pending I/O operations nor is it possible
|
|
|
|
// to have already been closed.
|
|
|
|
panic(closeError)
|
|
|
|
}
|
|
|
|
}()
|
2022-12-06 10:33:48 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
for _, zipFile := range zipReadCloser.File {
|
|
|
|
var deployPath = filepath.Join(game.Path, zipFile.Name)
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-24 02:50:14 +01:00
|
|
|
if dirError := os.MkdirAll(filepath.Dir(deployPath), os.ModePerm); dirError != nil {
|
2022-12-23 15:58:59 +01:00
|
|
|
return dirError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if zipFile.FileInfo().IsDir() {
|
|
|
|
// All work is done for creating a directory, rest is just for files.
|
|
|
|
continue
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
// Backup up any pre-existing file before it is overwritten by Zip entry
|
|
|
|
// extraction.
|
2022-12-23 16:49:29 +01:00
|
|
|
if _, statError := os.Stat(deployPath); statError == nil {
|
2022-12-23 15:58:59 +01:00
|
|
|
var backupPath = filepath.Join(
|
|
|
|
cachePath(), game.Name, "overwritten", zipFile.Name)
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if renameError := os.Rename(deployPath, backupPath); renameError != nil {
|
|
|
|
return renameError
|
|
|
|
}
|
|
|
|
|
|
|
|
game.OverwrittenFilePaths = append(game.OverwrittenFilePaths, zipFile.Name)
|
|
|
|
} else if !(os.IsNotExist(statError)) {
|
|
|
|
return statError
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-06 10:33:48 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if file, createError := os.Create(deployPath); createError == nil {
|
|
|
|
var zipFile, ioError = zipFile.Open()
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if ioError == nil {
|
|
|
|
_, ioError = io.Copy(file, zipFile)
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if syncError := file.Sync(); (syncError != nil) && (ioError == nil) {
|
|
|
|
ioError = syncError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if closeError := file.Close(); (closeError != nil) && (ioError == nil) {
|
|
|
|
ioError = closeError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if closeError := zipFile.Close(); closeError != nil {
|
|
|
|
ioError = closeError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if ioError != nil {
|
|
|
|
return ioError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if closeError := zipReadCloser.Close(); closeError != nil {
|
|
|
|
return closeError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return createError
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
game.DeployedFilePaths = append(game.DeployedFilePaths, zipFile.Name)
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported mod format: %s", mod.Format)
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return game.saveDeployment()
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
type Game struct {
|
2022-12-22 00:32:30 +01:00
|
|
|
Name string
|
2022-12-21 00:28:30 +01:00
|
|
|
DeployedFilePaths []string
|
|
|
|
OverwrittenFilePaths []string
|
|
|
|
Mods map[string]Mod
|
|
|
|
Path string
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func (game *Game) InstallMods(archivePaths []string) error {
|
|
|
|
var installDirPath = filepath.Join(configPath(), game.Name)
|
2022-12-21 00:28:30 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
for _, archivePath := range archivePaths {
|
|
|
|
var archiveName = filepath.Base(archivePath)
|
|
|
|
var archiveExtension = filepath.Ext(archiveName)
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if len(archiveExtension) == 0 {
|
|
|
|
return fmt.Errorf("unknown archive format: %s", archiveExtension)
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
var name = strings.TrimSuffix(archiveName, archiveExtension)
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if _, exists := game.Mods[name]; exists {
|
|
|
|
return fmt.Errorf("mod with name already exists: `%s`", name)
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
// Copy archive into installation directory.
|
|
|
|
if archiveFile, openError := os.Open(archivePath); openError == nil {
|
|
|
|
defer func() {
|
|
|
|
if closeError := archiveFile.Close(); closeError != nil {
|
|
|
|
// Archive file will not have any pending I/O operations nor is it possible to have
|
|
|
|
// already been closed.
|
|
|
|
panic(closeError)
|
|
|
|
}
|
|
|
|
}()
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
var archiveFileInfo, statError = archiveFile.Stat()
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if statError != nil {
|
|
|
|
return statError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if dirError := os.MkdirAll(installDirPath, archiveFileInfo.Mode()); dirError != nil {
|
|
|
|
return dirError
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if installFile, createError := os.Create(filepath.Join(installDirPath, archiveName)); createError == nil {
|
|
|
|
var _, copyError = io.Copy(installFile, archiveFile)
|
|
|
|
var syncError = installFile.Sync()
|
|
|
|
var closeError = installFile.Close()
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if (copyError != nil) || (syncError != nil) {
|
|
|
|
return fmt.Errorf("failed to install mod")
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if closeError != nil {
|
|
|
|
return closeError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return createError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return openError
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
game.Mods[name] = Mod{
|
|
|
|
Format: archiveExtension[1:],
|
|
|
|
Source: archivePath,
|
|
|
|
Version: "",
|
|
|
|
}
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return game.saveMods()
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-06 10:33:48 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
func (game *Game) Load() error {
|
|
|
|
// Read deployed files from disk.
|
2022-12-23 15:58:59 +01:00
|
|
|
var deployedListPath = filepath.Join(cachePath(), game.Name, "deployed.txt")
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
if file, openError := os.Open(deployedListPath); openError == nil {
|
2022-12-23 15:58:59 +01:00
|
|
|
for scanner := bufio.NewScanner(file); scanner.Scan(); {
|
2022-12-21 00:28:30 +01:00
|
|
|
game.DeployedFilePaths = append(game.DeployedFilePaths, scanner.Text())
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
2022-12-23 15:58:59 +01:00
|
|
|
|
|
|
|
if closeError := file.Close(); closeError != nil {
|
|
|
|
return closeError
|
|
|
|
}
|
2022-12-21 00:28:30 +01:00
|
|
|
} else if !(os.IsNotExist(openError)) {
|
|
|
|
return openError
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
// Read overwritten game files from disk.
|
|
|
|
var overwrittenFilesDirPath = filepath.Join(cachePath(), game.Name, "overwritten")
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if _, statError := os.Stat(overwrittenFilesDirPath); statError == nil {
|
|
|
|
if walkError := filepath.WalkDir(overwrittenFilesDirPath, func(
|
2022-12-21 00:28:30 +01:00
|
|
|
path string, dirEntry fs.DirEntry, walkError error) error {
|
|
|
|
|
|
|
|
if walkError != nil {
|
|
|
|
return walkError
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
if !(dirEntry.IsDir()) {
|
|
|
|
game.OverwrittenFilePaths = append(game.OverwrittenFilePaths, path)
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
return nil
|
|
|
|
}); walkError != nil {
|
|
|
|
return walkError
|
|
|
|
}
|
|
|
|
} else if !(os.IsNotExist(statError)) {
|
|
|
|
return statError
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
// Read mod info from disk.
|
2022-12-23 15:58:59 +01:00
|
|
|
if file, openError := os.Open(filepath.Join(configPath(), game.Name, "mods.ini")); openError == nil {
|
2022-12-21 00:28:30 +01:00
|
|
|
defer func() {
|
|
|
|
if closeError := file.Close(); closeError != nil {
|
2022-12-23 15:58:59 +01:00
|
|
|
// File will not have any pending I/O operations nor is it possible to have already
|
|
|
|
// been closed.
|
2022-12-21 00:28:30 +01:00
|
|
|
panic(closeError)
|
|
|
|
}
|
|
|
|
}()
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
var parser = ini.NewParser(file)
|
|
|
|
|
|
|
|
for entry := parser.Parse(); !(parser.IsEnd()); entry = parser.Parse() {
|
|
|
|
var mod = game.Mods[entry.Section]
|
|
|
|
|
|
|
|
switch entry.Key {
|
|
|
|
case "format":
|
|
|
|
mod.Format = entry.Value
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
case "source":
|
|
|
|
mod.Source = entry.Value
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
case "version":
|
|
|
|
mod.Version = entry.Value
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
game.Mods[entry.Section] = mod
|
|
|
|
}
|
|
|
|
|
|
|
|
if parserError := parser.Err(); parserError != nil {
|
|
|
|
return parserError
|
|
|
|
}
|
|
|
|
} else if !(os.IsNotExist(openError)) {
|
|
|
|
return openError
|
|
|
|
}
|
2022-12-03 19:27:53 +01:00
|
|
|
|
|
|
|
return nil
|
2022-12-03 18:56:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mod struct {
|
2022-12-21 00:28:30 +01:00
|
|
|
Format string
|
|
|
|
Source string
|
|
|
|
Version string
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func LoadGame(name string) (Game, error) {
|
2022-12-22 00:32:30 +01:00
|
|
|
var configPath, configPathError = os.UserConfigDir()
|
|
|
|
|
|
|
|
if configPathError != nil {
|
|
|
|
return Game{}, configPathError
|
|
|
|
}
|
|
|
|
|
|
|
|
var gamesFile, gamesOpenError = os.Open(filepath.Join(configPath, "modman", "games.ini"))
|
|
|
|
|
|
|
|
if gamesOpenError != nil {
|
|
|
|
if os.IsNotExist(gamesOpenError) {
|
|
|
|
return Game{}, fmt.Errorf("no games registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Game{}, gamesOpenError
|
|
|
|
}
|
|
|
|
|
|
|
|
var gamesParser = ini.NewParser(gamesFile)
|
|
|
|
|
|
|
|
for entry := gamesParser.Parse(); !(gamesParser.IsEnd()); entry = gamesParser.Parse() {
|
|
|
|
if (entry.Key == "path") && (entry.Section == name) {
|
|
|
|
var game = Game{
|
|
|
|
Name: name,
|
|
|
|
OverwrittenFilePaths: make([]string, 0, 512),
|
|
|
|
DeployedFilePaths: make([]string, 0, 512),
|
|
|
|
Mods: make(map[string]Mod),
|
2022-12-23 16:49:29 +01:00
|
|
|
Path: entry.Value,
|
2022-12-22 00:32:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if loadError := game.Load(); loadError != nil {
|
|
|
|
return Game{}, loadError
|
|
|
|
}
|
|
|
|
|
|
|
|
return game, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Game{}, fmt.Errorf("game not registered: %s", name)
|
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func (game *Game) RemoveMod(name string) error {
|
|
|
|
if _, exists := game.Mods[name]; !(exists) {
|
|
|
|
return fmt.Errorf("unknown mod: `%s`", name)
|
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if removeError := os.RemoveAll(filepath.Join(
|
|
|
|
configPath(), game.Name, name)); removeError != nil {
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return removeError
|
2022-12-05 14:13:58 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
delete(game.Mods, name)
|
|
|
|
|
|
|
|
return game.saveMods()
|
2022-12-03 18:56:36 +01:00
|
|
|
}
|
|
|
|
|
2022-12-22 01:34:41 +01:00
|
|
|
func RegisterGame(name string, dataPath string) error {
|
2022-12-23 15:58:59 +01:00
|
|
|
var gamesPath = filepath.Join(configPath(), "games.ini")
|
|
|
|
var gameNamePaths = make(map[string]string)
|
2022-12-22 01:34:41 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if file, openError := os.Open(gamesPath); openError == nil {
|
|
|
|
defer func() {
|
|
|
|
if closeError := file.Close(); closeError != nil {
|
|
|
|
// No way for this to fail.
|
|
|
|
panic(closeError)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var parser = ini.NewParser(file)
|
|
|
|
|
|
|
|
for entry := parser.Parse(); !(parser.IsEnd()); entry = parser.Parse() {
|
|
|
|
if entry.Key == "path" {
|
|
|
|
gameNamePaths[entry.Section] = entry.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if parserError := parser.Err(); parserError != nil {
|
|
|
|
return parserError
|
|
|
|
}
|
|
|
|
} else if !(os.IsNotExist(openError)) {
|
|
|
|
return openError
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if file, updateError := os.Create(gamesPath); updateError == nil {
|
|
|
|
var writer = bufio.NewWriter(file)
|
|
|
|
var builder = ini.NewBuilder(writer)
|
2022-12-22 01:34:41 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
for name, path := range gameNamePaths {
|
|
|
|
if buildError := builder.Section(name); buildError != nil {
|
|
|
|
updateError = buildError
|
2022-12-22 01:34:41 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
break
|
|
|
|
}
|
2022-12-22 01:34:41 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if buildError := builder.KeyValue("path", path); buildError != nil {
|
|
|
|
updateError = buildError
|
2022-12-22 01:34:41 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
break
|
|
|
|
}
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if writeError := writer.Flush(); (writeError != nil) && (updateError == nil) {
|
|
|
|
updateError = writeError
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if syncError := file.Sync(); (syncError != nil) && (updateError == nil) {
|
|
|
|
updateError = syncError
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if closeError := file.Close(); (closeError != nil) && (updateError == nil) {
|
|
|
|
updateError = closeError
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if updateError != nil {
|
|
|
|
return updateError
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-23 15:58:59 +01:00
|
|
|
return updateError
|
2022-12-22 01:34:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
func (game *Game) RenameMod(modName string, newName string) error {
|
2022-12-23 15:58:59 +01:00
|
|
|
var mod, exists = game.Mods[modName]
|
|
|
|
|
|
|
|
if !(exists) {
|
2022-12-21 00:28:30 +01:00
|
|
|
return fmt.Errorf("no mod with that name exists")
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
if _, nameTaken := game.Mods[newName]; nameTaken {
|
|
|
|
return fmt.Errorf("a mod with the new name already exists")
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
var modsDirPath = filepath.Join(configPath(), game.Name)
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-24 02:50:14 +01:00
|
|
|
if renameError := os.Rename(fmt.Sprintf("%s.%s",
|
|
|
|
filepath.Join(modsDirPath, modName), mod.Format),
|
|
|
|
fmt.Sprintf("%s.%s", filepath.Join(
|
|
|
|
modsDirPath, newName), mod.Format)); renameError != nil {
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
return renameError
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
game.Mods[newName] = mod
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-21 00:28:30 +01:00
|
|
|
delete(game.Mods, modName)
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return game.saveMods()
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func cachePath() string {
|
|
|
|
return fallbackPath(os.UserCacheDir, "cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
func configPath() string {
|
|
|
|
return fallbackPath(os.UserConfigDir, "config")
|
|
|
|
}
|
|
|
|
|
|
|
|
func fallbackPath(getFalliblePath func() (string, error), fallbackSubdir string) string {
|
|
|
|
var path, pathError = getFalliblePath()
|
2022-12-03 18:56:36 +01:00
|
|
|
|
|
|
|
if pathError != nil {
|
2022-12-23 15:58:59 +01:00
|
|
|
// Fallback to homedir.
|
|
|
|
path, pathError = os.UserHomeDir()
|
|
|
|
|
|
|
|
if pathError != nil {
|
|
|
|
// User home dir should exist / be accessible.
|
|
|
|
panic(pathError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(path, "modman", fallbackSubdir)
|
2022-12-03 18:56:36 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return filepath.Join(path, "modman")
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func (game *Game) saveDeployment() error {
|
2022-12-23 16:49:29 +01:00
|
|
|
var listPath = filepath.Join(cachePath(), game.Name, "deployed.txt")
|
2022-12-23 15:58:59 +01:00
|
|
|
|
|
|
|
if len(game.DeployedFilePaths) == 0 {
|
2022-12-23 16:49:29 +01:00
|
|
|
var truncateError = os.Truncate(listPath, 0)
|
|
|
|
|
|
|
|
if !(os.IsNotExist(truncateError)) {
|
|
|
|
return truncateError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirError := os.MkdirAll(filepath.Dir(listPath), os.ModePerm); dirError != nil {
|
|
|
|
return dirError
|
2022-12-03 18:56:36 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if file, updateError := os.Create(listPath); updateError == nil {
|
|
|
|
var writer = bufio.NewWriter(file)
|
|
|
|
|
|
|
|
for _, filePath := range game.DeployedFilePaths {
|
|
|
|
if _, printError := fmt.Fprintln(writer, filePath); printError != nil {
|
|
|
|
updateError = printError
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flushError := writer.Flush(); flushError != nil && updateError == nil {
|
|
|
|
updateError = flushError
|
|
|
|
}
|
|
|
|
|
|
|
|
if syncError := file.Sync(); syncError != nil && updateError == nil {
|
|
|
|
updateError = syncError
|
|
|
|
}
|
|
|
|
|
|
|
|
if closeError := file.Close(); closeError != nil && updateError == nil {
|
|
|
|
updateError = closeError
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateError != nil {
|
|
|
|
return updateError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return updateError
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-05 14:13:58 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
func (game *Game) saveMods() error {
|
2022-12-23 16:13:26 +01:00
|
|
|
var file, updateError = os.Create(filepath.Join(configPath(), game.Name, "mods.ini"))
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if updateError != nil {
|
|
|
|
return updateError
|
|
|
|
}
|
|
|
|
|
|
|
|
var writer = bufio.NewWriter(file)
|
|
|
|
var builder = ini.NewBuilder(writer)
|
|
|
|
|
|
|
|
for name, mod := range game.Mods {
|
|
|
|
if buildError := builder.Section(name); buildError != nil {
|
|
|
|
updateError = buildError
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildError := builder.KeyValue("format", mod.Format); buildError != nil {
|
|
|
|
updateError = buildError
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildError := builder.KeyValue("source", mod.Source); buildError != nil {
|
|
|
|
updateError = buildError
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildError := builder.KeyValue("version", mod.Version); buildError != nil {
|
|
|
|
updateError = buildError
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if writeError := writer.Flush(); (writeError != nil) && (updateError == nil) {
|
|
|
|
updateError = writeError
|
|
|
|
}
|
|
|
|
|
|
|
|
if syncError := file.Sync(); (syncError != nil) && (updateError == nil) {
|
|
|
|
updateError = syncError
|
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
if closeError := file.Close(); (closeError != nil) && (updateError == nil) {
|
|
|
|
updateError = closeError
|
2022-12-21 00:28:30 +01:00
|
|
|
}
|
2022-12-03 18:56:36 +01:00
|
|
|
|
2022-12-23 15:58:59 +01:00
|
|
|
return updateError
|
2022-12-03 18:56:36 +01:00
|
|
|
}
|