Skip to content

Commit

Permalink
Add config code (GUI next)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Apr 18, 2023
1 parent 6c44dd4 commit e2dbe8a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package io.github.gaming32.worldhost;

import io.github.gaming32.worldhost.upnp.Gateway;
import io.github.gaming32.worldhost.upnp.GatewayFinder;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.network.protocol.status.ServerStatus;
import org.quiltmc.json5.JsonReader;
import org.quiltmc.json5.JsonWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.*;

//#if MC >= 11700
Expand Down Expand Up @@ -56,7 +64,13 @@ public class WorldHost
//$$ LogManager.getLogger();
//#endif

public static final File CACHE_DIR = new File(Minecraft.getInstance().gameDirectory, ".world-host-cache");
public static final File GAME_DIR = Minecraft.getInstance().gameDirectory;
public static final File CACHE_DIR = new File(GAME_DIR, ".world-host-cache");

public static final File CONFIG_DIR = new File(GAME_DIR, "config");
public static final Path CONFIG_FILE = new File(CONFIG_DIR, "world-host.json5").toPath();
public static final Path OLD_CONFIG_FILE = new File(CONFIG_DIR, "world-host.json").toPath();
public static final WorldHostConfig CONFIG = new WorldHostConfig();

public static final Set<UUID> ONLINE_FRIENDS = new HashSet<>();
public static final Map<UUID, ServerStatus> ONLINE_FRIEND_PINGS = new HashMap<>();
Expand All @@ -66,6 +80,8 @@ public class WorldHost

public static final UUID CONNECTION_ID = UUID.randomUUID();

public static Gateway upnpGateway;

//#if FABRIC
@Override
public void onInitializeClient() {
Expand All @@ -74,7 +90,46 @@ public void onInitializeClient() {
//#endif

private static void init() {
LOGGER.info("World Host is initializing...");
LOGGER.info("Using client-generated connection ID {}", CONNECTION_ID);

loadConfig();

new GatewayFinder(gateway -> {
upnpGateway = gateway;
LOGGER.info("Found UPnP gateway: {}", gateway.getGatewayIP());
});
}

public static void loadConfig() {
try (JsonReader reader = JsonReader.json5(CONFIG_FILE)) {
CONFIG.read(reader);
if (Files.exists(OLD_CONFIG_FILE)) {
LOGGER.info("Old {} still exists. Maybe consider removing it?", OLD_CONFIG_FILE.getFileName());
}
} catch (NoSuchFileException e) {
try (JsonReader reader = JsonReader.json(OLD_CONFIG_FILE)) {
CONFIG.read(reader);
LOGGER.info(
"Found and read old {} into new {}. Maybe consider deleting the old {}?",
OLD_CONFIG_FILE.getFileName(), CONFIG_FILE.getFileName(), OLD_CONFIG_FILE.getFileName()
);
} catch (NoSuchFileException e1) {
LOGGER.info("Old {} not found. Writing default.", CONFIG_FILE.getFileName());
} catch (IOException e1) {
LOGGER.error("Failed to load old {}.", OLD_CONFIG_FILE.getFileName(), e1);
}
} catch (IOException e) {
LOGGER.error("Failed to load {}.", CONFIG_FILE.getFileName(), e);
}
writeConfig();
}

public static void writeConfig() {
try (JsonWriter writer = JsonWriter.json5(CONFIG_FILE)) {
CONFIG.write(writer);
} catch (IOException e) {
LOGGER.error("Failed to write {}.", CONFIG_FILE.getFileName(), e);
}
}

//#if FORGE
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/io/github/gaming32/worldhost/WorldHostConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.gaming32.worldhost;

import org.quiltmc.json5.JsonReader;
import org.quiltmc.json5.JsonWriter;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;

public class WorldHostConfig {
private String serverIp = "world-host.jemnetworks.com:9646";

private boolean showOnlineStatus = true;

private boolean enableFriends = true;

private boolean enableReconnectionToasts = false;

private final Set<UUID> friends = new LinkedHashSet<>();

public void read(JsonReader reader) throws IOException {
reader.beginObject();
while (reader.hasNext()) {
final String key;
switch (key = reader.nextName()) {
case "serverIp" -> serverIp = reader.nextString();
case "serverUri" -> {
WorldHost.LOGGER.info("Found old-style serverUri. Converting to new-style serverIp.");
final String serverUri = reader.nextString();
final int index = serverUri.indexOf("://");
if (index == -1) {
WorldHost.LOGGER.warn("Invalid serverUri. Missing ://");
serverIp = serverUri;
continue;
}
serverIp = serverUri.substring(index + 3);
}
case "showOnlineStatus" -> showOnlineStatus = reader.nextBoolean();
case "enableFriends" -> enableFriends = reader.nextBoolean();
case "enableReconnectionToasts" -> enableReconnectionToasts = reader.nextBoolean();
case "friends" -> {
friends.clear();
reader.beginArray();
while (reader.hasNext()) {
friends.add(UUID.fromString(reader.nextString()));
}
reader.endArray();
}
default -> {
WorldHost.LOGGER.warn("Unknown WH config key {}. Skipping.", key);
reader.skipValue();
}
}
}
reader.endObject();
}

public void write(JsonWriter writer) throws IOException {
writer.beginObject();
writer.name("serverIp").value(serverIp);
writer.name("showOnlineStatus").value(showOnlineStatus);
writer.name("enableFriends").value(enableFriends);
writer.name("enableReconnectionToasts").value(enableReconnectionToasts);

writer.name("friends").beginArray();
for (final UUID friend : friends) {
writer.value(friend.toString());
}
writer.endArray();

writer.endObject();
}
}
2 changes: 2 additions & 0 deletions version.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ dependencies {
compileOnly("org.spongepowered:mixin:0.7.11-SNAPSHOT")
shade("cc.polyfrost:oneconfig-wrapper-launchwrapper:1.0.0-beta+")
}

includeImplementation("org.quiltmc:quilt-json5:1.0.2")
}

val generatedResources = "$buildDir/generated-resources/main"
Expand Down

0 comments on commit e2dbe8a

Please sign in to comment.