Skip to content

Instantly share code, notes, and snippets.

@LexManos
Last active December 17, 2015 06:28
Show Gist options
  • Save LexManos/5565223 to your computer and use it in GitHub Desktop.
Save LexManos/5565223 to your computer and use it in GitHub Desktop.
ObfusicationHelper
@Override
public void injectData(Map<String, Object> data)
{
runtimeDeobfuscationEnabled = (Boolean)data.get("runtimeDeobfuscationEnabled");
FMLRelaunchLog.makeLog("RenderCore");
Logger log = Logger.getLogger("RenderCore");
for (String transformer : getASMTransformerClass())
{
try
{
ObfusicationHelper.remapNames(log, Class.forName(transformer), runtimeDeobfuscationEnabled);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
==============================================================================================
public static String cRenderBlocks = "net.minecraft.client.renderer.RenderBlocks";
public static String mRenderByType = "func_78612_b"; //RenderBlocks.renderBlockByRenderType
==============================================================================================
2013-05-12 15:40:19 [INFO] [RenderCore] Translating Obfusication fields in net.minecraftforge.lex.rendering.RenderBlocksTransformer
2013-05-12 15:40:19 [INFO] [RenderCore] cRenderBlocks: 'net.minecraft.client.renderer.RenderBlocks' -> 'bgf'
2013-05-12 15:40:19 [INFO] [RenderCore] mRenderByType: 'func_78612_b' -> 'b'
package net.minecraftforge.lex.rendering;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.collect.BiMap;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper;
import cpw.mods.fml.relauncher.FMLRelaunchLog;
public class ObfusicationHelper
{
private static HashMap<String, String> mcp = null;
private static HashMap<String, String> obf = null;
public static void remapNames(Logger log, Class cls, boolean runtimeDeobfuscationEnabled)
{
HashMap<String, String> translations = new HashMap<String, String>();
if (runtimeDeobfuscationEnabled)
{
translations = getObfNames(log);
}
else
{
translations = getMCPNames(log);
}
log.info("Translating Obfusication fields in " + cls.getName());
for (Field f : cls.getFields())
{
if (f.getType() == String.class && Modifier.isStatic(f.getModifiers()))
{
try
{
String srg = (String)f.get(null);
String mapped = translations.get(srg);
if (mapped == null)
{
if (runtimeDeobfuscationEnabled)
{
mapped = srg;
}
else
{
//Throw a fatal exception.... should print mappings and push a alert box..
}
}
log.info(String.format(" %s: '%s' -> '%s'", f.getName(), srg, mapped));
f.set(null, mapped);
}
catch (Exception e)
{
//Shouldn't ever happen but eah..
e.printStackTrace();
}
}
}
}
private static HashMap<String, String> getObfNames(Logger log)
{
if (obf != null) return obf;
HashMap<String, String> translations = new HashMap<String, String>();
try
{
Field rawFieldMapsField = FMLDeobfuscatingRemapper.class.getDeclaredField("rawFieldMaps");
Field rawMethodMapsField = FMLDeobfuscatingRemapper.class.getDeclaredField("rawMethodMaps");
Field classNameBiMapField = FMLDeobfuscatingRemapper.class.getDeclaredField("classNameBiMap");
rawFieldMapsField.setAccessible(true);
rawMethodMapsField.setAccessible(true);
classNameBiMapField.setAccessible(true);
Map<String,Map<String,String>> rawFieldMaps = (Map<String, Map<String, String>>)rawFieldMapsField.get(FMLDeobfuscatingRemapper.INSTANCE);
Map<String,Map<String,String>> rawMethodMaps = (Map<String, Map<String, String>>)rawMethodMapsField.get(FMLDeobfuscatingRemapper.INSTANCE);
BiMap<String, String> classNameBiMap = (BiMap<String, String>)classNameBiMapField.get(FMLDeobfuscatingRemapper.INSTANCE);
for (Entry<String, Map<String,String>> classes : rawFieldMaps.entrySet())
{
for (Entry<String, String> entry : classes.getValue().entrySet())
{
String srg = entry.getValue();
if (srg.startsWith("field_"))
{
translations.put(srg, entry.getKey().split(":")[0]);
}
}
}
int count = translations.size();
log.info(String.format("Loaded %d mappings for fields", count));
for (Entry<String, Map<String,String>> classes : rawMethodMaps.entrySet())
{
for (Entry<String, String> entry : classes.getValue().entrySet())
{
String srg = entry.getValue();
if (srg.startsWith("func_"))
{
translations.put(srg, entry.getKey().split("\\(")[0]);
}
}
}
log.info(String.format("Loaded %d mappings for methods", translations.size() - count));
count = translations.size();
for(Entry<String, String> entry : classNameBiMap.entrySet())
{
translations.put(entry.getValue().replace('/', '.'), entry.getKey());
}
log.info(String.format("Loaded %d mappings for classes", translations.size() - count));
}
catch (Exception e)
{
e.printStackTrace();
}
return translations;
}
private static HashMap<String, String> getMCPNames(Logger log)
{
if (mcp != null) return mcp;
String confLoc = System.getProperty("lex.mcpConfLocation", "../conf");
File[] maps = new File[]{
new File(confLoc, "methods.csv"),
new File(confLoc, "fields.csv")
};
HashMap<String, String> translations = new HashMap<String, String>();
for (File map : maps)
{
if (map.exists() && map.isFile())
{
try
{
BufferedReader br = new BufferedReader(new FileReader(map));
String line;
int count = 0;
while ((line = br.readLine()) != null)
{
String[] pts = line.split(",");
if (pts.length > 2 && (pts[0].startsWith("field_") || pts[0].startsWith("func_")))
{
translations.put(pts[0], pts[1]);
count++;
}
}
br.close();
log.info(String.format("Loaded %d mappings from %s", count, map));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return translations;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment