Files
AeroMc-Launcher/scripts/build-agent.js
T
2026-10-03 14:20:37 +00:00

73 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
/*
* build-agent.js – baut src/assets/agent/aeromc-capes.jar (AeroMC-Umhang-Agent)
* ------------------------------------------------------------------------------
* Braucht ein JDK (javac/java, ab 11) und ASM 9 – beides wird nur beim Bauen
* gebraucht, die fertige Datei liegt im Projekt. Gesucht wird das JDK über
* JAVA_HOME bzw. unter "C:\Program Files\Eclipse Adoptium", ASM im
* Bibliotheksordner von AeroMC (dort legen Fabric/Forge es ohnehin ab).
* Der Agent wird für Java 8 übersetzt, läuft also mit jeder Minecraft-Version.
*
* Aufruf: node scripts/build-agent.js
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const { execFileSync } = require('child_process');
const WURZEL = path.join(__dirname, '..');
const ZIEL = path.join(WURZEL, 'src', 'assets', 'agent', 'aeromc-capes.jar');
function findeJdk() {
const kandidaten = [];
if (process.env.JAVA_HOME) kandidaten.push(process.env.JAVA_HOME);
const adoptium = 'C:\\Program Files\\Eclipse Adoptium';
if (fs.existsSync(adoptium)) {
for (const d of fs.readdirSync(adoptium).filter((n) => /^jdk-/.test(n)).sort().reverse()) kandidaten.push(path.join(adoptium, d));
}
const jdk = kandidaten.find((d) => fs.existsSync(path.join(d, 'bin', process.platform === 'win32' ? 'javac.exe' : 'javac')));
if (!jdk) throw new Error('Kein JDK gefunden (JAVA_HOME setzen).');
return jdk;
}
function findeAsm(name) {
const basis = path.join(process.env.APPDATA || os.homedir(), 'aeromc', 'shared', 'libraries', 'org', 'ow2', 'asm', name);
if (!fs.existsSync(basis)) throw new Error(`ASM (${name}) nicht gefunden unter ${basis}`);
const versionen = fs.readdirSync(basis).filter((v) => /^9\./.test(v))
.sort((a, b) => b.split('.').map(Number).reduce((s, x, i) => s || x - (a.split('.').map(Number)[i] || 0), 0));
const datei = path.join(basis, versionen[0], `${name}-${versionen[0]}.jar`);
if (!fs.existsSync(datei)) throw new Error('ASM-Datei fehlt: ' + datei);
return datei;
}
function quellen(ordner) {
return fs.readdirSync(ordner, { withFileTypes: true }).flatMap((e) => {
const p = path.join(ordner, e.name);
return e.isDirectory() ? quellen(p) : (e.name.endsWith('.java') ? [p] : []);
});
}
const jdk = findeJdk();
const javac = path.join(jdk, 'bin', 'javac');
const java = path.join(jdk, 'bin', 'java');
const asm = findeAsm('asm');
const asmCommons = findeAsm('asm-commons');
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'aeromc-agent-'));
const klassen = path.join(tmp, 'klassen');
const werkzeug = path.join(tmp, 'werkzeug');
console.log('JDK:', jdk);
console.log('ASM:', asm);
try {
execFileSync(javac, ['--release', '8', '-nowarn', '-encoding', 'UTF-8', '-cp', asm, '-d', klassen,
...quellen(path.join(WURZEL, 'agent', 'src'))], { stdio: 'inherit' });
execFileSync(javac, ['-encoding', 'UTF-8', '-cp', [asm, asmCommons].join(path.delimiter), '-d', werkzeug,
path.join(WURZEL, 'agent', 'tools', 'Shade.java')], { stdio: 'inherit' });
fs.mkdirSync(path.dirname(ZIEL), { recursive: true });
execFileSync(java, ['-cp', [werkzeug, asm, asmCommons].join(path.delimiter), 'Shade', ZIEL, klassen, asm], { stdio: 'inherit' });
console.log(`Fertig: ${ZIEL} (${fs.statSync(ZIEL).size} Bytes)`);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}