Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@ <T extends ConfigSection> T instantiate(Class<T> type) {
);
}
}
}

<T extends ConfigSection> T initialize(T config) {
try {
return ConfigManager.initialize(config);
} catch (OkaeriException e) {
throw new ConfigCreateException(
"Failed to initialize config: " + config.getClass().getName(), e
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,25 @@ public ConfigService(File dataFolder) {
}

public <C extends ConfigSection> C create(Class<C> type) {
C config = factory.instantiate(type);
return adopt(factory.instantiate(type));
}

/**
* Binds an already existing config instance to its file, loads it and registers it.
* <p>
* This must be used for instances that are shared with other components (e.g. created
* by the dependency injector), otherwise those components would keep an unloaded
* instance holding only the hardcoded defaults.
*/
public <C extends ConfigSection> C adopt(C config) {
factory.initialize(config);

File file = new File(dataFolder, config.fileName());

configurer.configure(config, file, config.serdesPack());
lifecycle.initialize(config);

register(type, config);
register(config.getClass(), config);
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.imdmk.playtime.core.injector.annotations.ConfigFile;
import com.github.imdmk.playtime.core.injector.processor.ComponentProcessor;
import com.github.imdmk.playtime.core.injector.processor.ComponentProcessorContext;
import eu.okaeri.configs.OkaeriConfig;
import org.panda_lang.utilities.inject.Resources;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -35,11 +36,22 @@ public void process(
ConfigFile.class
);

configService.create(config.getClass());
// The very same instance must be loaded and bound, otherwise every component
// injecting this config would receive an unloaded copy holding only the defaults.
configService.adopt(config);

resources.on(config.getClass())
.assignInstance(instance);
.assignInstance(config);

bindSections(config, resources);
}

private void bindSections(ConfigSection config, Resources resources) {
for (Field field : config.getClass().getFields()) {
if (!OkaeriConfig.class.isAssignableFrom(field.getType())) {
continue;
}

try {
Object value = field.get(config);
if (value != null) {
Expand Down
Loading