Almost done!

This commit is contained in:
ZXMushroom63 2024-05-22 08:04:52 +08:00
parent 2c7c218471
commit b4da547505
3 changed files with 131 additions and 11 deletions

View File

@ -36,7 +36,7 @@ reflect_%classname%_constructors.add(reflect_%classname%_constructor_%constructo
`; `;
const templateMethod = ` const templateMethod = `
BaseData reflect_%classname%_method_%methodname%_%idx% = new ModData(); BaseData reflect_%classname%_method_%methodname%_%idx% = new ModData();
reflect_%classname%_method_%methodname%_%idx%.set("methodName", %methodname%); reflect_%classname%_method_%methodname%_%idx%.set("methodName", "%methodname%");
reflect_%classname%_method_%methodname%_%idx%.set("returnType", %returntype%); reflect_%classname%_method_%methodname%_%idx%.set("returnType", %returntype%);
reflect_%classname%_method_%methodname%_%idx%.set("static", %static%); reflect_%classname%_method_%methodname%_%idx%.set("static", %static%);
reflect_%classname%_method_%methodname%_%idx%.set("argnames", %argkeys%); reflect_%classname%_method_%methodname%_%idx%.set("argnames", %argkeys%);
@ -82,6 +82,7 @@ function process(file, reader, classDataDump, className) {
reader.addEventListener("load", ()=>{ reader.addEventListener("load", ()=>{
var output = reader.result; var output = reader.result;
classDataDump[className] = (reconJ(output, className)); classDataDump[className] = (reconJ(output, className));
console.log(classDataDump[className].usedClasses);
res(output); res(output);
}); });
reader.readAsText(file); reader.readAsText(file);
@ -92,9 +93,9 @@ function createManagerFile(managerTemplate, config, zip, dataDump) {
var filePath = config.managerFile.replaceAll(".", "/") + ".java"; var filePath = config.managerFile.replaceAll(".", "/") + ".java";
for (let i = 0; i < config.targetFiles.length; i++) { for (let i = 0; i < config.targetFiles.length; i++) {
manager = `import ${config.targetFiles[i]}\n` + ";" + manager; manager = `import ${config.targetFiles[i]}` + ";\n" + manager;
} }
manager = `package ${config.managerFile.match(/(.*)(?=\.[^.]*$)/g)[0]}\n` + ";" + manager; manager = `package ${config.managerFile.match(/(.*)(?=\.[^.]*$)/g)[0]}` + ";\n" + manager;
var classText = ""; var classText = "";
@ -141,6 +142,45 @@ function createManagerFile(managerTemplate, config, zip, dataDump) {
} }
tmpClassText = tmpClassText.replaceAll("%constructordefs%", constructorText); tmpClassText = tmpClassText.replaceAll("%constructordefs%", constructorText);
var methodText = "";
for (let i = 0; i < dataDump[className].methods.length; i++) {
const method = dataDump[className].methods[i];
var tmpMethodText = templateMethod;
tmpMethodText = tmpMethodText.replaceAll("%classname%", className);
tmpMethodText = tmpMethodText.replaceAll("%idx%", method.idx);
tmpMethodText = tmpMethodText.replaceAll("%static%", method.isStatic);
tmpMethodText = tmpMethodText.replaceAll("%methodname%", method.name);
tmpMethodText = tmpMethodText.replaceAll("%returntype%", "\""+className+"\"");
tmpMethodText = tmpMethodText.replaceAll("%argkeys%", `new String[]{${(()=>{
var txt = "";
var argumentKeys = Object.keys(method.arguments);
for (let i = 0; i < argumentKeys.length; i++) {
const k = argumentKeys[i];
txt += `"${k}"`;
if (i !== argumentKeys.length - 1) {
txt += ", ";
}
}
return txt;
})()}}`);
tmpMethodText = tmpMethodText.replaceAll("%argvalues%", `new String[]{${(()=>{
var txt = "";
var argumentKeys = Object.keys(method.arguments);
for (let i = 0; i < argumentKeys.length; i++) {
const k = argumentKeys[i];
txt += `"${method.arguments[k]}"`;
if (i !== argumentKeys.length - 1) {
txt += ", ";
}
}
return txt;
})()}}`);
tmpMethodText = tmpMethodText.replaceAll("%methodimpl%", method.impl);
methodText += tmpMethodText;
}
tmpClassText = tmpClassText.replaceAll("%methoddefs%", methodText);
classText += tmpClassText; classText += tmpClassText;
} }
manager = manager.replaceAll("%classdefs%", classText); manager = manager.replaceAll("%classdefs%", classText);

