From e3f8996d13bd370f517734eddede3f8665104623 Mon Sep 17 00:00:00 2001 From: Grayray75 <69988482+Grayray75@users.noreply.github.com> Date: Sat, 12 Aug 2023 21:49:52 +0200 Subject: [PATCH] Fix config system --- .../mods/phosphor/mod/PhosphorConfig.java | 37 +++---------------- .../mods/phosphor/mod/PhosphorMod.java | 12 +++++- .../mod/world/lighting/LightingEngine.java | 12 +++--- src/main/resources/fabric.mod.json | 6 ++- 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorConfig.java b/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorConfig.java index ceb5289..9b7536e 100644 --- a/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorConfig.java +++ b/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorConfig.java @@ -3,29 +3,19 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.SerializedName; +import net.fabricmc.loader.api.FabricLoader; import java.io.*; -// TODO: this is currently not used // This class will be initialized very early and should never load any game/mod code. public class PhosphorConfig { - private static final Gson gson = createGson(); - - private static PhosphorConfig INSTANCE; + private static final Gson GSON = createGson(); @SerializedName("enable_illegal_thread_access_warnings") public boolean enableIllegalThreadAccessWarnings = true; - @SerializedName("enable_phosphor") - public boolean enablePhosphor = true; - public static PhosphorConfig loadConfig() { - if (INSTANCE != null) { - return INSTANCE; - } - File file = getConfigFile(); - PhosphorConfig config; if (!file.exists()) { @@ -34,42 +24,25 @@ public static PhosphorConfig loadConfig() { } else { try (Reader reader = new FileReader(file)) { - config = gson.fromJson(reader, PhosphorConfig.class); + config = GSON.fromJson(reader, PhosphorConfig.class); } catch (IOException e) { throw new RuntimeException("Failed to deserialize config from disk", e); } } - INSTANCE = config; - return config; } public void saveConfig() { - File dir = getConfigDirectory(); - - if (!dir.exists()) { - if (!dir.mkdirs()) { - throw new RuntimeException("Could not create configuration directory at '" + dir.getAbsolutePath() + "'"); - } - } - else if (!dir.isDirectory()) { - throw new RuntimeException("Configuration directory at '" + dir.getAbsolutePath() + "' is not a directory"); - } - try (Writer writer = new FileWriter(getConfigFile())) { - gson.toJson(this, writer); + GSON.toJson(this, writer); } catch (IOException e) { throw new RuntimeException("Failed to serialize config to disk", e); } } - private static File getConfigDirectory() { - return new File("config"); - } - private static File getConfigFile() { - return new File(getConfigDirectory(), "phosphor.json"); + return new File(FabricLoader.getInstance().getConfigDir().toString(), "phosphor-legacy.json"); } private static Gson createGson() { diff --git a/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorMod.java b/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorMod.java index bdd6ef4..bcb0b96 100644 --- a/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorMod.java +++ b/src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorMod.java @@ -1,8 +1,16 @@ package me.jellysquid.mods.phosphor.mod; +import net.fabricmc.api.ModInitializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -public class PhosphorMod { - public static final Logger LOGGER = LogManager.getLogger("Phosphor"); +public class PhosphorMod implements ModInitializer { + public static Logger LOGGER; + public static PhosphorConfig CONFIG; + + @Override + public void onInitialize() { + LOGGER = LogManager.getLogger("phosphor-legacy"); + CONFIG = PhosphorConfig.loadConfig(); + } } diff --git a/src/main/java/me/jellysquid/mods/phosphor/mod/world/lighting/LightingEngine.java b/src/main/java/me/jellysquid/mods/phosphor/mod/world/lighting/LightingEngine.java index 1a8fefb..09e09df 100644 --- a/src/main/java/me/jellysquid/mods/phosphor/mod/world/lighting/LightingEngine.java +++ b/src/main/java/me/jellysquid/mods/phosphor/mod/world/lighting/LightingEngine.java @@ -3,6 +3,7 @@ import me.jellysquid.mods.phosphor.api.IChunkLighting; import me.jellysquid.mods.phosphor.api.ILightingEngine; import me.jellysquid.mods.phosphor.mixins.DirectionAccessor; +import me.jellysquid.mods.phosphor.mod.PhosphorMod; import me.jellysquid.mods.phosphor.mod.collections.PooledLongQueue; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -24,7 +25,7 @@ public class LightingEngine implements ILightingEngine { private static final int MAX_LIGHT = 15; - //private final Thread ownedThread = Thread.currentThread(); + private final Thread ownedThread = Thread.currentThread(); private final World world; @@ -196,12 +197,11 @@ private boolean isCallingFromMainThread() { private void acquireLock() { if (!this.lock.tryLock()) { // If we cannot lock, something has gone wrong... Only one thread should ever acquire the lock. - // Validate that we're on the right thread immediately so we can gather information. + // Validate that we're on the right thread immediately, so we can gather information. // It is NEVER valid to call World methods from a thread other than the owning thread of the world instance. // Users can safely disable this warning, however it will not resolve the issue. - /* - if (LightingEnginePlugin.ENABLE_ILLEGAL_THREAD_ACCESS_WARNINGS) { + if (PhosphorMod.CONFIG.enableIllegalThreadAccessWarnings) { Thread current = Thread.currentThread(); if (current != this.ownedThread) { @@ -216,10 +216,8 @@ private void acquireLock() { " of the current soft warning. You should report this issue to our issue tracker with the following stacktrace information.\n(If you are" + " aware you have misbehaving mods and cannot resolve this issue, you can safely disable this warning by setting" + " `enable_illegal_thread_access_warnings` to `false` in Phosphor's configuration file for the time being.)", e); - } - - }*/ + } // Wait for the lock to be released. This will likely introduce unwanted stalls, but will mitigate the issue. this.lock.lock(); diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 519ebc9..1ce3603 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -16,7 +16,11 @@ "license": "LGPL-3.0-only", "icon": "assets/phosphor-legacy/icon.png", "environment": "*", - "entrypoints": {}, + "entrypoints": { + "main": [ + "me.jellysquid.mods.phosphor.mod.PhosphorMod" + ] + }, "mixins": [ "phosphor.mixins.json" ],