Generify config patch to also work with Night Config Fixes mod
This commit is contained in:
parent
b499a054b9
commit
36eb73b28e
|
|
@ -12,6 +12,8 @@ import org.embeddedt.modernfix.util.CommonModUtil;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
@ -34,17 +36,32 @@ public class NightConfigFixer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Class<?> WATCHED_FILE = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.FileWatcher$WatchedFile"));
|
private static final Class<?> WATCHED_FILE = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.FileWatcher$WatchedFile"));
|
||||||
private static final Class<?> CONFIG_WATCHER = LamdbaExceptionUtils.uncheck(() -> Class.forName("net.minecraftforge.fml.config.ConfigFileTypeHandler$ConfigWatcher"));
|
|
||||||
private static final Field CHANGE_HANDLER = ObfuscationReflectionHelper.findField(WATCHED_FILE, "changeHandler");
|
private static final Field CHANGE_HANDLER = ObfuscationReflectionHelper.findField(WATCHED_FILE, "changeHandler");
|
||||||
|
|
||||||
private static final Field COMMENTED_FILE_CONFIG = ObfuscationReflectionHelper.findField(CONFIG_WATCHER, "commentedFileConfig");
|
|
||||||
|
|
||||||
private static final Class<?> WRITE_SYNC_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.WriteSyncFileConfig"));
|
private static final Class<?> WRITE_SYNC_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.WriteSyncFileConfig"));
|
||||||
private static final Class<?> AUTOSAVE_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.AutosaveCommentedFileConfig"));
|
private static final Class<?> AUTOSAVE_CONFIG = LamdbaExceptionUtils.uncheck(() -> Class.forName("com.electronwill.nightconfig.core.file.AutosaveCommentedFileConfig"));
|
||||||
private static final Field AUTOSAVE_FILECONFIG = ObfuscationReflectionHelper.findField(AUTOSAVE_CONFIG, "fileConfig");
|
private static final Field AUTOSAVE_FILECONFIG = ObfuscationReflectionHelper.findField(AUTOSAVE_CONFIG, "fileConfig");
|
||||||
|
|
||||||
private static final Field CURRENTLY_WRITING = ObfuscationReflectionHelper.findField(WRITE_SYNC_CONFIG, "currentlyWriting");
|
private static final Field CURRENTLY_WRITING = ObfuscationReflectionHelper.findField(WRITE_SYNC_CONFIG, "currentlyWriting");
|
||||||
|
|
||||||
|
private static final Map<Class<?>, Field> CONFIG_WATCHER_TO_CONFIG_FIELD = Collections.synchronizedMap(new HashMap<>());
|
||||||
|
|
||||||
|
private static Field getCurrentlyWritingFieldOnWatcher(Object watcher) {
|
||||||
|
return CONFIG_WATCHER_TO_CONFIG_FIELD.computeIfAbsent(watcher.getClass(), clz -> {
|
||||||
|
while(clz != null && clz != Object.class) {
|
||||||
|
for(Field f : clz.getDeclaredFields()) {
|
||||||
|
if(CommentedFileConfig.class.isAssignableFrom(f.getType())) {
|
||||||
|
f.setAccessible(true);
|
||||||
|
ModernFixMixinPlugin.instance.logger.debug("Found CommentedFileConfig: field '{}' on {}", f.getName(), clz.getName());
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clz = clz.getSuperclass();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static class MonitoringMap extends ConcurrentHashMap<Path, Object> {
|
static class MonitoringMap extends ConcurrentHashMap<Path, Object> {
|
||||||
public MonitoringMap(ConcurrentHashMap<Path, ?> oldMap) {
|
public MonitoringMap(ConcurrentHashMap<Path, ?> oldMap) {
|
||||||
super(oldMap);
|
super(oldMap);
|
||||||
|
|
@ -56,11 +73,7 @@ public class NightConfigFixer {
|
||||||
Object watchedFile = mappingFunction.apply(path);
|
Object watchedFile = mappingFunction.apply(path);
|
||||||
try {
|
try {
|
||||||
Runnable changeHandler = (Runnable)CHANGE_HANDLER.get(watchedFile);
|
Runnable changeHandler = (Runnable)CHANGE_HANDLER.get(watchedFile);
|
||||||
// replace Forge's config tracker with our own
|
CHANGE_HANDLER.set(watchedFile, new MonitoringConfigTracker(changeHandler));
|
||||||
if(CONFIG_WATCHER.isAssignableFrom(changeHandler.getClass()))
|
|
||||||
CHANGE_HANDLER.set(watchedFile, new MonitoringConfigTracker(changeHandler));
|
|
||||||
else
|
|
||||||
ModernFixMixinPlugin.instance.logger.warn("Unexpected Forge config tracker class: {}", changeHandler.getClass().getName());
|
|
||||||
} catch(ReflectiveOperationException e) {
|
} catch(ReflectiveOperationException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -99,15 +112,18 @@ public class NightConfigFixer {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
CommentedFileConfig cfg = (CommentedFileConfig)COMMENTED_FILE_CONFIG.get(this.configTracker);
|
Field theField = getCurrentlyWritingFieldOnWatcher(this.configTracker);
|
||||||
while(isSaving(cfg)) {
|
if(theField != null) {
|
||||||
// block until the config is no longer marked as saving
|
CommentedFileConfig cfg = (CommentedFileConfig)theField.get(this.configTracker);
|
||||||
// Forge shouldn't save the config twice in a row under normal conditions so this should fix the
|
while(isSaving(cfg)) {
|
||||||
// original bug
|
// block until the config is no longer marked as saving
|
||||||
try {
|
// Forge shouldn't save the config twice in a row under normal conditions so this should fix the
|
||||||
Thread.sleep(500);
|
// original bug
|
||||||
} catch (InterruptedException e) {
|
try {
|
||||||
return;
|
Thread.sleep(500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(ReflectiveOperationException e) {
|
} catch(ReflectiveOperationException e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user