diff --git a/go.mod b/go.mod index 90d38b5..0c470a7 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module git.merith.xyz/packages/jar-dedupe go 1.20 + +require ( + github.com/pelletier/go-toml v1.9.5 // indirect + github.com/pelletier/go-toml/v2 v2.0.7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cc985f1 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= +github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us= +github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 8e2eb22..4c88406 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,9 @@ import ( "io/ioutil" "log" "os" + "regexp" + + "github.com/pelletier/go-toml/v2" ) // Fabric/Quilt Mod Json @@ -23,10 +26,20 @@ type QuiltModJson struct { } `json:"metadata"` } `json:"quilt_loader"` } +type ForgeModToml struct { + Mods []struct { + ModID string `toml:"modId"` + Version string `toml:"version"` + DisplayName string `toml:"displayName"` + } `toml:"mods"` +} var ( fabricMods []FabricModJson quiltMods []QuiltModJson + forgeMods []ForgeModToml + + cleanPattern = regexp.MustCompile(`[<>;:\"|?*]`) ) // deduplicate mod jars based off the name of the jar @@ -43,9 +56,8 @@ func main() { for _, file := range files { // read the mod.json file and return the name, version, and filename name, version, loader := readMod("./mods/" + file.Name()) + name = cleanPattern.ReplaceAllString(name, "") if loader != "" { - // remove invalid path characters from the name using regex - // name = regexp.MustCompile(`[^\w\-. ]`).ReplaceAllString(name, "") err := os.Rename("./mods/"+file.Name(), "./mods/"+name+"-"+version+".jar") if err != nil { log.Fatalln("[RENAME MOD]", err) @@ -62,7 +74,8 @@ func main() { if mod.Name == mod2.Name && mod.Version != mod2.Version { // if there is a duplicate, delete the one with the lower semver version if mod.Version < mod2.Version { - err := os.Remove("./mods/" + mod.Name + "-" + mod.Version + ".jar") + name := cleanPattern.ReplaceAllString(mod.Name, "") + err := os.Remove("./mods/" + name + "-" + mod.Version + ".jar") if err != nil { log.Fatalln("[REMOVE MOD]", err) } @@ -70,14 +83,13 @@ func main() { } } } - } -// read the mod.json file and return the name, version, and filename - +// read the mod.json file and return the name, version, and loader func readMod(filename string) (string, string, string) { var fabricJson FabricModJson var quiltJson QuiltModJson + var forgeMod ForgeModToml var loader string zipReader, err := zip.OpenReader(filename) if err != nil { @@ -94,6 +106,7 @@ func readMod(filename string) (string, string, string) { log.Println("[DECODE ERROR]", filename) log.Fatalln("[DECODE FABRIC MOD]", fabricJsonErr) } + break } if file.Name == "quilt.mod.json" { loader = "quilt" @@ -103,6 +116,17 @@ func readMod(filename string) (string, string, string) { log.Println("[DECODE ERROR]", filename) log.Fatalln("[DECODE QUILT MOD]", quiltJsonErr) } + break + } + if file.Name == "META-INF/mods.toml" { + loader = "forge" + //add to forge list + forgeTomlErr := toml.NewDecoder(contents).Decode(&forgeMod) + if forgeTomlErr != nil { + log.Println("[DECODE ERROR]", filename) + log.Fatalln("[DECODE FORGE MOD]", forgeTomlErr) + } + break } } @@ -114,5 +138,9 @@ func readMod(filename string) (string, string, string) { quiltMods = append(quiltMods, quiltJson) return quiltJson.QuiltLoader.Metadata.Name, quiltJson.QuiltLoader.Version, loader } + if loader == "forge" { + forgeMods = append(forgeMods, forgeMod) + return forgeMod.Mods[0].DisplayName, forgeMod.Mods[0].Version, loader + } return "", "", "" } diff --git a/quilt.mod.json b/quilt.mod.json deleted file mode 100644 index cf7768a..0000000 --- a/quilt.mod.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "schema_version": 1, - "quilt_loader": { - "group": "eu.midnightdust", - "id": "midnightlib", - "version": "1.3.0", - "intermediate_mappings": "net.fabricmc:intermediary", - "entrypoints": { - "client_init": [ - "eu.midnightdust.quilt.core.MidnightLibClientQuilt" - ], - "server_init": [ - "eu.midnightdust.quilt.core.MidnightLibServerQuilt" - ], - "modmenu": [ - "eu.midnightdust.lib.config.AutoModMenu" - ] - }, - "depends": [ - { - "id": "quilt_loader", - "version": "*" - }, - { - "id": "quilt_base", - "version": "*" - }, - { - "id": "minecraft", - "version": ">=1.19.4" - } - ], - "metadata": { - "name": "MidnightLib (Patched)", - "description": "Common Library for Team MidnightDust's mods. \nProvides a config api, automatic integration with other mods, common utils, and cosmetics.", - "license": "MIT", - "environment": "*", - "contributors": { - "Motschen": "Author", - "TeamMidnightDust": "Mascot" - }, - "contact": { - "email": "mail@midnightdust.eu", - "homepage": "https://modrinth.com/mod/midnightlib", - "issues": "https://github.com/TeamMidnightDust/MidnightLib/issues", - "sources": "https://github.com/TeamMidnightDust/MidnightLib" - }, - "icon": "assets/midnightlib/icon.png" - } - }, - "mixin": [ - "midnightlib.mixins.json" - ], - "modmenu": { - "links": { - "modmenu.discord": "https://discord.midnightdust.eu/", - "modmenu.website": "https://www.midnightdust.eu/", - "midnightlib.curseforge": "https://www.curseforge.com/minecraft/mc-mods/midnightlib", - "midnightlib.modrinth": "https://modrinth.com/mod/midnightlib", - "midnightlib.wiki": "https://github.com/TeamMidnightDust/MidnightLib/wiki" - }, - "badges": [ "library" ] - } - } \ No newline at end of file