All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.k3nder.core.mc.utils.LibsUtils Maven / Gradle / Ivy

The newest version!
package net.k3nder.core.mc.utils;

import net.k3nder.core.json.libreries.librarie;
import net.k3nder.core.json.libreries.rule;
import net.kender.kutils.OperativeSystem;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;


public class LibsUtils {
    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;
            if(lib.rules != null){
                if(comproveOs(lib.rules)) allowDownload = comproveClassFiers(lib);
            } else allowDownload = comproveClassFiers(lib);
            if (allowDownload) download(__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);
        String etc = libraryNode.name.substring(0, indexOfp);
        int indexOf = libraryNode.name.indexOf(":");
        String nana = libraryNode.name.substring(indexOf + 1, indexOfp);
        FileUtils.copyURLToFile(new URI(libraryNode.url + etc.replace(":", "/").replace(".", "/") + "/" + subversion + "/"
                + nana + "-" + subversion + ".jar").toURL(), 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 download(Path dest, librarie lib) throws URISyntaxException, IOException {
        if(lib.downloads.artifact != null){
            FileUtils.copyURLToFile(new URI(lib.downloads.artifact.url).toURL(),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;
            FileUtils.copyURLToFile(new URI(url).toURL(),new File(dest + "\\" + new File(path).getName()));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy