Skip to content

Commit

Permalink
refactor: rename SyncScriptInstanceFiles to SyncScriptDotMinecraftFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellet committed Jun 26, 2024
1 parent b4c0972 commit 8b35553
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 50 deletions.
6 changes: 3 additions & 3 deletions sync-script/src/main/kotlin/JarAutoUpdater.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import constants.ProjectInfoConstants
import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import generated.BuildConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -19,7 +19,7 @@ object JarAutoUpdater {
private suspend fun downloadLatestJarFile(): Result<File> =
try {
val newJarFile =
SyncScriptInstanceFiles.SyncScriptData.Temp.file
SyncScriptDotMinecraftFiles.SyncScriptData.Temp.file
.resolve("${ProjectInfoConstants.NORMALIZED_NAME}-new.jar")
if (newJarFile.exists()) {
newJarFile.delete()
Expand Down Expand Up @@ -116,7 +116,7 @@ object JarAutoUpdater {
OperatingSystem.Windows -> {
// On Windows, we can't rename, delete or modify the current running JAR file due to file locking
val updateBatScriptFile =
SyncScriptInstanceFiles.SyncScriptData.Temp.file
SyncScriptDotMinecraftFiles.SyncScriptData.Temp.file
.resolve("update.bat")
withContext(Dispatchers.IO) {
updateBatScriptFile.parentFile.mkdirs()
Expand Down
12 changes: 6 additions & 6 deletions sync-script/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import config.data.LocalJsonScriptConfigDataSource
import config.data.ScriptConfigDataSource
import config.models.ScriptConfig
import constants.Constants
import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import generated.BuildConfig
import gui.GuiState
import gui.dialogs.CreateScriptConfigDialog
Expand Down Expand Up @@ -59,7 +59,7 @@ suspend fun main(args: Array<String>) {
}.also { println(it) }
}

SyncScriptInstanceFiles.SyncScriptData.Temp.file.apply {
SyncScriptDotMinecraftFiles.SyncScriptData.Temp.file.apply {
if (exists()) {
println(
"\uFE0F The temporary folder: $path exist. " +
Expand Down Expand Up @@ -88,7 +88,7 @@ suspend fun main(args: Array<String>) {

// Loading the script config file from json file

val scriptConfigFile = SyncScriptInstanceFiles.SyncScriptData.ScriptConfig.file
val scriptConfigFile = SyncScriptDotMinecraftFiles.SyncScriptData.ScriptConfig.file
if (!scriptConfigFile.exists()) {
if (GuiState.isGuiEnabled) {
println(
Expand Down Expand Up @@ -158,7 +158,7 @@ suspend fun main(args: Array<String>) {

// TODO: Plan if we should implement this in non GUI mode
if (GuiState.isGuiEnabled &&
!SyncScriptInstanceFiles.SyncScriptData.IsPreferencesConfigured.file
!SyncScriptDotMinecraftFiles.SyncScriptData.IsPreferencesConfigured.file
.exists()
) {
val newScriptConfig = QuickPreferencesDialog().showDialog()
Expand All @@ -177,7 +177,7 @@ suspend fun main(args: Array<String>) {
GuiState.updateIsGuiEnabled()

withContext(Dispatchers.IO) {
SyncScriptInstanceFiles.SyncScriptData.IsPreferencesConfigured.file
SyncScriptDotMinecraftFiles.SyncScriptData.IsPreferencesConfigured.file
.createNewFile()
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ suspend fun main(args: Array<String>) {

// 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.
SyncScriptInstanceFiles.SyncScriptData.Temp.file.apply {
SyncScriptDotMinecraftFiles.SyncScriptData.Temp.file.apply {
if (exists()) {
println("\uD83D\uDEAB Deleting the temporary folder: '$path' (no longer needed).")
deleteRecursively()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package config.data

import config.models.ScriptConfig
import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import utils.JsonPrettyPrint

class LocalJsonScriptConfigDataSource : ScriptConfigDataSource {
override suspend fun getConfig(): Result<ScriptConfig> {
return try {
override suspend fun getConfig(): Result<ScriptConfig> =
try {
val scriptConfig =
withContext(Dispatchers.IO) {
Json.decodeFromString<ScriptConfig>(SyncScriptInstanceFiles.SyncScriptData.ScriptConfig.file.readText())
Json.decodeFromString<ScriptConfig>(
SyncScriptDotMinecraftFiles.SyncScriptData.ScriptConfig.file
.readText(),
)
}
Result.success(scriptConfig)
} catch (e: Exception) {
Result.failure(e)
}
}

override suspend fun replaceConfig(scriptConfig: ScriptConfig): Result<Unit> {
return try {
override suspend fun replaceConfig(scriptConfig: ScriptConfig): Result<Unit> =
try {
withContext(Dispatchers.IO) {
val configFile = SyncScriptInstanceFiles.SyncScriptData.ScriptConfig.file
val configFile = SyncScriptDotMinecraftFiles.SyncScriptData.ScriptConfig.file
if (!configFile.parentFile.exists()) {
configFile.parentFile.mkdirs()
}
Expand All @@ -33,5 +35,4 @@ class LocalJsonScriptConfigDataSource : ScriptConfigDataSource {
} catch (e: Exception) {
Result.failure(e)
}
}
}
4 changes: 2 additions & 2 deletions sync-script/src/main/kotlin/config/models/ScriptConfig.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package config.models

import constants.Constants
import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import gui.theme.Theme
import gui.theme.ThemeMode
import kotlinx.serialization.Serializable
import syncInfo.models.Environment
import syncInfo.models.SyncInfo

/**
* A data class that represent the json for the [SyncScriptInstanceFiles.SyncScriptData.ScriptConfig]
* A data class that represent the json for the [SyncScriptDotMinecraftFiles.SyncScriptData.ScriptConfig]
* where the user can pass some data that is needed to launch the script
* */
@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ import java.io.File
*
* This should be only used by the script that syncs the content
* */
sealed class SyncScriptInstanceFiles(
sealed class SyncScriptDotMinecraftFiles(
val file: File,
) {
data object Mods : SyncScriptInstanceFiles(File(DotMinecraftFileNames.MODS_DIRECTORY))
data object Mods : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.MODS_DIRECTORY))

data object ResourcePacks : SyncScriptInstanceFiles(File(DotMinecraftFileNames.RESOURCE_PACKS_DIRECTORY))
data object ResourcePacks : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.RESOURCE_PACKS_DIRECTORY))

data object ShaderPacks : SyncScriptInstanceFiles(File(DotMinecraftFileNames.SHADER_PACKS_DIRECTORY))
data object ShaderPacks : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.SHADER_PACKS_DIRECTORY))

data object Config : SyncScriptInstanceFiles(File(DotMinecraftFileNames.CONFIG_DIRECTORY))
data object Config : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.CONFIG_DIRECTORY))

data object Saves : SyncScriptInstanceFiles(File(DotMinecraftFileNames.SAVES_DIRECTORY))
data object Saves : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.SAVES_DIRECTORY))

/**
* A txt file contains the minecraft options/settings or data like the key bindings, sound settings
* enabled resource-packs, language and the minecraft video settings, and more
* */
data object Options : SyncScriptInstanceFiles(File(DotMinecraftFileNames.OPTIONS_FILE))
data object Options : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.OPTIONS_FILE))

/**
* A file that contains all the information about the list of servers
* */
data object ServersDat : SyncScriptInstanceFiles(File(DotMinecraftFileNames.SERVERS_FILE))
data object ServersDat : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.SERVERS_FILE))

/**
* This directory will be created and managed by the script, it's specific to it
* */
data object SyncScriptData : SyncScriptInstanceFiles(File(DotMinecraftFileNames.SYNC_SCRIPT_DIRECTORY)) {
data object SyncScriptData : SyncScriptDotMinecraftFiles(File(DotMinecraftFileNames.SYNC_SCRIPT_DIRECTORY)) {
/**
* The script needs a config file that contains required data and other optional to configure it
* */
data object ScriptConfig : SyncScriptInstanceFiles(File(SyncScriptData.file, "config.json"))
data object ScriptConfig : SyncScriptDotMinecraftFiles(File(SyncScriptData.file, "config.json"))

/**
* A directory that contain the temporary files used by the script,
* for example, when downloading a file, the content will be saved to this directory
* and after finish downloading, the file will be moved into where it should.
* */
data object Temp : SyncScriptInstanceFiles(File(SyncScriptData.file, "temp"))
data object Temp : SyncScriptDotMinecraftFiles(File(SyncScriptData.file, "temp"))

/**
* A file will be used to indicate if the user did set up the preferences for using the script, and it will
Expand All @@ -56,6 +56,6 @@ sealed class SyncScriptInstanceFiles(
* it later and launch the dialog again.
* */
data object IsPreferencesConfigured :
SyncScriptInstanceFiles(File(SyncScriptData.file, "isPreferencesConfigured"))
SyncScriptDotMinecraftFiles(File(SyncScriptData.file, "isPreferencesConfigured"))
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package services.minecraft

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.jetbrains.annotations.VisibleForTesting
import java.io.File

/**
* Helper class for reading [SyncScriptInstanceFiles.Options] file which contains Minecraft settings
* Helper class for reading [SyncScriptDotMinecraftFiles.Options] file which contains Minecraft settings
* for reading and getting the properties or set them if it doesn't exist
* */
object MinecraftOptionsManager {
private var optionsFile = SyncScriptInstanceFiles.Options.file
private var optionsFile = SyncScriptDotMinecraftFiles.Options.file
private val properties: MutableMap<String, String> = mutableMapOf()

private var isLoaded = false
Expand Down
6 changes: 3 additions & 3 deletions sync-script/src/main/kotlin/syncService/ModsSyncService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package syncService

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import gui.dialogs.LoadingIndicatorDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -23,7 +23,7 @@ import java.io.File
// mods issue when allowing the user to install other mods

class ModsSyncService : SyncService {
private val modsDirectory = SyncScriptInstanceFiles.Mods.file
private val modsDirectory = SyncScriptDotMinecraftFiles.Mods.file

companion object {
private const val MOD_FILE_EXTENSION = "jar"
Expand Down Expand Up @@ -82,7 +82,7 @@ class ModsSyncService : SyncService {
title = "❌ Invalid Mods Folder",
message =
"\uD83D\uDEE0 Mods must be stored in a directory/folder \uD83D\uDCC2 called " +
"`${SyncScriptInstanceFiles.Mods.file.name}`" +
"`${SyncScriptDotMinecraftFiles.Mods.file.name}`" +
", a file was found instead.",
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package syncService

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles

class ResourcePacksSyncService : SyncService {
private val resourcePacksDirectory = SyncScriptInstanceFiles.ResourcePacks.file
private val resourcePacksDirectory = SyncScriptDotMinecraftFiles.ResourcePacks.file

override suspend fun syncData() {
TODO("Not yet implemented")
Expand Down
4 changes: 2 additions & 2 deletions sync-script/src/main/kotlin/syncService/ServersSyncService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package syncService

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles

class ServersSyncService : SyncService {
private val serversFile = SyncScriptInstanceFiles.ServersDat.file
private val serversFile = SyncScriptDotMinecraftFiles.ServersDat.file

override suspend fun syncData() {
TODO("Not yet implemented")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package syncService

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles

class ShaderPacksSyncService : SyncService {
private val shaderPacksDirectory = SyncScriptInstanceFiles.ShaderPacks.file
private val shaderPacksDirectory = SyncScriptDotMinecraftFiles.ShaderPacks.file

override suspend fun syncData() {
TODO("Not yet implemented")
Expand Down
6 changes: 3 additions & 3 deletions sync-script/src/main/kotlin/utils/FileDownloader.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package utils

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.Request
Expand Down Expand Up @@ -60,7 +60,7 @@ class FileDownloader(
// We could use File.createTempFile from JVM, to avoid creating
// files on the user system we will handle it manually
val tempFile =
SyncScriptInstanceFiles.SyncScriptData.Temp.file.resolve(
SyncScriptDotMinecraftFiles.SyncScriptData.Temp.file.resolve(
"${targetFile.nameWithoutExtension}-${System.currentTimeMillis()}.${targetFile.extension}",
)
if (!tempFile.exists()) {
Expand Down Expand Up @@ -107,7 +107,7 @@ class FileDownloader(
e.printStackTrace()
showErrorMessageAndTerminate(
title = "🚫 Couldn't download the file",
message = "An unknown error occurred while downloading the file: ${e.message} from the url: $downloadUrl",
message = "An unknown error occurred while downloading the file ($downloadUrl): ${e.message}:",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package services.minecraft

import constants.SyncScriptInstanceFiles
import constants.SyncScriptDotMinecraftFiles
import kotlinx.serialization.SerializationException
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -33,8 +33,8 @@ class MinecraftOptionsManagerTest {
fun setUp() {
testsOptionsFile =
File.createTempFile(
SyncScriptInstanceFiles.Options.file.nameWithoutExtension,
".${SyncScriptInstanceFiles.Options.file.extension}",
SyncScriptDotMinecraftFiles.Options.file.nameWithoutExtension,
".${SyncScriptDotMinecraftFiles.Options.file.extension}",
)
testsOptionsFile.deleteOnExit()

Expand Down

0 comments on commit 8b35553

Please sign in to comment.