Skip to content

Commit

Permalink
main mod part is now object
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneParadox committed Dec 12, 2019
1 parent 1ea4c89 commit 4734590
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 70 deletions.
138 changes: 71 additions & 67 deletions src/main/java/redstoneparadox/paradoxconfig/ParadoxConfig.kt
Original file line number Diff line number Diff line change
@@ -1,105 +1,109 @@
package redstoneparadox.paradoxconfig

import net.fabricmc.loader.api.FabricLoader
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint
import net.minecraft.util.Identifier
import org.apache.logging.log4j.LogManager
import redstoneparadox.paradoxconfig.ParadoxConfig.MODID
import redstoneparadox.paradoxconfig.conditions.registerConditions
import redstoneparadox.paradoxconfig.config.RootConfigCategory
import java.io.File
import java.io.FileNotFoundException

const val MODID: String = "pconfig"
object ParadoxConfig: PreLaunchEntrypoint {
const val MODID: String = "pconfig"

internal var initialized: Boolean = false
internal val CONFIGS: HashMap<String, RootConfigCategory> = hashMapOf()
internal var initialized: Boolean = false
internal val CONFIGS: HashMap<String, RootConfigCategory> = hashMapOf()

@Suppress("unused")
fun init() {
initConfigs()
@Suppress("unused")
override fun onPreLaunch() {
initConfigs()

if (FabricLoader.getInstance().isModLoaded("libcd")) {
registerConditions()
if (FabricLoader.getInstance().isModLoaded("libcd")) {
registerConditions()
}
}
}

/**
* Reloads the specified config file.
*
* @param id The identifier (mod id + path) of the config.
*/
fun forceReloadConfig(id: Identifier) {
val config = CONFIGS[id.toString()]
if (config != null) {
loadConfig(config, id.namespace)
/**
* Reloads the specified config file.
*
* @param id The identifier (mod id + path) of the config.
*/
fun forceReloadConfig(id: Identifier) {
val config = CONFIGS[id.toString()]
if (config != null) {
loadConfig(config, id.namespace)
}
}
}

internal fun getConfigData(): Collection<ConfigData> {
val data = arrayListOf<ConfigData>();
internal fun getConfigData(): Collection<ConfigData> {
val data = arrayListOf<ConfigData>();

for (mod in FabricLoader.getInstance().allMods) {
if (mod.metadata.containsCustomValue(MODID)) {
val classNames = mod.metadata.getCustomValue(MODID).asArray
for (mod in FabricLoader.getInstance().allMods) {
if (mod.metadata.containsCustomValue(MODID)) {
val classNames = mod.metadata.getCustomValue(MODID).asArray

val configNames = arrayListOf<String>()
for (className in classNames) {
configNames.add(className.asString)
}
val configNames = arrayListOf<String>()
for (className in classNames) {
configNames.add(className.asString)
}

if (mod.metadata.id == MODID && !FabricLoader.getInstance().isDevelopmentEnvironment) continue
if (mod.metadata.id == MODID && !FabricLoader.getInstance().isDevelopmentEnvironment) continue

data.add(ConfigData(configNames, mod.metadata.id))
data.add(ConfigData(configNames, mod.metadata.id))
}
}
}

return data
}
return data
}

internal fun initConfigs() {
if (initialized) return
internal fun initConfigs() {
if (initialized) return

val configData = getConfigData()
val baseClass = RootConfigCategory::class
val configData = getConfigData()
val baseClass = RootConfigCategory::class

for (data in configData) {
for (configName in data.configNames) {
val config = Class.forName(configName).kotlin.objectInstance
for (data in configData) {
for (configName in data.configNames) {
val config = Class.forName(configName).kotlin.objectInstance

if (config is RootConfigCategory) {
config.init()
CONFIGS["${data.modid}:${config.file}"] = config
loadConfig(config, data.modid)
}
else {
println("Object $configName either doesn't extend ${baseClass.simpleName} or is not an object.")
if (config is RootConfigCategory) {
config.init()
CONFIGS["${data.modid}:${config.file}"] = config
loadConfig(config, data.modid)
}
else {
println("Object $configName either doesn't extend ${baseClass.simpleName} or is not an object.")
}
}
}
}

initialized = true
}
initialized = true
}

private fun loadConfig(config: RootConfigCategory, modid: String) {
val serializer = config.serializer
val deserializer = config.deserializer
val configFile = File(FabricLoader.getInstance().configDirectory, "${modid}/${config.file}")
private fun loadConfig(config: RootConfigCategory, modid: String) {
val serializer = config.serializer
val deserializer = config.deserializer
val configFile = File(FabricLoader.getInstance().configDirectory, "${modid}/${config.file}")

try {
val configString = configFile.readText()
if (deserializer.receiveSource(configString)) config.deserialize(deserializer)
} catch (e: FileNotFoundException) {
PConfigLogger.log("Config file $modid:${config.file} was not found; a new one will be created.")
try {
configFile.parentFile.mkdirs()
} catch (e: SecurityException) {
PConfigLogger.error("Could not create config file $modid:${config.file} due to security issues.")
e.printStackTrace()
return
val configString = configFile.readText()
if (deserializer.receiveSource(configString)) config.deserialize(deserializer)
} catch (e: FileNotFoundException) {
PConfigLogger.log("Config file $modid:${config.file} was not found; a new one will be created.")
try {
configFile.parentFile.mkdirs()
} catch (e: SecurityException) {
PConfigLogger.error("Could not create config file $modid:${config.file} due to security issues.")
e.printStackTrace()
return
}
}
config.serialize(serializer)
val configString = serializer.complete()
configFile.writeText(configString)
}
config.serialize(serializer)
val configString = serializer.complete()
configFile.writeText(configString)
}

internal class ConfigData(val configNames: Collection<String>, val modid: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import blue.endless.jankson.JsonPrimitive
import io.github.cottonmc.libcd.condition.ConditionalData
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.util.Identifier
import redstoneparadox.paradoxconfig.CONFIGS
import redstoneparadox.paradoxconfig.MODID
import redstoneparadox.paradoxconfig.ParadoxConfig.CONFIGS
import redstoneparadox.paradoxconfig.ParadoxConfig.MODID
import redstoneparadox.paradoxconfig.util.compareTo

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"environment": "*",
"entrypoints": {
"preLaunch": [
"redstoneparadox.paradoxconfig.ParadoxConfigKt::init"
{
"adapter": "kotlin",
"value": "redstoneparadox.paradoxconfig.ParadoxConfig"
}
]
},
"mixins": [
Expand Down

0 comments on commit 4734590

Please sign in to comment.