Skip to content

Commit

Permalink
CompactChatClient: Migrate old .json5 files to .json
Browse files Browse the repository at this point in the history
  • Loading branch information
caoimhebyrne committed Jul 14, 2023
1 parent ce72453 commit fdd8676
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/dev/caoimhe/compactchat/CompactChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import dev.caoimhe.compactchat.util.CollectionUtil;
import dev.caoimhe.compactchat.util.FabricLoaderUtil;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Files;
import java.util.List;

public class CompactChatClient implements ClientModInitializer {
Expand All @@ -22,12 +24,41 @@ public class CompactChatClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
this.migrateJson5Configuration();

Configuration.register();
// TODO: Migrate old .json5 config files to .json

var message = CollectionUtil.randomFrom(STARTUP_MESSAGES);
message = message.replace("{RANDOM_MOD}", FabricLoaderUtil.getRandomModName());

LOGGER.info(message + " (Compact Chat is ready!)");
}

/**
* Migrates the old JSON5 configuration file to the new JSON configuration file.
* We can't really remove this, because it would break the configuration for users who are updating from 2.0.1 or below.
*/
private void migrateJson5Configuration() {
// Exceptions shouldn't really happen (apart from Files#move), but we don't want this to prevent the user from starting their game.
try {
var oldFile = FabricLoader.getInstance().getConfigDir().resolve("compact-chat.json5");
if (!Files.exists(oldFile)) {
LOGGER.debug("No old configuration file found. Skipping automatic migration.");
return;
}

var newFile = FabricLoader.getInstance().getConfigDir().resolve("compact-chat.json");
if (Files.exists(newFile)) {
LOGGER.warn("Both an old (<=2.0.1) and new configuration file (>=2.1.0) exist. Skipping automatic migration.");
return;
}

Files.move(oldFile, newFile);
LOGGER.info("Successfully migrated old configuration file.");
} catch (Exception e) {
LOGGER.error("Failed to migrate old configuration file!", e);
LOGGER.error("Please manually migrate your old configuration file (config/compact-chat.json5) to the new location (config/compact-chat.json).");
LOGGER.error("If you don't do this, your configuration will be reset to the default values.");
}
}
}

0 comments on commit fdd8676

Please sign in to comment.