Add no-internal-hashes mode

This commit is contained in:
comp500 2021-05-04 19:56:56 +01:00
parent 3462dd7f21
commit 49d7b26b2e
6 changed files with 51 additions and 26 deletions

View File

@ -78,3 +78,5 @@ For use on servers, add the `-g` flag to disable the GUI and `-s server` to down
- Additional options can be configured in the `[options]` section of `pack.toml`, as follows: - Additional options can be configured in the `[options]` section of `pack.toml`, as follows:
- `mods-folder` The folder to save mod metadata files into, for the install commands - `mods-folder` The folder to save mod metadata files into, for the install commands
- `acceptable-game-versions` A list of additional Minecraft versions to accept when installing or updating mods - `acceptable-game-versions` A list of additional Minecraft versions to accept when installing or updating mods
- `no-internal-hashes` If this is set to true, packwiz will not generate hashes of local files, to prevent merge conflicts and inconsistent hashes when using git/etc.
- `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer

View File

@ -157,7 +157,7 @@ var initCmd = &cobra.Command{
Index: struct { Index: struct {
File string `toml:"file"` File string `toml:"file"`
HashFormat string `toml:"hash-format"` HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"` Hash string `toml:"hash,omitempty"`
}{ }{
File: indexFilePath, File: indexFilePath,
}, },

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/spf13/viper"
"os" "os"
"github.com/comp500/packwiz/core" "github.com/comp500/packwiz/core"
@ -20,6 +21,12 @@ var refreshCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
build, err := cmd.Flags().GetBool("build")
if err == nil && build {
viper.Set("no-internal-hashes", false)
} else if viper.GetBool("no-internal-hashes") {
fmt.Println("Note: no-internal-hashes mode is set, no hashes will be saved. Use --build to override this for distribution.")
}
index, err := pack.LoadIndex() index, err := pack.LoadIndex()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -51,4 +58,6 @@ var refreshCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(refreshCmd) rootCmd.AddCommand(refreshCmd)
refreshCmd.Flags().Bool("build", false, "Only has an effect in no-internal-hashes mode: generates internal hashes for distribution with packwiz-installer")
} }

View File

@ -28,7 +28,7 @@ type Index struct {
type IndexFile struct { type IndexFile struct {
// Files are stored in forward-slash format relative to the index file // Files are stored in forward-slash format relative to the index file
File string `toml:"file"` File string `toml:"file"`
Hash string `toml:"hash"` Hash string `toml:"hash,omitempty"`
HashFormat string `toml:"hash-format,omitempty"` HashFormat string `toml:"hash-format,omitempty"`
Alias string `toml:"alias,omitempty"` Alias string `toml:"alias,omitempty"`
MetaFile bool `toml:"metafile,omitempty"` // True when it is a .toml metadata file MetaFile bool `toml:"metafile,omitempty"` // True when it is a .toml metadata file
@ -121,6 +121,10 @@ func (in *Index) updateFileHashGiven(path, format, hash string, mod bool) error
// updateFile calculates the hash for a given path and updates it in the index // updateFile calculates the hash for a given path and updates it in the index
func (in *Index) updateFile(path string) error { func (in *Index) updateFile(path string) error {
var hashString string
if viper.GetBool("no-internal-hashes") {
hashString = ""
} else {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return err return err
@ -142,7 +146,8 @@ func (in *Index) updateFile(path string) error {
if err != nil { if err != nil {
return err return err
} }
hashString := hex.EncodeToString(h.Sum(nil)) hashString = hex.EncodeToString(h.Sum(nil))
}
mod := false mod := false
// If the file has an extension of toml and is in the mods folder, set mod to true // If the file has an extension of toml and is in the mods folder, set mod to true
@ -290,6 +295,9 @@ func (in Index) Write() error {
// RefreshFileWithHash updates a file in the index, given a file hash and whether it is a mod or not // RefreshFileWithHash updates a file in the index, given a file hash and whether it is a mod or not
func (in *Index) RefreshFileWithHash(path, format, hash string, mod bool) error { func (in *Index) RefreshFileWithHash(path, format, hash string, mod bool) error {
if viper.GetBool("no-internal-hashes") {
hash = ""
}
err := in.updateFileHashGiven(path, format, hash, mod) err := in.updateFileHashGiven(path, format, hash, mod)
if err != nil { if err != nil {
return err return err
@ -351,7 +359,7 @@ func (in Index) SaveFile(f IndexFile, dest io.Writer) error {
} }
calculatedHash := hex.EncodeToString(h.Sum(nil)) calculatedHash := hex.EncodeToString(h.Sum(nil))
if calculatedHash != f.Hash { if calculatedHash != f.Hash && !viper.GetBool("no-internal-hashes") {
return errors.New("hash of saved file is invalid") return errors.New("hash of saved file is invalid")
} }

View File

@ -20,7 +20,7 @@ type Pack struct {
// Path is stored in forward slash format relative to pack.toml // Path is stored in forward slash format relative to pack.toml
File string `toml:"file"` File string `toml:"file"`
HashFormat string `toml:"hash-format"` HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"` Hash string `toml:"hash,omitempty"`
} `toml:"index"` } `toml:"index"`
Versions map[string]string `toml:"versions"` Versions map[string]string `toml:"versions"`
Client map[string]toml.Primitive `toml:"client"` Client map[string]toml.Primitive `toml:"client"`
@ -61,6 +61,12 @@ func (pack Pack) LoadIndex() (Index, error) {
// UpdateIndexHash recalculates the hash of the index file of this modpack // UpdateIndexHash recalculates the hash of the index file of this modpack
func (pack *Pack) UpdateIndexHash() error { func (pack *Pack) UpdateIndexHash() error {
if viper.GetBool("no-internal-hashes") {
pack.Index.HashFormat = "sha256"
pack.Index.Hash = ""
return nil
}
fileNative := filepath.FromSlash(pack.Index.File) fileNative := filepath.FromSlash(pack.Index.File)
indexFile := filepath.Join(filepath.Dir(viper.GetString("pack-file")), fileNative) indexFile := filepath.Join(filepath.Dir(viper.GetString("pack-file")), fileNative)
if filepath.IsAbs(pack.Index.File) { if filepath.IsAbs(pack.Index.File) {

View File

@ -158,7 +158,7 @@ var importCmd = &cobra.Command{
Index: struct { Index: struct {
File string `toml:"file"` File string `toml:"file"`
HashFormat string `toml:"hash-format"` HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"` Hash string `toml:"hash,omitempty"`
}{ }{
File: indexFilePath, File: indexFilePath,
}, },