Skip to content

Commit

Permalink
Merge pull request #8 from DragonRex004/master
Browse files Browse the repository at this point in the history
Dependency Loader
  • Loading branch information
ezTxmMC authored Dec 29, 2024
2 parents a226f11 + dd8680c commit dee15ed
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ dependency-reduced-pom.xml

### Testing ###
config.json
config.toml
Dependencys/
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package eu.smoothcloud.launcher;

import eu.smoothcloud.launcher.dependency.*;

public class SmoothCloudLauncher {

public static void main(String[] args) {

DependencyLoader.loadExternalDependencys();
System.out.println("Finished Downloading Default Dependencys");
System.out.println("Begin Download for Internal Dependencys");
InternalDepLoader.loadInternalDependencys();
System.out.println("Finished Dependency Downloading!");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package eu.smoothcloud.launcher.dependency;

public enum Dependency {
NETTY("io.netty", "netty5-all", "5.0.0.Alpha5");
NETTY("io.netty", "netty5-all", "5.0.0.Alpha5"),
JLINE("org.jline", "jline", "3.28.0"),
TOMLJ("org.tomlj", "tomlj", "1.1.1"),
JSON("org.json", "json", "20240303");

private final String groupId;
private final String artifactId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package eu.smoothcloud.launcher.dependency;

import java.io.File;

public class DependencyLoader {
private static final String MAIN_FOLDER = "Dependencys/";
private static final String JAR_EXT = ".jar";

public static void loadExternalDependencys() {
for (Dependency dep : Dependency.values()) {
String link = MavenBuilder.buildMavenLink(dep);
String depDir = MAIN_FOLDER + dep.getGroupId() + "." + dep.getArtifactId();
String depFileName = dep.getVersion() + JAR_EXT;
System.out.println("Try to Download " + depFileName);
if(new File(depDir, depFileName).exists()) {
System.out.println("Skip Dependency " + depFileName);
continue;
}
Downloader.download(
link,
depDir,
depFileName
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package eu.smoothcloud.launcher.dependency;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class Downloader {

public static void download(String link, String targetDir, String targetFileName) {
File downloadDir = new File(targetDir);
if(!downloadDir.exists())
downloadDir.mkdirs();
if(!downloadDir.isDirectory())
return;
System.out.println("Begin Download!");
try (BufferedInputStream input = new BufferedInputStream(new URL(link).openStream())) {
FileOutputStream fileOutputStream = new FileOutputStream(new File(targetDir, targetFileName));
byte dataBuffer[] = new byte[1024];
int bytesRead;
int totalBytes = 0;
while ((bytesRead = input.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
totalBytes += bytesRead;
}
System.out.println("Download Finished! Bytes Written: " + totalBytes / 1024 + " KB");
} catch (IOException e) {
System.out.println("Unavailable Link");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package eu.smoothcloud.launcher.dependency;

import com.sun.tools.javac.Main;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLClassLoader;

public class InternalDepLoader {
private static final String MAIN_FOLDER = "Dependencys/";
private static final String JAR_EXT = ".jar";
private static final String MANIFEST_URL = "https://cdn.smoothcloud.eu/dependencies/manifest.json";


public static void loadInternalDependencys() {
try {
File jarFile = new File(
MAIN_FOLDER + Dependency.JSON.getGroupId() + "." + Dependency.JSON.getArtifactId(),
Dependency.JSON.getVersion() + JAR_EXT
);
URL jarUrl = jarFile.toURI().toURL();

StringBuilder jsonData = new StringBuilder();
HttpURLConnection connection = (HttpURLConnection) new URL(MANIFEST_URL).openConnection();
connection.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
}

try (URLClassLoader classLoader = new URLClassLoader(new URL[] { jarUrl }, Main.class.getClassLoader())) {
Class<?> jsonArrayClass = classLoader.loadClass("org.json.JSONArray");
Class<?> jsonObjectClass = classLoader.loadClass("org.json.JSONObject");

Object jsonArray = jsonArrayClass.getDeclaredConstructor(String.class).newInstance(jsonData.toString());

int length = (int) jsonArrayClass.getMethod("length").invoke(jsonArray);
for (int i = 0; i < length; i++) {
Object jsonObject = jsonArrayClass.getMethod("get", int.class).invoke(jsonArray, i);

String name = (String) jsonObjectClass.getMethod("getString", String.class).invoke(jsonObject,
"name");
String version = (String) jsonObjectClass.getMethod("getString", String.class).invoke(jsonObject,
"version");
String url = "https://cdn.smoothcloud.eu/dependencies/" + name + "-" + version + ".jar";

String inDepDir = MAIN_FOLDER + name;
String inDepFileName = name + "-" + version + JAR_EXT;
if(new File(inDepDir, inDepFileName).exists()) {
System.out.println("Skip Dependency " + inDepFileName);
continue;
}
File tempFile = new File(inDepDir);
if(tempFile.exists()) {
for (File file : tempFile.listFiles()) {
file.delete();
}
}
Downloader.download(
url,
inDepDir,
inDepFileName
);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eu.smoothcloud.launcher.dependency;

public class MavenBuilder {
public static String buildMavenLink(Dependency dependency) {
return String.format(
Repository.MAVEN.getLink(),
dependency.getGroupId().replace(".", "/"),
dependency.getArtifactId(),
dependency.getVersion(),
dependency.getArtifactId(),
dependency.getVersion()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package eu.smoothcloud.launcher.dependency;

public enum Repository {
MAVEN("https://repo1.maven.org/maven2/%s/%s/%s/%s-%s.jar");

private final String link;

Repository(String link) {
this.link = link;
}

public String getLink() {
return link;
}
}

0 comments on commit dee15ed

Please sign in to comment.