Skip to content

Commit

Permalink
Feat: migrate to Java NIO (#10)
Browse files Browse the repository at this point in the history
* refactor(io): Update ModsSyncService, and HashGenerator to use Path Java class

* chore: fix an unrelated regression caused by the previous PR

* chore: update converting mods Mods unavailable error message

* chore(script):  one variable (localModFilePathsToProcess) in deleteUnSyncedLocalModFiles() instead of two
  • Loading branch information
EchoEllet authored Jun 27, 2024
1 parent c093960 commit c865642
Show file tree
Hide file tree
Showing 43 changed files with 805 additions and 577 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import javax.swing.JComponent
import javax.swing.JFileChooser
import javax.swing.JTextField
import javax.swing.plaf.basic.BasicComboBoxEditor
import kotlin.io.path.absolutePathString

fun instanceDirectoryInputField(
inputComboBox: JComboBox<ComboItem<Instance>>,
Expand Down Expand Up @@ -106,7 +107,7 @@ fun instanceDirectoryInputField(
// The second which is changed in this lambda, which is why we need to check to avoid
// unexpected error on the second change. The (item) will be null if the selected item
// is null or is different from the JComboBox<E> type of the elements
selectedItem = item.value.launcherInstanceDirectory.absolutePath
selectedItem = item.value.launcherInstanceDirectoryPath.absolutePathString()
}
},
JButton("Browse").onClick {
Expand All @@ -116,7 +117,7 @@ fun instanceDirectoryInputField(
fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
}
val result = fileChooser.showOpenDialog(parentComponent)
val selectedFile =
val selectedFilePath =
fileChooser.handleResult(
result = result,
onErrorWhileChoosingFile = {
Expand All @@ -127,7 +128,7 @@ fun instanceDirectoryInputField(
)
},
) ?: return@onClick
inputComboBox.selectedItem = selectedFile.absolutePath
inputComboBox.selectedItem = selectedFilePath.absolutePathString()
},
),
preferredLabelWidth = preferredLabelWidth,
Expand Down
9 changes: 5 additions & 4 deletions admin/src/main/kotlin/gui/tabs/ModsConverterTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import javax.swing.JComboBox
import javax.swing.JFileChooser
import javax.swing.JPanel
import javax.swing.filechooser.FileNameExtensionFilter
import kotlin.io.path.writeText

