EaglerForge/NoReflect/javaRecon.js

142 lines
5.2 KiB
JavaScript
Raw Normal View History

2024-05-21 06:07:24 -05:00
const getStatements = {
"int": "params.getInt(%s)",
"String": "params.getString(%s)",
"boolean": "params.getBoolean(%s)",
"float": "params.getFloat(%s)",
"byte": "params.getByte(%s)",
"char": "params.getChar(%s)",
"double": "params.getDouble(%s)",
}
2024-05-21 19:04:52 -05:00
const callbackStatements = {
"boolean": "setCallbackBooleanWithDataArg",
"int": "setCallbackIntWithDataArg",
"String": "setCallbackStringWithDataArg",
"double": "setCallbackDoubleWithDataArg",
"float": "setCallbackFloatWithDataArg",
}
2024-05-21 06:07:24 -05:00
const getStatementsTypes = Object.keys(getStatements);
2024-05-21 19:04:52 -05:00
const callbackStatementsTypes = Object.keys(callbackStatements);
2024-05-21 06:07:24 -05:00
function reconJ(java, className) {
2024-05-21 19:04:52 -05:00
var usedClasses = [];
var javaText = java;
javaText = javaText.replace(/\/\/.*$/gm, '');
javaText = javaText.replace(/\/\*[\s\S]*?\*\//gm, '');
2024-05-21 06:07:24 -05:00
let constructorRegex = /(public|protected|private|static|\s) +(\w+) *\(([^)]*)\)/g;
2024-05-21 19:04:52 -05:00
let constructors = [...javaText.matchAll(constructorRegex).filter((line)=>{
return !line[0].includes(" private ") && !line[0].includes(" protected ");
2024-05-21 06:07:24 -05:00
})];
let constructorDetails = constructors.map((constructor) => {
let constructorName = constructor[2];
let argumentString = constructor[3];
let arguments = {};
if (argumentString.trim().length > 0) {
let argumentList = argumentString.split(",");
argumentList.forEach((argument) => {
let [type, name] = argument.trim().split(" ");
arguments[name] = type;
});
}
let argStr = "";
var argumentsKeys = Object.keys(arguments);
for (let i = 0; i < argumentsKeys.length; i++) {
const key = argumentsKeys[i];
2024-05-21 19:04:52 -05:00
if (!getStatementsTypes.includes(arguments[key])) {
usedClasses.push(arguments[key]);
}
2024-05-21 06:07:24 -05:00
argStr += `(${arguments[key]}) ${getStatementsTypes.includes(arguments[key]) ? getStatements[arguments[key]].replaceAll("%s", "\""+key+"\"") : `params.getReflective("${key}")`}`;
if (i !== argumentsKeys.length - 1) {
argStr += ", "
}
}
2024-05-21 19:04:52 -05:00
let impl = `setCallbackReflectiveWithArgs("${constructorName}", (BaseData params) -> {
return new ${className}(${argStr});
2024-05-21 06:07:24 -05:00
});
`;
return {
name: constructorName,
idx: constructors.indexOf(constructor),
arguments: arguments,
impl: impl
};
});
2024-05-21 19:04:52 -05:00
let methodRegex = /(public|static|\s)* +([\w\<\>\[\]]+)\s+(\w+) *\(([^)]*)\)/g;
let methods = [...javaText.matchAll(methodRegex).filter((line)=>{
return !line[0].includes("> ") && !line[0].startsWith(" else ") && !line[0].startsWith(" new ") && !line[0].includes(" private ") && !line[0].includes(" protected ");
//Doesn't support Type<Subtype> yet
})];
let methodDetails = methods.map((method) => {
let isStatic = `${method[1]}`.includes("static");
let returnType = method[2];
let methodName = method[3];
let argumentString = method[4];
let arguments = {};
if (argumentString.trim().length > 0) {
let argumentList = argumentString.split(",");
argumentList.forEach((argument) => {
let [type, name] = argument.trim().split(" ");
arguments[name] = type;
});
}
let argStr = "";
var argumentsKeys = Object.keys(arguments);
for (let i = 0; i < argumentsKeys.length; i++) {
const key = argumentsKeys[i];
if (!getStatementsTypes.includes(arguments[key])) {
usedClasses.push(arguments[key]);
}
argStr += `(${arguments[key]}) ${getStatementsTypes.includes(arguments[key]) ? getStatements[arguments[key]].replaceAll("%s", "\""+key+"\"") : `params.getReflective("${key}")`}`;
if (i !== argumentsKeys.length - 1) {
argStr += ", "
}
}
let prefix = isStatic ? className : `((${className}) params.get("_self"))`;
2024-05-21 19:04:52 -05:00
let impl;
if (returnType === "void") {
impl = `setCallbackVoidWithArgs("${methodName}", (BaseData params) -> {
${prefix}.${methodName}(${argStr});
2024-05-21 19:04:52 -05:00
});
`;
} else if (callbackStatementsTypes.includes(returnType)) {
impl = `${callbackStatements[returnType]}("${methodName}", (BaseData params) -> {
return (${returnType}) ${prefix}.${methodName}(${argStr});
2024-05-21 19:04:52 -05:00
});
`;
} else {
usedClasses.push(returnType);
impl = `setCallbackReflectiveWithArgs("${methodName}", (BaseData params) -> {
return (${returnType}) ${prefix}.${methodName}(${argStr});
2024-05-21 19:04:52 -05:00
});
`;
}
return {
name: methodName,
returnType: returnType,
isStatic: isStatic,
arguments: arguments,
impl: impl,
idx: methods.indexOf(method)
2024-05-21 19:04:52 -05:00
};
});
2024-05-21 06:07:24 -05:00
return {
className: className,
2024-05-21 19:04:52 -05:00
constructors: constructorDetails,
methods: methodDetails,
usedClasses: [...new Set(usedClasses)]
2024-05-21 06:07:24 -05:00
}
}