Skip to content

Commit

Permalink
Don't override modified files when installing
Browse files Browse the repository at this point in the history
This patch will install files under .ftbinstall/{version_slug} if the
player has modified the file from the previous install.
  • Loading branch information
jamierocks committed May 11, 2020
1 parent cdebbf4 commit f7eaa31
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
27 changes: 20 additions & 7 deletions ftbinstall/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,27 @@ func InstallPackVersion(installTarget mcinstall.InstallTarget, dest string, pack
return err
}

var install *InstallSettings
if readJson(filepath.Join(destination, DataDir, SettingsFile), &install) != nil {
install = &InstallSettings{
var settings *InstallSettings
if readJson(filepath.Join(destination, DataDir, SettingsFile), &settings) != nil {
settings = &InstallSettings{
ID: uuid.New().String(),
Pack: pack.Slug,
Version: version.Slug,
Target: installTarget,
Files: map[string]string{},
}
} else {
fmt.Println("Existing installation of " + install.Pack + " v" + install.Version + " detected")
fmt.Println("Existing installation of " + settings.Pack + " v" + settings.Version + " detected")

if pack.Slug != install.Pack {
if pack.Slug != settings.Pack {
return OtherPackAlreadyInstalled
}
}
install := &Install{
Version: version.Slug,
OriginalFiles: settings.Files,
NewFiles: map[string]string{},
}

err = InstallTargets(installTarget, destination, version.Targets)
if err != nil {
Expand Down Expand Up @@ -112,7 +117,7 @@ func InstallPackVersion(installTarget mcinstall.InstallTarget, dest string, pack
forgeVersion = mcVersion.String() + "-forge" + mcVersion.String() + "-" + target.Version
}

if err := mcinstall.InstallProfile(install.ID, &mcinstall.Profile{
if err := mcinstall.InstallProfile(settings.ID, &mcinstall.Profile{
Name: pack.Name + " " + version.Name,
Type: "custom",
GameDir: destination,
Expand All @@ -130,7 +135,15 @@ func InstallPackVersion(installTarget mcinstall.InstallTarget, dest string, pack
}

// Write install settings
return writeJson(filepath.Join(destination, DataDir, SettingsFile), &install)
settings.Version = install.Version
settings.Files = install.NewFiles
return writeJson(filepath.Join(destination, DataDir, SettingsFile), &settings)
}

type Install struct {
Version string
OriginalFiles map[string]string
NewFiles map[string]string
}

// ftbinstall.json
Expand Down
30 changes: 22 additions & 8 deletions ftbinstall/installer_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Installs the given files, for the target environment, to the given
// destination.
func InstallFiles(install *InstallSettings, target mcinstall.InstallTarget, dest string, files []*ftbmeta.File) error {
func InstallFiles(install *Install, target mcinstall.InstallTarget, dest string, files []*ftbmeta.File) error {
// Collect the target-specific files, so we can keep an accurate count
// of how many files we've installed.
var targetFiles []*ftbmeta.File
Expand All @@ -33,24 +33,23 @@ func InstallFiles(install *InstallSettings, target mcinstall.InstallTarget, dest

// Install files for the target
for i, file := range targetFiles {
msg, err := installFile(dest, file)
msg, err := installFile(install, dest, file)
if err != nil {
fmt.Printf("[%d / %d] Failed to install '%s%s', ignoring file...`n", i + 1, len(targetFiles), file.Path, file.Name)
fmt.Printf("[%d / %d] Failed to install '%s%s', ignoring file...\n", i + 1, len(targetFiles), file.Path, file.Name)
fmt.Println(err)
continue
} else if msg != "" {
fmt.Printf("[%d / %d] %s\n", i + 1, len(targetFiles), msg)
}
fmt.Printf("[%d / %d] %s\n", i + 1, len(targetFiles), msg)

// Log the files information in the install settings
install.Files[file.Path + file.Name] = file.Sha1
install.NewFiles[file.Path + file.Name] = file.Sha1
}

return nil
}

// Installs the given file, to the destination
func installFile(dest string, file *ftbmeta.File) (string, error) {
func installFile(install *Install, dest string, file *ftbmeta.File) (string, error) {
dirPath := filepath.Join(dest, filepath.FromSlash(file.Path))
fileDest := filepath.Join(dirPath, file.Name)

Expand All @@ -67,10 +66,25 @@ func installFile(dest string, file *ftbmeta.File) (string, error) {
return "", err
}

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

// If already exists, continue to next file
if hex.EncodeToString(hasher.Sum(nil)) == file.Sha1 {
if hash == file.Sha1 {
return fmt.Sprintf("%s%s found, skipping...", file.Path, file.Name), nil
}

// If the file previously existed, don't override if the player made changes
originalHash := install.OriginalFiles[file.Path + file.Name]
if originalHash != "" && hash != originalHash {
fmt.Println("************************************************************************************************")
fmt.Printf("%s%s has a sha1 has of '%s', when\n", file.Path, file.Name, hash)
fmt.Printf("'%s' was expected.\n", originalHash)
fmt.Printf("To prevent overriding configurations, it will be installed under %s/%s.\n", DataDir, install.Version)
fmt.Println("Please investigate any collisions before playing!")
fmt.Println("************************************************************************************************")

return installFile(install, filepath.Join(dest, DataDir, install.Version), file)
}
}

// Ensure directory exists
Expand Down

0 comments on commit f7eaa31

Please sign in to comment.