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

84 lines
4.4 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-mod.js – baut den Fabric/Quilt-Mod "AeroMC Umhänge" (dist/aeromc-umhaenge-fabric-<version>.jar)
* -------------------------------------------------------------------------------------------------
* Für Spieler, die NICHT den AeroMC Launcher nutzen. Der Mod greift wie der
* Umhang-Agent nur in Mojangs authlib ein (Mixin, ohne Namensumsetzung) – darum
* braucht es weder Gradle noch Fabric Loom, und eine Datei passt zu allen
* Minecraft-Versionen, die Fabric unterstützt. Übersetzt wird für Java 8.
*
* Gebraucht werden (nur beim Bauen) ein JDK sowie sponge-mixin und authlib aus
* dem Bibliotheksordner von AeroMC. Der Nachschlage-Code ist derselbe wie im
* Agenten (agent/src/de/aeromc/capes/Nachschlagen.java).
*
* Aufruf: node scripts/build-mod.js
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const { execFileSync } = require('child_process');
const WURZEL = path.join(__dirname, '..');
const LIB = path.join(process.env.APPDATA || os.homedir(), 'aeromc', 'shared', 'libraries');
const modInfo = JSON.parse(fs.readFileSync(path.join(WURZEL, 'mod', 'resources', 'fabric.mod.json'), 'utf8'));
const ZIEL = path.join(WURZEL, 'dist', `aeromc-umhaenge-fabric-${modInfo.version}.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;
}
// neueste (oder mit aelteste=true älteste) Fassung einer Bibliothek im AeroMC-Bibliotheksordner
function bibliothek(gruppe, name, aelteste = false) {
const basis = path.join(LIB, ...gruppe.split('.'), name);
if (!fs.existsSync(basis)) throw new Error(`${name} nicht gefunden unter ${basis}`);
const fassungen = fs.readdirSync(basis).sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
const v = aelteste ? fassungen[0] : fassungen.pop();
const datei = path.join(basis, v, `${name}-${v}.jar`);
if (!fs.existsSync(datei)) throw new Error('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] : []);
});
}
// authlib in der ältesten Fassung: SkinLaderMixin ruft getTextures(GameProfile, boolean) auf, das es nur bis authlib 4 gibt
const jdk = findeJdk();
const klassenpfad = [bibliothek('net.fabricmc', 'sponge-mixin'), bibliothek('com.mojang', 'authlib', true)].join(path.delimiter);
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'aeromc-mod-'));
const inhalt = path.join(tmp, 'inhalt');
const stubs = path.join(tmp, 'stubs');
console.log('JDK:', jdk);
try {
// Platzhalter für Minecraft-Klassen (nur zum Kompilieren, landen nicht im Jar)
execFileSync(path.join(jdk, 'bin', 'javac'), ['--release', '8', '-nowarn', '-encoding', 'UTF-8', '-d', stubs,
...quellen(path.join(WURZEL, 'mod', 'stubs'))], { stdio: 'inherit' });
execFileSync(path.join(jdk, 'bin', 'javac'), ['--release', '8', '-nowarn', '-encoding', 'UTF-8', '-proc:none', '-cp', klassenpfad + path.delimiter + stubs, '-d', inhalt,
...quellen(path.join(WURZEL, 'mod', 'src')),
path.join(WURZEL, 'agent', 'src', 'de', 'aeromc', 'capes', 'Nachschlagen.java')], { stdio: 'inherit' });
for (const datei of fs.readdirSync(path.join(WURZEL, 'mod', 'resources'))) {
fs.copyFileSync(path.join(WURZEL, 'mod', 'resources', datei), path.join(inhalt, datei));
}
fs.mkdirSync(path.join(inhalt, 'assets', 'aeromc-capes'), { recursive: true });
fs.copyFileSync(path.join(WURZEL, 'src', 'assets', 'logo.png'), path.join(inhalt, 'assets', 'aeromc-capes', 'icon.png'));
fs.mkdirSync(path.dirname(ZIEL), { recursive: true });
if (fs.existsSync(ZIEL)) fs.rmSync(ZIEL);
execFileSync(path.join(jdk, 'bin', 'jar'), ['--create', '--file', ZIEL, '-C', inhalt, '.'], { stdio: 'inherit' });
console.log(`Fertig: ${ZIEL} (${fs.statSync(ZIEL).size} Bytes)`);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}