/*
 * Copyright 2016 FabricMC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.fabricmc.loader.entrypoint.patches;

import net.fabricmc.loader.launch.common.FabricLauncherBase;
import net.fabricmc.loader.util.UrlConversionException;
import net.fabricmc.loader.util.UrlUtil;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * Wrapper class replacing pre-1.3 FML's ModClassLoader (which relies on
 * URLClassLoader implementation details - no longer applicable in Java 9+)
 * with an implementation effectively wrapping Knot.
 */
public class ModClassLoader_125_FML extends URLClassLoader {
	public ModClassLoader_125_FML() {
		super(new URL[0], FabricLauncherBase.getLauncher().getTargetClassLoader());
	}

	@Override
	protected void addURL(URL url) {
		FabricLauncherBase.getLauncher().propose(url);
	}

	/**
	 * This is used to add mods to the classpath.
	 * @param file The mod file.
	 * @throws MalformedURLException If the File->URL transformation fails.
	 */
	public void addFile(File file) throws MalformedURLException {
		try {
			addURL(UrlUtil.asUrl(file));
		} catch (UrlConversionException e) {
			throw new MalformedURLException(e.getMessage());
		}
	}

	/**
	 * This is used to find the Minecraft .JAR location.
	 *
	 * @return The "parent source" file.
	 */
	public File getParentSource() {
		return FabricLauncherBase.minecraftJar;
	}

	/**
	 * @return The "parent source" files array.
	 */
	public File[] getParentSources() {
		return new File[] { getParentSource() };
	}

	static {
		registerAsParallelCapable();
	}
}

@@ -815,16 +815,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -874,32 +874,32 @@
 edURLException;%0A
-
 import java.net.
@@ -934,16 +934,46 @@
 sLoader;
+%0Aimport java.util.Enumeration;
 %0A%0A/**%0A *
@@ -1237,16 +1237,43 @@
 oader %7B%0A
+%09private URL%5B%5D localUrls;%0A%0A
 %09public 
@@ -1377,16 +1377,42 @@
 der());%0A
+%09%09localUrls = new URL%5B0%5D;%0A
 %09%7D%0A%0A%09@Ov
@@ -1452,16 +1452,16 @@
  url) %7B%0A
-
 %09%09Fabric
@@ -1500,16 +1500,505 @@
 se(url);
+%0A%0A%09%09URL%5B%5D newLocalUrls = new URL%5BlocalUrls.length + 1%5D;%0A%09%09System.arraycopy(localUrls, 0, newLocalUrls, 0, localUrls.length);%0A%09%09newLocalUrls%5BlocalUrls.length%5D = url;%0A%09%09localUrls = newLocalUrls;%0A%09%7D%0A%0A%09@Override%0A%09public URL%5B%5D getURLs() %7B%0A%09%09return localUrls;%0A%09%7D%0A%0A%09@Override%0A%09public URL findResource(final String name) %7B%0A%09%09return getParent().getResource(name);%0A%09%7D%0A%0A%09@Override%0A%09public Enumeration%3CURL%3E findResources(final String name) throws IOException %7B%0A%09%09return getParent().getResources(name);
 %0A%09%7D%0A%0A%09/*

improve emulation