Skip to content

Commit

Permalink
Adding Partial matching, Filename Matching, and minor bug fixes (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: christopher blodgett <christopher.blodgett@gmail.com>
  • Loading branch information
shotah and christopher blodgett authored Feb 28, 2022
1 parent 2204980 commit b1fc7e1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ To get started make sure you have a [CursedForge API Key](https://docs.curseforg
- `destination:` Default is OS Client Mod folder, Target folder for the downloaded mods.
- `family:` Used to filter mods based on server type. Options are Forge, Fabric, and Bukkit
- `release:` Default is Release, Used to allow for Beta and Alpha mod downloads.
- `version:` Default is LATEST, but this is Minecraft VERSION. e.g. 1.18.2
- `version:` Default is LATEST, but this is Minecraft VERSION. e.g. 1.18.2,
- PARTIAL matching is enabled, e.g. use 1.18 to pull back 1.18.2, 1.18.1, 1.18 mods
- `clear:` Default is false, allows CLI to remove all mods before downloading new Mods.
- `dependencies:` Default is True, this uses the mods required dependencies to download missing mods.
- `debug:` Enable extra logging.
Expand All @@ -72,7 +73,7 @@ To get started make sure you have a [CursedForge API Key](https://docs.curseforg
**Basic Usage Command**

```bash
. ./forgecli.exe -forgekey '$2a$10...' -file "forgeMods.json" -family "fabric" -debug
. ./forgecli.exe -forgekey '$2a$10...' -file "forgeMods.json" -family "fabric"
```

**Field Description**
Expand Down Expand Up @@ -128,4 +129,6 @@ go build

## TODO List:

- add fileName filter from json mods. Currently we have no method to pin versions.
- ~~add fileName filter from json mods. Currently we have no method to pin versions.~~
- ~~add partial matching for versions 1.18 to pull 1.18.1 and 1.18.2~~
- ~~better output for normal use~~
51 changes: 37 additions & 14 deletions forgecli/forgecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ func (app *appEnv) GetModsByProjectIDs() {
}
projectIDs := strings.Split(app.projectIDs, ",")
for _, projectID := range projectIDs {
logrus.Debugf("Getting Mod: %s", projectID)
err := app.GetModsFromForge(projectID, app.modReleaseType)
var convertedJSONMod JSONMod
convertedJSONMod.ProjectID = projectID
logrus.Debugf("Getting Mod: %s", convertedJSONMod.ProjectID)
err := app.GetModsFromForge(convertedJSONMod, app.modReleaseType)
check(err)
}
}
Expand All @@ -151,7 +153,7 @@ func (app *appEnv) GetModsByJSONFile() {
} else {
releaseType = app.modReleaseType
}
err := app.GetModsFromForge(fileMod.ProjectID, releaseType)
err := app.GetModsFromForge(fileMod, releaseType)
check(err)
}
}
Expand All @@ -163,50 +165,71 @@ func (app *appEnv) GetModsDependencies() error {
for _, mod := range app.modsToDownload {
for _, modDep := range mod.Dependencies {
if modDep.RelationType == 3 {
logrus.Debugf("Getting Mod dependency: %s", strconv.Itoa(modDep.ModID))
err := app.GetModsFromForge(strconv.Itoa(modDep.ModID), releaseLookup["release"])
var convertedJSONMod JSONMod
convertedJSONMod.ProjectID = strconv.Itoa(modDep.ModID)
logrus.Debugf("Getting Mod dependency: %s", convertedJSONMod.ProjectID)
err := app.GetModsFromForge(convertedJSONMod, releaseLookup["release"])
check(err)
}
}
}
return nil
}

func (app *appEnv) GetModsFromForge(modID string, releaseType ReleaseType) error {
func (app *appEnv) GetModsFromForge(modToGet JSONMod, releaseType ReleaseType) error {
var resp ForgeMods
pageIndex := 0
pageSize := 999
url := fmt.Sprintf(
"https://api.curseforge.com/v1/mods/%s/files?gameVersionTypeID=%d&index=%d&pageSize=%d",
modID, app.forgeGameVersionType, pageIndex, pageSize,
modToGet.ProjectID, app.forgeGameVersionType, pageIndex, pageSize,
)
err := app.FetchForgeAPIJSON(url, &resp)
check(err)

foundID := 0
var foundMod ForgeMod
for _, currMod := range resp.Data {
if currMod.ID > foundID && app.ModFilter(currMod) {
if currMod.ID > foundID && app.ModFilter(currMod, modToGet) {
foundID = currMod.ID
foundMod = currMod
}
}
if foundID == 0 {
return fmt.Errorf("could not find %s for minecraft version: %s or family: %s", modID, app.version, app.modfamily)
return fmt.Errorf("could not find %s for minecraft version: %s or family: %s", modToGet.ProjectID, app.version, app.modfamily)
}
app.modsToDownload[foundID] = foundMod
logrus.Infof("Found Lastest FileID: %d for Mod: %s", foundID, modID)
logrus.Infof("Found Lastest FileID: %d for Mod: %s", foundID, modToGet.ProjectID)
return nil
}

func (app *appEnv) ModFilter(currMod ForgeMod) bool {
if app.modfamily != "" {
if contains(currMod.GameVersions, string(app.version)) && contains(currMod.GameVersions, string(app.modfamily)) {
func (app *appEnv) ModFilter(currMod ForgeMod, modToGet JSONMod) bool {
// Filtering on Version, Filename, and Family
if modToGet.Filename != "" && app.modfamily != "" {
if containsPrefix(currMod.GameVersions, string(app.version)) && contains(currMod.GameVersions, string(app.modfamily)) && currMod.Filename == modToGet.Filename {
return true
}
return false
}
if contains(currMod.GameVersions, string(app.version)) {

// Filtering on Version and Family
if app.modfamily != "" && modToGet.Filename == "" {
if containsPrefix(currMod.GameVersions, string(app.version)) && contains(currMod.GameVersions, string(app.modfamily)) {
return true
}
return false
}

// Filtering on Version and Filename
if modToGet.Filename != "" && app.modfamily == "" {
if containsPrefix(currMod.GameVersions, string(app.version)) && currMod.Filename == modToGet.Filename {
return true
}
return false
}

// Filter on just Version
if containsPrefix(currMod.GameVersions, string(app.version)) {
return true
}
return false
Expand Down
15 changes: 14 additions & 1 deletion forgecli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ func contains(s []string, e string) bool {
return false
}

func containsPrefix(s []string, e string) bool {
for _, a := range s {
logrus.Debugf("Checking has prefix: %s for Mod: %s", a, e)
if strings.HasPrefix(a, e) {
logrus.Debug("true")
return true
}
}
logrus.Debug("false")
return false
}

func (app *appEnv) GetTargetDirectory() {
if app.destination != "" {
return
Expand All @@ -52,7 +64,8 @@ func (app *appEnv) GetTargetDirectory() {
case "linux":
app.destination = fmt.Sprintf("%s/Library/Application Support/minecraft/mods", user.HomeDir)
default:
log.Fatalf(fmt.Sprintf("%s does not have a default directory, please provide target directory", os))
err := fmt.Errorf("%s does not have a default directory, please provide target directory", os)
check(err)
}
}

Expand Down

0 comments on commit b1fc7e1

Please sign in to comment.