
net.kender.core.mc.EXTRAS.LIBS Maven / Gradle / Ivy
package net.kender.core.mc.EXTRAS;
import static net.kender.core.mc.EXTRAS.EXTRAS.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import net.kender.core.Json.libreries.librarie;
import net.kender.core.Json.libreries.os;
import net.kender.core.Json.libreries.rule;
import com.fasterxml.jackson.databind.JsonNode;
import net.kender.kutils.OperativeSystem;
public class LIBS {
public static final OperativeSystem sys = OperativeSystem.thisOperativeSystem();
public static String getlibs(Path __DEST__, ArrayList libs) throws URISyntaxException, IOException {
for (librarie lib:libs) {
if(lib.downloads == null) {getCustomsLibs(lib,__DEST__); continue;}
boolean allowDownload = false;
System.out.println(lib.name);
if(lib.rules != null){
if(comproveOs(lib.rules)){
allowDownload = comproveClassFiers(lib);
}
}else{
allowDownload = comproveClassFiers(lib);
}
if (allowDownload) Dwnlad(__DEST__,lib);
}
return null;
}
private static void getCustomsLibs(librarie libraryNode,Path __DEST__) throws URISyntaxException, IOException {
int indexOfp = libraryNode.name.lastIndexOf(":");
String subversion = libraryNode.name.substring(indexOfp + 1,
libraryNode.name.length());
String etc = libraryNode.name.substring(0, indexOfp);
int indexOf = libraryNode.name.indexOf(":");
String nana = libraryNode.name.substring(indexOf + 1, indexOfp);
download(new URI(libraryNode.url + etc.replace(":", "/").replace(".", "/") + "/" + subversion + "/"
+ nana + "-" + subversion + ".jar"), new File(__DEST__ + "\\" + nana + ".jar"));
}
private static boolean comproveOs(List ruls){
boolean result = false;
for(rule rul:ruls){
if("allow".equals(rul.action)){
if(rul.os == null){
result = true;
}else if(sys.name().toLowerCase().equals(rul.os.name)){
result = true;
}
} else if((rul.os != null) && !(sys.name().toLowerCase().equals(rul.os.name))){
result = true;
}
}
return result;
}
private static boolean comproveClassFiers(librarie lib){
boolean result = false;
if(lib.downloads.classifiers == null){
result = true;
} else {
if(lib.downloads.classifiers.windows != null && "windows".equalsIgnoreCase(sys.name())) result = true;
if(lib.downloads.classifiers.osx != null && "osx".equalsIgnoreCase(sys.name())) result = true;
if(lib.downloads.classifiers.linux != null && "linux".equalsIgnoreCase(sys.name())) result = true;
if(lib.natives.windows != null && "windows".equalsIgnoreCase(sys.name())) result = true;
if(lib.natives.osx != null && "osx".equalsIgnoreCase(sys.name())) result = true;
if(lib.natives.linux != null && "linux".equalsIgnoreCase(sys.name())) result = true;
}
return result;
}
private static void Dwnlad(Path dest,librarie lib) throws URISyntaxException, IOException {
if(lib.downloads.artifact != null){
download(new URI(lib.downloads.artifact.url),new File(dest + "\\" + new File(lib.downloads.artifact.path).getName()));
}
if(lib.downloads.classifiers != null){
String url = "";
String path = "";
if(lib.downloads.classifiers.windows == null && lib.downloads.classifiers.windows64 != null) url = lib.downloads.classifiers.windows64.url;
if(lib.downloads.classifiers.windows == null && lib.downloads.classifiers.windows64 != null) path = lib.downloads.classifiers.windows64.path;
if(lib.downloads.classifiers.windows != null ) url = lib.downloads.classifiers.windows.url;
if(lib.downloads.classifiers.windows != null ) path = lib.downloads.classifiers.windows.path;
download(new URI(url),new File(dest + "\\" + new File(path).getName()));
}
}
// private static String quitarUltimaParteRuta(String ruta) {
// if (ruta == null || ruta.isEmpty()) {
// return ruta;
// }
// int ultimoSeparador = ruta.lastIndexOf(File.separator);
// if (ultimoSeparador != -1) {
// return ruta.substring(0, ultimoSeparador);
// } else {
// return ruta;
// }
// }
public static String obtenerNombreArchivo(String ruta) {
// Crear un objeto File a partir de la ruta
File archivo = new File(ruta);
// Obtener el nombre del archivo
return archivo.getName();
}
public static void dons(Path dest, ArrayList a) throws URISyntaxException, IOException {
// Iterar a través de las librerías
for (librarie libNode : a) {
if (libNode.rules == null) {
if (libNode.downloads.artifact != null) {
String libraryName = libNode.downloads.artifact.path;
download(new URI(libNode.downloads.artifact.url),
new File(dest + "\\" + quitarUltimaParteRuta(libraryName)));
}
continue;
}
// Verificar reglas para la compatibilidad del sistema operativo
boolean allowDownload = true;
for (rule ruleNode : libNode.rules) {
String action = ruleNode.action;
os osNode = ruleNode.os;
if (osNode != null) {
String osName = osNode.name;
if ("allow".equals(action) && !isCurrentOSCompatible(osName)) {
allowDownload = false;
break;
} else if ("disallow".equals(action) && isCurrentOSCompatible(osName)) {
allowDownload = false;
break;
}
}
}
if (libNode.natives != null && libNode.natives.windows != null && libNode.downloads.classifiers.windows != null) {
String libraryName = libNode.downloads.classifiers.windows.path;
if (libNode.downloads.classifiers.windows == null) {
libraryName = libNode.downloads.classifiers.windows64.path;
if(libNode.downloads.classifiers.windows64 == null){
continue;
}
download(
new URI(libNode.downloads.classifiers.windows64.url),
new File(dest + "\\" + quitarUltimaParteRuta(libraryName)));
} else {
libraryName = libNode.downloads.classifiers.windows.path;
download(
new URI(libNode.downloads.classifiers.windows.url),
new File(dest + "\\" + quitarUltimaParteRuta(libraryName)));
}
}
if (libNode.downloads.classifiers != null) {
if (libNode.downloads.classifiers.windows64 != null) {
String libraryName = libNode.downloads.classifiers.windows64.path;
download(
new URI(libNode.downloads.classifiers.windows64.url),
new File(dest + "\\" + quitarUltimaParteRuta(libraryName)));
}
}
// Descargar la librería si es compatible
if (allowDownload) {
if (libNode.downloads.artifact != null) {
String downloadUrl = libNode.downloads.artifact.url;
System.out.println("Descargando librería desde: " + downloadUrl);
String libraryName = libNode.downloads.artifact.path;
download(new URI(downloadUrl), new File(dest + "\\" + quitarUltimaParteRuta(libraryName)));
if (libNode.downloads.classifiers != null) {
libraryName = libNode.downloads.classifiers.windows.path;
download(
new URI(libNode.downloads.classifiers.windows.url),
new File(dest + "\\" + quitarUltimaParteRuta(libraryName)));
}
}
} else {
}
}
}
private static boolean isCurrentOSCompatible(String osName) {
// Lógica para verificar la compatibilidad con el sistema operativo actual
String currentOS = System.getProperty("os.name").toLowerCase();
return currentOS.contains(osName.toLowerCase());
}
private static String quitarUltimaParteRuta(String a) {
Path b = Paths.get(a);
return b.getFileName().toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy