package net.acomputerdog.BlazeLoader.tweaklauncher;

import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import net.acomputerdog.BlazeLoader.util.BLLogger;
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;

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

public class TweakLauncher implements ITweaker {
    protected BLLogger logger = new BLLogger("BL_Launcher");
    protected List<String> tweaks = new ArrayList<String>();
    protected boolean hasInit = false;

    public TweakLauncher(){
        logger.logInfo("BL tweak loader starting.");
    }

    @Override
    public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
        hasInit = true;
        OptionParser parser = new OptionParser();
        parser.allowsUnrecognizedOptions();
        ArgumentAcceptingOptionSpec<String> optionSecondaryTweak = parser.accepts("secondaryTweaks", "Secondary tweak classes to be loaded after BL").withRequiredArg().ofType(String.class).withValuesSeparatedBy(',');
        OptionSet options = parser.parse(args.toArray(new String[args.size()]));
        if(options.has(optionSecondaryTweak)){
            logger.logInfo("Secondary tweaks detected.");
            tweaks = optionSecondaryTweak.values(options);
        }else{
            logger.logInfo("No secondary tweaks detected.");
        }
    }

    @Override
    public void injectIntoClassLoader(LaunchClassLoader classLoader) {
        if(hasInit){
            List<String> tweakList = (List<String>) Launch.blackboard.get("TweakClasses");
            if(tweakList != null){
                for(String tweak : tweaks){
                    try {
                        classLoader.addURL(new URL(tweak));
                        tweakList.add(tweak);
                    } catch (Exception e) {
                        logger.logError("Caught exception while injecting tweak: "+ tweak);
                        e.printStackTrace();
                    }
                }
            }else{
                logger.logFatal("tweakList is null!  Unable to inject secondary tweaks!");
            }
        }else{
            logger.logFatal("attempted to inject tweaks before scanning for other tweaks!");
        }
    }

    @Override
    /**
     * Get the main class.
     */
    public String getLaunchTarget() {
        return "net.acomputerdog.BlazeLoader.tweaklauncher.BLMain";
    }

    @Override
    public String[] getLaunchArguments() {
        return new String[0];
    }
}

@@ -154,16 +154,66 @@
 ionSet;%0A
+import net.acomputerdog.BlazeLoader.main.Version;%0A
 import n
@@ -251,24 +251,24 @@
 l.BLLogger;%0A
-
 import net.m
@@ -490,16 +490,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Apublic
@@ -723,16 +723,61 @@
 = false;
+%0A    protected File gameDir, assetDir = null;
 %0A%0A    pu
@@ -993,16 +993,83 @@
 = true;%0A
+        this.gameDir = gameDir;%0A        this.assetDir = assetsDir;%0A
         
@@ -1623,32 +1623,32 @@
 %0A        %7Delse%7B%0A
-
             logg
@@ -1694,32 +1694,566 @@
 d.%22);%0A        %7D%0A
+        Map%3CString, String%3E launchArgs = (Map%3CString, String%3E)Launch.blackboard.get(%22launchArgs%22);%0A        if (!launchArgs.containsKey(%22--version%22))%7B%0A            launchArgs.put(%22--version%22, Version.getMinecraftVersion());%0A        %7D%0A        if (!launchArgs.containsKey(%22--gameDir%22) && gameDir != null)%7B%0A            launchArgs.put(%22--gameDir%22, gameDir.getAbsolutePath());%0A        %7D%0A        if (!launchArgs.containsKey(%22--assetsDir%22) && assetDir != null)%7B%0A            launchArgs.put(%22--assetsDir%22, assetDir.getAbsolutePath());%0A        %7D%0A
     %7D%0A%0A    @Over

Update TweakLauncher to counteract bug with tweaker. Thanks to Mumfrey for providing LiteLoader tweaker source!