class ModsConverterTab : Tab() {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
Expand Down Expand Up @@ -126,7 +127,7 @@ class ModsConverterTab : Tab() {
val result =
ModsConverterInstance.convertMods(
launcher = launcherComboBox.getSelectedItemOrThrow(),
launcherInstanceDirectoryPath =
launcherInstanceDirectoryPathString =
(launcherInstanceDirectoryComboBox.selectedItem as? String) ?: throw IllegalStateException(
"The selected item of ${::launcherInstanceDirectoryComboBox.name} is null",
),
Expand Down Expand Up @@ -209,7 +210,7 @@ class ModsConverterTab : Tab() {
"Some launchers might save the changes after closing the launcher/app.",
)
newLine()
text("If you created the instance/profile recently, try closing the launcher and try again.")
text("If you created the instance/profile recently, close the launcher and try again.")
}.buildBodyAsText(),
parentComponent = this@ModsConverterTab,
)
Expand Down Expand Up @@ -299,13 +300,13 @@ class ModsConverterTab : Tab() {

val filePickResult = outputFileChooser.showSaveDialog(this@ModsConverterTab)

val outputFile =
val outputFilePath =
outputFileChooser.handleResult(
result = filePickResult,
onErrorWhileChoosingFile = {},
) ?: return
try {
outputFile.writeText(result.modsOutputText)
outputFilePath.writeText(result.modsOutputText)
} catch (e: Exception) {
e.printStackTrace()
GuiUtils.showErrorMessage(
Expand Down
19 changes: 10 additions & 9 deletions admin/src/main/kotlin/gui/tabs/SyncScriptInstallerTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import javax.swing.JComponent
import javax.swing.JFileChooser
import javax.swing.JLabel
import javax.swing.filechooser.FileNameExtensionFilter
import kotlin.io.path.absolutePathString

class SyncScriptInstallerTab : Tab() {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
Expand Down Expand Up @@ -78,20 +79,20 @@ class SyncScriptInstallerTab : Tab() {
configureInstallation(
installationConfig =
SyncScriptInstallationConfig.Install(
getSyncScriptJarFilePath = {
getSyncScriptJarFilePathString = {
val fileChooser =
JFileChooser().apply {
dialogTitle = "Choose the JAR File for the sync script."
fileSelectionMode = JFileChooser.FILES_ONLY
fileFilter = FileNameExtensionFilter("JAR Files", "jar")
}
val result = fileChooser.showOpenDialog(this@SyncScriptInstallerTab)
val selectedFile =
val selectedFilePath =
fileChooser.handleResult(
result = result,
onErrorWhileChoosingFile = {},
) ?: return@Install null
selectedFile.path
selectedFilePath.absolutePathString()
},
),
confirmReplaceExistingPreLaunchCommand = false,
Expand All @@ -116,7 +117,7 @@ class SyncScriptInstallerTab : Tab() {
val result =
SyncScriptInstallerInstance.configureInstallation(
installationConfig = installationConfig,
launcherInstanceDirectoryPath =
launcherInstanceDirectoryPathString =
(launcherInstanceDirectoryComboBox.selectedItem as? String) ?: throw IllegalStateException(
"The selected item of ${::launcherInstanceDirectoryComboBox.name} is null",
),
Expand Down Expand Up @@ -167,18 +168,18 @@ class SyncScriptInstallerTab : Tab() {
)
}

SyncScriptInstallationError.CouldNotDeleteSyncScriptJarFileWhileUninstall -> {
is SyncScriptInstallationError.CouldNotDeleteSyncScriptJarFileWhileUninstall -> {
GuiUtils.showErrorMessage(
title = "❌ Unexpected error",
message = "An error occurred while deleting the sync script JAR file.",
message = "An error occurred while deleting the sync script JAR file: ${result.error.message}",
parentComponent = this@SyncScriptInstallerTab,
)
}

SyncScriptInstallationError.CouldNotDeleteSyncScriptDataWhileUninstall -> {
is SyncScriptInstallationError.CouldNotDeleteSyncScriptDataWhileUninstall -> {
GuiUtils.showErrorMessage(
title = "❌ Unexpected error",
message = "An error occurred while deleting the sync script data \uD83D\uDCC1.",
message = "An error occurred while deleting the sync script data \uD83D\uDCC1: ${result.error.message}",
parentComponent = this@SyncScriptInstallerTab,
)
}
Expand All @@ -187,7 +188,7 @@ class SyncScriptInstallerTab : Tab() {
GuiUtils.showErrorMessage(
title = "❌ Unexpected error",
message =
"An error occurred while updating the Pre Launch command/hook: \uD83D\uDEE0: ${result.error.message}",
"An error occurred while updating the Pre-Launch command/hook: \uD83D\uDEE0: ${result.error.message}",
parentComponent = this@SyncScriptInstallerTab,
)
}
Expand Down
6 changes: 3 additions & 3 deletions admin/src/main/kotlin/gui/utils/FileChooserExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gui.utils

import java.io.File
import java.nio.file.Path
import javax.swing.JFileChooser

/**
Expand All @@ -9,7 +9,7 @@ import javax.swing.JFileChooser
fun JFileChooser.handleResult(
result: Int,
onErrorWhileChoosingFile: () -> Unit,
): File? {
): Path? {
when (result) {
JFileChooser.CANCEL_OPTION -> {
return null
Expand All @@ -21,7 +21,7 @@ fun JFileChooser.handleResult(
}

JFileChooser.APPROVE_OPTION -> {
return selectedFile
return selectedFile.toPath()
}
}
return null
Expand Down
4 changes: 2 additions & 2 deletions admin/src/main/kotlin/launchers/Instance.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package launchers

import java.io.File
import java.nio.file.Path

/**
* An abstraction of Instance/Profile for all Minecraft launchers that
Expand All @@ -12,6 +12,6 @@ import java.io.File
* them as dropdown options for the text input field that request the instance directory path.
* */
data class Instance(
val launcherInstanceDirectory: File,
val launcherInstanceDirectoryPath: Path,
val instanceName: String,
)
17 changes: 10 additions & 7 deletions admin/src/main/kotlin/launchers/LauncherDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package launchers

import minecraftAssetProviders.MinecraftAssetProvider
import syncInfo.models.Mod
import java.io.File
import java.nio.file.Path

// TODO: Rename launcherInstanceDirectoryPath to dotMinecraftDirectoryPath,
// review other usages such as launcherInstanceDirectory

/**
* An interface that abstract dealing with the launcher data, like converting mods from the launcher data format
Expand All @@ -21,7 +24,7 @@ interface LauncherDataSource {
* with an exception.
* @return [Result.success] if valid, otherwise [Result.failure]
* */
suspend fun validateInstanceDirectory(launcherInstanceDirectory: File): Result<Unit>
suspend fun validateInstanceDirectory(launcherInstanceDirectoryPath: Path): Result<Unit>

/**
* If an HTTP get request is needed to convert all the mods, this will be true if
Expand All @@ -31,19 +34,19 @@ interface LauncherDataSource {
* 3. Other reasons that are specific to the launcher implementation, if the download url or related info wasn't available
* for some reason and a request to Curse Forge API is needed to get the info, this should return true
* */
suspend fun isCurseForgeApiRequestNeededForConvertingMods(launcherInstanceDirectory: File): Result<Boolean>
suspend fun isCurseForgeApiRequestNeededForConvertingMods(launcherInstanceDirectoryPath: Path): Result<Boolean>

/**
* Check if the instance has mods installed (not empty).
* */
suspend fun hasMods(launcherInstanceDirectory: File): Result<Boolean>
suspend fun hasMods(launcherInstanceDirectoryPath: Path): Result<Boolean>

/**
* @return A list of [Mod] which contains the information about the mod, converting it from the specified launcher
* into the script data format
* */
suspend fun getLauncherInstanceMods(
launcherInstanceDirectory: File,
launcherInstanceDirectoryPath: Path,
overrideCurseForgeApiKey: String?,
): Result<List<Mod>>

Expand All @@ -53,7 +56,7 @@ interface LauncherDataSource {
*
* @return Null if it's not set
* */
suspend fun getPreLaunchCommand(launcherInstanceDirectory: File): Result<String?>
suspend fun getPreLaunchCommand(launcherInstanceDirectoryPath: Path): Result<String?>

/**
* Update the Pre Launch command.
Expand All @@ -65,7 +68,7 @@ interface LauncherDataSource {
* */
suspend fun setPreLaunchCommand(
command: String?,
launcherInstanceDirectory: File,
launcherInstanceDirectoryPath: Path,
): Result<Unit>

/**
Expand Down
Loading

0 comments on commit c865642

Please sign in to comment.