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:
@@ -69,18 +69,55 @@ public class Bootstrap {
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
private static final Path VERSION_FILE = baseDir.resolve("bin/.version");
|
||||
|
||||
private static String readCurrentVersion() {
|
||||
// Читаем версию из манифеста zernmclauncher.jar в папке bin/
|
||||
Path launcherJar = getLauncherJar();
|
||||
// Читаем версию из файла (быстро и надёжно)
|
||||
if (Files.exists(VERSION_FILE)) {
|
||||
try {
|
||||
if (Files.exists(launcherJar)) {
|
||||
try (FileInputStream fis = new FileInputStream(launcherJar.toFile())) {
|
||||
Manifest manifest = new Manifest(fis);
|
||||
String version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
|
||||
String v = Files.readString(VERSION_FILE).trim();
|
||||
if (!v.isBlank()) return v;
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
return "0.0.0";
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
@@ -473,10 +510,10 @@ public class Bootstrap {
|
||||
|
||||
if (conn.getResponseCode() == 200) {
|
||||
Path jarFile = getLauncherJar();
|
||||
Path tmp = jarFile.resolveSibling("zernmc-launcher-new.jar");
|
||||
|
||||
// Скачиваем сразу в целевой файл (без tmp)
|
||||
try (InputStream in = conn.getInputStream();
|
||||
OutputStream out = new FileOutputStream(tmp.toFile())) {
|
||||
OutputStream out = new FileOutputStream(jarFile.toFile())) {
|
||||
byte[] buf = new byte[BUFFER_SIZE];
|
||||
int len;
|
||||
long total = 0;
|
||||
@@ -486,14 +523,16 @@ public class Bootstrap {
|
||||
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);
|
||||
Files.move(tmp, jarFile, StandardCopyOption.REPLACE_EXISTING);
|
||||
if (Files.exists(backup)) Files.delete(backup);
|
||||
// Сохраняем версию в файл
|
||||
try {
|
||||
Files.writeString(VERSION_FILE, newVersion);
|
||||
log("Версия сохранена в " + VERSION_FILE);
|
||||
} catch (Exception e) {
|
||||
log("Ошибка сохранения версии: " + e.getMessage());
|
||||
}
|
||||
|
||||
// Версия уже в манифесте JAR
|
||||
log("Обновлено до v" + newVersion + " (JAR метод)");
|
||||
} else {
|
||||
throw new IOException("Сервер вернул код: " + conn.getResponseCode());
|
||||
|
||||
Reference in New Issue
Block a user