Изменение получения self-version

This commit is contained in:
Sashegdev
2026-04-05 10:46:32 +00:00
parent c0a85658f4
commit 369f8c2f9d
4 changed files with 45 additions and 59 deletions
+1
View File
@@ -7,3 +7,4 @@ server/packs
server/data server/data
jre jre
.vscode .vscode
launcher/dependency-reduced-pom.xml
+7 -15
View File
@@ -17,49 +17,36 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- HTTP Client -->
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.5.14</version> <version>4.5.14</version>
</dependency> </dependency>
<!-- JSON Processing -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.15.2</version> <version>2.15.2</version>
</dependency> </dependency>
<!-- Gson (для Instance.java) -->
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.10.1</version> <version>2.10.1</version>
</dependency> </dependency>
<!-- org.json (для VersionInstaller, ZHttpClient и т.д.) -->
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>org.json</groupId>
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20231013</version> <version>20231013</version>
</dependency> </dependency>
<!-- Terminal UI -->
<dependency> <dependency>
<groupId>org.fusesource.jansi</groupId> <groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId> <artifactId>jansi</artifactId>
<version>2.4.1</version> <version>2.4.1</version>
</dependency> </dependency>
<!-- Progress Bar -->
<dependency> <dependency>
<groupId>me.tongfei</groupId> <groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId> <artifactId>progressbar</artifactId>
<version>0.9.5</version> <version>0.9.5</version>
</dependency> </dependency>
<!-- Commons IO -->
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
@@ -69,7 +56,7 @@
<build> <build>
<plugins> <plugins>
<!-- Shade Plugin для uber-jar --> <!-- Shade Plugin -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
@@ -85,6 +72,11 @@
<transformers> <transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainClass}</mainClass> <mainClass>${mainClass}</mainClass>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Title>ZernMC Launcher</Implementation-Title>
<Implementation-Vendor>SashegDev</Implementation-Vendor>
</manifestEntries>
</transformer> </transformer>
</transformers> </transformers>
</configuration> </configuration>
@@ -126,7 +118,7 @@
</executions> </executions>
</plugin> </plugin>
<!-- Antrun: копирование JRE и создание build.version --> <!-- Antrun: копирование JRE и создание build.version + zip -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId> <artifactId>maven-antrun-plugin</artifactId>
@@ -10,9 +10,6 @@ import me.sashegdev.zernmc.launcher.utils.ZAnsi;
import java.util.List; import java.util.List;
/**
* Главный фасад MinecraftLib — точка входа для всей логики установки и запуска
*/
public class MinecraftLib { public class MinecraftLib {
private final Instance instance; private final Instance instance;
@@ -21,11 +18,10 @@ public class MinecraftLib {
this.instance = instance; this.instance = instance;
} }
// ====================== УСТАНОВКА ====================== //Очистка
/**
* Установить vanilla версию Minecraft //Установка
*/
public boolean installMinecraft(String versionId) throws Exception { public boolean installMinecraft(String versionId) throws Exception {
VersionInstaller installer = new VersionInstaller(instance.getPath()); VersionInstaller installer = new VersionInstaller(instance.getPath());
boolean success = installer.install(versionId); boolean success = installer.install(versionId);
@@ -42,9 +38,6 @@ public class MinecraftLib {
return installer.install(minecraftVersion, forgeVersion); return installer.install(minecraftVersion, forgeVersion);
} }
/**
* Установить Fabric для выбранной версии Minecraft
*/
public boolean installFabric(String minecraftVersion, String loaderVersion) throws Exception { public boolean installFabric(String minecraftVersion, String loaderVersion) throws Exception {
FabricInstaller installer = new FabricInstaller(instance); FabricInstaller installer = new FabricInstaller(instance);
boolean success = installer.install(minecraftVersion, loaderVersion); boolean success = installer.install(minecraftVersion, loaderVersion);
@@ -90,23 +83,7 @@ public class MinecraftLib {
return true; return true;
} }
// ====================== ЗАПУСК ====================== //Запуск
/**
* Сгенерировать команду запуска (пока заглушка)
*/
public List<String> buildLaunchCommand(LaunchOptions options) throws Exception {
System.out.println(ZAnsi.cyan("Генерация команды запуска для " + instance.getName() + "..."));
// TODO: Полная реализация LaunchCommandBuilder (перенос из MLL)
System.out.println(ZAnsi.yellow("Генерация команды запуска пока в разработке"));
return List.of("java", "-jar", "placeholder.jar", "--version", instance.getName());
}
/**
* Запустить сборку
*/
public void launch(LaunchOptions options) throws Exception { public void launch(LaunchOptions options) throws Exception {
System.out.println(ZAnsi.brightGreen("Запуск сборки: " + instance.getName())); System.out.println(ZAnsi.brightGreen("Запуск сборки: " + instance.getName()));
@@ -118,7 +95,6 @@ public class MinecraftLib {
System.out.println(" " + arg); System.out.println(" " + arg);
} }
// === Реальный запуск ===
ProcessBuilder pb = new ProcessBuilder(command); ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(instance.getPath().toFile()); pb.directory(instance.getPath().toFile());
@@ -138,8 +114,6 @@ public class MinecraftLib {
System.out.println(ZAnsi.yellow("\nMinecraft завершился с кодом: " + exitCode)); System.out.println(ZAnsi.yellow("\nMinecraft завершился с кодом: " + exitCode));
} }
// ====================== ГЕТТЕРЫ ======================
public Instance getInstance() { public Instance getInstance() {
return instance; return instance;
} }
@@ -1,33 +1,52 @@
package me.sashegdev.zernmc.launcher.utils; package me.sashegdev.zernmc.launcher.utils;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
public class Version { public class Version {
public static String getCurrentVersion() { public static String getCurrentVersion() {
String version = Version.class.getPackage().getImplementationVersion(); try {
return (version != null && !version.isBlank()) ? version : "1.0.0"; // Способ 1: Из манифеста (самый правильный)
Manifest manifest = new Manifest(
Version.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF")
);
String version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
if (version != null && !version.isBlank()) {
return version;
}
// Способ 2: Из Package (запасной)
version = Version.class.getPackage().getImplementationVersion();
if (version != null && !version.isBlank()) {
return version;
}
} catch (Exception ignored) {
// если не получилось прочитать манифест — идём дальше
}
// Финальный fallback
return "1.0.0";
} }
/**
* Универсальное сравнение версий
* Возвращает true, если serverVersion новее currentVersion
*/
public static boolean isNewer(String current, String server) { public static boolean isNewer(String current, String server) {
if (current == null || server == null) return false; if (current == null || server == null) return false;
// Убираем -SNAPSHOT для сравнения
current = current.replace("-SNAPSHOT", "").trim(); current = current.replace("-SNAPSHOT", "").trim();
server = server.replace("-SNAPSHOT", "").trim(); server = server.replace("-SNAPSHOT", "").trim();
if (current.equals(server)) return false; if (current.equals(server)) return false;
String[] currentParts = current.split("\\."); String[] cParts = current.split("\\.");
String[] serverParts = server.split("\\."); String[] sParts = server.split("\\.");
int maxLength = Math.max(currentParts.length, serverParts.length); int max = Math.max(cParts.length, sParts.length);
for (int i = 0; i < maxLength; i++) { for (int i = 0; i < max; i++) {
int c = i < currentParts.length ? Integer.parseInt(currentParts[i]) : 0; int c = i < cParts.length ? Integer.parseInt(cParts[i]) : 0;
int s = i < serverParts.length ? Integer.parseInt(serverParts[i]) : 0; int s = i < sParts.length ? Integer.parseInt(sParts[i]) : 0;
if (s > c) return true; if (s > c) return true;
if (s < c) return false; if (s < c) return false;