NN.Curseforge.psm1

#Region './Private/Get-CfAccessToken.ps1' 0
function Get-CfAccessToken {
    [CmdletBinding()]
    param (
        [string]$AccessTokenPath = "$env:USERPROFILE\.creds\Curseforge\CurseforgeAccessToken.xml"
    )

    process {
        if (!(Test-Path $AccessTokenPath)) {
            New-CfAccessToken
        }
    
        Import-Clixml $AccessTokenPath | ConvertFrom-SecureString -AsPlainText
    }
}
#EndRegion './Private/Get-CfAccessToken.ps1' 15
#Region './Public/Get-CfGames.ps1' 0
function Get-CfGames {
    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName="List games")][int]$index,
        [Parameter(ParameterSetName="List games")][int]$pageSize,
        [Parameter(Mandatory,ParameterSetName="Get game by id")][int]$gameId
    )

    process {
        $uri = "https://api.curseforge.com/v1/games"

        switch ($PsCmdlet.ParameterSetName) {
            "List games" {
                $PSBoundParameters.Keys.ForEach({
                    $key = $_
                    $value = $PSBoundParameters.$key
    
                    if (([array]$PSBoundParameters.Keys)[0] -eq $key) {
                        $delimiter = "?"
                    } else {
                        $delimiter = "&"
                    }
    
                    $uri = "$uri$delimiter$key=$value"
                })
            }
            "Get game by id" {
                $uri = "$uri/$gameId"
            }
        }

        $splat = @{
            "Method" = "GET"
            "Uri" = $uri
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
            }
        }
        $result = Invoke-RestMethod @splat
        $result.data
    }
}
#EndRegion './Public/Get-CfGames.ps1' 43
#Region './Public/Get-CfGameVersions.ps1' 0
function Get-CfGameVersions {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][int]$gameId
    )

    process {
        $splat = @{
            "Method" = "GET"
            "Uri" = "https://api.curseforge.com/v1/games/$gameId/versions"
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
            }
        }
        $result = Invoke-RestMethod @splat
        $result.data
    }
}
#EndRegion './Public/Get-CfGameVersions.ps1' 19
#Region './Public/Get-CfGameVersionTypes.ps1' 0
function Get-CfGameVersionTypes {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][int]$gameId
    )

    process {
        $splat = @{
            "Method" = "GET"
            "Uri" = "https://api.curseforge.com/v1/games/$gameId/version-types"
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
            }
        }
        $result = Invoke-RestMethod @splat
        $result.data
    }
}
#EndRegion './Public/Get-CfGameVersionTypes.ps1' 19
#Region './Public/Get-CfMcModloaders.ps1' 0
function Get-CfMcModloaders {
    [CmdletBinding(DefaultParameterSetName="List modloaders")]
    param (
        [Parameter(ParameterSetName="List modloaders")][string]$version,
        [Parameter(ParameterSetName="List modloaders")][bool]$includeAll = $true,
        [Parameter(Mandatory,ParameterSetName="Get specific modloader")][string]$modLoaderName
    )

    process {
        $Uri = "https://api.curseforge.com/v1/minecraft/modloader"

        switch ($PsCmdlet.ParameterSetName) {
            "List modloaders" {
                #Build request Uri
                $PSBoundParameters.Keys.ForEach({
                    [string]$Key = $_
                    [string]$Value = $PSBoundParameters.$key

                    #Check for "?" in Uri and set delimiter
                    if (!($Uri -replace "[^?]+")) {
                        $Delimiter = "?"
                    }
                    else {
                        $Delimiter = "&"
                    }

                    $Uri = "$Uri$Delimiter$Key=$Value"
                })
            }
            "Get specific modloader" {
                $Uri = "$Uri/$ModLoaderName"
            }
        }

        $splat = @{
            "Method"  = "GET"
            "Uri"     = $Uri
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
            }
        }
        $Result = Invoke-RestMethod @splat
        $Result.data
    }
}
#EndRegion './Public/Get-CfMcModloaders.ps1' 46
#Region './Public/Get-CfMcVersions.ps1' 0
function Get-CfMcVersions {
    [CmdletBinding()]
    param (
        [bool]$sortDescending
    )

    process {
        $Uri = "https://api.curseforge.com/v1/minecraft/version"

        #Build request Uri
        $PSBoundParameters.Keys.ForEach({
            [string]$Key = $_
            [string]$Value = $PSBoundParameters.$key

            #Check for "?" in Uri and set delimiter
            if (!($Uri -replace "[^?]+")) {
                $Delimiter = "?"
            }
            else {
                $Delimiter = "&"
            }

            $Uri = "$Uri$Delimiter$Key=$Value"
        })

        $splat = @{
            "Method"  = "GET"
            "Uri"     = $Uri
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
            }
        }
        $Result = Invoke-RestMethod @splat
        $Result.data
    }
}
#EndRegion './Public/Get-CfMcVersions.ps1' 37
#Region './Public/Get-CfModFiles.ps1' 0
function Get-CfModFiles {
    [CmdletBinding(DefaultParameterSetName="List mod files")]
    param (
        [Parameter(Mandatory,ParameterSetName="List mod files")][int]$modId,
        [Parameter(ParameterSetName="List mod files")][string]$gameVersion,
        [Parameter(ParameterSetName="List mod files")][int]$modLoaderType,
        [Parameter(ParameterSetName="List mod files")][int]$gameVersionTypeId,
        [Parameter(ParameterSetName="List mod files")][int]$index,
        [Parameter(ParameterSetName="List mod files")][int]$pageSize,
        [Parameter(Mandatory,ParameterSetName="Get mod file")]
        [Parameter(Mandatory,ParameterSetName="Get mod file download url")]$fileId
    )

    process {
        $Uri = "https://api.curseforge.com/v1/mods/$ModId/files"

        switch ($PsCmdlet.ParameterSetName) {
            "List mod files" {
                #Parameters to exclude in Uri build
                $ParameterExclusion = @("ModId")

                #Build request Uri
                $PSBoundParameters.Keys.ForEach({
                    $Key = $_
                    $Value = $PSBoundParameters.$key

                    #Check if parameter is excluded
                    if ($ParameterExclusion -contains $Key) {
                        return
                    }

                    #Check for "?" in Uri and set delimiter
                    if (!($Uri -replace "[^?]+")) {
                        $Delimiter = "?"
                    }
                    else {
                        $Delimiter = "&"
                    }

                    $Uri = "$Uri$Delimiter$Key=$Value"
                })
            }
            "Get mod file" {
                $Uri = "$Uri/$FileId"
            }
            "Get mod file download url" {
                $Uri = "$Uri/$FileId/download-url"
            }
        }

        $splat = $BodySplat + @{
            "Method" = "GET"
            "Uri" = $Uri
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
                "Content-Type" = "application/json"
            }
        }
        $result = Invoke-RestMethod @splat
        $result.data
    }
}
#EndRegion './Public/Get-CfModFiles.ps1' 63
#Region './Public/Get-CfMods.ps1' 0
function Get-CfMods {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory,ParameterSetName="Search mods")]$gameId,
        [Parameter(ParameterSetName="Search mods")]$classId,
        [Parameter(ParameterSetName="Search mods")]$categoryId,
        [Parameter(ParameterSetName="Search mods")]$gameVersion,
        [Parameter(ParameterSetName="Search mods")]$searchFilter,
        [Parameter(ParameterSetName="Search mods")]$sortField,
        [Parameter(ParameterSetName="Search mods")][ValidateSet("asc","desc")]$sortOrder,
        [Parameter(ParameterSetName="Search mods")][int]$modLoaderType,
        [Parameter(ParameterSetName="Search mods")][int]$gameVersionTypeId,
        [Parameter(ParameterSetName="Search mods")][string]$slug,
        [Parameter(ParameterSetName="Search mods")][int]$index,
        [Parameter(ParameterSetName="Search mods")][int]$pageSize,
        [Parameter(Mandatory,ParameterSetName="Get mod by id")]$modId,
        [Parameter(Mandatory,ParameterSetName="Get a list of mods")][array]$modIds
    )

    process {
        $uri = "https://api.curseforge.com/v1/mods"

        switch ($PsCmdlet.ParameterSetName) {
            "Search mods" {
                $Method = "GET"
                $uri = "$uri/search"
                #Build request Uri
                $PSBoundParameters.Keys.ForEach({
                    $Key = $_
                    $Value = $PSBoundParameters.$key

                    #Check for "?" in Uri and set delimiter
                    if (!($Uri -replace "[^?]+")) {
                        $Delimiter = "?"
                    }
                    else {
                        $Delimiter = "&"
                    }

                    $Uri = "$Uri$Delimiter$Key=$Value"
                })
            }
            "Get mod by id" {
                $Method = "GET"
                $uri = "$uri/$modId"
            }
            "Get a list of mods" {
                $Method = "POST"
                $BodySplat = @{
                    "Body" = @{
                        "modIds" = $modIds
                    } | ConvertTo-Json
                }
            }
        }

        $splat = $BodySplat + @{
            "Method" = $Method
            "Uri" = $uri
            "Headers" = @{
                "x-api-key" = Get-CfAccessToken
                "Content-Type" = "application/json"
            }
        }
        $result = Invoke-RestMethod @splat
        $result.data
    }
}
#EndRegion './Public/Get-CfMods.ps1' 69
#Region './Public/New-CfAccessToken.ps1' 0
function New-CfAccessToken {
    [CmdletBinding()]
    param (
        [string]$AccessTokenPath = "$env:USERPROFILE\.creds\Curseforge\CurseforgeAccessToken.xml"
    )

    process {
        $ApiKey = Read-Host "Enter Curseforge API key" -AsSecureString
    
        #Create parent folders of the access token file
        $AccessTokenDir = $AccessTokenPath.Substring(0, $AccessTokenPath.lastIndexOf('\'))
        if (!(Test-Path $AccessTokenDir)) {
            $null = New-Item -ItemType Directory $AccessTokenDir
        }
    
        #Create access token file
        $ApiKey | Export-Clixml $AccessTokenPath
    }
}
#EndRegion './Public/New-CfAccessToken.ps1' 20