Skip to content

Instantly share code, notes, and snippets.

@malte0811
Created March 11, 2018 18:28
Show Gist options
  • Save malte0811/1b1494a1e3bb86e9b8aa101c969a954e to your computer and use it in GitHub Desktop.
Save malte0811/1b1494a1e3bb86e9b8aa101c969a954e to your computer and use it in GitHub Desktop.
A way to replace srg names with mcp names in arbitrary files, as long as they are separated by spaces
package malte0811.crudedeobf;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CrudeDeobf
{
public static void main(String[] args) throws Exception
{
File in = new File("srg-mcp.srg");
File replayIn = new File("run/replay_half.log");
File replayOut = new File("run/replay.log");
Map<String, String> names = new HashMap<>();
readNames(in, names);
process(new FileInputStream(replayIn), new FileOutputStream(replayOut), names);
}
public static void readNames(File in, Map<String, String> names) throws FileNotFoundException
{
Scanner s = new Scanner(in);
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("FD")) {
processLine(line, names, 0);
} else if (line.startsWith("MD")) {
processLine(line, names, 1);
}
}
}
private static void processLine(String line, Map<String, String> output, int skip) {
StringTokenizer tok = new StringTokenizer(line);
tok.nextToken();//MD/FD
String nameFull = tok.nextToken();
String srg = nameFull.substring(nameFull.lastIndexOf('/')+1);
for (int i = 0; i < skip; i++)
{
tok.nextToken();//Skip signature if present
}
String deobfFull = tok.nextToken();
String mcp = deobfFull.substring(nameFull.lastIndexOf('/')+1);
output.put(srg, mcp);
}
public static void process(InputStream input, OutputStream output, Map<String, String> replacements) throws IOException
{
boolean done = false;
while (!done) {
int c;
while ((c = input.read())>=0&&Character.isWhitespace(c)) {
output.write(c);
}
if (c<=0) {
return;
}
StringBuilder token = new StringBuilder("" + (char) c);
while ((c = input.read())>=0&&!Character.isWhitespace(c)) {
token.append((char) c);
}
String read = token.toString();
read = replacements.getOrDefault(read, read);
output.write(read.getBytes());
if (c>=0) {
output.write(c);
} else {
done = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment