Skip to content

Commit 13d2060

Browse files
committed
ConfigurationAPI.kt refactor.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent ebada2b commit 13d2060

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed
Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
@file:Suppress("unused")
2+
13
package com.mairwunnx.projectessentials.core.api.v1.configuration
24

5+
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI
6+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.ConfigurationEventData
7+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.ModuleCoreEventType
8+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
9+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderType
10+
import org.apache.logging.log4j.LogManager
11+
312
/**
413
* Configuration API, for interacting with configurations.
514
* @since 2.0.0-SNAPSHOT.1.
615
*/
7-
@Suppress("unused")
816
object ConfigurationAPI {
17+
private val logger = LogManager.getLogger()
18+
private var configurations = listOf<IConfiguration<*>>()
19+
920
/**
1021
* @return all installed and checked configurations.
1122
* @since 2.0.0-SNAPSHOT.1.
1223
*/
13-
fun getAllConfigurations() = ConfigurationProcessor.getConfigurations()
24+
fun getConfigurations() = configurations
1425

1526
/**
1627
* @param name processor name.
@@ -21,11 +32,11 @@ object ConfigurationAPI {
2132
*/
2233
@Suppress("UNCHECKED_CAST")
2334
fun <T> getConfigurationByName(name: String): T where T : IConfiguration<*> =
24-
getAllConfigurations().find { it.name == name }?.let {
35+
configurations.asSequence().find {
36+
it.name.toLowerCase() == name.toLowerCase()
37+
}?.let {
2538
return@let it as T
26-
} ?: throw ConfigurationNotFoundException(
27-
"Configuration with name $name not found."
28-
)
39+
} ?: throw ConfigurationNotFoundException("Configuration with name $name not found.")
2940

3041
/**
3142
* Reloads all initialized and processed configurations.
@@ -34,9 +45,8 @@ object ConfigurationAPI {
3445
* @since 2.0.0-SNAPSHOT.1.
3546
*/
3647
fun reloadAll(saveBeforeLoad: Boolean = true) {
37-
getAllConfigurations().forEach {
38-
if (saveBeforeLoad) it.save()
39-
it.load()
48+
getConfigurations().forEach { cfg ->
49+
if (saveBeforeLoad) cfg.save().also { cfg.load() }
4050
}
4151
}
4252

@@ -51,20 +61,34 @@ object ConfigurationAPI {
5161
configuration: IConfiguration<*>,
5262
saveBeforeLoad: Boolean = true
5363
) {
54-
if (saveBeforeLoad) configuration.save()
55-
configuration.load()
64+
if (saveBeforeLoad) configuration.save().also { configuration.load() }
5665
}
5766

5867
/**
5968
* Saves all initialized and processed configurations.
6069
* @since 2.0.0-SNAPSHOT.1.
6170
*/
62-
fun saveAll() = getAllConfigurations().forEach { it.save() }
71+
fun saveAll() = getConfigurations().forEach { it.save() }
6372

64-
/**
65-
* Saves specified configuration.
66-
* @param configuration configuration for saving.
67-
* @since 2.0.0-SNAPSHOT.1.
68-
*/
69-
fun saveSpecified(configuration: IConfiguration<*>) = configuration.save()
73+
internal fun loadAll() {
74+
ProviderAPI.getProvidersByType(ProviderType.Configuration).forEach {
75+
val clazz = it.getDeclaredField("INSTANCE").get(null) as IConfiguration<*>
76+
ModuleEventAPI.fire(
77+
ModuleCoreEventType.OnConfigurationClassProcessing, ConfigurationEventData(clazz)
78+
)
79+
logger.debug(
80+
"Configuration taken! ${it.simpleName}, name: ${clazz.name}, version: ${clazz.version}, at ${clazz.path}"
81+
)
82+
configurations = configurations + clazz
83+
load(clazz)
84+
ModuleEventAPI.fire(
85+
ModuleCoreEventType.OnConfigurationClassProcessed, ConfigurationEventData(clazz)
86+
)
87+
}
88+
}
89+
90+
private fun load(configuration: IConfiguration<*>) =
91+
logger.info("Starting loading configuration ${configuration.name}").also {
92+
configuration.load()
93+
}
7094
}

0 commit comments

Comments
 (0)