Skip to content

Commit

Permalink
chore: fix a few typos, add docs for FileDownloader, outdated comment…
Browse files Browse the repository at this point in the history
…s, the Proguard obfuscation mapping now follow the Proguard JAR file name.
  • Loading branch information
Ellet committed Jun 22, 2024
1 parent 84486bb commit c58c733
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Let's say you're running a Minecraft server and every time you add, remove, upda
player has to make the changes manually each time you do it or let's say you're changing the server address, the
players have to manually update it in the game.

It's designed primarily for server owners, yet it can also be effective for mod-pack developers.
The script will run before each time when launching the game will make a `GET` request to a file you upload somewhere (
GitHub for example) and it contains information about the sync info like the mods and some other configurations that
allow you to customize the behavior, you don't need to upload the mods anywhere it will simply need a public download
URL for the mod (For example CurseForge or Modrinth) and it will download the mod from there.
It's designed for server owners, yet it can also be effective for mod-pack developers and other use cases.
The script will run before each time when launching the game will make a `GET` request to a file you upload somewhere
(GitHub for example) and it contains information about the sync information like the **Mods** and other
configurations that allow you to customize the behavior, you don't need to upload the mods anywhere it will need
a public download URL for the mod (e.g, **Modrinth**) and it will download the mod directly from there.

