-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0662b7
commit 58218e9
Showing
7 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/org/violetmoon/zetaimplforge/config/ForgeBackedConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.violetmoon.zetaimplforge.config; | ||
|
||
import net.neoforged.neoforge.common.ModConfigSpec; | ||
import org.violetmoon.zeta.config.IZetaConfigInternals; | ||
import org.violetmoon.zeta.config.SectionDefinition; | ||
import org.violetmoon.zeta.config.ValueDefinition; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ForgeBackedConfig implements IZetaConfigInternals { | ||
private final Map<ValueDefinition<?>, ModConfigSpec.ConfigValue<?>> definitionsToValues = new HashMap<>(); | ||
private long debounceTime = System.currentTimeMillis(); | ||
|
||
public ForgeBackedConfig(SectionDefinition rootSection, ModConfigSpec.Builder forgeBuilder) { | ||
walkSection(rootSection, forgeBuilder, true); | ||
} | ||
|
||
private void walkSection(SectionDefinition sect, ModConfigSpec.Builder builder, boolean root) { | ||
if(!root) { | ||
builder.comment(sect.commentToArray()); | ||
builder.push(sect.name); | ||
} | ||
|
||
for(ValueDefinition<?> value : sect.getValues()) | ||
addValue(value, builder); | ||
|
||
for(SectionDefinition subsection : sect.getSubsections()) | ||
walkSection(subsection, builder, false); | ||
|
||
if(!root) | ||
builder.pop(); | ||
} | ||
|
||
private <T> void addValue(ValueDefinition<T> val, ModConfigSpec.Builder builder) { | ||
builder.comment(val.commentToArray()); | ||
|
||
ModConfigSpec.ConfigValue<?> forge; | ||
if(val.defaultValue instanceof List<?> list) | ||
forge = builder.defineList(val.name, list, val::validate); | ||
else | ||
forge = builder.define(List.of(val.name), () -> val.defaultValue, val::validate, val.defaultValue.getClass()); //forge is weird | ||
|
||
definitionsToValues.put(val, forge); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T get(ValueDefinition<T> definition) { | ||
ModConfigSpec.ConfigValue<T> forge = (ModConfigSpec.ConfigValue<T>) definitionsToValues.get(definition); | ||
return forge.get(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> void set(ValueDefinition<T> definition, T value) { | ||
ModConfigSpec.ConfigValue<T> forge = (ModConfigSpec.ConfigValue<T>) definitionsToValues.get(definition); | ||
debounceTime = System.currentTimeMillis(); | ||
forge.set(value); | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
debounceTime = 0; //force ConfigChangedEvent to not debounce this, it's important | ||
|
||
//just pick one; they all point to the same FileConfig anyway | ||
definitionsToValues.values().iterator().next().save(); | ||
} | ||
|
||
@Override | ||
public long debounceTime() { | ||
return debounceTime; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/org/violetmoon/zetaimplforge/config/TerribleForgeConfigHackery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.violetmoon.zetaimplforge.config; | ||
|
||
import com.electronwill.nightconfig.core.file.CommentedFileConfig; | ||
import com.electronwill.nightconfig.core.io.WritingMode; | ||
import net.neoforged.fml.ModContainer; | ||
import net.neoforged.fml.ModLoadingContext; | ||
import net.neoforged.fml.config.ConfigTracker; | ||
import net.neoforged.fml.config.ModConfig; | ||
import net.neoforged.fml.util.ObfuscationReflectionHelper; | ||
import net.neoforged.neoforge.common.ModConfigSpec; | ||
|
||
import java.io.Serial; | ||
import java.lang.reflect.Method; | ||
import java.nio.file.Path; | ||
|
||
public class TerribleForgeConfigHackery { | ||
|
||
// private static final Method SET_CONFIG_DATA = ObfuscationReflectionHelper.findMethod(ModConfig.class, "setConfig", LoadedConfig.class, FunctionalInterface.class); | ||
private static final Method SETUP_CONFIG_FILE = ObfuscationReflectionHelper.findMethod(ConfigTracker.class, "setupConfigFile", ModConfig.class, Path.class); | ||
|
||
// TODO: Replace the name string + not 100% sure about this | ||
public static void registerAndLoadConfigEarlierThanUsual(ModConfigSpec spec) { | ||
ModContainer container = ModLoadingContext.get().getActiveContainer(); | ||
ModConfig modConfig = ConfigTracker.INSTANCE.registerConfig(ModConfig.Type.COMMON, spec, container, "zeta-common.toml"); | ||
|
||
ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.COMMON, Path.of(modConfig.getFileName())); | ||
|
||
//same stuff that forge config tracker does | ||
//read config without setting file watcher which could cause resets. forge will load it later | ||
//CommentedFileConfig configData = readConfig(ConfigTracker.INSTANCE, FMLPaths.CONFIGDIR.get(), modConfig); | ||
//CommentedFileConfig configData = handler.reader(FMLPaths.CONFIGDIR.get()).apply( modConfig); | ||
|
||
/* | ||
SET_CONFIG_DATA.setAccessible(true); | ||
try { | ||
SET_CONFIG_DATA.invoke(modConfig, new LoadedConfig(configData, modConfig.getFullPath(), modConfig), ModConfigEvent.Loading::new); | ||
} catch (Exception ignored) {} | ||
//container.dispatchConfigEvent(IConfigEvent.loading(this.config)); | ||
configData.save(); | ||
*/ | ||
} | ||
|
||
//we need this so we dont add a second file watcher. Same as handler::reader | ||
private static CommentedFileConfig readConfig(ConfigTracker handler, Path configBasePath, ModConfig c) { | ||
Path configPath = configBasePath.resolve(c.getFileName()); | ||
CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync(). | ||
preserveInsertionOrder(). | ||
autosave(). | ||
onFileNotFound((newfile, configFormat)->{ | ||
try { | ||
return (Boolean) SETUP_CONFIG_FILE.invoke(handler, c, newfile, configFormat); | ||
} catch (Exception e) { | ||
throw new ConfigLoadingException(c, e); | ||
} | ||
}). | ||
writingMode(WritingMode.REPLACE). | ||
build(); | ||
try { | ||
configData.load(); | ||
} | ||
catch (Exception ex) { | ||
throw new ConfigLoadingException(c, ex); | ||
} | ||
return configData; | ||
} | ||
|
||
private static class ConfigLoadingException extends RuntimeException { | ||
@Serial | ||
private static final long serialVersionUID = 1554369973578001612L; | ||
|
||
public ConfigLoadingException(ModConfig config, Exception cause) { | ||
super("Failed loading config file " + config.getFileName() + " of type " + config.getType() + " for modid " + config.getModId(), cause); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters