q13x-eaglerproxy/proxy/Packet.js

25 lines
944 B
JavaScript
Raw Normal View History

2024-09-04 05:02:00 -07:00
import { dirname, join } from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { Util } from "./Util.js";
export async function loadPackets(dir) {
const files = (await Util.recursiveFileSearch(dir ?? join(dirname(fileURLToPath(import.meta.url)), "packets"))).filter((f) => f.endsWith(".js") && !f.endsWith(".disabled.js"));
const packetRegistry = new Map();
for (const file of files) {
const imp = await import(process.platform == "win32" ? pathToFileURL(file).toString() : file);
for (const val of Object.values(imp)) {
if (val != null) {
let e;
try {
e = new val();
}
catch { }
if (e != null && e.type == "packet") {
e.class = val;
packetRegistry.set(e.packetId, e);
}
}
}
}
return packetRegistry;
}