Skip to content

Commit

Permalink
Remove unmodified files that have been removed from the pack
Browse files Browse the repository at this point in the history
  • Loading branch information
jamierocks committed May 11, 2020
1 parent f7eaa31 commit 0a3babb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
78 changes: 78 additions & 0 deletions ftbinstall/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,36 @@ package ftbinstall

import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/jamiemansfield/ftbinstall/mcinstall"
"github.com/jamiemansfield/ftbinstall/util"
"github.com/jamiemansfield/go-ftbmeta/ftbmeta"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)

const (
DataDir = ".ftbinstall"
SettingsFile = "install.json"
)

var (
ExcludedDirs = []string{
DataDir,
"saves",
}
)

var (
OtherPackAlreadyInstalled = errors.New("ftbinstall: a pack is already installed at this location")
)
Expand Down Expand Up @@ -73,6 +84,73 @@ func InstallPackVersion(installTarget mcinstall.InstallTarget, dest string, pack
return err
}

// Remove any unmodified files that are no longer apart the pack
err = filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

relPath, err := filepath.Rel(destination, path)
if err != nil {
return err
}
ftbPath := "./" + filepath.ToSlash(relPath)

// While this should never be an issue anyway - for peace of mind,
// ftbinstall will not delete ANYTHING from a protected directory.
parts := strings.Split(relPath, string(filepath.Separator))
for _, excludedDir := range ExcludedDirs {
if parts[0] == excludedDir {
return nil
}
}

// Ignore if its a current file
if install.NewFiles[ftbPath] != "" {
return nil
}

// Check if its been modified
if install.OriginalFiles[ftbPath] != "" {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

hasher := sha1.New()
if _, err := io.Copy(hasher, f); err != nil {
return err
}
hash := hex.EncodeToString(hasher.Sum(nil))

// Delete file
if hash == install.OriginalFiles[ftbPath] {
fmt.Printf("%s has been removed from the modpack, as its\n", ftbPath)
fmt.Println("sha1 hash matches the original, it has been removed.")
return os.Remove(path)
}

// The file has been removed from the pack, but the player has modified it
fmt.Printf("%s has been removed from the modpack, as its\n", ftbPath)
fmt.Println("sha1 hash doesn't match the original - we have left it in place.")
fmt.Println("Please investigate whether you still need the file before playing!")
fmt.Printf("You can remove the '%s' line from %s/%s if\n", ftbPath, DataDir, SettingsFile)
fmt.Println("still required")

// So that this message continues on, store the original hash in the new file list
install.NewFiles[ftbPath] = install.OriginalFiles[ftbPath]
}

return nil
})
if err != nil {
return err
}

// Install profile for the Minecraft launcher
if installTarget == mcinstall.Client {
// Get the target Minecraft version for the pack
Expand Down
1 change: 0 additions & 1 deletion ftbinstall/installer_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func installFile(install *Install, dest string, file *ftbmeta.File) (string, err
if _, err := io.Copy(hasher, f); err != nil {
return "", err
}

hash := hex.EncodeToString(hasher.Sum(nil))

// If already exists, continue to next file
Expand Down

0 comments on commit 0a3babb

Please sign in to comment.