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

package com.hypixel.hytale.common.util.java;

import com.hypixel.hytale.function.supplier.SupplierUtil;
import java.util.jar.Attributes;
import java.io.InputStream;
import java.util.Enumeration;
import java.net.URL;
import java.util.Objects;
import java.util.logging.Level;
import com.hypixel.hytale.logger.HytaleLogger;
import javax.annotation.Nullable;
import com.hypixel.hytale.common.semver.Semver;
import java.util.jar.Manifest;
import com.hypixel.hytale.function.supplier.CachedSupplier;

public class ManifestUtil
{
    public static final String VENDOR_ID_PROPERTY = "Implementation-Vendor-Id";
    public static final String VERSION_PROPERTY = "Implementation-Version";
    public static final String REVISION_ID_PROPERTY = "Implementation-Revision-Id";
    public static final String PATCHLINE_PROPERTY = "Implementation-Patchline";
    private static final CachedSupplier<Manifest> MANIFEST;
    private static final CachedSupplier<String> IMPLEMENTATION_VERSION;
    private static final CachedSupplier<String> IMPLEMENTATION_REVISION_ID;
    private static final CachedSupplier<String> IMPLEMENTATION_PATCHLINE;
    private static final CachedSupplier<Semver> VERSION;
    
    public static boolean isJar() {
        return ManifestUtil.MANIFEST.get() != null;
    }
    
    @Nullable
    public static Manifest getManifest() {
        return ManifestUtil.MANIFEST.get();
    }
    
    @Nullable
    public static String getImplementationVersion() {
        return ManifestUtil.IMPLEMENTATION_VERSION.get();
    }
    
    @Nullable
    public static Semver getVersion() {
        return ManifestUtil.VERSION.get();
    }
    
    @Nullable
    public static String getImplementationRevisionId() {
        return ManifestUtil.IMPLEMENTATION_REVISION_ID.get();
    }
    
    @Nullable
    public static String getPatchline() {
        return ManifestUtil.IMPLEMENTATION_PATCHLINE.get();
    }
    
    static {
        MANIFEST = SupplierUtil.cache(() -> {
            try {
                final ClassLoader cl = ManifestUtil.class.getClassLoader();
                final Enumeration<URL> enumeration = cl.getResources("META-INF/MANIFEST.MF");
                Manifest theManifest = null;
                while (enumeration.hasMoreElements()) {
                    final URL url = enumeration.nextElement();
                    Manifest possible;
                    try (final InputStream is = url.openStream()) {
                        possible = new Manifest(is);
                    }
                    final Attributes mainAttributes = possible.getMainAttributes();
                    final String vendorId = mainAttributes.getValue("Implementation-Vendor-Id");
                    if (vendorId != null && vendorId.equals("com.hypixel.hytale")) {
                        theManifest = possible;
                        break;
                    }
                }
                return theManifest;
            }
            catch (final Throwable t) {
                HytaleLogger.getLogger().at(Level.WARNING).log("Exception was thrown getting manifest!", t);
                return null;
            }
        });
        IMPLEMENTATION_VERSION = SupplierUtil.cache(() -> {
            try {
                final Manifest localManifest = ManifestUtil.MANIFEST.get();
                if (localManifest == null) {
                    return "NoJar";
                }
                else {
                    return (String)Objects.requireNonNull(localManifest.getMainAttributes().getValue("Implementation-Version"), "Null implementation version!");
                }
            }
            catch (final Throwable t2) {
                HytaleLogger.getLogger().at(Level.WARNING).log("Exception was thrown getting implementation version!", t2);
                return "UNKNOWN";
            }
        });
        IMPLEMENTATION_REVISION_ID = SupplierUtil.cache(() -> {
            try {
                final Manifest localManifest2 = ManifestUtil.MANIFEST.get();
                if (localManifest2 == null) {
                    return "NoJar";
                }
                else {
                    return (String)Objects.requireNonNull(localManifest2.getMainAttributes().getValue("Implementation-Revision-Id"), "Null implementation revision id!");
                }
            }
            catch (final Throwable t3) {
                HytaleLogger.getLogger().at(Level.WARNING).log("Exception was thrown getting implementation revision id!", t3);
                return "UNKNOWN";
            }
        });
        IMPLEMENTATION_PATCHLINE = SupplierUtil.cache(() -> {
            try {
                final Manifest localManifest3 = ManifestUtil.MANIFEST.get();
                if (localManifest3 == null) {
                    return "dev";
                }
                else {
                    final String value = localManifest3.getMainAttributes().getValue("Implementation-Patchline");
                    return (value != null && !value.isEmpty()) ? value : "dev";
                }
            }
            catch (final Throwable t4) {
                HytaleLogger.getLogger().at(Level.WARNING).log("Exception was thrown getting implementation patchline!", t4);
                return "dev";
            }
        });
        VERSION = SupplierUtil.cache(() -> {
            final String version = ManifestUtil.IMPLEMENTATION_VERSION.get();
            if ("NoJar".equals(version)) {
                return null;
            }
            else {
                return Semver.fromString(version);
            }
        });
    }
}
