Index fixes and performance improvements (fixes #223, #224)

- Fixed creation of duplicate index entries when importing from CurseForge (fixes #224)
- Automatically remove duplicates in index
- Fixed `packwiz serve` with a custom `--pack-root` argument (fixes #223)
- Fixed `packwiz serve` with a custom index.toml location
- Cleaned up internal serving code, added comments and better errors
- Refactored path handling code
- Improved refreshing/exporting performance
- Factored out duplicated exporting logic
- Replaced GetAllMods calls with cleaner LoadAllMods calls and made the former private
- Improved variable names in update command
- Improved handling of aliassed files
- Changed CheckUpdate to take references to metadata
- Removed the ability to use an absolute path to the index file (that probably didn't work anyway)
- Behaviour change: order of entries in exported files may be random
This commit is contained in:
comp500
2023-05-29 23:15:55 +01:00
parent d25817273b
commit 4fb1f1b59d
14 changed files with 401 additions and 352 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/packwiz/packwiz/core"
"io"
"os"
"path"
"path/filepath"
)
@@ -29,7 +30,7 @@ func ListManualDownloads(session core.DownloadSession) {
}
}
func AddToZip(dl core.CompletedDownload, exp *zip.Writer, dir string, indexPath string) bool {
func AddToZip(dl core.CompletedDownload, exp *zip.Writer, dir string, index *core.Index) bool {
if dl.Error != nil {
fmt.Printf("Download of %s (%s) failed: %v\n", dl.Mod.Name, dl.Mod.FileName, dl.Error)
return false
@@ -38,24 +39,24 @@ func AddToZip(dl core.CompletedDownload, exp *zip.Writer, dir string, indexPath
fmt.Printf("Warning for %s (%s): %v\n", dl.Mod.Name, dl.Mod.FileName, warning)
}
path, err := filepath.Rel(filepath.Dir(indexPath), dl.Mod.GetDestFilePath())
p, err := index.RelIndexPath(dl.Mod.GetDestFilePath())
if err != nil {
fmt.Printf("Error resolving external file: %v\n", err)
return false
}
modFile, err := exp.Create(filepath.ToSlash(filepath.Join(dir, path)))
modFile, err := exp.Create(path.Join(dir, p))
if err != nil {
fmt.Printf("Error creating metadata file %s: %v\n", path, err)
fmt.Printf("Error creating metadata file %s: %v\n", p, err)
return false
}
_, err = io.Copy(modFile, dl.File)
if err != nil {
fmt.Printf("Error copying file %s: %v\n", path, err)
fmt.Printf("Error copying file %s: %v\n", p, err)
return false
}
err = dl.File.Close()
if err != nil {
fmt.Printf("Error closing file %s: %v\n", path, err)
fmt.Printf("Error closing file %s: %v\n", p, err)
return false
}
@@ -63,6 +64,37 @@ func AddToZip(dl core.CompletedDownload, exp *zip.Writer, dir string, indexPath
return true
}
// AddNonMetafileOverrides saves all non-metadata files into an overrides folder in the zip
func AddNonMetafileOverrides(index *core.Index, exp *zip.Writer) {
for p, v := range index.Files {
if !v.IsMetaFile() {
file, err := exp.Create(path.Join("overrides", p))
if err != nil {
fmt.Printf("Error creating file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
// Attempt to read the file from disk, without checking hashes (assumed to have no errors)
src, err := os.Open(index.ResolveIndexPath(p))
if err != nil {
_ = src.Close()
fmt.Printf("Error reading file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
_, err = io.Copy(file, src)
if err != nil {
_ = src.Close()
fmt.Printf("Error copying file: %s\n", err.Error())
// TODO: exit(1)?
continue
}
_ = src.Close()
}
}
}
func PrintDisclaimer(isCf bool) {
fmt.Println("Disclaimer: you are responsible for ensuring you comply with ALL the licenses, or obtain appropriate permissions, for the files \"added to zip\" below")
if isCf {