You don't have to do that manually, see [Mods Info Converter] 💾 for more
info.
Expand Down
34 changes: 16 additions & 18 deletions admin/src/main/kotlin/launchers/atLauncher/ATLauncherDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@ class ATLauncherDataSource : LauncherDataSource {
*
* @see ATLauncherInstance.Launcher.Mod.Type
* */
private fun getATLauncherMods(instance: ATLauncherInstance): List<ATLauncherInstance.Launcher.Mod> {
return instance.launcher.mods
private fun getATLauncherMods(instance: ATLauncherInstance): List<ATLauncherInstance.Launcher.Mod> =
instance.launcher.mods
.filter { it.type == ATLauncherInstance.Launcher.Mod.Type.Mods }
}

override suspend fun isValidInstanceDirectory(launcherInstanceDirectory: File): Result<Unit> {
return try {
override suspend fun isValidInstanceDirectory(launcherInstanceDirectory: File): Result<Unit> =
try {
getInstance(launcherInstanceDirectory = launcherInstanceDirectory).getOrThrow()
Result.success(Unit)
} catch (e: Exception) {
e.printStackTrace()
Result.failure(e)
}
}

override suspend fun isCurseForgeApiRequestNeeded(launcherInstanceDirectory: File): Result<Boolean> {
return try {
override suspend fun isCurseForgeApiRequestNeeded(launcherInstanceDirectory: File): Result<Boolean> =
try {
val instance = getInstance(launcherInstanceDirectory = launcherInstanceDirectory).getOrThrow()
val isCurseForgeApiRequestNeeded =
getATLauncherMods(instance).any { atLauncherMod ->
Expand All @@ -59,14 +57,13 @@ class ATLauncherDataSource : LauncherDataSource {
e.printStackTrace()
Result.failure(e)
}
}

// TODO: Needs to be tested with Curse Forge and Modrinth with resource-packs at the same time, also convert/import more data like description field
override suspend fun getMods(
launcherInstanceDirectory: File,
overrideCurseForgeApiKey: String?,
): Result<List<Mod>> {
return try {
): Result<List<Mod>> =
try {
val instance = getInstance(launcherInstanceDirectory = launcherInstanceDirectory).getOrThrow()
val mods =
instance.launcher.mods
Expand All @@ -80,19 +77,21 @@ class ATLauncherDataSource : LauncherDataSource {
// ATLauncher always store the info for both CurseForge and Modrinth even if it's downloaded
// from CurseForge, we will use Curse Forge instead if the file doesn't exist for some reason
val curseForgeModFile =
curseForgeDataSource.getModFile(
modId = atLauncherMod.curseForgeProjectId.toString(),
fileId = atLauncherMod.curseForgeFileId.toString(),
overrideApiKey = overrideCurseForgeApiKey,
).getOrThrow()
curseForgeDataSource
.getModFile(
modId = atLauncherMod.curseForgeProjectId.toString(),
fileId = atLauncherMod.curseForgeFileId.toString(),
overrideApiKey = overrideCurseForgeApiKey,
).getOrThrow()
downloadUrl = curseForgeModFile.data.downloadUrl
fileIntegrityInfo = curseForgeModFile.data.getFileIntegrityInfo()
}

requireNotNull(downloadUrl)
requireNotNull(fileIntegrityInfo)

// ATLauncher seems to always store the modrinth project regarding if it's downloaded by Curse Forge or Modrinth
// ATLauncher seems to always store the Modrinth project,
// regardless of whether it's downloaded from CurseForge or Modrinth.
val modrinthProject = atLauncherMod.modrinthProject

Mod(
Expand All @@ -108,5 +107,4 @@ class ATLauncherDataSource : LauncherDataSource {
} catch (e: Exception) {
Result.failure(e)
}
}
}
8 changes: 4 additions & 4 deletions sync-script/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,17 @@ val minimizedJar =
dependsOn(tasks.shadowJar)

val fatJarFile = tasks.shadowJar.flatMap { it.archiveFile }
val fatJarFileNameWithoutExtension = fatJarFile.get().asFile.nameWithoutExtension
val fatJarFileDestinationDirectory = tasks.shadowJar.get().destinationDirectory

val minimizedJarFile =
getMinimizedJarFile(
fatJarFileNameWithoutExtension = fatJarFileNameWithoutExtension,
fatJarFileNameWithoutExtension = fatJarFile.get().asFile.nameWithoutExtension,
fatJarFileDestinationDirectory = fatJarFileDestinationDirectory,
)

injars(fatJarFile)
outjars(minimizedJarFile)

// TODO: Improve this, avoid hardcoding
val javaHome = System.getProperty("java.home")
if (System.getProperty("java.version").startsWith("1.")) {
// Before Java 9, runtime classes are packaged in a single JAR file.
Expand Down Expand Up @@ -147,7 +145,9 @@ val minimizedJar =
// is essential for resolving Kotlin and other library warnings without using '-dontwarn kotlin.**'
injars(sourceSets.main.get().compileClasspath)

printmapping(fatJarFileDestinationDirectory.get().file("$fatJarFileNameWithoutExtension.map"))
printmapping(
fatJarFileDestinationDirectory.get().file("${minimizedJarFile.get().asFile.nameWithoutExtension}.map"),
)

// Disabling obfuscation makes the JAR file size a bit larger, and the debugging process a bit less easy
// dontobfuscate()
Expand Down
4 changes: 1 addition & 3 deletions sync-script/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ suspend fun main(args: Array<String>) {

println("\uFE0F Script Configuration: ${ScriptConfig.instance}")

// This will not show as the script is configured to disable GUI mode on headless environments

// Switch to the themes specified by config
if (GuiState.isGuiEnabled) {
GuiUtils.applyThemeIfNeeded(
Expand Down Expand Up @@ -249,7 +247,7 @@ suspend fun main(args: Array<String>) {

syncServices.forEach { it.syncData() }

// Finally, finish the script
// Finish the script

// The temporary folder usually contains the downloaded files which will be moved once finished
// after finish syncing the contents successfully, we don't need it anymore.
Expand Down
7 changes: 3 additions & 4 deletions sync-script/src/main/kotlin/syncService/ModsSyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ModsSyncService : SyncService {

private val syncInfo = SyncInfo.instance

override suspend fun syncData() {
return withContext(Dispatchers.IO) {
override suspend fun syncData() =
withContext(Dispatchers.IO) {
val modsExecutionTimer = ExecutionTimer()
modsExecutionTimer.setStartTime()
println("\n\uD83D\uDD04 Syncing mods...")
Expand Down Expand Up @@ -70,7 +70,6 @@ class ModsSyncService : SyncService {

println("\uD83D\uDD52 Finished syncing the mods in ${modsExecutionTimer.getRunningUntilNowDuration().inWholeMilliseconds}ms.")
}
}

private fun validateModsFolder() {
if (!modsFolder.exists()) {
Expand Down Expand Up @@ -223,7 +222,7 @@ class ModsSyncService : SyncService {
},
).downloadFile()

// Notice that currently will always validate newly downloaded mods regarding of the configurations
// This will always validate newly downloaded mods regardless of the configurations
val isNewlyDownloadedFileHasValidFileIntegrity = mod.hasValidFileIntegrityOrError(modFile)
if (isNewlyDownloadedFileHasValidFileIntegrity == false) {
showErrorMessageAndTerminate(
Expand Down
12 changes: 10 additions & 2 deletions sync-script/src/main/kotlin/utils/FileDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption

// TODO: Add the option to cancel a download, might need to refactor this class too, close using response.body.close(quietly)
/**
* A utility class for downloading files from [downloadUrl] to [targetFile] with [progressListener].
* TODO: Add the option to cancel a download, might need to refactor this class too
*
* Currently will handle errors internally by showing a error message and close.
* This might change in case
* we want to share this with other modules.
* */
class FileDownloader(
private val downloadUrl: String,
private val targetFile: File,
Expand All @@ -31,7 +38,8 @@ class FileDownloader(
)
}
val request =
Request.Builder()
Request
.Builder()
.url(downloadUrl)
.build()

Expand Down

0 comments on commit c58c733

Please sign in to comment.