Skip to content

Commit

Permalink
chore: better naming & types
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Oct 29, 2024
1 parent 8c882a6 commit 2090a1b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ data class MrModpackModel(
runBlocking {
debug { println("CurseForge sub-import") }

val slugs = projects.mapNotNull { project ->
val projectToSlugs = projects.mapNotNull { project ->
project.slug[Modrinth.serialName]?.let { project to it }
}

val cfProjects = slugs.map { slug ->
val cfProjects = projectToSlugs.map { (project, slug) ->
async {
CurseForge.requestProjectFromSlug(slug.second)?.apply {
CurseForge.requestProjectFromSlug(slug)?.apply {
files += CurseForge.requestFilesForProject(
lockFile.getMcVersions(), lockFile.getLoaders(), this, projectType = slug.first.type
lockFile.getMcVersions(), lockFile.getLoaders(), this, projectType = project.type
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ object CurseForge : Platform(
} ?: true // If no loaders found, accept model
}

internal fun compareByLoaders(loaders: List<String>) = { file: CfModModel.File ->
internal fun compareByLoaders(loaders: List<String>): (CfModModel.File) -> Comparable<*> = { file: CfModModel.File ->
val fileLoaders = file.sortableGameVersions.filter { it.gameVersionTypeId == LOADER_VERSION_TYPE_ID }
.map { it.gameVersionName.lowercase() }
loaders.indexOfFirst { it in fileLoaders }.let { if (it == -1) loaders.size else it }
Expand Down Expand Up @@ -219,7 +219,7 @@ object CurseForge : Platform(
}

override suspend fun requestMultipleProjectFiles(
mcVersions: List<String>, loaders: List<String>, projectInfos: Map<String, ProjectType?>, ids: List<String>
mcVersions: List<String>, loaders: List<String>, projectIdsToTypes: Map<String, ProjectType?>, ids: List<String>
): MutableSet<ProjectFile>
{
// Handle mcVersions
Expand All @@ -240,19 +240,19 @@ object CurseForge : Platform(
}

override suspend fun requestMultipleProjectsWithFiles(
mcVersions: List<String>, loaders: List<String>, projectInfos: Map<String, ProjectType?>, numberOfFiles: Int
mcVersions: List<String>, loaders: List<String>, projectIdsToTypes: Map<String, ProjectType?>, numberOfFiles: Int
): MutableSet<Project>
{
val response = json.decodeFromString<GetMultipleProjectsResponse>(
this.requestProjectBody("mods", MultipleProjectsRequest(projectInfos.keys.map(String::toInt)))
this.requestProjectBody("mods", MultipleProjectsRequest(projectIdsToTypes.keys.map(String::toInt)))
?: return mutableSetOf()
).data

val fileIds = response.flatMap { model ->
model.latestFilesIndexes.map { it.fileId.toString() }
}

val projectFiles = requestMultipleProjectFiles(mcVersions, loaders, projectInfos, fileIds)
val projectFiles = requestMultipleProjectFiles(mcVersions, loaders, projectIdsToTypes, fileIds)
val projects = response.mapNotNull { it.toProject() }

projects.assignFiles(projectFiles, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object GitHub : Http(), Provider
loaders: List<String>,
input: String,
fileId: String?,
numberOfFiles: Int ,
numberOfFiles: Int,
projectType: ProjectType?
): Project?
{
Expand Down
30 changes: 18 additions & 12 deletions src/commonMain/kotlin/teksturepako/pakku/api/platforms/Modrinth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ object Modrinth : Platform(
} ?: true // If no loaders found, accept model
}

internal fun compareByLoaders(loaders: List<String>) = if (loaders.size <= 1)
internal fun compareByLoaders(loaders: List<String>): (MrVersionModel) -> Comparable<*> = if (loaders.size <= 1)
{
{ 0 }
}
Expand Down Expand Up @@ -187,7 +187,12 @@ object Modrinth : Platform(
mcVersions: List<String>, loaders: List<String>, projectId: String, fileId: String?, projectType: ProjectType?
): MutableSet<ProjectFile>
{
val actualLoaders = projectType?.let { if (it == ProjectType.DATA_PACK) listOf(DATAPACK_LOADER) else null } ?: loaders
val actualLoaders = when (projectType)
{
ProjectType.DATA_PACK -> listOf(DATAPACK_LOADER)
else -> loaders
}

return if (fileId == null)
{
// Multiple files
Expand All @@ -213,10 +218,10 @@ object Modrinth : Platform(
}

override suspend fun requestMultipleProjectFiles(
mcVersions: List<String>, loaders: List<String>, projectInfos: Map<String, ProjectType?>, ids: List<String>
mcVersions: List<String>, loaders: List<String>, projectIdsToTypes: Map<String, ProjectType?>, ids: List<String>
): MutableSet<ProjectFile> = coroutineScope {
val loadersWithType =
(if (projectInfos.values.any { it == ProjectType.DATA_PACK }) listOf(DATAPACK_LOADER) else listOf()) + loaders
(if (projectIdsToTypes.values.any { it == ProjectType.DATA_PACK }) listOf(DATAPACK_LOADER) else listOf()) + loaders
// Chunk requests if there are too many ids; Also do this in parallel
return@coroutineScope ids.chunked(1_000).map { list ->
async {
Expand All @@ -233,29 +238,30 @@ object Modrinth : Platform(
.sortedWith(
compareByDescending<MrVersionModel> { Instant.parse(it.datePublished) }
.thenBy { file ->
compareByLoaders(projectInfos[file.projectId]?.let {
if (it == ProjectType.DATA_PACK) listOf(
DATAPACK_LOADER
)
else null
compareByLoaders(projectIdsToTypes[file.projectId]?.let {
when (it)
{
ProjectType.DATA_PACK -> listOf(DATAPACK_LOADER)
else -> null
}
} ?: loaders)(file)
})
.flatMap { version -> version.toProjectFiles() }
.toMutableSet()
}

override suspend fun requestMultipleProjectsWithFiles(
mcVersions: List<String>, loaders: List<String>, projectInfos: Map<String, ProjectType?>, numberOfFiles: Int
mcVersions: List<String>, loaders: List<String>, projectIdsToTypes: Map<String, ProjectType?>, numberOfFiles: Int
): MutableSet<Project>
{
val url = encode("projects?ids=${projectInfos.keys.map { "\"$it\"" }}".filterNot { it.isWhitespace() }, allow = "?=")
val url = encode("projects?ids=${projectIdsToTypes.keys.map { "\"$it\"" }}".filterNot { it.isWhitespace() }, allow = "?=")

val response = json.decodeFromString<List<MrProjectModel>>(
this.requestProjectBody(url) ?: return mutableSetOf()
)

val fileIds = response.flatMap { it.versions }
val projectFiles = requestMultipleProjectFiles(mcVersions, loaders, projectInfos, fileIds)
val projectFiles = requestMultipleProjectFiles(mcVersions, loaders, projectIdsToTypes, fileIds)
val projects = response.mapNotNull { it.toProject() }

projects.assignFiles(projectFiles, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract class Platform(
): MutableSet<ProjectFile>

abstract suspend fun requestMultipleProjectFiles(
mcVersions: List<String>, loaders: List<String>, projectInfos: Map<String, ProjectType?>, ids: List<String>
mcVersions: List<String>, loaders: List<String>, projectIdsToTypes: Map<String, ProjectType?>, ids: List<String>
): MutableSet<ProjectFile>


Expand Down Expand Up @@ -95,7 +95,7 @@ abstract class Platform(
}

abstract suspend fun requestMultipleProjectsWithFiles(
mcVersions: List<String>, loaders: List<String>, projectInfos: Map<String, ProjectType?>, numberOfFiles: Int
mcVersions: List<String>, loaders: List<String>, projectIdsToTypes: Map<String, ProjectType?>, numberOfFiles: Int
): MutableSet<Project>

companion object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface Provider
val siteUrl: String?

/** Requests a [project][Project] based on provided [input]. */
suspend fun requestProject(input: String, projectType: ProjectType?): Project?
suspend fun requestProject(input: String, projectType: ProjectType? = null): Project?

/**
* Requests project with files for specified combinations of Minecraft versions and mod loaders.
Expand Down
8 changes: 4 additions & 4 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/Add.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Add : CliktCommand()

private val noDepsFlag: Boolean by option("-D", "--no-deps", help = "Ignore resolving dependencies").flag()

private val type: ProjectType? by option(
private val projectTypeOpt: ProjectType? by option(
"-t",
"--type",
help = "Project type of projects to add",
Expand Down Expand Up @@ -82,7 +82,7 @@ class Add : CliktCommand()
{
suspend fun handleMissingProject(error: NotFoundOn, arg: ProjectArg)
{
val prompt = promptForProject(error.provider, terminal, lockFile, arg.fold({it.fileId}, {it.tag}), type).onFailure {
val prompt = promptForProject(error.provider, terminal, lockFile, arg.fold({it.fileId}, {it.tag}), projectTypeOpt).onFailure {
if (it is EmptyArg) return add(projectIn, arg, strict = false)
}.getOrElse {
return terminal.pError(it)
Expand Down Expand Up @@ -132,12 +132,12 @@ class Add : CliktCommand()
arg.fold(
commonArg = {
projectProvider.requestProjectWithFiles(
lockFile.getMcVersions(), lockFile.getLoaders(), it.input, it.fileId, projectType = type
lockFile.getMcVersions(), lockFile.getLoaders(), it.input, it.fileId, projectType = projectTypeOpt
) to it
},
gitHubArg = {
GitHub.requestProjectWithFiles(
listOf(), listOf(), "${it.owner}/${it.repo}", it.tag, projectType = type
listOf(), listOf(), "${it.owner}/${it.repo}", it.tag, projectType = projectTypeOpt
) to it
}
)
Expand Down
10 changes: 5 additions & 5 deletions src/commonMain/kotlin/teksturepako/pakku/cli/cmd/AddPrj.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AddPrj : CliktCommand("prj")
}
}

private val type: ProjectType? by option(
private val projectTypeOpt: ProjectType? by option(
"-t",
"--type",
help = "Project type of project to add",
Expand Down Expand Up @@ -100,7 +100,7 @@ class AddPrj : CliktCommand("prj")
{
suspend fun handleMissingProject(error: NotFoundOn)
{
val prompt = promptForProject(error.provider, terminal, lockFile, projectType = type).onFailure {
val prompt = promptForProject(error.provider, terminal, lockFile, projectType = projectTypeOpt).onFailure {
if (it is EmptyArg) return add(projectIn, strict = false)
}.getOrElse {
return terminal.pError(it)
Expand Down Expand Up @@ -147,19 +147,19 @@ class AddPrj : CliktCommand("prj")

val cf = cfOpt?.let {
CurseForge.requestProjectWithFiles(
lockFile.getMcVersions(), lockFile.getLoaders(), it.input, it.fileId, projectType = type
lockFile.getMcVersions(), lockFile.getLoaders(), it.input, it.fileId, projectType = projectTypeOpt
)
}

val mr = mrOpt?.let {
Modrinth.requestProjectWithFiles(
lockFile.getMcVersions(), lockFile.getLoaders(), it.input, it.fileId, projectType = type
lockFile.getMcVersions(), lockFile.getLoaders(), it.input, it.fileId, projectType = projectTypeOpt
)
}

val gh = ghOpt?.let {
GitHub.requestProjectWithFiles(
listOf(), listOf(), "${it.owner}/${it.repo}", it.tag, projectType = type
listOf(), listOf(), "${it.owner}/${it.repo}", it.tag, projectType = projectTypeOpt
)
}

Expand Down

0 comments on commit 2090a1b

Please sign in to comment.