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

174
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,43 +262,33 @@ 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) {
return console.log("Unable to scan directory: " + err);
}
const cmd_last = fs.readFileSync(read_cmdid_output); const json_cmdid_last = read_json(read_cmdid_output);
const cmd_old = fs.readFileSync(read_cmdid_output_gc); const json_cmdid_old = read_json(read_cmdid_output_gc);
const cmdidfix_raw = fs.readFileSync(read_cmdid_output_gc_update); const json_cmdidfix_raw = read_json(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) { files.forEach(function (file) {
var f = path + "/" + file; //var f = path + "/" + file;
const read = fs.readFileSync(f); const read = fs.readFileSync(file);
var real = read.toString(); var real = read.toString();
var r = real.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;
}
});
var name = getPacketOpcodes(real);
if (!name) { if (!name) {
console.log("no found"); console.log("no found");
return; return;
} }
if (name === "NONE") {
return;
}
var subdata = new Object();
subdata["name"] = name;
file_gc_need.push(subdata);
//var name = c[1]; //var name = c[1];
//console.log(r); //console.log(r);
@ -371,6 +366,7 @@ function fix_packet(p = "recv", saveit = false) {
//return; //return;
index_file_packet++; index_file_packet++;
}); });
save_json(file_gc_need, file_gc_needed);
console.log( console.log(
"Index file: " + "Index file: " +
index_file_packet + index_file_packet +
@ -383,15 +379,133 @@ function fix_packet(p = "recv", saveit = false) {
" | NoRename " + " | NoRename " +
index_file_packet_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();