Commit 6763ac3e5b0787f41a1319613b872867fcd9eae1

Authored by Mumfrey
1 parent 611d0208

Set client brand with mixin instead of using reflection

src/client/java/com/mumfrey/liteloader/client/mixin/MixinClientBrandRetriever.java 0 → 100644
  1 +/*
  2 + * This file is part of LiteLoader.
  3 + * Copyright (C) 2012-16 Adam Mummery-Smith
  4 + * All Rights Reserved.
  5 + */
  6 +package com.mumfrey.liteloader.client.mixin;
  7 +
  8 +import org.spongepowered.asm.mixin.Mixin;
  9 +import org.spongepowered.asm.mixin.injection.At;
  10 +import org.spongepowered.asm.mixin.injection.Inject;
  11 +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
  12 +
  13 +import net.minecraft.client.ClientBrandRetriever;
  14 +
  15 +@Mixin(value = ClientBrandRetriever.class, remap = false)
  16 +public abstract class MixinClientBrandRetriever
  17 +{
  18 + private static final String BRANDING_VANILLA = "vanilla";
  19 + private static final String BRANDING_LITELOADER = "liteloader";
  20 +
  21 + @Inject(method = "getClientModName", at = @At("RETURN"), cancellable = true)
  22 + private static void appendLiteLoaderBranding(CallbackInfoReturnable<String> cir)
  23 + {
  24 + String branding = cir.getReturnValue();
  25 + if (MixinClientBrandRetriever.BRANDING_VANILLA.equals(branding))
  26 + {
  27 + // If the branding is vanilla, just overwrite it
  28 + cir.setReturnValue(MixinClientBrandRetriever.BRANDING_LITELOADER);
  29 + }
  30 + else
  31 + {
  32 + // Otherwise append it
  33 + cir.setReturnValue(branding + "," + MixinClientBrandRetriever.BRANDING_LITELOADER);
  34 + }
  35 + }
  36 +}
src/client/resources/mixins.liteloader.client.json
1 { 1 {
2 "required": true, 2 "required": true,
3 - "minVersion": "0.7", 3 + "minVersion": "0.7.4",
4 "compatibilityLevel": "JAVA_8", 4 "compatibilityLevel": "JAVA_8",
5 "target": "@env(DEFAULT)", 5 "target": "@env(DEFAULT)",
6 "package": "com.mumfrey.liteloader.client.mixin", 6 "package": "com.mumfrey.liteloader.client.mixin",
@@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
24 "MixinGuiTextField", 24 "MixinGuiTextField",
25 "MixinIntIdentityHashBiMap", 25 "MixinIntIdentityHashBiMap",
26 "MixinGuiOverlayDebug", 26 "MixinGuiOverlayDebug",
  27 + "MixinClientBrandRetriever",
27 "IGuiButton", 28 "IGuiButton",
28 "IKeyBinding" 29 "IKeyBinding"
29 ], 30 ],
src/main/java/com/mumfrey/liteloader/core/LiteLoader.java
@@ -896,9 +896,6 @@ public final class LiteLoader @@ -896,9 +896,6 @@ public final class LiteLoader
896 */ 896 */
897 void onStartupComplete() 897 void onStartupComplete()
898 { 898 {
899 - // Set the loader branding in ClientBrandRetriever using reflection  
900 - LiteLoaderBootstrap.setBranding("LiteLoader");  
901 -  
902 this.coreProviders.all().onStartupComplete(); 899 this.coreProviders.all().onStartupComplete();
903 900
904 if (this.panelManager != null) 901 if (this.panelManager != null)
src/main/java/com/mumfrey/liteloader/core/LiteLoaderBootstrap.java
@@ -12,8 +12,6 @@ import java.io.FileWriter; @@ -12,8 +12,6 @@ import java.io.FileWriter;
12 import java.io.IOException; 12 import java.io.IOException;
13 import java.io.InputStream; 13 import java.io.InputStream;
14 import java.io.Serializable; 14 import java.io.Serializable;
15 -import java.lang.reflect.Field;  
16 -import java.lang.reflect.Method;  
17 import java.util.List; 15 import java.util.List;
18 import java.util.Properties; 16 import java.util.Properties;
19 17
@@ -43,7 +41,6 @@ import com.mumfrey.liteloader.util.log.LiteLoaderLogger; @@ -43,7 +41,6 @@ import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
43 import com.mumfrey.liteloader.util.log.LiteLoaderLogger.Verbosity; 41 import com.mumfrey.liteloader.util.log.LiteLoaderLogger.Verbosity;
44 42
45 import net.minecraft.launchwrapper.ITweaker; 43 import net.minecraft.launchwrapper.ITweaker;
46 -import net.minecraft.launchwrapper.Launch;  
47 import net.minecraft.launchwrapper.LaunchClassLoader; 44 import net.minecraft.launchwrapper.LaunchClassLoader;
48 45
49 /** 46 /**
@@ -772,52 +769,6 @@ class LiteLoaderBootstrap implements LoaderBootstrap, LoaderEnvironment, LoaderP @@ -772,52 +769,6 @@ class LiteLoaderBootstrap implements LoaderBootstrap, LoaderEnvironment, LoaderP
772 return this.branding; 769 return this.branding;
773 } 770 }
774 771
775 - /**  
776 - * Set the brand in ClientBrandRetriever to the specified brand  
777 - *  
778 - * @param brand  
779 - */  
780 - static void setBranding(String brand)  
781 - {  
782 - try  
783 - {  
784 - Method mGetClientModName;  
785 -  
786 - try  
787 - {  
788 - Class<?> cbrClass = Class.forName("net.minecraft.client.ClientBrandRetriever", false, Launch.classLoader);  
789 - mGetClientModName = cbrClass.getDeclaredMethod("getClientModName");  
790 - }  
791 - catch (ClassNotFoundException ex)  
792 - {  
793 - return;  
794 - }  
795 -  
796 - String oldBrand = (String)mGetClientModName.invoke(null);  
797 -  
798 - if ("vanilla".equals(oldBrand))  
799 - {  
800 - char[] newValue = brand.toCharArray();  
801 -  
802 - Field stringValue = String.class.getDeclaredField("value");  
803 - stringValue.setAccessible(true);  
804 - stringValue.set(oldBrand, newValue);  
805 -  
806 - try  
807 - {  
808 - Field stringCount = String.class.getDeclaredField("count");  
809 - stringCount.setAccessible(true);  
810 - stringCount.set(oldBrand, newValue.length);  
811 - }  
812 - catch (NoSuchFieldException ex) {} // java 1.7 doesn't have this member  
813 - }  
814 - }  
815 - catch (Throwable th)  
816 - {  
817 - LiteLoaderLogger.warning(th, "Setting branding failed");  
818 - }  
819 - }  
820 -  
821 /* (non-Javadoc) 772 /* (non-Javadoc)
822 * @see com.mumfrey.liteloader.launch.LoaderBootstrap 773 * @see com.mumfrey.liteloader.launch.LoaderBootstrap
823 * #getRequiredTransformers() 774 * #getRequiredTransformers()
src/main/resources/mixins.liteloader.core.json
1 { 1 {
2 "required": true, 2 "required": true,
3 - "minVersion": "0.7", 3 + "minVersion": "0.7.4",
4 "compatibilityLevel": "JAVA_8", 4 "compatibilityLevel": "JAVA_8",
5 "target": "@env(DEFAULT)", 5 "target": "@env(DEFAULT)",
6 "package": "com.mumfrey.liteloader.common.mixin", 6 "package": "com.mumfrey.liteloader.common.mixin",