Assets: try meta download first, fallback to JAR extract

This commit is contained in:
SashegDev
2026-05-08 18:37:24 +00:00
parent 01668dd3bf
commit 74cd5ffdf3
@@ -12,8 +12,12 @@ import me.sashegdev.zernmc.launcher.auth.AuthManager;
import me.sashegdev.zernmc.launcher.minecraft.Instance; import me.sashegdev.zernmc.launcher.minecraft.Instance;
import me.sashegdev.zernmc.launcher.minecraft.InstanceManager; import me.sashegdev.zernmc.launcher.minecraft.InstanceManager;
import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@@ -86,6 +90,16 @@ public class JFXLauncher extends Application {
return; return;
} }
String serverVersion = getServerVersion();
if (serverVersion != null && !serverVersion.isEmpty()) {
System.out.println("[JFX] Загрузка assets через мета для версии " + serverVersion);
if (downloadAssetsFromMeta(serverVersion)) {
System.out.println("[JFX] Assets загружены через мета");
return;
}
System.out.println("[JFX] Мета недоступна, использую fallback");
}
System.out.println("[JFX] Извлечение assets из JAR..."); System.out.println("[JFX] Извлечение assets из JAR...");
Path jarPath = Paths.get(JFXLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI()); Path jarPath = Paths.get(JFXLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI());
if (Files.exists(jarPath) && jarPath.toString().endsWith(".jar")) { if (Files.exists(jarPath) && jarPath.toString().endsWith(".jar")) {
@@ -106,13 +120,84 @@ public class JFXLauncher extends Application {
} }
} }
} }
System.out.println("[JFX] Assets извлечены"); System.out.println("[JFX] Assets извлечены из JAR");
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("[JFX] Ошибка извлечения assets: " + e.getMessage()); System.out.println("[JFX] Ошибка извлечения assets: " + e.getMessage());
} }
} }
private static String getServerVersion() {
try {
URL url = new URL("http://localhost:1582/launcher/version");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
if (conn.getResponseCode() == 200) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) sb.append(line);
String response = sb.toString();
int versionStart = response.indexOf("\"version\":\"");
if (versionStart >= 0) {
int afterVersion = versionStart + 11;
int versionEnd = response.indexOf("\"", afterVersion);
if (versionEnd > afterVersion) {
return response.substring(afterVersion, versionEnd);
}
}
}
}
} catch (Exception ignored) {}
return null;
}
private static boolean downloadAssetsFromMeta(String version) {
try {
URL metaUrl = new URL("http://localhost:1582/launcher/meta/" + version);
HttpURLConnection conn = (HttpURLConnection) metaUrl.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
if (conn.getResponseCode() != 200) return false;
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) sb.append(line);
org.json.JSONObject meta = new org.json.JSONObject(sb.toString());
Path assetsDir = Paths.get("assets");
Files.createDirectories(assetsDir);
for (Object fileObj : meta.getJSONArray("files")) {
org.json.JSONObject file = (org.json.JSONObject) fileObj;
String path = file.getString("path");
if (path.startsWith("assets/")) {
String downloadUrl = "http://localhost:1582/launcher/file/" + version + "/" + path;
Path outPath = assetsDir.resolve(path.substring(7));
Files.createDirectories(outPath.getParent());
URL fileUrl = new URL(downloadUrl);
HttpURLConnection fileConn = (HttpURLConnection) fileUrl.openConnection();
fileConn.setConnectTimeout(10000);
fileConn.setReadTimeout(30000);
if (fileConn.getResponseCode() == 200) {
try (InputStream is = fileConn.getInputStream()) {
Files.copy(is, outPath);
}
}
fileConn.disconnect();
}
}
return true;
}
} catch (Exception e) {
System.out.println("[JFX] Ошибка загрузки через мета: " + e.getMessage());
return false;
}
}
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
this.mainStage = stage; this.mainStage = stage;