Skip to content

Instantly share code, notes, and snippets.

@rz7d
Created December 23, 2019 19:11
Show Gist options
  • Save rz7d/9e9b3a56d5629e7827680a9fc351dc7b to your computer and use it in GitHub Desktop.
Save rz7d/9e9b3a56d5629e7827680a9fc351dc7b to your computer and use it in GitHub Desktop.
import * as fs from "fs";
type Descriptor = "class" | "method" | "field";
type ObfMap = {
d: Descriptor;
name: string;
obfName: string;
};
type ClassMap = ObfMap;
type MethodMap = {
returnType: string;
params: string;
} & ObfMap;
type FieldMap = {
type: string;
} & ObfMap;
const classPattern = /^.+\s*->\s*.+:$/;
const fieldPattern = /^\s+.+\s*(?!=\(\).+)\s*->\s*.+$/;
const methodPattern = /^\s+.+\s*.+\(.*\)\s*->\s*.+$/;
function classOf(chunk: string): ClassMap {
const arrow = chunk.trim().split(/\s*->\s*/);
return {
d: "class",
name: arrow[0],
obfName: arrow[1].substring(0, arrow[1].length - 1)
};
}
function methodOf(chunk: string): MethodMap {
const arrow = chunk.trim().split(/\s*->\s*/);
const left = arrow[0].split(/\s+/);
return {
d: "method",
returnType: left[0],
name: left[1].split("(")[0],
params: "(" + left[1].split("(")[1],
obfName: arrow[1]
};
}
function fieldOf(chunk: string): FieldMap {
const arrow = chunk.trim().split(/\s*->\s*/);
const left = arrow[0].split(/\s+/);
return {
d: "field",
type: left[0],
name: left[1],
obfName: arrow[1]
};
}
function parse(line: string): ClassMap | MethodMap | FieldMap {
if (line.startsWith("#")) return;
if (classPattern.test(line)) return classOf(line);
if (methodPattern.test(line)) return methodOf(line);
if (fieldPattern.test(line)) return fieldOf(line);
}
function format(data: ClassMap | MethodMap | FieldMap): string {
switch (data.d) {
case "class": {
let d = data as ClassMap;
return `${d.name} -> ${d.obfName}:`;
}
case "method": {
let d = data as MethodMap;
return ` ${d.returnType} ${d.name}${d.params} -> ${d.obfName}`;
}
case "field": {
let d = data as FieldMap;
return ` ${d.type} ${d.name} -> ${d.obfName}`;
}
}
}
function swap<T extends ObfMap>(map: T): T {
return {
...map,
obfName: map.name,
name: map.obfName
};
}
function main(all: string) {
const lines = all.split(/\r?\n/);
lines
.map(parse)
.filter(x => x)
.map(swap)
.map(format)
.forEach(x => console.error(x));
}
fs.readFile("./client.txt", (e, d) => {
main(d.toString("utf-8"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment