Skip to content

Commit

Permalink
Fix config system
Browse files Browse the repository at this point in the history
  • Loading branch information
Grayray75 committed Aug 12, 2023
1 parent c460c7b commit e3f8996
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
37 changes: 5 additions & 32 deletions src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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() {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/me/jellysquid/mods/phosphor/mod/PhosphorMod.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down

0 comments on commit e3f8996

Please sign in to comment.