// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale;

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.net.URLClassLoader;
import java.lang.reflect.Method;
import java.net.URL;
import java.lang.reflect.InvocationTargetException;
import com.hypixel.hytale.plugin.early.TransformingClassLoader;
import javax.annotation.Nonnull;
import com.hypixel.hytale.plugin.early.EarlyPluginLoader;
import java.util.Locale;

public final class Main
{
    public static void main(final String[] args) {
        Locale.setDefault(Locale.ENGLISH);
        System.setProperty("java.awt.headless", "true");
        System.setProperty("file.encoding", "UTF-8");
        EarlyPluginLoader.loadEarlyPlugins(args);
        if (EarlyPluginLoader.hasTransformers()) {
            launchWithTransformingClassLoader(args);
        }
        else {
            LateMain.lateMain(args);
        }
    }
    
    private static void launchWithTransformingClassLoader(@Nonnull final String[] args) {
        try {
            final URL[] urls = getClasspathUrls();
            final ClassLoader appClassLoader = Main.class.getClassLoader();
            final TransformingClassLoader transformingClassLoader = new TransformingClassLoader(urls, EarlyPluginLoader.getTransformers(), appClassLoader.getParent(), appClassLoader);
            Thread.currentThread().setContextClassLoader(transformingClassLoader);
            final Class<?> lateMainClass = transformingClassLoader.loadClass("com.hypixel.hytale.LateMain");
            final Method mainMethod = lateMainClass.getMethod("lateMain", String[].class);
            mainMethod.invoke(null, args);
        }
        catch (final ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
            throw new RuntimeException("Failed to launch with transforming classloader", e);
        }
        catch (final InvocationTargetException e2) {
            final Throwable cause = e2.getCause();
            if (cause instanceof final RuntimeException re) {
                throw re;
            }
            if (cause instanceof final Error err) {
                throw err;
            }
            throw new RuntimeException("LateMain.lateMain() threw an exception", cause);
        }
    }
    
    private static URL[] getClasspathUrls() {
        final ClassLoader classLoader = Main.class.getClassLoader();
        if (classLoader instanceof final URLClassLoader urlClassLoader) {
            return urlClassLoader.getURLs();
        }
        final ObjectArrayList<URL> urls = new ObjectArrayList<URL>();
        final String classpath = System.getProperty("java.class.path");
        if (classpath != null && !classpath.isEmpty()) {
            final String[] split = classpath.split(System.getProperty("path.separator"));
            for (int length = split.length, i = 0; i < length; ++i) {
                final String pathStr = split[i];
                try {
                    final Path path = Path.of(pathStr, new String[0]);
                    if (Files.exists(path, new LinkOption[0])) {
                        urls.add(path.toUri().toURL());
                    }
                }
                catch (final Exception e) {
                    System.err.println("[EarlyPlugin] Failed to parse classpath entry: " + pathStr);
                }
            }
        }
        return urls.toArray(URL[]::new);
    }
}
