Skip to content

Commit

Permalink
feat: allow to sync current environment optional mods to either some …
Browse files Browse the repository at this point in the history
…of them or all (#20)
  • Loading branch information
EchoEllet committed Jul 6, 2024
1 parent 9e917bb commit a80a8e3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions common/src/main/kotlin/syncInfo/models/mod/Mod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ data class Mod(
* for a specific mod
* */
val verifyFileIntegrity: Boolean? = null,
/**
* Will override [ModSyncInfo.syncAllOptionalForCurrentEnv] for a specific mod
* */
val syncOptionalForCurrentEnv: Boolean? = null,
) {
@Serializable
enum class ModSupport {
Expand Down
16 changes: 15 additions & 1 deletion common/src/main/kotlin/syncInfo/models/mod/ModSyncInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@ data class ModSyncInfo(
* You will get an error from the mod loader.
*
* */
val syncOnlyForCurrentEnvironment: Boolean = true,
val syncOnlyForCurrentEnv: Boolean = true,
/**
* Indicates whether optional mods for the current [Environment] should be synced.
*
* Some mods might be optional to install on either server or client side or both.
*
* This setting only takes effect if [syncOnlyForCurrentEnv] is `true`.
* If [syncOnlyForCurrentEnv] is `false`, this property has no impact.
*
* if [syncAllOptionalForCurrentEnv] is `false`, then will only sync the
* required mods for the current system [Environment].
*
* @see Mod.syncOptionalForCurrentEnv
* */
val syncAllOptionalForCurrentEnv: Boolean = true,
/**
* Determines whether the script allows the player to use mods other than those synchronized by the script.
*
Expand Down
1 change: 1 addition & 0 deletions sync-script/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ suspend fun fetchSyncInfo() {
.fetchSyncInfo(
url = ScriptConfig.getInstanceOrThrow().syncInfoUrl,
).getOrElse {
it.printStackTrace()
showErrorMessageAndTerminate(
title = "Sync Info Unavailable \uD83D\uDD04",
message = "An error occurred while trying to fetch sync info from the server: ${it.message}",
Expand Down
17 changes: 10 additions & 7 deletions sync-script/src/main/kotlin/syncInfo/models/ModExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,32 @@ import kotlin.system.exitProcess
* */
fun Mod.getDisplayName(): String = name ?: getFileNameFromUrlOrError(downloadUrl)

private fun Mod.shouldSyncOptionalModForCurrentEnvironment() =
syncOptionalForCurrentEnv
?: SyncInfo.instance.modSyncInfo.syncAllOptionalForCurrentEnv

/**
* If this mod should be downloaded on the current [Environment].
*
* For example, if this is a client side-only mod and the environment is [Environment.Server],
* it will return false
* */
fun Mod.shouldSyncOnCurrentEnvironment(): Boolean {
// TODO: Currently, this function will always download the mod even if it's optional on both client and server.
// Consider providing an option for the admin to decide if the optional mods or a specific mods will be downloaded
return when (ScriptConfig.getInstanceOrThrow().environment) {
fun Mod.shouldSyncOnCurrentEnvironment(): Boolean =
when (ScriptConfig.getInstanceOrThrow().environment) {
Environment.Client ->
when (clientSupport) {
Mod.ModSupport.Required, Mod.ModSupport.Optional -> true
Mod.ModSupport.Required -> true
Mod.ModSupport.Optional -> shouldSyncOptionalModForCurrentEnvironment()
Mod.ModSupport.Unsupported -> false
}

Environment.Server ->
when (serverSupport) {
Mod.ModSupport.Required, Mod.ModSupport.Optional -> true
Mod.ModSupport.Required -> true
Mod.ModSupport.Optional -> shouldSyncOptionalModForCurrentEnvironment()
Mod.ModSupport.Unsupported -> false
}
}
}

/**
* Allow overriding the value for a specific mod, or all the mods, or use a global value for all the assets.
Expand Down
2 changes: 1 addition & 1 deletion sync-script/src/main/kotlin/syncService/ModsSyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ModsSyncService :
}

private fun getCurrentEnvironmentModsOrAll(mods: List<Mod>): List<Mod> {
if (!modSyncInfo.syncOnlyForCurrentEnvironment) {
if (!modSyncInfo.syncOnlyForCurrentEnv) {
return mods
}
val currentEnvironmentMods =
Expand Down

0 comments on commit a80a8e3

Please sign in to comment.