about 2 times faster, a more sane default cache

This commit is contained in:
JFronny 2020-07-11 12:45:27 +02:00
parent a36e2b8079
commit ebef6df51b
10 changed files with 4725 additions and 4691 deletions

View File

@ -13,7 +13,7 @@ sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8
archivesBaseName = project.archives_base_name archivesBaseName = project.archives_base_name
version = project.mod_version version = "${project.mod_version}+${project.minecraft_version}"
group = project.maven_group group = project.maven_group
dependencies { dependencies {

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.8.8+build.202 loader_version=0.8.8+build.202
# Mod Properties # Mod Properties
mod_version = 1.0.1 mod_version = 1.0.2
maven_group = io.gitlab.jfronny maven_group = io.gitlab.jfronny
archives_base_name = translater archives_base_name = translater

View File

@ -1,5 +1,7 @@
package io.gitlab.jfronny.translater; package io.gitlab.jfronny.translater;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.ConfigManager;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import java.io.*; import java.io.*;
@ -15,8 +17,10 @@ public abstract class CachingTransformer implements StringTransformer {
return str; return str;
if (cache == null) { if (cache == null) {
cache = new Properties(); cache = new Properties();
if (!ModInit.cfg.forceRegenerate) { if (ModInit.cfg.forceRegenerate) {
ModInit.cfg.forceRegenerate = false; ModInit.cfg.forceRegenerate = false;
((ConfigManager<Cfg>) AutoConfig.getConfigHolder(Cfg.class)).save();
} else {
if (cacheFile.exists()) { if (cacheFile.exists()) {
try { try {
FileInputStream inS = new FileInputStream(cacheFile); FileInputStream inS = new FileInputStream(cacheFile);
@ -26,7 +30,7 @@ public abstract class CachingTransformer implements StringTransformer {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
if (!ModInit.cfg.breakFully && ModInit.cfg.rounds == 10) { if (!ModInit.cfg.breakFully && ModInit.cfg.rounds == 5) {
try { try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inS = classLoader.getResourceAsStream("namecache.ini"); InputStream inS = classLoader.getResourceAsStream("namecache.ini");

View File

@ -7,11 +7,15 @@ import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.Comment;
@Config(name = "TranslaterCF") @Config(name = "TranslaterCF")
public class Cfg implements ConfigData { public class Cfg implements ConfigData {
@Comment("NOTE: remove the cache after you change the config!\nRegenerating the cache can take up to 3 hours for vanilla (5000 strings)\nWatch the log when in doubt\n\nThe amount of times to translate each element") @Comment("NOTE: remove the cache after you change the config!\nRegenerating the cache can take up to 3 hours for vanilla (5000 strings)\nWatch the log when in doubt\n\nThe amount of times to translate each element")
public int rounds = 10; public int rounds = 5;
@Comment("Whether to fully break the texts content by translating from the wrong language (enable for complete breaking)") @Comment("Whether to fully break the texts content by translating from the wrong language (enable for complete breaking)")
public boolean breakFully = false; public boolean breakFully = false;
@Comment("The API key for Yandex Translate (this is REQUIRED for updating the cache, since Yandex doesn't give them out for free anymore you might want to google \"trnsl.1.1.\" to find keys on the web)") @Comment("The API key for Yandex Translate (this is REQUIRED for updating the cache, since Yandex doesn't give them out for free anymore you might want to google \"trnsl.1.1.\" to find keys on the web)")
public String key = "trnsl.1.1.20130811T164454Z.2facd8a3323b8111.e9f682063308aff12357de3c8a3260d6d6b71be7"; public String key = "trnsl.1.1.20130811T164454Z.2facd8a3323b8111.e9f682063308aff12357de3c8a3260d6d6b71be7";
@Comment("Enable this to get more information about what is happening currently - useful when caching takes long")
public boolean verboseLogging = false;
@Comment("The language to translate to - Leave empty for auto-detection (might break text even more)")
public String targetLanguage = "en";
@Comment("Use this if something is broken. This initiate the regeneration of the cache") @Comment("Use this if something is broken. This initiate the regeneration of the cache")
public boolean forceRegenerate = false; public boolean forceRegenerate = false;
} }

View File

@ -19,15 +19,15 @@ public class ModInit implements ModInitializer {
} }
public static void Log(String msg) { public static void Log(String msg) {
logger.log(Level.INFO, "[" + MOD_NAME + "]" + msg); logger.log(Level.INFO, "[" + MOD_NAME + "] " + msg);
} }
public static void Warn(String msg) { public static void Warn(String msg) {
logger.log(Level.WARN, "[" + MOD_NAME + "]" + msg); logger.log(Level.WARN, "[" + MOD_NAME + "] " + msg);
} }
public static void LogDebug(String msg) { public static void LogDebug(String msg) {
logger.log(Level.DEBUG, "[" + MOD_NAME + "]" + msg); logger.log(Level.DEBUG, "[" + MOD_NAME + "] " + msg);
} }
@Override @Override

View File

@ -11,7 +11,13 @@ public class TransformingMap implements Map<String, String> {
public TransformingMap(Map<String, String> m, StringTransformer t) { public TransformingMap(Map<String, String> m, StringTransformer t) {
backer = m; backer = m;
transformer = t; transformer = t;
for (String value : m.values()) { Collection<String> strings = m.values();
int i = 0;
for (String value : strings) {
if (ModInit.cfg.verboseLogging) {
i++;
ModInit.Log("Transforming " + i + "/" + strings.size());
}
transformer.transform(value); transformer.transform(value);
} }
} }

View File

@ -70,7 +70,10 @@ public class YnTransformer extends CachingTransformer {
try { try {
if (!valid(str)) if (!valid(str))
return str; return str;
Language startLang = api.detectionApi().detect(str).orElse(Language.EN);
Language startLang = StringUtils.isAlpha(ModInit.cfg.targetLanguage) && ModInit.cfg.targetLanguage.length() == 2
? Language.of(ModInit.cfg.targetLanguage)
: api.detectionApi().detect(str).orElse(Language.EN);
if (!valid(startLang.code())) { if (!valid(startLang.code())) {
ModInit.Warn("Could not detect language for: " + str); ModInit.Warn("Could not detect language for: " + str);
ModInit.Warn("Defaulting to EN"); ModInit.Warn("Defaulting to EN");
@ -84,7 +87,8 @@ public class YnTransformer extends CachingTransformer {
currentLang = newLang; currentLang = newLang;
} }
currentState = api.translationApi().translate(currentState, startLang).text(); currentState = api.translationApi().translate(currentState, startLang).text();
ModInit.Log("Transformed: \"" + str + "\" to: \"" + currentState + "\""); if (ModInit.cfg.verboseLogging)
ModInit.Log("Transformed: \"" + str + "\" to: \"" + currentState + "\"");
return currentState; return currentState;
} catch (Exception e) { } catch (Exception e) {
ModInit.Warn("Failed to break: " + str + " (" + str.length() + " characters)"); ModInit.Warn("Failed to break: " + str + " (" + str.length() + " characters)");

View File

@ -3,5 +3,7 @@
"text.autoconfig.TranslaterCF.option.rounds": "Rounds", "text.autoconfig.TranslaterCF.option.rounds": "Rounds",
"text.autoconfig.TranslaterCF.option.breakFully": "Break Fully", "text.autoconfig.TranslaterCF.option.breakFully": "Break Fully",
"text.autoconfig.TranslaterCF.option.key": "API Key", "text.autoconfig.TranslaterCF.option.key": "API Key",
"text.autoconfig.TranslaterCF.option.verboseLogging": "Verbose Logging",
"text.autoconfig.TranslaterCF.option.targetLanguage": "Target Language",
"text.autoconfig.TranslaterCF.option.forceRegenerate": "Force Regenerate" "text.autoconfig.TranslaterCF.option.forceRegenerate": "Force Regenerate"
} }

View File

@ -33,5 +33,12 @@
"fabricloader": ">=0.7.4", "fabricloader": ">=0.7.4",
"fabric": "*", "fabric": "*",
"minecraft": "1.16.x" "minecraft": "1.16.x"
},
"custom": {
"modupdater": {
"strategy": "curseforge",
"projectID": 394823
}
} }
} }

File diff suppressed because it is too large Load Diff