Skip to content

Commit

Permalink
add jvm flag to dump generated lang keys to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Oct 3, 2024
1 parent ea50d8f commit b258736
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void preInit(FMLPreInitializationEvent event) {

public void init(FMLInitializationEvent event) {
NetworkHandler.init();
ConfigurationManager.onInit();
}

public void postInit(FMLPostInitializationEvent event) {}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
* </p>
* Default pattern: {@code %mod.%cat.%field}. Categories use the pattern without {@code %field}. Can be overridden
* for fields with {@link Config.LangKey}. <br>
* The generated keys can be printed to log by setting the {@code -Dgtnhlib.printkeys=true} JVM flag.
* The generated keys can be printed to log by setting the {@code -Dgtnhlib.printkeys=true} JVM flag or dumped to a
* file in the base minecraft directory by setting the {@code -Dgtnhlib.dumpkeys=true} JVM flag.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gtnewhorizon.gtnhlib.config;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -23,6 +24,7 @@
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
Expand All @@ -43,6 +45,8 @@ public class ConfigurationManager {
private static final Map<Configuration, Map<String, Set<Class<?>>>> configToCategoryClassMap = new HashMap<>();
private static final String[] langKeyPlaceholders = new String[] { "%mod", "%file", "%cat", "%field" };
private static final Boolean PRINT_KEYS = Boolean.getBoolean("gtnhlib.printkeys");
private static final Boolean DUMP_KEYS = Boolean.getBoolean("gtnhlib.dumpkeys");
private static final Map<String, List<String>> generatedLangKeys = new HashMap<>();

private static final ConfigurationManager instance = new ConfigurationManager();

Expand All @@ -51,7 +55,7 @@ public class ConfigurationManager {
private static Path configDir;

/**
* Registers a configuration class to be loaded. This should be done in preInit.
* Registers a configuration class to be loaded. This should be done before or during preInit.
*
* @param configClass The class to register.
*/
Expand Down Expand Up @@ -377,6 +381,10 @@ private static String getLangKey(Class<?> configClass, @Nullable Config.LangKey
LOGGER.info("Lang key for category {}: {}", category.getName(), key);
}
}

if (DUMP_KEYS) {
generatedLangKeys.computeIfAbsent(cfg.modid(), k -> new ArrayList<>()).add(key + "=");
}
return key;
}

Expand Down Expand Up @@ -439,4 +447,25 @@ private static void init() {
configDir = minecraftHome().toPath().resolve("config");
initialized = true;
}

public static void onInit() {
if (DUMP_KEYS) {
writeLangKeysToFile();
}
}

private static void writeLangKeysToFile() {
for (Map.Entry<String, List<String>> entry : generatedLangKeys.entrySet()) {
val langFile = new File("generated_keys_" + entry.getKey() + ".txt");
try {
FileUtils.writeLines(langFile, entry.getValue());
} catch (IOException e) {
LOGGER.error(
"Failed to write lang keys for {} to file {}",
entry.getKey(),
langFile.getAbsolutePath(),
e);
}
}
}
}

0 comments on commit b258736

Please sign in to comment.