View File

@ -54,6 +54,7 @@
overflow-x: hidden; overflow-x: hidden;
margin-top: 2rem; margin-top: 2rem;
color: orange; color: orange;
text-shadow: 0px 0px 2px;
} }
</style> </style>
<script src="FileSaver.js"></script> <script src="FileSaver.js"></script>

View File

@ -7,12 +7,26 @@ const getStatements = {
"char": "params.getChar(%s)", "char": "params.getChar(%s)",
"double": "params.getDouble(%s)", "double": "params.getDouble(%s)",
} }
const callbackStatements = {
"boolean": "setCallbackBooleanWithDataArg",
"int": "setCallbackIntWithDataArg",
"String": "setCallbackStringWithDataArg",
"double": "setCallbackDoubleWithDataArg",
"float": "setCallbackFloatWithDataArg",
}
const getStatementsTypes = Object.keys(getStatements); const getStatementsTypes = Object.keys(getStatements);
const callbackStatementsTypes = Object.keys(callbackStatements);
function reconJ(java, className) { function reconJ(java, className) {
var usedClasses = [];
var javaText = java;
javaText = javaText.replace(/\/\/.*$/gm, '');
javaText = javaText.replace(/\/\*[\s\S]*?\*\//gm, '');
let constructorRegex = /(public|protected|private|static|\s) +(\w+) *\(([^)]*)\)/g; let constructorRegex = /(public|protected|private|static|\s) +(\w+) *\(([^)]*)\)/g;
let constructors = [...java.matchAll(constructorRegex).filter((line)=>{ let constructors = [...javaText.matchAll(constructorRegex).filter((line)=>{
return !line.includes(" private ") || !line.includes(" protected "); return !line[0].includes(" private ") && !line[0].includes(" protected ");
})]; })];
let constructorDetails = constructors.map((constructor) => { let constructorDetails = constructors.map((constructor) => {
@ -32,7 +46,9 @@ function reconJ(java, className) {
var argumentsKeys = Object.keys(arguments); var argumentsKeys = Object.keys(arguments);
for (let i = 0; i < argumentsKeys.length; i++) { for (let i = 0; i < argumentsKeys.length; i++) {
const key = argumentsKeys[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}")`}`; argStr += `(${arguments[key]}) ${getStatementsTypes.includes(arguments[key]) ? getStatements[arguments[key]].replaceAll("%s", "\""+key+"\"") : `params.getReflective("${key}")`}`;
if (i !== argumentsKeys.length - 1) { if (i !== argumentsKeys.length - 1) {
argStr += ", " argStr += ", "
@ -40,9 +56,8 @@ function reconJ(java, className) {
} }
let impl = ` let impl = `setCallbackReflectiveWithArgs("${constructorName}", (BaseData params) -> {
setCallbackReflectiveWithArgs("${constructorName}", (BaseData params) -> { return new ${className}(${argStr});
return new ${className}(${argStr})
}); });
`; `;
@ -54,9 +69,73 @@ function reconJ(java, className) {
}; };
}); });
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 impl;
if (returnType === "void") {
impl = `setCallbackVoidWithArgs("${methodName}", (BaseData params) -> {
((${className}) params.get("_self")).${methodName}(${argStr});
});
`;
} else if (callbackStatementsTypes.includes(returnType)) {
impl = `${callbackStatements[returnType]}("${methodName}", (BaseData params) -> {
return (${returnType}) ((${className}) params.get("_self")).${methodName}(${argStr});
});
`;
} else {
usedClasses.push(returnType);
impl = `setCallbackReflectiveWithArgs("${methodName}", (BaseData params) -> {
return (${returnType}) ((${className}) params.get("_self")).${methodName}(${argStr});
});
`;
}
return {
name: methodName,
returnType: returnType,
isStatic: isStatic,
arguments: arguments,
impl: impl
};
});
return { return {
className: className, className: className,
constructors: constructorDetails constructors: constructorDetails,
methods: methodDetails,
usedClasses: [...new Set(usedClasses)]
} }
} }