@@ -38,7 +38,7 @@ public static void monitorFileWatcher() {
3838 private static final Class <?> WATCHED_FILE = LamdbaExceptionUtils .uncheck (() -> Class .forName ("com.electronwill.nightconfig.core.file.FileWatcher$WatchedFile" ));
3939 private static final Field CHANGE_HANDLER = ObfuscationReflectionHelper .findField (WATCHED_FILE , "changeHandler" );
4040
41- private static final Class <?> WRITE_SYNC_CONFIG = LamdbaExceptionUtils .uncheck (() -> Class .forName ("com.electronwill.nightconfig.core.file.WriteSyncFileConfig" ));
41+ public static final Class <?> WRITE_SYNC_CONFIG = LamdbaExceptionUtils .uncheck (() -> Class .forName ("com.electronwill.nightconfig.core.file.WriteSyncFileConfig" ));
4242 private static final Class <?> AUTOSAVE_CONFIG = LamdbaExceptionUtils .uncheck (() -> Class .forName ("com.electronwill.nightconfig.core.file.AutosaveCommentedFileConfig" ));
4343 private static final Field AUTOSAVE_FILECONFIG = ObfuscationReflectionHelper .findField (AUTOSAVE_CONFIG , "fileConfig" );
4444
@@ -82,24 +82,42 @@ public Object computeIfAbsent(Path key, Function<? super Path, ?> mappingFunctio
8282 }
8383 }
8484
85+ private static final Set <Class <?>> UNKNOWN_FILE_CONFIG_CLASSES = Collections .synchronizedSet (new ReferenceOpenHashSet <>());
86+
87+ public static Object toWriteSyncConfig (Object config ) {
88+ try {
89+ if (WRITE_SYNC_CONFIG .isAssignableFrom (config .getClass ())) {
90+ return config ;
91+ } else if (AUTOSAVE_CONFIG .isAssignableFrom (config .getClass ())) {
92+ FileConfig fc = (FileConfig )AUTOSAVE_FILECONFIG .get (config );
93+ return toWriteSyncConfig (fc );
94+ } else {
95+ if (UNKNOWN_FILE_CONFIG_CLASSES .add (config .getClass ()))
96+ ModernFixMixinPlugin .instance .logger .warn ("Unexpected FileConfig class: {}" , config .getClass ().getName ());
97+ return null ;
98+ }
99+ } catch (ReflectiveOperationException e ) {
100+ return null ;
101+ }
102+ }
103+
85104 static class MonitoringConfigTracker implements Runnable {
86105 private final Runnable configTracker ;
87106
88107 MonitoringConfigTracker (Runnable r ) {
89108 this .configTracker = r ;
90109 }
91110
92- private static final Set <Class <?>> UNKNOWN_FILE_CONFIG_CLASSES = Collections .synchronizedSet (new ReferenceOpenHashSet <>());
93-
94111 private void protectFromSaving (FileConfig config , Runnable runnable ) throws ReflectiveOperationException {
95- if (WRITE_SYNC_CONFIG .isAssignableFrom (config .getClass ())) {
112+ Object writeSyncConfig = toWriteSyncConfig (config );
113+ if (writeSyncConfig != null ) {
96114 // keep trying to write, releasing the config lock each time in case something else needs to lock it
97115 // for any reason
98116 while (true ) {
99117 // acquiring synchronized block here should in theory prevent any other concurrent loads/saves, based
100118 // off WriteSyncFileConfig implementation
101- synchronized (config ) {
102- if (CURRENTLY_WRITING .getBoolean (config )) {
119+ synchronized (writeSyncConfig ) {
120+ if (CURRENTLY_WRITING .getBoolean (writeSyncConfig )) {
103121 ModernFixMixinPlugin .instance .logger .fatal ("Config being written during load!!!" );
104122 try { Thread .sleep (500 ); } catch (InterruptedException e ) { Thread .currentThread ().interrupt (); }
105123 continue ;
@@ -110,12 +128,7 @@ private void protectFromSaving(FileConfig config, Runnable runnable) throws Refl
110128 break ;
111129 }
112130 }
113- } else if (AUTOSAVE_CONFIG .isAssignableFrom (config .getClass ())) {
114- FileConfig fc = (FileConfig )AUTOSAVE_FILECONFIG .get (config );
115- protectFromSaving (fc , runnable );
116131 } else {
117- if (UNKNOWN_FILE_CONFIG_CLASSES .add (config .getClass ()))
118- ModernFixMixinPlugin .instance .logger .warn ("Unexpected FileConfig class: {}" , config .getClass ().getName ());
119132 runnable .run ();
120133 }
121134 }
0 commit comments