Switch to forward slashes, add metafile bool

This commit is contained in:
comp500 2019-04-26 18:23:52 +01:00
parent 2f3b80f251
commit e2c772ee30
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5
3 changed files with 25 additions and 20 deletions

View File

@ -22,6 +22,7 @@ type Index struct {
// IndexFile is a file in the index // IndexFile is a file in the index
type IndexFile struct { type IndexFile struct {
// Files are stored in relative forward-slash format to the index file
File string `toml:"file"` File string `toml:"file"`
Hash string `toml:"hash"` Hash string `toml:"hash"`
HashFormat string `toml:"hash-format,omitempty"` HashFormat string `toml:"hash-format,omitempty"`
@ -57,7 +58,7 @@ func (in *Index) RemoveFile(path string) error {
i := 0 i := 0
for _, file := range in.Files { for _, file := range in.Files {
if filepath.Clean(file.File) != relPath { if filepath.Clean(filepath.FromSlash(file.File)) != relPath {
// Keep file, as it doesn't match // Keep file, as it doesn't match
in.Files[i] = file in.Files[i] = file
i++ i++
@ -76,8 +77,7 @@ func (in *Index) resortIndex() {
}) })
} }
// updateFile calculates the hash for a given path relative to the pack folder, // updateFile calculates the hash for a given path and updates it in the index
// and updates it in the index
func (in *Index) updateFile(path string) error { func (in *Index) updateFile(path string) error {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@ -101,7 +101,7 @@ func (in *Index) updateFile(path string) error {
return err return err
} }
for k, v := range in.Files { for k, v := range in.Files {
if filepath.Clean(v.File) == relPath { if filepath.Clean(filepath.FromSlash(v.File)) == relPath {
found = true found = true
// Update hash // Update hash
in.Files[k].Hash = hashString in.Files[k].Hash = hashString
@ -113,14 +113,14 @@ func (in *Index) updateFile(path string) error {
// Mark this file as found // Mark this file as found
in.Files[k].fileExistsTemp = true in.Files[k].fileExistsTemp = true
// Clean up path if it's untidy // Clean up path if it's untidy
in.Files[k].File = relPath in.Files[k].File = filepath.ToSlash(relPath)
// Don't break out of loop, as there may be aliased versions that // Don't break out of loop, as there may be aliased versions that
// also need to be updated // also need to be updated
} }
} }
if !found { if !found {
newFile := IndexFile{ newFile := IndexFile{
File: relPath, File: filepath.ToSlash(relPath),
Hash: hashString, Hash: hashString,
fileExistsTemp: true, fileExistsTemp: true,
} }
@ -128,6 +128,20 @@ func (in *Index) updateFile(path string) error {
if in.HashFormat != "sha256" { if in.HashFormat != "sha256" {
newFile.HashFormat = "sha256" newFile.HashFormat = "sha256"
} }
// If the file is in the mods folder, set MetaFile to true (mods are metafiles by default)
// This is incredibly powerful: you can put a normal jar in the mods folder just by
// setting MetaFile to false. Or you can use the "mod" metadata system for other types
// of files, like CraftTweaker resources.
absFileDir, err := filepath.Abs(filepath.Dir(path))
if err == nil {
absModsDir, err := filepath.Abs(in.flags.ModsFolder)
if err == nil {
if absFileDir == absModsDir {
newFile.MetaFile = true
}
}
}
in.Files = append(in.Files, newFile) in.Files = append(in.Files, newFile)
} }

View File

@ -14,6 +14,7 @@ import (
type Pack struct { type Pack struct {
Name string `toml:"name"` Name string `toml:"name"`
Index struct { Index struct {
// 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"`
@ -47,12 +48,14 @@ func (pack Pack) LoadIndex() (Index, error) {
if filepath.IsAbs(pack.Index.File) { if filepath.IsAbs(pack.Index.File) {
return LoadIndex(pack.Index.File, pack.flags) return LoadIndex(pack.Index.File, pack.flags)
} }
return LoadIndex(filepath.Join(filepath.Dir(pack.flags.PackFile), pack.Index.File), pack.flags) fileNative := filepath.FromSlash(pack.Index.File)
return LoadIndex(filepath.Join(filepath.Dir(pack.flags.PackFile), fileNative), pack.flags)
} }
// 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 {
indexFile := filepath.Join(filepath.Dir(pack.flags.PackFile), pack.Index.File) fileNative := filepath.FromSlash(pack.Index.File)
indexFile := filepath.Join(filepath.Dir(pack.flags.PackFile), fileNative)
if filepath.IsAbs(pack.Index.File) { if filepath.IsAbs(pack.Index.File) {
indexFile = pack.Index.File indexFile = pack.Index.File
} }

View File

@ -13,15 +13,3 @@ func ResolveMod(modName string, flags Flags) string {
return filepath.Join(flags.ModsFolder, fileName) return filepath.Join(flags.ModsFolder, fileName)
} }
// ResolveIndex returns the path to the index file
func ResolveIndex(flags Flags) (string, error) {
pack, err := LoadPack(flags)
if err != nil {
return "", err
}
if filepath.IsAbs(pack.Index.File) {
return pack.Index.File, nil
}
return filepath.Join(filepath.Dir(flags.PackFile), pack.Index.File), nil
}