Skip to content

Commit

Permalink
refactor(AutoConfig): auto set loading now flag (#4944)
Browse files Browse the repository at this point in the history
  • Loading branch information
MukjepScarlet authored Dec 17, 2024
1 parent 57c5896 commit e1f0a5a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 66 deletions.
38 changes: 25 additions & 13 deletions src/main/kotlin/net/ccbluex/liquidbounce/config/AutoConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import net.ccbluex.liquidbounce.features.module.ModuleManager
import net.ccbluex.liquidbounce.features.module.modules.render.ModuleClickGui
import net.ccbluex.liquidbounce.utils.client.*
import net.minecraft.util.Formatting
import net.minecraft.util.Util
import java.io.Writer
import java.text.SimpleDateFormat
import java.util.*
import kotlin.concurrent.thread

data class IncludeConfiguration(
val includeBinds: Boolean = false,
Expand All @@ -50,6 +50,7 @@ data class IncludeConfiguration(

object AutoConfig {

@Volatile
var loadingNow = false
set(value) {
field = value
Expand All @@ -59,6 +60,7 @@ object AutoConfig {
ModuleClickGui.reloadView()
}
}

var includeConfiguration = IncludeConfiguration.DEFAULT

var configsCache: Array<AutoSettings>? = null
Expand All @@ -67,21 +69,31 @@ object AutoConfig {
configsCache = this
}

fun loadAutoConfig(autoConfig: AutoSettings) = thread(name = "config-loader") {
fun startLoaderTask(task: Runnable) = Util.getDownloadWorkerExecutor().execute(task)

inline fun withLoading(block: () -> Unit) {
loadingNow = true
runCatching {
ClientApi.requestSettingsScript(autoConfig.settingId).apply {
ConfigSystem.deserializeConfigurable(ModuleManager.modulesConfigurable, reader(), publicGson)
}
try {
block()
} finally {
loadingNow = false
}
}

}.onFailure {
notification("Auto Config", "Failed to load config ${autoConfig.name}.",
NotificationEvent.Severity.ERROR)
}.onSuccess {
notification("Auto Config", "Successfully loaded config ${autoConfig.name}.",
NotificationEvent.Severity.SUCCESS)
fun loadAutoConfig(autoConfig: AutoSettings) = startLoaderTask {
withLoading {
runCatching {
ClientApi.requestSettingsScript(autoConfig.settingId).apply {
ConfigSystem.deserializeConfigurable(ModuleManager.modulesConfigurable, reader(), publicGson)
}
}.onFailure {
notification("Auto Config", "Failed to load config ${autoConfig.name}.",
NotificationEvent.Severity.ERROR)
}.onSuccess {
notification("Auto Config", "Successfully loaded config ${autoConfig.name}.",
NotificationEvent.Severity.SUCCESS)
}
}
loadingNow = false
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,12 @@ object CommandClient : CommandFactory {
private fun resetCommand() = CommandBuilder
.begin("reset")
.handler { command, _ ->
AutoConfig.loadingNow = true
ModuleManager
// TODO: Remove when HUD no longer contains the Element Configuration
.filter { module -> module !is ModuleHud }
.forEach { it.restore() }
AutoConfig.loadingNow = false
AutoConfig.withLoading {
ModuleManager
// TODO: Remove when HUD no longer contains the Element Configuration
.filter { module -> module !is ModuleHud }
.forEach { it.restore() }
}
chat(regular(command.result("successfullyReset")))
}
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import net.ccbluex.liquidbounce.utils.io.HttpClient.get
import net.minecraft.text.ClickEvent
import net.minecraft.text.HoverEvent
import net.minecraft.text.Text
import kotlin.concurrent.thread

/**
* Config Command
Expand Down Expand Up @@ -75,7 +74,7 @@ object CommandConfig : CommandFactory {
val modules = ModuleManager.parseModulesFromParameter(moduleNames)

// Load the config in a separate thread to prevent the client from freezing
thread(name = "config-loader") {
AutoConfig.startLoaderTask {
runCatching {
if(name.startsWith("http")) {
// Load the config from the specified URL
Expand All @@ -85,27 +84,27 @@ object CommandConfig : CommandFactory {
ClientApi.requestSettingsScript(name).reader()
}
}.onSuccess { sourceReader ->
AutoConfig.loadingNow = true
runCatching {
sourceReader.apply {
if(modules.isEmpty()) {
ConfigSystem.deserializeConfigurable(
ModuleManager.modulesConfigurable, this,
publicGson
)
} else {
ConfigSystem.deserializeModuleConfigurable(
modules, this,
publicGson
)
AutoConfig.withLoading {
runCatching {
sourceReader.apply {
if (modules.isEmpty()) {
ConfigSystem.deserializeConfigurable(
ModuleManager.modulesConfigurable, this,
publicGson
)
} else {
ConfigSystem.deserializeModuleConfigurable(
modules, this,
publicGson
)
}
}
}.onFailure {
chat(markAsError(command.result("failedToLoad", variable(name))))
}.onSuccess {
chat(regular(command.result("loaded", variable(name))))
}
}.onFailure {
chat(markAsError(command.result("failedToLoad", variable(name))))
}.onSuccess {
chat(regular(command.result("loaded", variable(name))))
}
AutoConfig.loadingNow = false
}.onFailure {
chat(markAsError(command.result("failedToLoad", variable(name))))
}
Expand All @@ -123,7 +122,7 @@ object CommandConfig : CommandFactory {
val width = configs.maxOf { mc.textRenderer.getWidth(it.settingId) }

// In case of the chat, we want to show the newest config at the bottom for visibility
configs.sortedBy { it.javaDate.time }.forEach {
configs.sortedBy { it.javaDate }.forEach {
val settingName = it.settingId // there is also .name, but we use it for GUI instead

// Append spaces to the setting name to align the date and status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package net.ccbluex.liquidbounce.features.command.commands.client

import net.ccbluex.liquidbounce.config.AutoConfig.loadingNow
import net.ccbluex.liquidbounce.config.AutoConfig
import net.ccbluex.liquidbounce.config.AutoConfig.serializeAutoConfig
import net.ccbluex.liquidbounce.config.ConfigSystem
import net.ccbluex.liquidbounce.config.IncludeConfiguration
Expand Down Expand Up @@ -66,16 +66,18 @@ object CommandLocalConfig : CommandFactory {
return@handler
}

loadingNow = true
ConfigSystem.deserializeConfigurable(ModuleManager.modulesConfigurable, bufferedReader(),
publicGson)
AutoConfig.withLoading {
ConfigSystem.deserializeConfigurable(
ModuleManager.modulesConfigurable,
bufferedReader(),
publicGson
)
}
}.onFailure {
chat(markAsError(command.result("failedToLoad", variable(name))))
}.onSuccess {
chat(regular(command.result("loaded", variable(name))))
}

loadingNow = false
}
.build()
)
Expand Down Expand Up @@ -117,7 +119,7 @@ object CommandLocalConfig : CommandFactory {
.begin<String>("include")
.verifiedBy(ParameterBuilder.STRING_VALIDATOR)
.autocompletedWith { s ->
listOf(
arrayOf(
"binds",
"hidden"
).filter { it.startsWith(s) }
Expand All @@ -142,7 +144,7 @@ object CommandLocalConfig : CommandFactory {
}

createNewFile()
serializeAutoConfig(writer(), includeConfiguration)
serializeAutoConfig(bufferedWriter(), includeConfiguration)
}.onFailure {
chat(regular(command.result("failedToCreate", variable(name))))
}.onSuccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object CommandPanic : CommandFactory {
when (val type = args.getOrNull(0) as String? ?: "nonrender") {
"all" -> msg = command.result("disabledAllModules")
"nonrender" -> {
modules = modules.filter { it.category != Category.RENDER && it.category != Category.CLIENT}
modules = modules.filter { it.category != Category.RENDER && it.category != Category.CLIENT }
msg = command.result("disabledAllCategoryModules", command.result("nonRender"))
}

Expand All @@ -67,16 +67,15 @@ object CommandPanic : CommandFactory {
}
}

AutoConfig.loadingNow = true
runCatching {
for (module in modules) {
module.enabled = false
AutoConfig.withLoading {
for (module in modules) {
module.enabled = false
}
}
}.onSuccess {
AutoConfig.loadingNow = false
chat(regular(msg), command)
}.onFailure {
AutoConfig.loadingNow = false
throw CommandException(command.result("panicFailed"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,21 @@ fun putSettings(requestObject: RequestObject): FullHttpResponse {
@Suppress("UNUSED_PARAMETER")
fun postPanic(requestObject: RequestObject): FullHttpResponse {
RenderSystem.recordRenderCall {
AutoConfig.loadingNow = true
AutoConfig.withLoading {
runCatching {
for (module in ModuleManager) {
if (module.category == Category.RENDER || module.category == Category.CLIENT) {
continue
}

runCatching {
for (module in ModuleManager) {
if (module.category == Category.RENDER || module.category == Category.CLIENT) {
continue
module.enabled = false
}

module.enabled = false
ConfigSystem.storeConfigurable(modulesConfigurable)
}.onFailure {
logger.error("Failed to panic disable modules", it)
}

ConfigSystem.storeConfigurable(modulesConfigurable)
}.onFailure {
logger.error("Failed to panic disable modules", it)
}

AutoConfig.loadingNow = false
}
return httpOk(JsonObject())
}
Expand Down

0 comments on commit e1f0a5a

Please sign in to comment.