From e2c772ee30eceb807d1d84401764abd120859e42 Mon Sep 17 00:00:00 2001 From: comp500 Date: Fri, 26 Apr 2019 18:23:52 +0100 Subject: [PATCH] Switch to forward slashes, add metafile bool --- core/index.go | 26 ++++++++++++++++++++------ core/pack.go | 7 +++++-- core/resolve.go | 12 ------------ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/core/index.go b/core/index.go index dbcecb4..17571a8 100644 --- a/core/index.go +++ b/core/index.go @@ -22,6 +22,7 @@ type Index struct { // IndexFile is a file in the index type IndexFile struct { + // Files are stored in relative forward-slash format to the index file File string `toml:"file"` Hash string `toml:"hash"` HashFormat string `toml:"hash-format,omitempty"` @@ -57,7 +58,7 @@ func (in *Index) RemoveFile(path string) error { i := 0 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 in.Files[i] = file i++ @@ -76,8 +77,7 @@ func (in *Index) resortIndex() { }) } -// updateFile calculates the hash for a given path relative to the pack folder, -// 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 { f, err := os.Open(path) if err != nil { @@ -101,7 +101,7 @@ func (in *Index) updateFile(path string) error { return err } for k, v := range in.Files { - if filepath.Clean(v.File) == relPath { + if filepath.Clean(filepath.FromSlash(v.File)) == relPath { found = true // Update hash in.Files[k].Hash = hashString @@ -113,14 +113,14 @@ func (in *Index) updateFile(path string) error { // Mark this file as found in.Files[k].fileExistsTemp = true // 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 // also need to be updated } } if !found { newFile := IndexFile{ - File: relPath, + File: filepath.ToSlash(relPath), Hash: hashString, fileExistsTemp: true, } @@ -128,6 +128,20 @@ func (in *Index) updateFile(path string) error { if in.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) } diff --git a/core/pack.go b/core/pack.go index 3be83c8..f80b26a 100644 --- a/core/pack.go +++ b/core/pack.go @@ -14,6 +14,7 @@ import ( type Pack struct { Name string `toml:"name"` Index struct { + // Path is stored in forward slash format relative to pack.toml File string `toml:"file"` HashFormat string `toml:"hash-format"` Hash string `toml:"hash"` @@ -47,12 +48,14 @@ func (pack Pack) LoadIndex() (Index, error) { if filepath.IsAbs(pack.Index.File) { 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 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) { indexFile = pack.Index.File } diff --git a/core/resolve.go b/core/resolve.go index e64dbb3..745edf6 100644 --- a/core/resolve.go +++ b/core/resolve.go @@ -13,15 +13,3 @@ func ResolveMod(modName string, flags Flags) string { 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 -} -