Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.resource.hooking;
- import com.google.gson.GsonBuilder;
- import com.google.gson.reflect.TypeToken;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @author Daniel
- *
- * appends the {@link ReflectionMapper#file} into a parsable .json format
- * mostly in a pretty terrible way currently, but it works for now until
- * we figure out the exact format we want to use with the reflection hooks
- */
- public class ReflectionMapper {
- /**
- * The {@link File} to parse our {@link ReflectionMapper#classes} & {@link ReflectionMapper#hooks} from
- */
- private final File file = new File("./proguard/Mapping.txt");
- /**
- * The {@link java.util.Map} of the Obfuscated class names
- */
- private final Map<String, String> classes = new HashMap<String, String>();
- /**
- * The {@link java.util.Map} of the Obfuscated class names along side their respective methods & fields
- */
- private final Map<String, List<Obfuscated>> hooks = new HashMap<String, List<Obfuscated>>();
- /**
- * Constructor
- */
- public ReflectionMapper() {
- mapClasses();
- mapChildren();
- write();
- }
- /**
- * Creates a {@link java.util.Map} of the classes {@link ReflectionMapper#classes} in order to properly {@link java.util.Map} the hooks to the {@link ReflectionMapper#hooks}
- */
- private void mapClasses() {
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line;
- while ((line = reader.readLine()) != null) {
- String[] split = line.split(" -> ");
- if (!line.startsWith(" ")) {
- classes.put(split[0], split[1].substring(0, split[1].length() - 1));
- }
- }
- reader.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * Creates a {@link java.util.Map} in order to store the {@link ReflectionMapper#hooks} into a parsable .json file
- */
- private void mapChildren() {
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line;
- String cor = "";
- String cob = "";
- while ((line = reader.readLine()) != null) {
- String[] split = line.split(" -> ");
- if (line.startsWith(" ")) {
- String parameters = "";
- if (split[0].endsWith(")")) {
- parameters = split[0].substring(split[0].lastIndexOf("("), split[0].length());
- }
- String value = cor + ":" + cob;
- if (!hooks.containsKey(value)) {
- hooks.put(value, new ArrayList<Obfuscated>());
- }
- if (!parameters.trim().isEmpty()) {
- String trimmed = parameters.substring(1, parameters.length() - 1);
- if (trimmed.contains(",")) {
- StringBuilder sb = new StringBuilder();
- sb.append("(");
- String[] parts = trimmed.split(",");
- for (int index = 0; index < parts.length; index++) {
- String clazz = classes.get(parts[index]);
- sb.append(clazz != null ? clazz : parts[index]);
- if (index != parts.length - 1) {
- sb.append(",");
- }
- }
- sb.append(")");
- parameters = sb.toString();
- } else {
- String clazz = classes.get(trimmed);
- if (clazz != null) {
- parameters = "(" + clazz + ")";
- }
- }
- }
- hooks.get(value).add(new Obfuscated(split[0].trim().split(" ")[1], split[1] + parameters));
- //System.out.println("\t[Child] ->\n\t\t[Original]:\t" + split[0] + "\n\t\t[Obfuscated]:\t" + split[1] + parameters + "\n");
- } else {
- cor = split[0];
- cob = split[1].substring(0, split[1].length() - 1);
- String value = cor + ":" + cob;
- if (!hooks.containsKey(value)) {
- hooks.put(value, new ArrayList<Obfuscated>());
- }
- //System.out.println("[Original]:\t\t" + split[0] + "\n[Obfuscated]:\t" + split[1].substring(0, split[1].length() - 1));
- }
- }
- reader.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * Stores the {@link ReflectionMapper#hooks} into a parsable .json format
- */
- private void write() {
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./proguard/Mapping.json")));
- writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(hooks, new TypeToken<Map<String, List<Obfuscated>>>() {}.getType()));
- writer.newLine();
- writer.flush();
- writer.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * Inner class to get what we need done for now
- */
- private class Obfuscated {
- /**
- * original type name
- */
- private final String original;
- /**
- * obfuscated type name
- */
- private final String modified;
- /**
- * {@link ReflectionMapper.Obfuscated} Constructor
- *
- * @param original - {@link ReflectionMapper.Obfuscated#original}
- * @param modified - {@link ReflectionMapper.Obfuscated#modified}
- */
- public Obfuscated(String original, String modified) {
- this.original = original;
- this.modified = modified;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement