Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Add no-modrinth flow, start work on updating proxy system
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverAndro committed Feb 17, 2023
1 parent 99fea43 commit 1e231d4
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import kotlin.time.Duration.Companion.milliseconds

object CurseforgeAPI : APIInterface() {
override val ratelimit = Ratelimit(ModifoldArgs.args.curseforgeSpeed.milliseconds, true)
const val root = "https://cfproxy.fly.dev/v1"
const val root = "https://api.cfwidget.com"

private val cache = mutableMapOf<Int, CurseforgeProject?>()

fun getProjectData(id: Int): CurseforgeProject? {
return try {
getWithoutAuth<ProjectWrapper>("$root/mods/$id").data
} catch (ignored: Exception) {
ignored.printStackTrace()
null
return cache.computeIfAbsent(id) {
try {
getWithoutAuth<ProjectWrapper>("$root/$id").data
} catch (ignored: Exception) {
ignored.printStackTrace()
null
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class ModifoldArgsContainer(parser: ArgParser) {
help = "Limits how many files to transfer (recent first), -1 (default) to disable/transfer all"
) { toInt() }.default(-1)

val noModrinth by parser.flagging(
"--no-modrinth",
help = "Disables all functionality related to modrinth"
)

//
// Required args
//
Expand Down
10 changes: 10 additions & 0 deletions CLITools/src/main/kotlin/com/github/p03w/modifold/cli/UI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ fun userUnderstandsUsage(): Boolean {
""".trimIndent()
)
return KInquirer.promptConfirm("I understand this tool should only be used on my own projects")
}

fun userUnderstandsUsageAlternative(): Boolean {
println(
"""
ONLY USE THIS TOOL ON PROJECTS YOU OWN
I built this for honest users who want to move off curseforge, and not for anyone else, as that has significant legal complications.
""".trimIndent()
)
return KInquirer.promptConfirm("I understand this tool should only be used on my own projects")
}
14 changes: 8 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.20"
kotlin("jvm") version "1.8.10"
id("com.github.johnrengelman.shadow") version "7.1.2"
application
}

group = "com.github.p03w"
version = "2.2.3"
version = "2.2.4"

repositories {
mavenCentral()
Expand Down Expand Up @@ -57,13 +57,15 @@ subprojects {
implementation(it)
}
}
}

allprojects {
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "9"
}
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "9"
tasks.withType<JavaCompile> {
targetCompatibility = "9"
}
}

application {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/com/github/p03w/modifold/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ suspend fun main(args: Array<String>) {
}
}

if (ModifoldArgs.args.noModrinth) {
noModrinthFlow()
AnsiConsole.systemUninstall()
return
}

if (!ModifoldArgs.args.donts.contains(DONT.MAP_CATEGORIES)) {
checkForUnknownCategories(ModrinthAPI.getPossibleCategories().mapTo(mutableSetOf()) {it.name})
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/kotlin/com/github/p03w/modifold/NoModrinthFlow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.p03w.modifold

import com.github.p03w.modifold.cli.ModifoldArgs
import com.github.p03w.modifold.cli.ModifoldArgsContainer.*
import com.github.p03w.modifold.cli.log
import com.github.p03w.modifold.cli.userUnderstandsUsageAlternative
import com.github.p03w.modifold.core.collectCurseforgeProjects
import com.github.p03w.modifold.core.getFilesToTransfer
import com.github.p03w.modifold.core.getInfoToSaveLocally
import com.github.p03w.modifold.core.transferProjectFiles
import kotlin.system.exitProcess

fun noModrinthFlow() {
if (!ModifoldArgs.args.donts.contains(DONT.VERIFY_END_USER)) {
if (!userUnderstandsUsageAlternative()) {
println("Quiting")
exitProcess(1)
}
}

val curseforgeProjects = collectCurseforgeProjects(ModifoldArgs.args.curseforgeIDs)

if (curseforgeProjects.isEmpty()) {
error("No curseforge projects")
}

log("Done getting projects")

val toSave = getInfoToSaveLocally()
val toTransfer = getFilesToTransfer()

log("Beginning file \"transfer\"")
transferProjectFiles(mutableMapOf(), toSave, toTransfer, true)

log("Done!")
}
108 changes: 66 additions & 42 deletions src/main/kotlin/com/github/p03w/modifold/core/TransferProjectFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import java.util.*
fun transferProjectFiles(
mapping: MutableMap<CurseforgeProject, ModrinthProject>,
toSave: EnumSet<InformationToSave>,
toTransfer: FileSet
toTransfer: FileSet,
dummy: Boolean = false
) {
fun File.make(): File {
mkdirs()
Expand All @@ -35,57 +36,80 @@ fun transferProjectFiles(
}
}

val modrinthProject = ModrinthAPI.getProjectInfo(mapping[project]!!.id)
if (!dummy) {
val modrinthProject = ModrinthAPI.getProjectInfo(mapping[project]!!.id)

// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}

// Transfer screenshots
project.screenshots.forEach nextAttach@{
withSpinner("Transferring ${it.title} to gallery") { spinner ->
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy = File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
try {
ModrinthAPI.addProjectImage(modrinthProject, it)
} catch (err: Throwable) {
log("Failed to add ${it.title} to gallery! ${err.localizedMessage}".error().toString())
spinner.fail()
// Transfer screenshots
project.screenshots.forEach nextAttach@{
withSpinner("Transferring ${it.title} to gallery") { spinner ->
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy =
File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
try {
ModrinthAPI.addProjectImage(modrinthProject, it)
} catch (err: Throwable) {
log("Failed to add ${it.title} to gallery! ${err.localizedMessage}".error().toString())
spinner.fail()
}
}
}

}
files.forEach { file ->
withSpinner("Transferring ${file.fileName}") { spinner ->
val buffered = CurseforgeAPI.getFileStream(file).buffered()

files.forEach { file ->
withSpinner("Transferring ${file.fileName}") { spinner ->
val buffered = CurseforgeAPI.getFileStream(file).buffered()
val stream = if (toSave.contains(InformationToSave.VERSIONS)) {
debug("Saving version to disk")
val localCopy = File("ModifoldSaved/${project.display()}/versions/${file.fileName}").make()
Files.copy(buffered, localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
localCopy.inputStream().buffered()
} else {
buffered
}

val stream = if (toSave.contains(InformationToSave.VERSIONS)) {
debug("Saving version to disk")
val localCopy = File("ModifoldSaved/${project.display()}/versions/${file.fileName}").make()
Files.copy(buffered, localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
localCopy.inputStream().buffered()
} else { buffered }
try {
ModrinthAPI.makeProjectVersion(
modrinthProject,
file,
stream,
project
)
} catch (err: Throwable) {
log("Failed to upload ${file.fileName}! ${err.localizedMessage}".error().toString())
spinner.fail()
}
}
}
println()
} else {
// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}

try {
ModrinthAPI.makeProjectVersion(
modrinthProject,
file,
stream,
project
)
} catch (err: Throwable) {
log("Failed to upload ${file.fileName}! ${err.localizedMessage}".error().toString())
spinner.fail()
// Transfer screenshots
if (toSave.contains(InformationToSave.IMAGES)) {
project.screenshots.forEach {
withSpinner("Saving gallery image ${it.title}") { spinner ->
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy =
File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
}
}
}
println()
}
}
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3
2.2.4

0 comments on commit 1e231d4

Please sign in to comment.