Use new SHA1/MD5 hashes from CurseForge API

This commit is contained in:
comp500
2022-02-14 18:21:22 +00:00
parent 9585272311
commit 55ce2fb581
2 changed files with 46 additions and 10 deletions

View File

@@ -124,6 +124,12 @@ const (
modloaderTypeFabric
)
//noinspection GoUnusedConst
const (
hashAlgoSHA1 int = iota + 1
hashAlgoMD5
)
// modInfo is a subset of the deserialised JSON response from the Curse API for mods (addons)
type modInfo struct {
Name string `json:"name"`
@@ -244,6 +250,37 @@ type modFileInfo struct {
ModID int `json:"addonId"`
Type int `json:"type"`
} `json:"dependencies"`
Hashes []struct {
Value string `json:"value"`
Algorithm int `json:"algorithm"`
} `json:"hashes"`
}
func (i modFileInfo) getBestHash() (hash string, hashFormat string) {
// TODO: check if the hash is invalid (e.g. 0)
hash = strconv.Itoa(i.Fingerprint)
hashFormat = "murmur2"
hashPreferred := 0
// Prefer SHA1, then MD5 if found:
if i.Hashes != nil {
for _, v := range i.Hashes {
if v.Algorithm == hashAlgoMD5 && hashPreferred < 1 {
hashPreferred = 1
hash = v.Value
hashFormat = "md5"
} else if v.Algorithm == hashAlgoSHA1 && hashPreferred < 2 {
hashPreferred = 2
hash = v.Value
hashFormat = "sha1"
}
}
}
return
}
func getFileInfo(modID int, fileID int) (modFileInfo, error) {