package me.deftware.client.framework.main;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import me.deftware.client.framework.main.preprocessor.PreProcessorMan;
import me.deftware.client.framework.path.LocationUtil;
import me.deftware.client.framework.utils.HashUtils;
import me.deftware.client.framework.utils.WebUtils;
import net.fabricmc.loader.launch.knot.KnotClient;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * This class is only used when running in the fabric environment
 * Minecraft > 1.14
 */
public class Main {

    private static PreProcessorMan preProcessor;
    public static List<String> logging = new ArrayList<>();

    public static void main(String[] args) {
        File emcJar = LocationUtil.getEMC().toFile(), mcDir = LocationUtil.getMinecraftDir().toFile();
        System.setProperty("EMCDir", emcJar != null ? emcJar.getParentFile().getAbsolutePath() : "null");
        System.setProperty("MCDir", mcDir != null ? mcDir.getAbsolutePath() : "null");
        if (emcJar != null && mcDir != null) {
            validateOptiFine(emcJar.getParentFile().getAbsolutePath(), mcDir.getAbsolutePath());
            validateFilesSizes(emcJar.getParentFile());
        }
        System.setProperty("SUBSYSTEM", "true");
        preProcessor = new PreProcessorMan(emcJar);
        try {
            preProcessor.run();
        } catch (Throwable ignored) { }
        for (int i = 0; i < logging.size(); i++) {
            System.setProperty("logging" + i, logging.get(i));
        }
        KnotClient.main(args);
    }

    /**
     * Validates that files are not empty prior to launching
     */
    private static void validateFilesSizes(File path) {
        File[] files = path.listFiles();
        if (files == null) return;
        for (File file : files) {
            try {
                if (file.exists()) {
                    if (file.length() == 0) {
                        logging.add("Removed invalid fabric mod " + file.getName());
                        if (!file.delete()) {
                            // cant really do much now
                        }
                    }
                }
            } catch (Throwable ignored) {}
        }
    }

    private static void validateOptiFine(String emcPath, String mcPath) {
       try {
           if (emcPath.contains("latest-")) {
               String dataUrl = "https://gitlab.com/EMC-Framework/maven/raw/master/marketplace/plugins/optifine/versions.json";
               String mcVersion = emcPath.split("latest-")[1];
               File optifabricJar = new File(emcPath + File.separator + "optifabric.jar");
               File optifineModJar = new File(mcPath + File.separator + "mods" + File.separator + "optifine.jar");
               JsonObject json = new Gson().fromJson(WebUtils.get(dataUrl), JsonObject.class);
               if (json.has(mcVersion)) {
                   JsonObject data = json.get(mcVersion).getAsJsonObject();
                   if (data.get("available").getAsBoolean()) {
                       if (optifineModJar.exists()) {
                           if (!data.get("sha1").getAsString().equalsIgnoreCase(HashUtils.getSha1(optifineModJar))) {
                               optifineModJar.delete();
                               if (optifabricJar.exists()) {
                                   optifabricJar.delete();
                               }
                           }
                       } else if (optifabricJar.exists()) {
                           optifabricJar.delete();
                       }
                   }
               }
           }
       } catch (Exception ex) {
           ex.printStackTrace();
       }
    }

}

@@ -41,72 +41,8 @@
 n;%0A%0A
-import com.google.gson.Gson;%0Aimport com.google.gson.JsonObject;%0A
 impo
@@ -167,113 +167,8 @@
 il;%0A
-import me.deftware.client.framework.utils.HashUtils;%0Aimport me.deftware.client.framework.utils.WebUtils;%0A
 impo
@@ -407,57 +407,8 @@
  %7B%0A%0A
-    private static PreProcessorMan preProcessor;%0A
     
@@ -809,218 +809,8 @@
 %22);%0A
-        if (emcJar != null && mcDir != null) %7B%0A            validateOptiFine(emcJar.getParentFile().getAbsolutePath(), mcDir.getAbsolutePath());%0A            validateFilesSizes(emcJar.getParentFile());%0A        %7D%0A
     
@@ -854,32 +854,48 @@
 true%22);%0A        
+PreProcessorMan 
 preProcessor = n
@@ -1174,2146 +1174,6 @@
  %7D%0A%0A
-    /**%0A     * Validates that files are not empty prior to launching%0A     */%0A    private static void validateFilesSizes(File path) %7B%0A        File%5B%5D files = path.listFiles();%0A        if (files == null) return;%0A        for (File file : files) %7B%0A            try %7B%0A                if (file.exists()) %7B%0A                    if (file.length() == 0) %7B%0A                        logging.add(%22Removed invalid fabric mod %22 + file.getName());%0A                        if (!file.delete()) %7B%0A                            // cant really do much now%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D catch (Throwable ignored) %7B%7D%0A        %7D%0A    %7D%0A%0A    private static void validateOptiFine(String emcPath, String mcPath) %7B%0A       try %7B%0A           if (emcPath.contains(%22latest-%22)) %7B%0A               String dataUrl = %22https://gitlab.com/EMC-Framework/maven/raw/master/marketplace/plugins/optifine/versions.json%22;%0A               String mcVersion = emcPath.split(%22latest-%22)%5B1%5D;%0A               File optifabricJar = new File(emcPath + File.separator + %22optifabric.jar%22);%0A               File optifineModJar = new File(mcPath + File.separator + %22mods%22 + File.separator + %22optifine.jar%22);%0A               JsonObject json = new Gson().fromJson(WebUtils.get(dataUrl), JsonObject.class);%0A               if (json.has(mcVersion)) %7B%0A                   JsonObject data = json.get(mcVersion).getAsJsonObject();%0A                   if (data.get(%22available%22).getAsBoolean()) %7B%0A                       if (optifineModJar.exists()) %7B%0A                           if (!data.get(%22sha1%22).getAsString().equalsIgnoreCase(HashUtils.getSha1(optifineModJar))) %7B%0A                               optifineModJar.delete();%0A                               if (optifabricJar.exists()) %7B%0A                                   optifabricJar.delete();%0A                               %7D%0A                           %7D%0A                       %7D else if (optifabricJar.exists()) %7B%0A                           optifabricJar.delete();%0A                       %7D%0A                   %7D%0A               %7D%0A           %7D%0A       %7D catch (Exception ex) %7B%0A           ex.printStackTrace();%0A       %7D%0A    %7D%0A%0A
 %7D%0A

Remove optifine validation