add clean_proto_gen

This commit is contained in:
Akbar Yahya 2022-11-12 12:22:50 +08:00
parent 238dd43a77
commit 302d35f51a
No known key found for this signature in database
GPG Key ID: 0C9985FEC5C018C9
2 changed files with 1711 additions and 110 deletions

1487
gc_needed.json Normal file

File diff suppressed because it is too large Load Diff

334
op.js
View File

@ -9,6 +9,7 @@ const read_cmdid_output = "cmdid.json";
const read_cmdid_output_gc = "cmdid_gc.json"; const read_cmdid_output_gc = "cmdid_gc.json";
const read_cmdid_output_gc_update = "cmdid_gc_update.json"; const read_cmdid_output_gc_update = "cmdid_gc_update.json";
const read_cmdid_output_gc_nofound = "cmdid_gc_nofound.json"; const read_cmdid_output_gc_nofound = "cmdid_gc_nofound.json";
const file_gc_needed = "gc_needed.json";
const write_op = "PacketOpcodes.java"; const write_op = "PacketOpcodes.java";
@ -213,6 +214,10 @@ function update_cmdid_gc() {
save_json(data_gc_cmdid_nofound, read_cmdid_output_gc_nofound); save_json(data_gc_cmdid_nofound, read_cmdid_output_gc_nofound);
} }
function read_json(file) {
return JSON.parse(fs.readFileSync(file));
}
// save json // save json
function save_json(raw, file) { function save_json(raw, file) {
var j = JSON.stringify(raw, null, 4); var j = JSON.stringify(raw, null, 4);
@ -257,141 +262,250 @@ var index_file_packet_nofound = 0;
var index_file_packet_rename = 0; var index_file_packet_rename = 0;
var index_file_packet_norename = 0; var index_file_packet_norename = 0;
var index_file_packet_renamemulti = 0; var index_file_packet_renamemulti = 0;
function fix_packet(p = "recv", saveit = false) { var file_gc_need = [];
var path = folder_packet_gc + p; function fix_packet(saveit = false) {
fs.readdir(path, function (err, files) { const files = getAllFiles(folder_packet_gc);
//handling error
if (err) { const json_cmdid_last = read_json(read_cmdid_output);
return console.log("Unable to scan directory: " + err); const json_cmdid_old = read_json(read_cmdid_output_gc);
const json_cmdidfix_raw = read_json(read_cmdid_output_gc_update);
files.forEach(function (file) {
//var f = path + "/" + file;
const read = fs.readFileSync(file);
var real = read.toString();
var name = getPacketOpcodes(real);
if (!name) {
console.log("no found");
return;
} }
const cmd_last = fs.readFileSync(read_cmdid_output); if (name === "NONE") {
const cmd_old = fs.readFileSync(read_cmdid_output_gc); return;
const cmdidfix_raw = fs.readFileSync(read_cmdid_output_gc_update); }
const json_cmdid_last = JSON.parse(cmd_last);
const json_cmdid_old = JSON.parse(cmd_old);
const json_cmdidfix_raw = JSON.parse(cmdidfix_raw);
files.forEach(function (file) { var subdata = new Object();
var f = path + "/" + file; subdata["name"] = name;
const read = fs.readFileSync(f); file_gc_need.push(subdata);
var real = read.toString();
var r = real.match(/\(.*?\)/g).map((x) => x.replace(/[()]/g, ""));
var name; //var name = c[1];
r.forEach(function (s, index) {
if (s.match("PacketOpcodes.")) {
name = s.split("PacketOpcodes.")[1];
if (name.match(",")) {
name = name.split(",")[0];
}
return;
}
});
if (!name) { //console.log(r);
console.log("no found");
return;
}
//var name = c[1]; var found_old = json_cmdid_old.find((j) => j.name === name);
if (found_old) {
//console.log(found_old);
index_file_packet_found++;
var found_new = json_cmdid_last.find((j) => j.id == found_old.id);
if (found_new) {
if (found_new.name != found_old.name) {
index_file_packet_rename++;
console.log(
"Found need rename: " + found_old.name + " > " + found_new.name
);
//console.log(r); // rename all
json_cmdidfix_raw.forEach(function (s) {
var r = s.replace;
if (r) {
// var notify = HomeNewUnlockedBgmIdListNotify.Unk2700_MEBFPBDNPGO_ServerNotify
// var notify = Unk2700MEBFPBDNPGOServerNotify.HomeNewUnlockedBgmIdListNotify
var found_old = json_cmdid_old.find((j) => j.name === name); // Unk2700MEBFPBDNPGOServerNotify to HomeNewUnlockedBgmIdListNotifyOuterClass
if (found_old) { // Unk2700OGHMHELMBNNServerRsp to HomeChangeBgmRspOuterClass
//console.log(found_old);
index_file_packet_found++;
var found_new = json_cmdid_last.find((j) => j.id == found_old.id);
if (found_new) {
if (found_new.name != found_old.name) {
index_file_packet_rename++;
console.log(
"Found need rename: " + found_old.name + " > " + found_new.name
);
// rename all // - Need More Auto like -
json_cmdidfix_raw.forEach(function (s) { // addUnk2700ELJPLMIHNIP to addNewUnlockedBgmIdList (this should be found inside gen proto)
var r = s.replace; // setUnk2700BJHAMKKECEI to setBgmId
if (r) { if (r.match("Unk")) {
// var notify = HomeNewUnlockedBgmIdListNotify.Unk2700_MEBFPBDNPGO_ServerNotify //console.log(r);
// var notify = Unk2700MEBFPBDNPGOServerNotify.HomeNewUnlockedBgmIdListNotify var x = r.split("_");
var tr = x.join("");
var realneed = tr;
// Unk2700MEBFPBDNPGOServerNotify to HomeNewUnlockedBgmIdListNotifyOuterClass if (tr.match("ServerNotify")) {
// Unk2700OGHMHELMBNNServerRsp to HomeChangeBgmRspOuterClass //console.log("found: "+tr);
let re = new RegExp(`${tr}`, "g");
// - Need More Auto like - tr = tr.replace(re, `${s.name}OuterClass`);
// addUnk2700ELJPLMIHNIP to addNewUnlockedBgmIdList (this should be found inside gen proto) //console.log("found: " + tr);
// setUnk2700BJHAMKKECEI to setBgmId } else if (tr.match("ServerRsp")) {
if (r.match("Unk")) { let re = new RegExp(`${tr}`, "g");
//console.log(r); tr = tr.replace(re, `${s.name}OuterClass`);
var x = r.split("_"); } else {
var tr = x.join(""); let re = new RegExp(`${tr}`, "g");
var realneed = tr; tr = tr.replace(re, s.name);
//console.log("found: " + tr);
if (tr.match("ServerNotify")) {
//console.log("found: "+tr);
let re = new RegExp(`${tr}`, "g");
tr = tr.replace(re, `${s.name}OuterClass`);
//console.log("found: " + tr);
} else if(tr.match("ServerRsp")){
let re = new RegExp(`${tr}`, "g");
tr = tr.replace(re, `${s.name}OuterClass`);
} else {
let re = new RegExp(`${tr}`, "g");
tr = tr.replace(re, s.name);
//console.log("found: " + tr);
}
let re = new RegExp(`${realneed}`, "g");
real = real.replace(re, tr);
} }
let re = new RegExp(`${r}`, "g"); let re = new RegExp(`${realneed}`, "g");
real = real.replace(re, s.name); real = real.replace(re, tr);
} }
}); let re = new RegExp(`${r}`, "g");
real = real.replace(re, s.name);
// simpel rename
//let re = new RegExp(`${found_old.name}`, "g");
//real = real.replace(re, found_new.name);
//console.log(real);
if (saveit) {
save(real, f);
} }
});
// simpel rename
//let re = new RegExp(`${found_old.name}`, "g");
//real = real.replace(re, found_new.name);
//console.log(real);
if (saveit) {
save(real, f);
} }
} else {
index_file_packet_norename++;
//console.log("Same name "+name);
} }
} else { } else {
index_file_packet_nofound++; index_file_packet_norename++;
console.log("No found " + name); //console.log("Same name "+name);
} }
} else {
index_file_packet_nofound++;
console.log("No found " + name);
}
//return; //return;
index_file_packet++; index_file_packet++;
});
console.log(
"Index file: " +
index_file_packet +
" | found " +
index_file_packet_found +
" | No found " +
index_file_packet_nofound +
" | Rename " +
index_file_packet_rename +
" | NoRename " +
index_file_packet_norename
);
}); });
save_json(file_gc_need, file_gc_needed);
console.log(
"Index file: " +
index_file_packet +
" | found " +
index_file_packet_found +
" | No found " +
index_file_packet_nofound +
" | Rename " +
index_file_packet_rename +
" | NoRename " +
index_file_packet_norename
);
} }
function isBlank(str) { function isBlank(str) {
return !!!str || /^\s*$/.test(str); return !!!str || /^\s*$/.test(str);
} }
fix_packet("send", false); function getAllFiles(dirPath, arrayOfFiles) {
//fix_packet("recv"); files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, "/", file));
}
});
return arrayOfFiles;
}
function getPacketOpcodes(raw) {
var r = raw.match(/\(.*?\)/g).map((x) => x.replace(/[()]/g, ""));
var name;
r.forEach(function (s, index) {
if (s.match("PacketOpcodes.")) {
name = s.split("PacketOpcodes.")[1];
if (name.match(",")) {
name = name.split(",")[0];
}
return;
}
});
return name;
}
var file_toclean = [];
var file_toaddmore = [];
var found_noclean = 0;
var found_needclean = 0;
var found_maybe_related = 0;
function clean_proto_gen() {
//const files = getAllFiles(folder_proto_gc_gen);
const files = getAllFiles("./proto");
const json_gc_needed = read_json(file_gc_needed);
const json_gc_now = read_json(read_cmdid_output_gc);
const json_gc_update = read_json(read_cmdid_output_gc_update);
//console.log(json_gc_needed);
console.log(
"File proto: " + files.length + " | Need " + json_gc_needed.length
);
const regex = /\import "(.*?)\.proto"/g;
// find all file import
files.forEach(function (file) {
var found = json_gc_needed.find((j) => file.match(j.name));
if (found) {
// read file
const read = fs.readFileSync(file);
var rd = read.toString();
// find import
while ((m = regex.exec(rd)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
// only index 1 grup
if (groupIndex == 1) {
var found_rt = file_toaddmore.find((j) => j.name === match);
if (found_rt) {
//found_maybe_related++;
//console.log(`Skip ${match}`);
} else {
var subdata = new Object();
subdata["name"] = match;
file_toaddmore.push(subdata);
found_maybe_related++;
}
}
});
}
}
});
// all
files.forEach(function (file) {
// main file
var found = json_gc_needed.find((j) => file.match(j.name));
if (found) {
found_noclean++;
} else {
var found1 = file_toaddmore.find((j) => file.match(j.name));
if (found1) {
found_noclean++;
//console.log("Files sub are required: "+file);
} else {
found_needclean++;
try {
fs.unlinkSync(file);
//file removed
} catch (err) {
console.error(err);
}
}
}
});
//console.log(file_toaddmore);
console.log(
"No clean: " +
found_noclean +
" | Need to clean: " +
found_needclean +
" | Related " +
found_maybe_related
);
}
clean_proto_gen();
//fix_packet(false);
//cmdid_to_op(); //cmdid_to_op();
//update_cmdid_gc(); //update_cmdid_gc();
//get_cmdid_gc(); //get_cmdid_gc();