Fix version reading from JAR manifest

- Read version from bin/.version file (reliable, no JAR locking issues)
- Save version to bin/.version when downloading JAR
- Remove complex JAR/ZIP reading code
- Use simple file-based version storage
This commit is contained in:
SashegDev
2026-05-08 12:39:14 +00:00
parent 3a0570e7da
commit 6f56012e3a
@@ -69,16 +69,53 @@ public class Bootstrap {
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
private static final Path VERSION_FILE = baseDir.resolve("bin/.version");
private static String readCurrentVersion() { private static String readCurrentVersion() {
// Читаем версию из манифеста zernmclauncher.jar в папке bin/ // Читаем версию из файла (быстро и надёжно)
Path launcherJar = getLauncherJar(); if (Files.exists(VERSION_FILE)) {
try { try {
if (Files.exists(launcherJar)) { String v = Files.readString(VERSION_FILE).trim();
try (FileInputStream fis = new FileInputStream(launcherJar.toFile())) { if (!v.isBlank()) return v;
Manifest manifest = new Manifest(fis); } catch (Exception ignored) {}
String version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); }
if (version != null && !version.isBlank()) { return "0.0.0";
return version; }
} catch (Exception e) {
System.out.println("[DEBUG] Ошибка чтения манифеста: " + e.getMessage());
}
return "0.0.0";
}
// Стандартный способ чтения манифеста JAR
try (java.util.jar.JarFile jar = new java.util.jar.JarFile(launcherJar.toFile())) {
java.util.jar.Manifest manifest = jar.getManifest();
if (manifest != null) {
java.util.jar.Attributes attrs = manifest.getMainAttributes();
String version = attrs.getValue(java.util.jar.Attributes.Name.IMPLEMENTATION_VERSION);
if (version != null && !version.isBlank()) {
return version;
}
}
} catch (Exception ignored) {}
return "0.0.0";
}
// Простой способ: читаем JAR как ZIP и ищем строку Implementation-Version
try (java.util.zip.ZipFile zip = new java.util.zip.ZipFile(launcherJar.toFile())) {
java.util.zip.ZipEntry entry = zip.getEntry("META-INF/MANIFEST.MF");
if (entry != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(zip.getInputStream(entry)))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("Implementation-Version:")) {
String version = line.substring(line.indexOf(':') + 1).trim();
if (!version.isBlank()) {
return version;
}
}
} }
} }
} }
@@ -473,10 +510,10 @@ public class Bootstrap {
if (conn.getResponseCode() == 200) { if (conn.getResponseCode() == 200) {
Path jarFile = getLauncherJar(); Path jarFile = getLauncherJar();
Path tmp = jarFile.resolveSibling("zernmc-launcher-new.jar");
// Скачиваем сразу в целевой файл (без tmp)
try (InputStream in = conn.getInputStream(); try (InputStream in = conn.getInputStream();
OutputStream out = new FileOutputStream(tmp.toFile())) { OutputStream out = new FileOutputStream(jarFile.toFile())) {
byte[] buf = new byte[BUFFER_SIZE]; byte[] buf = new byte[BUFFER_SIZE];
int len; int len;
long total = 0; long total = 0;
@@ -486,14 +523,16 @@ public class Bootstrap {
System.out.print("\rСкачано: " + (total/1024/1024) + " MB"); System.out.print("\rСкачано: " + (total/1024/1024) + " MB");
} }
} }
log("JAR скачан"); log("JAR скачан: " + Files.size(jarFile) + " bytes");
Path backup = jarFile.resolveSibling(JAR_NAME + ".old"); // Сохраняем версию в файл
if (Files.exists(jarFile)) Files.move(jarFile, backup, StandardCopyOption.REPLACE_EXISTING); try {
Files.move(tmp, jarFile, StandardCopyOption.REPLACE_EXISTING); Files.writeString(VERSION_FILE, newVersion);
if (Files.exists(backup)) Files.delete(backup); log("Версия сохранена в " + VERSION_FILE);
} catch (Exception e) {
log("Ошибка сохранения версии: " + e.getMessage());
}
// Версия уже в манифесте JAR
log("Обновлено до v" + newVersion + " (JAR метод)"); log("Обновлено до v" + newVersion + " (JAR метод)");
} else { } else {
throw new IOException("Сервер вернул код: " + conn.getResponseCode()); throw new IOException("Сервер вернул код: " + conn.getResponseCode());