From 7cd7ee0298a7fc28d2da2137b8ee9e7554c1d20b Mon Sep 17 00:00:00 2001 From: JFronny <33260128+JFronny@users.noreply.github.com> Date: Mon, 13 Jul 2020 16:41:21 +0200 Subject: [PATCH] Very hacky progress view --- .../io/gitlab/jfronny/translater/Cfg.java | 10 ++- .../gitlab/jfronny/translater/IMClient.java | 5 ++ .../io/gitlab/jfronny/translater/ModInit.java | 1 + .../jfronny/translater/TransformingMap.java | 23 +++-- .../jfronny/translater/YnTransformer.java | 2 +- .../translater/mixin/MixinLanguage.java | 4 +- .../mixin/MixinMinecraftClient.java | 17 ++++ .../translater/mixin/MixinSplashScreen.java | 85 +++++++++++++++++++ .../assets/translater/lang/en_us.json | 2 +- src/main/resources/translater.mixins.json | 4 +- 10 files changed, 142 insertions(+), 11 deletions(-) create mode 100644 src/main/java/io/gitlab/jfronny/translater/IMClient.java create mode 100644 src/main/java/io/gitlab/jfronny/translater/mixin/MixinMinecraftClient.java create mode 100644 src/main/java/io/gitlab/jfronny/translater/mixin/MixinSplashScreen.java diff --git a/src/main/java/io/gitlab/jfronny/translater/Cfg.java b/src/main/java/io/gitlab/jfronny/translater/Cfg.java index 7893d0c..7e35a04 100644 --- a/src/main/java/io/gitlab/jfronny/translater/Cfg.java +++ b/src/main/java/io/gitlab/jfronny/translater/Cfg.java @@ -12,10 +12,16 @@ public class Cfg implements ConfigData { 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)") 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("Significantly slows down the loading time but gives a visual of the progress. Values: Full, Console, None") + public progressMode renderProgress = progressMode.None; @Comment("Use this if something is broken. This initiate the regeneration of the cache") public boolean forceRegenerate = false; + + public enum progressMode { + Full, + Console, + None + } } diff --git a/src/main/java/io/gitlab/jfronny/translater/IMClient.java b/src/main/java/io/gitlab/jfronny/translater/IMClient.java new file mode 100644 index 0000000..f6d64f2 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/translater/IMClient.java @@ -0,0 +1,5 @@ +package io.gitlab.jfronny.translater; + +public interface IMClient { + void runRender(boolean tick); +} diff --git a/src/main/java/io/gitlab/jfronny/translater/ModInit.java b/src/main/java/io/gitlab/jfronny/translater/ModInit.java index 084990f..a176660 100644 --- a/src/main/java/io/gitlab/jfronny/translater/ModInit.java +++ b/src/main/java/io/gitlab/jfronny/translater/ModInit.java @@ -35,4 +35,5 @@ public class ModInit implements ModInitializer { Log("Loaded translater"); } + public static TransformingMap map = null; } diff --git a/src/main/java/io/gitlab/jfronny/translater/TransformingMap.java b/src/main/java/io/gitlab/jfronny/translater/TransformingMap.java index a517ee4..aee2661 100644 --- a/src/main/java/io/gitlab/jfronny/translater/TransformingMap.java +++ b/src/main/java/io/gitlab/jfronny/translater/TransformingMap.java @@ -1,5 +1,7 @@ package io.gitlab.jfronny.translater; +import net.minecraft.client.MinecraftClient; + import java.util.Collection; import java.util.Map; import java.util.Set; @@ -7,19 +9,30 @@ import java.util.Set; public class TransformingMap implements Map { private final Map backer; private final StringTransformer transformer; + public boolean initializing; + public int initI; + public int initMax; public TransformingMap(Map m, StringTransformer t) { backer = m; transformer = t; - Collection strings = m.values(); - int i = 0; + } + + public void init() { + Collection strings = backer.values(); + initI = 0; + initMax = strings.size(); + initializing = true; for (String value : strings) { - if (ModInit.cfg.verboseLogging) { - i++; - ModInit.Log("Transforming " + i + "/" + strings.size()); + if (ModInit.cfg.renderProgress == Cfg.progressMode.Console || ModInit.cfg.renderProgress == Cfg.progressMode.Full) { + initI++; + ModInit.Log("Transforming " + initI + "/" + initMax); } transformer.transform(value); + if (ModInit.cfg.renderProgress == Cfg.progressMode.Full && initI % 10 == 0) + ((IMClient)MinecraftClient.getInstance()).runRender(false); } + initializing = false; } @Override diff --git a/src/main/java/io/gitlab/jfronny/translater/YnTransformer.java b/src/main/java/io/gitlab/jfronny/translater/YnTransformer.java index eb57ead..29ed2f6 100644 --- a/src/main/java/io/gitlab/jfronny/translater/YnTransformer.java +++ b/src/main/java/io/gitlab/jfronny/translater/YnTransformer.java @@ -87,7 +87,7 @@ public class YnTransformer extends CachingTransformer { currentLang = newLang; } currentState = api.translationApi().translate(currentState, startLang).text(); - if (ModInit.cfg.verboseLogging) + if (ModInit.cfg.renderProgress == Cfg.progressMode.Console || ModInit.cfg.renderProgress == Cfg.progressMode.Full) ModInit.Log("Transformed: \"" + str + "\" to: \"" + currentState + "\""); return currentState; } catch (Exception e) { diff --git a/src/main/java/io/gitlab/jfronny/translater/mixin/MixinLanguage.java b/src/main/java/io/gitlab/jfronny/translater/mixin/MixinLanguage.java index b3ba297..0ad6e2d 100644 --- a/src/main/java/io/gitlab/jfronny/translater/mixin/MixinLanguage.java +++ b/src/main/java/io/gitlab/jfronny/translater/mixin/MixinLanguage.java @@ -37,7 +37,9 @@ public class MixinLanguage { } if (language instanceof TranslationStorage) { try { - FIELD.set(language, new TransformingMap((Map) FIELD.get(language), transformer)); + ModInit.map = new TransformingMap((Map) FIELD.get(language), transformer); + ModInit.map.init(); + FIELD.set(language, ModInit.map); } catch (IllegalAccessException | ClassCastException e) { e.printStackTrace(); } diff --git a/src/main/java/io/gitlab/jfronny/translater/mixin/MixinMinecraftClient.java b/src/main/java/io/gitlab/jfronny/translater/mixin/MixinMinecraftClient.java new file mode 100644 index 0000000..aafb454 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/translater/mixin/MixinMinecraftClient.java @@ -0,0 +1,17 @@ +package io.gitlab.jfronny.translater.mixin; + +import io.gitlab.jfronny.translater.IMClient; +import net.minecraft.client.MinecraftClient; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(MinecraftClient.class) +public class MixinMinecraftClient implements IMClient { + @Override + public void runRender(boolean tick) { + render(tick); + } + + @Shadow + private void render(boolean tick) {} +} diff --git a/src/main/java/io/gitlab/jfronny/translater/mixin/MixinSplashScreen.java b/src/main/java/io/gitlab/jfronny/translater/mixin/MixinSplashScreen.java new file mode 100644 index 0000000..7305662 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/translater/mixin/MixinSplashScreen.java @@ -0,0 +1,85 @@ +package io.gitlab.jfronny.translater.mixin; + +import com.google.gson.JsonParser; +import io.gitlab.jfronny.translater.Cfg; +import io.gitlab.jfronny.translater.ModInit; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.FontStorage; +import net.minecraft.client.font.FontType; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.screen.Overlay; +import net.minecraft.client.gui.screen.SplashScreen; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.util.Identifier; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.awt.*; +import java.util.Collections; + +@Mixin(SplashScreen.class) +public abstract class MixinSplashScreen extends Overlay { + @Shadow @Final + private MinecraftClient client; + + @Shadow public abstract void render(MatrixStack matrices, int mouseX, int mouseY, float delta); + + @Inject(at = @At("RETURN"), method = "renderProgressBar") + private void RenderTrnslProgress(MatrixStack matrixStack, int i, int j, int k, int l, float f, CallbackInfo ci) { + if (ModInit.cfg.renderProgress == Cfg.progressMode.Full && ModInit.map != null && ModInit.map.initializing) { + int n = Math.round(f * 255.0F); + renderer.draw(matrixStack, "Transforming " + ModInit.map.initI + "/" + ModInit.map.initMax, 10, 10, colorString("0,0,0") & 16777215 | n); + } + } + + private TextRenderer renderer; + + private static final String FONT_JSON = //Taken from loadingspice (https://github.com/therealfarfetchd/loadingspice) + "{\n" + + " \"type\": \"bitmap\",\n" + + " \"file\": \"minecraft:font/ascii.png\",\n" + + " \"ascent\": 7,\n" + + " \"chars\": [\n" + + " \"\\u00c0\\u00c1\\u00c2\\u00c8\\u00ca\\u00cb\\u00cd\\u00d3\\u00d4\\u00d5\\u00da\\u00df\\u00e3\\u00f5\\u011f\\u0130\",\n" + + " \"\\u0131\\u0152\\u0153\\u015e\\u015f\\u0174\\u0175\\u017e\\u0207\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\",\n" + + " \"\\u0020\\u0021\\\"\\u0023\\u0024\\u0025\\u0026\\u0027\\u0028\\u0029\\u002a\\u002b\\u002c\\u002d\\u002e\\u002f\",\n" + + " \"\\u0030\\u0031\\u0032\\u0033\\u0034\\u0035\\u0036\\u0037\\u0038\\u0039\\u003a\\u003b\\u003c\\u003d\\u003e\\u003f\",\n" + + " \"\\u0040\\u0041\\u0042\\u0043\\u0044\\u0045\\u0046\\u0047\\u0048\\u0049\\u004a\\u004b\\u004c\\u004d\\u004e\\u004f\",\n" + + " \"\\u0050\\u0051\\u0052\\u0053\\u0054\\u0055\\u0056\\u0057\\u0058\\u0059\\u005a\\u005b\\\\\\u005d\\u005e\\u005f\",\n" + + " \"\\u0060\\u0061\\u0062\\u0063\\u0064\\u0065\\u0066\\u0067\\u0068\\u0069\\u006a\\u006b\\u006c\\u006d\\u006e\\u006f\",\n" + + " \"\\u0070\\u0071\\u0072\\u0073\\u0074\\u0075\\u0076\\u0077\\u0078\\u0079\\u007a\\u007b\\u007c\\u007d\\u007e\\u0000\",\n" + + " \"\\u00c7\\u00fc\\u00e9\\u00e2\\u00e4\\u00e0\\u00e5\\u00e7\\u00ea\\u00eb\\u00e8\\u00ef\\u00ee\\u00ec\\u00c4\\u00c5\",\n" + + " \"\\u00c9\\u00e6\\u00c6\\u00f4\\u00f6\\u00f2\\u00fb\\u00f9\\u00ff\\u00d6\\u00dc\\u00f8\\u00a3\\u00d8\\u00d7\\u0192\",\n" + + " \"\\u00e1\\u00ed\\u00f3\\u00fa\\u00f1\\u00d1\\u00aa\\u00ba\\u00bf\\u00ae\\u00ac\\u00bd\\u00bc\\u00a1\\u00ab\\u00bb\",\n" + + " \"\\u2591\\u2592\\u2593\\u2502\\u2524\\u2561\\u2562\\u2556\\u2555\\u2563\\u2551\\u2557\\u255d\\u255c\\u255b\\u2510\",\n" + + " \"\\u2514\\u2534\\u252c\\u251c\\u2500\\u253c\\u255e\\u255f\\u255a\\u2554\\u2569\\u2566\\u2560\\u2550\\u256c\\u2567\",\n" + + " \"\\u2568\\u2564\\u2565\\u2559\\u2558\\u2552\\u2553\\u256b\\u256a\\u2518\\u250c\\u2588\\u2584\\u258c\\u2590\\u2580\",\n" + + " \"\\u03b1\\u03b2\\u0393\\u03c0\\u03a3\\u03c3\\u03bc\\u03c4\\u03a6\\u0398\\u03a9\\u03b4\\u221e\\u2205\\u2208\\u2229\",\n" + + " \"\\u2261\\u00b1\\u2265\\u2264\\u2320\\u2321\\u00f7\\u2248\\u00b0\\u2219\\u00b7\\u221a\\u207f\\u00b2\\u25a0\\u0000\"\n" + + " ]\n" + + "}"; + + @Inject(method = "", at = @At("RETURN")) + public void setup(CallbackInfo ci) { + final FontStorage fontStorage_1 = new FontStorage(client.getTextureManager(), new Identifier("loading")); + fontStorage_1.setFonts(Collections.singletonList(FontType.BITMAP.createLoader(new JsonParser().parse(FONT_JSON).getAsJsonObject()).load(client.getResourceManager()))); + renderer = new TextRenderer(id -> fontStorage_1); + } + + private static int colorString(String color) { + String[] rgb = color.split(","); + if (rgb.length != 3) { + throw new IllegalArgumentException(color + " is not a valid color string. Too many values - format is '255,255,255'"); + } else { + int[] rgb2 = new int[3]; + for (int i = 0; i < rgb.length; i++) { + rgb2[i] = Integer.valueOf(rgb[i]); + } + return new Color(rgb2[0], rgb2[1], rgb2[2]).getRGB(); + } + } +} diff --git a/src/main/resources/assets/translater/lang/en_us.json b/src/main/resources/assets/translater/lang/en_us.json index ebc13af..c74756c 100644 --- a/src/main/resources/assets/translater/lang/en_us.json +++ b/src/main/resources/assets/translater/lang/en_us.json @@ -3,7 +3,7 @@ "text.autoconfig.TranslaterCF.option.rounds": "Rounds", "text.autoconfig.TranslaterCF.option.breakFully": "Break Fully", "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.renderProgress": "Progress Renderer ", "text.autoconfig.TranslaterCF.option.forceRegenerate": "Force Regenerate" } \ No newline at end of file diff --git a/src/main/resources/translater.mixins.json b/src/main/resources/translater.mixins.json index c102d18..a61ff1c 100644 --- a/src/main/resources/translater.mixins.json +++ b/src/main/resources/translater.mixins.json @@ -4,7 +4,9 @@ "package": "io.gitlab.jfronny.translater.mixin", "compatibilityLevel": "JAVA_8", "client": [ - "MixinLanguage" + "MixinLanguage", + "MixinSplashScreen", + "MixinMinecraftClient" ], "injectors": { "defaultRequire": 1