|
1 | 1 | package org.violetmoon.zetaimplforge.config;
|
2 | 2 |
|
| 3 | +import java.util.concurrent.atomic.AtomicInteger; |
| 4 | + |
| 5 | +import net.minecraftforge.event.server.ServerAboutToStartEvent; |
3 | 6 | import net.minecraftforge.fml.event.config.ModConfigEvent;
|
| 7 | +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; |
4 | 8 | import org.violetmoon.zeta.Zeta;
|
5 |
| -import org.violetmoon.zeta.event.load.ZConfigChanged; |
6 |
| -import org.violetmoon.zeta.util.zetalist.ZetaList; |
| 9 | +import org.violetmoon.zeta.util.ZetaSide; |
7 | 10 | import org.violetmoon.zetaimplforge.event.load.ForgeZConfigChange;
|
8 | 11 |
|
9 | 12 | public class ConfigEventDispatcher {
|
10 |
| - public static void configChanged(ModConfigEvent event) { |
11 |
| - for(Zeta z : ZetaList.INSTANCE.getZetas()) { |
12 |
| - String modid = z.modid; |
13 |
| - if(!event.getConfig().getModId().equals(modid) || z.configInternals == null) |
14 |
| - continue; |
15 |
| - |
16 |
| - // https://github.com/VazkiiMods/Quark/commit/b0e00864f74539d8650cb349e88d0302a0fda8e4 |
17 |
| - // "The Forge config api writes to the config file on every single change |
18 |
| - // to the config, which would cause the file watcher to trigger |
19 |
| - // a config reload while the config gui is committing changes." |
20 |
| - //TODO: investigate. this doesnt look that sound |
21 |
| - if(System.currentTimeMillis() - z.configInternals.debounceTime() > 20) |
22 |
| - handleConfigChange(z); |
| 13 | + public ConfigEventDispatcher(Zeta z) { |
| 14 | + this.z = z; |
| 15 | + } |
| 16 | + |
| 17 | + private final Zeta z; |
| 18 | + |
| 19 | + // The story is: |
| 20 | + // - Forge dispatches ModConfigEvent.Reloading directly from the NightConfig filewatcher thread |
| 21 | + // (which means it can be received multiple times in a row, as the file save settles) |
| 22 | + // (and also, updating our data structures/event bus ("refreshing the config") becomes |
| 23 | + // a thread safety hazard) |
| 24 | + // - Forge seemingly dispatches ModConfigEvent.Reloading for fun sometimes |
| 25 | + // (including in the middle of other modloading events, and 3-4 times during FMLCommonSetupEvent#enqueueWork) |
| 26 | + // - We call dispatchAllInitialLoads from FMLCommonSetupEvent#enqueueWork |
| 27 | + // (on the server, this at least reliably gets us onto a thread called 'main'? |
| 28 | + // who's thread is that? idk) |
| 29 | + // - We don't always have access to a "server thread" because this all can run before the server starts! |
| 30 | + // |
| 31 | + // To try and untangle this mess, here's what we do on the client: |
| 32 | + // - All configs start in the NOT_READY state. |
| 33 | + // - In FMLCommonSetupEvent#enqueueWork we refresh the config once on the enqueueWork thread |
| 34 | + // and transition to ACCEPT_FILE_RELOADS. |
| 35 | + // |
| 36 | + // here's what we do on the server: |
| 37 | + // - All configs start in the NOT_READY state. |
| 38 | + // - In FMLCommonSetupEvent#enqueueWork we refresh the config once on the enqueueWork thread |
| 39 | + // and transition to WAITING_FOR_SERVER_START. |
| 40 | + // - In ServerAboutToStartEvent we transition to ACCEPT_FILE_RELOADS. |
| 41 | + // |
| 42 | + // In ModConfigEvent.Reloading, we: |
| 43 | + // - try to find a better thread (which on the client is the render thread, and on the server, |
| 44 | + // since we wait for the server to start, is hopefully the server thread) |
| 45 | + // - if the config is in ACCEPT_FILE_RELOADS: transition to BUSY, refresh the config, and |
| 46 | + // transition back to ACCEPT_FILE_RELOADS. |
| 47 | + // - otherwise just drop the request to refresh the config on the floor. |
| 48 | + // (It's too early, or it's reentrant, and that will cause problems.) |
| 49 | + // This also removes an ugly "debounce-time" hack where we'd just ignore ModConfigEvents |
| 50 | + // if enough time hadn't passed since the last one. You lose the ability to reliably refresh |
| 51 | + // the config *while* the server is starting, but like, that's fine. |
| 52 | + // |
| 53 | + // If this is wrong/incorrect/causing problems, BLAME QUAT |
| 54 | + // |
| 55 | + // Parallel Mod Loading Considered Harmful episode 309128313 |
| 56 | + |
| 57 | + //leaving this comment here but the statemachine seems to subsume the old debounce-time hack |
| 58 | + // https://github.com/VazkiiMods/Quark/commit/b0e00864f74539d8650cb349e88d0302a0fda8e4 |
| 59 | + // "The Forge config api writes to the config file on every single change |
| 60 | + // to the config, which would cause the file watcher to trigger |
| 61 | + // a config reload while the config gui is committing changes." |
| 62 | + |
| 63 | + private static final int BEFORE_INIT = 0; |
| 64 | + private static final int WAITING_FOR_SERVER_START = 1; |
| 65 | + private static final int ACCEPT_FILE_RELOADS = 2; |
| 66 | + private static final int BUSY = 3; |
| 67 | + |
| 68 | + private final AtomicInteger state = new AtomicInteger(BEFORE_INIT); |
| 69 | + |
| 70 | + public void commonSetup(FMLCommonSetupEvent event) { |
| 71 | + event.enqueueWork(() -> { |
| 72 | + z.log.info("Common setup: Performing initial refresh of {}'s config on thread '{}'", z.modid, Thread.currentThread().getName()); |
| 73 | + |
| 74 | + z.configManager.onReload(); |
| 75 | + z.loadBus.fire(new ForgeZConfigChange()); |
| 76 | + |
| 77 | + int oldState; |
| 78 | + if(z.side == ZetaSide.CLIENT) { |
| 79 | + //on the client we always have a render thread, we can start accepting config refreshes now |
| 80 | + oldState = state.getAndSet(ACCEPT_FILE_RELOADS); |
| 81 | + z.log.info("{}'s config is now ready to accept filewatcher changes", z.modid); |
| 82 | + } else { |
| 83 | + //on the server we want to wait for the existence of the server thread to process config refreshes on |
| 84 | + oldState = state.getAndSet(WAITING_FOR_SERVER_START); |
| 85 | + z.log.info("Waiting for server start before accepting filewatcher changes to {}'s config", z.modid); |
| 86 | + } |
| 87 | + |
| 88 | + if(oldState != BEFORE_INIT) |
| 89 | + z.log.warn("Common setup: {}'s config was previously in state '{}'... weird, but trying to continue. Report this.", z.modid, fmtState(oldState)); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public void serverAboutToStart(ServerAboutToStartEvent e) { |
| 94 | + if(z.side == ZetaSide.SERVER) { |
| 95 | + z.log.info("Server starting, accepting filewatcher changes for {}'s config", z.modid); |
| 96 | + |
| 97 | + int oldState = state.getAndSet(ACCEPT_FILE_RELOADS); |
| 98 | + if(oldState != WAITING_FOR_SERVER_START) |
| 99 | + z.log.warn("Server start: {}'s config was previously in state '{}'... weird, but trying to continue. Report this.", z.modid, fmtState(oldState)); |
23 | 100 | }
|
24 | 101 | }
|
25 |
| - |
26 |
| - public static void dispatchAllInitialLoads() { |
27 |
| - for(Zeta z : ZetaList.INSTANCE.getZetas()) |
28 |
| - handleConfigChange(z); |
| 102 | + |
| 103 | + public void modConfigReloading(ModConfigEvent.Reloading event) { |
| 104 | + String modid = z.modid; |
| 105 | + if(!event.getConfig().getModId().equals(modid)) |
| 106 | + return; |
| 107 | + |
| 108 | + if(z.configManager == null || z.configInternals == null) { |
| 109 | + z.log.info("Ignoring request to refresh {}'s config WAY too early", z.modid); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + z.log.info("About to refresh {}'s config, looking for better thread than '{}'...", z.modid, Thread.currentThread().getName()); |
| 114 | + z.proxy.tryToExecuteOnMainThread(() -> { |
| 115 | + //if the state is ACCEPT_FILE_RELOADS, transition it to BUSY and continue |
| 116 | + //otherwise drop the request to refresh this config; we might be waiting for |
| 117 | + //FMLCommonSetup/ServerAboutToStart, or it might currently be in the process of refreshing |
| 118 | + int oldState = state.compareAndExchange(ACCEPT_FILE_RELOADS, BUSY); |
| 119 | + if(oldState != ACCEPT_FILE_RELOADS) { |
| 120 | + z.log.info("{}'s config is '{}', ignoring config refresh. Current thread: {}", z.modid, fmtState(oldState), Thread.currentThread().getName()); |
| 121 | + return; |
| 122 | + } else |
| 123 | + z.log.info("Refreshing {}'s config on thread '{}'", z.modid, Thread.currentThread().getName()); |
| 124 | + |
| 125 | + try { |
| 126 | + //actually refresh the config |
| 127 | + z.configManager.onReload(); |
| 128 | + z.loadBus.fire(new ForgeZConfigChange()); |
| 129 | + } finally { |
| 130 | + //ready for another file reload |
| 131 | + z.log.info("All done refreshing {}'s config", z.modid); |
| 132 | + state.setRelease(ACCEPT_FILE_RELOADS); |
| 133 | + } |
| 134 | + }); |
29 | 135 | }
|
30 |
| - |
31 |
| - private static void handleConfigChange(Zeta z) { |
32 |
| - z.configManager.onReload(); |
33 |
| - z.loadBus.fire(new ForgeZConfigChange()); |
| 136 | + |
| 137 | + private static String fmtState(int state) { |
| 138 | + return switch(state) { |
| 139 | + case BEFORE_INIT -> "BEFORE_INIT"; |
| 140 | + case WAITING_FOR_SERVER_START -> "WAITING_FOR_SERVER_START"; |
| 141 | + case ACCEPT_FILE_RELOADS -> "ACCEPT_FILE_RELOADS"; |
| 142 | + case BUSY -> "BUSY"; |
| 143 | + default -> "weird unknown state " + state + "???"; |
| 144 | + }; |
34 | 145 | }
|
35 |
| - |
36 | 146 | }
|
0 commit comments