Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/main/java/net/vulkanmod/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.renderer.v1.Renderer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.chat.Component;
import net.vulkanmod.config.Config;
import net.vulkanmod.config.Platform;
import net.vulkanmod.config.UpdateChecker;
import net.vulkanmod.config.option.Option;
import net.vulkanmod.config.option.OptionRegistry;
import net.vulkanmod.config.option.Options;
import net.vulkanmod.config.video.VideoModeManager;
import net.vulkanmod.render.chunk.build.frapi.VulkanModRenderer;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -44,14 +48,7 @@ public void onInitializeClient() {
}

private static Config loadConfig(Path path) {
Config config = Config.load(path);

if(config == null) {
config = new Config();
config.write();
}

return config;
return Config.load(path);
}

public static String getVersion() {
Expand Down
141 changes: 104 additions & 37 deletions src/main/java/net/vulkanmod/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,142 @@
package net.vulkanmod.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import net.vulkanmod.Initializer;
import net.vulkanmod.config.video.VideoMode;
import net.vulkanmod.config.video.VideoModeManager;
import net.vulkanmod.config.video.VideoModeSet;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;

@JsonAdapter(Config.GsonAdapter.class)
public class Config {
public VideoModeSet.VideoMode videoMode = VideoModeManager.getFirstAvailable().getVideoMode();

public VideoMode videoMode;
public int windowMode = 0;

public int advCulling = 2;
public boolean indirectDraw = true;

public boolean uniqueOpaqueLayer = true;
public boolean entityCulling = true;
public int device = -1;

public int ambientOcclusion = 1;
public int frameQueueSize = 2;
public int builderThreads = 0;

public boolean backFaceCulling = true;
public boolean textureAnimations = true;

public void write() {
public int device = -1;

if(!Files.exists(CONFIG_PATH.getParent())) {
try {
Files.createDirectories(CONFIG_PATH);
} catch (IOException e) {
e.printStackTrace();
}
}
private static Path CONFIG_PATH;
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(Config.class, new GsonAdapter())
.create();

public void save() {
try {
Files.write(CONFIG_PATH, Collections.singleton(GSON.toJson(this)));
Files.createDirectories(CONFIG_PATH.getParent());
Files.writeString(CONFIG_PATH, GSON.toJson(this));
} catch (IOException e) {
e.printStackTrace();
Initializer.LOGGER.error("Error saving config file!", e);
}
}

private static Path CONFIG_PATH;

private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.create();

public static Config load(Path path) {
Config config;
Config.CONFIG_PATH = path;
CONFIG_PATH = path;

if (Files.exists(path)) {
try (FileReader fileReader = new FileReader(path.toFile())) {
config = GSON.fromJson(fileReader, Config.class);
try {
String content = Files.readString(path);
Config config = GSON.fromJson(content, Config.class);

if (config.videoMode == null ||
VideoModeManager.findSetFor(config.videoMode) == null) {
config.videoMode = VideoModeManager.currentOsMode();
}

return config;
} catch (IOException | JsonSyntaxException e) {
System.err.println("Failed to load config, using defaults: " + e.getMessage());
}
catch (IOException exception) {
throw new RuntimeException(exception.getMessage());
}

Config config = new Config();
config.videoMode = VideoModeManager.currentOsMode();
return config;
}

public static class GsonAdapter implements JsonSerializer<Config>, JsonDeserializer<Config> {

@Override
public JsonElement serialize(Config src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();

if (src.videoMode != null) {
JsonObject vm = new JsonObject();
vm.addProperty("width", src.videoMode.width());
vm.addProperty("height", src.videoMode.height());
vm.addProperty("bitDepth", src.videoMode.bitDepth());
vm.addProperty("refreshRate", src.videoMode.refreshRate());
obj.add("videoMode", vm);
}

obj.addProperty("windowMode", src.windowMode);
obj.addProperty("advCulling", src.advCulling);
obj.addProperty("indirectDraw", src.indirectDraw);
obj.addProperty("uniqueOpaqueLayer", src.uniqueOpaqueLayer);
obj.addProperty("entityCulling", src.entityCulling);
obj.addProperty("ambientOcclusion", src.ambientOcclusion);
obj.addProperty("frameQueueSize", src.frameQueueSize);
obj.addProperty("builderThreads", src.builderThreads);
obj.addProperty("backFaceCulling", src.backFaceCulling);
obj.addProperty("textureAnimations", src.textureAnimations);
obj.addProperty("device", src.device);

return obj;
}
else {
config = null;

@Override
public Config deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Config config = new Config();
JsonObject obj = json.getAsJsonObject();

if (obj.has("videoMode")) {
JsonObject vm = obj.getAsJsonObject("videoMode");
int w = getInt(vm, "width", 1920);
int h = getInt(vm, "height", 1080);
int bd = getInt(vm, "bitDepth", 8);
int rr = getInt(vm, "refreshRate", 60);
config.videoMode = new VideoMode(w, h, bd, rr);
} else {
config.videoMode = VideoModeManager.currentOsMode();
}

config.windowMode = getInt(obj, "windowMode", 0);
config.advCulling = getInt(obj, "advCulling", 2);
config.indirectDraw = getBoolean(obj, "indirectDraw");
config.uniqueOpaqueLayer = getBoolean(obj, "uniqueOpaqueLayer");
config.entityCulling = getBoolean(obj, "entityCulling");
config.ambientOcclusion = getInt(obj, "ambientOcclusion", 1);
config.frameQueueSize = getInt(obj, "frameQueueSize", 2);
config.builderThreads = getInt(obj, "builderThreads", 0);
config.backFaceCulling = getBoolean(obj, "backFaceCulling");
config.textureAnimations = getBoolean(obj, "textureAnimations");
config.device = getInt(obj, "device", -1);

return config;
}

return config;
private int getInt(JsonObject obj, String key, int def) {
JsonElement el = obj.get(key);
return el != null && el.isJsonPrimitive() ? el.getAsInt() : def;
}

private boolean getBoolean(JsonObject obj, String key) {
JsonElement el = obj.get(key);
return el == null || !el.isJsonPrimitive() || el.getAsBoolean();
}
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/net/vulkanmod/config/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class Platform {

public static void init() {
GLFW.glfwInitHint(GLFW_PLATFORM, activePlat);
LOGGER.info("Selecting Platform: {}", getStringFromPlat(activePlat));
LOGGER.info("Selecting Platform: {}", getStringFromPlat());
LOGGER.info("GLFW: {}", GLFW.glfwGetVersionString());
GLFW.glfwInit();
}
Expand All @@ -40,14 +40,14 @@ private static int getSupportedPlat() {
return GLFW_ANY_PLATFORM; //Unknown platform
}

private static String getStringFromPlat(int plat) {
return switch (plat) {
private static String getStringFromPlat() {
return switch (Platform.activePlat) {
case GLFW_PLATFORM_WIN32 -> "WIN32";
case GLFW_PLATFORM_WAYLAND -> "WAYLAND";
case GLFW_PLATFORM_X11 -> "X11";
case GLFW_PLATFORM_COCOA -> "MACOS";
case GLFW_ANY_PLATFORM -> "ANDROID";
default -> throw new IllegalStateException("Unexpected value: " + plat);
default -> throw new IllegalStateException("Unexpected value: " + Platform.activePlat);
};
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/vulkanmod/config/gui/GuiElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.input.MouseButtonEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class GuiElement implements GuiEventListener, NarratableEntry {
Expand All @@ -22,6 +22,7 @@ public abstract class GuiElement implements GuiEventListener, NarratableEntry {
protected int hoverTime;
protected long hoverStopTime;

@SuppressWarnings("unused") // this will surely be used some day
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
Expand All @@ -34,6 +35,7 @@ public void setPosition(int x, int y, int width, int height) {
this.height = height;
}

@SuppressWarnings("unused") // this will surely be used someday
public void resize(int width, int height) {
this.width = width;
this.height = height;
Expand Down Expand Up @@ -102,7 +104,7 @@ public ComponentPath getCurrentFocusPath() {
}

@Override
public ScreenRectangle getRectangle() {
public @NotNull ScreenRectangle getRectangle() {
return GuiEventListener.super.getRectangle();
}

Expand All @@ -117,7 +119,7 @@ public boolean isFocused() {
}

@Override
public NarrationPriority narrationPriority() {
public @NotNull NarrationPriority narrationPriority() {
return NarrationPriority.NONE;
}

Expand Down
Loading