net.fabricmc.loader.util.UrlUtil Java Examples

The following examples show how to use net.fabricmc.loader.util.UrlUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OptifineSetup.java    From OptiFabric with Mozilla Public License 2.0 6 votes vote down vote up
static Optional<Path> getSource(ClassLoader loader, String filename) {
	URL url;
	if ((url = loader.getResource(filename)) != null) {
		try {
			URL urlSource = UrlUtil.getSource(filename, url);
			Path classSourceFile = UrlUtil.asPath(urlSource);

			return Optional.of(classSourceFile);
		} catch (UrlConversionException e) {
			// TODO: Point to a logger
			e.printStackTrace();
		}
	}

	return Optional.empty();
}
 
Example #2
Source File: GameProviderHelper.java    From fabric-loader with Apache License 2.0 6 votes vote down vote up
static Optional<Path> getSource(ClassLoader loader, String filename) {
	URL url;
	if ((url = loader.getResource(filename)) != null) {
		try {
			URL urlSource = UrlUtil.getSource(filename, url);
			Path classSourceFile = UrlUtil.asPath(urlSource);

			return Optional.of(classSourceFile);
		} catch (UrlConversionException e) {
			// TODO: Point to a logger
			e.printStackTrace();
		}
	}

	return Optional.empty();
}
 
Example #3
Source File: Knot.java    From fabric-loader with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<URL> getLoadTimeDependencies() {
	String cmdLineClasspath = System.getProperty("java.class.path");

	return Arrays.stream(cmdLineClasspath.split(File.pathSeparator)).filter((s) -> {
		if (s.equals("*") || s.endsWith(File.separator + "*")) {
			System.err.println("WARNING: Knot does not support wildcard classpath entries: " + s + " - the game may not load properly!");
			return false;
		} else {
			return true;
		}
	}).map((s) -> {
		File file = new File(s);
		if (!file.equals(gameJarFile)) {
			try {
				return (UrlUtil.asUrl(file));
			} catch (UrlConversionException e) {
				LOGGER.debug(e);
				return null;
			}
		} else {
			return null;
		}
	}).filter(Objects::nonNull).collect(Collectors.toSet());
}
 
Example #4
Source File: ModContainer.java    From fabric-loader with Apache License 2.0 6 votes vote down vote up
void setupRootPath() {
	if (root != null) {
		throw new RuntimeException("Not allowed to setup mod root path twice!");
	}

	try {
		Path holder = UrlUtil.asPath(originUrl).toAbsolutePath();
		if (Files.isDirectory(holder)) {
			root = holder.toAbsolutePath();
		} else /* JAR */ {
			FileSystemUtil.FileSystemDelegate delegate = FileSystemUtil.getJarFileSystem(holder, false);
			if (delegate.get() == null) {
				throw new RuntimeException("Could not open JAR file " + holder.getFileName() + " for NIO reading!");
			}

			root = delegate.get().getRootDirectories().iterator().next();

			// We never close here. It's fine. getJarFileSystem() will handle it gracefully, and so should mods
		}
	} catch (IOException | UrlConversionException e) {
		throw new RuntimeException("Failed to find root directory for mod '" + info.getId() + "'!", e);
	}
}
 
Example #5
Source File: OptifineSetup.java    From OptiFabric with Mozilla Public License 2.0 5 votes vote down vote up
List<Path> getLibs() {
	return fabricLauncher.getLoadTimeDependencies().stream().map(url -> {
		try {
			return UrlUtil.asPath(url);
		} catch (UrlConversionException e) {
			throw new RuntimeException(e);
		}
	}).filter(Files::exists).collect(Collectors.toList());
}
 
Example #6
Source File: ModClassLoader_125_FML.java    From fabric-loader with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}
 
Example #7
Source File: ModClassLoader_125_FML.java    From fabric-loader with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to find the Minecraft .JAR location.
 *
 * @return The "parent source" file.
 */
public File getParentSource() {
	try {
		return UrlUtil.asFile(UrlUtil.asUrl(FabricLauncherBase.minecraftJar));
	} catch (UrlConversionException e) {
		throw new RuntimeException(e);
	}
}
 
Example #8
Source File: FabricTweaker.java    From fabric-loader with Apache License 2.0 4 votes vote down vote up
@Override
public void injectIntoClassLoader(LaunchClassLoader launchClassLoader) {
	isDevelopment = Boolean.parseBoolean(System.getProperty("fabric.development", "false"));
	Launch.blackboard.put("fabric.development", isDevelopment);
	setProperties(Launch.blackboard);

	this.launchClassLoader = launchClassLoader;
	launchClassLoader.addClassLoaderExclusion("org.objectweb.asm.");
	launchClassLoader.addClassLoaderExclusion("org.spongepowered.asm.");
	launchClassLoader.addClassLoaderExclusion("net.fabricmc.loader.");

	launchClassLoader.addClassLoaderExclusion("net.fabricmc.api.Environment");
	launchClassLoader.addClassLoaderExclusion("net.fabricmc.api.EnvType");

	GameProvider provider = new MinecraftGameProvider();
	provider.acceptArguments(arguments);

	if (!provider.locateGame(getEnvironmentType(), launchClassLoader)) {
		throw new RuntimeException("Could not locate Minecraft: provider locate failed");
	}

	@SuppressWarnings("deprecation")
	FabricLoader loader = FabricLoader.INSTANCE;
	loader.setGameProvider(provider);
	loader.load();
	loader.freeze();

	launchClassLoader.registerTransformer("net.fabricmc.loader.launch.FabricClassTransformer");

	if (!isDevelopment) {
		// Obfuscated environment
		Launch.blackboard.put("fabric.development", false);
		try {
			String target = getLaunchTarget();
			URL loc = launchClassLoader.findResource(target.replace('.', '/') + ".class");
			JarURLConnection locConn = (JarURLConnection) loc.openConnection();
			File jarFile = UrlUtil.asFile(locConn.getJarFileURL());
			if (!jarFile.exists()) {
				throw new RuntimeException("Could not locate Minecraft: " + jarFile.getAbsolutePath() + " not found");
			}

			FabricLauncherBase.deobfuscate(provider.getGameId(), provider.getNormalizedGameVersion(), provider.getLaunchDirectory(), jarFile.toPath(), this);
		} catch (IOException | UrlConversionException e) {
			throw new RuntimeException("Failed to deobfuscate Minecraft!", e);
		}
	}

	FabricLoader.INSTANCE.getAccessWidener().loadFromMods();

	MinecraftGameProvider.TRANSFORMER.locateEntrypoints(this);

	// Setup Mixin environment
	MixinBootstrap.init();
	FabricMixinBootstrap.init(getEnvironmentType(), FabricLoader.INSTANCE);
	MixinEnvironment.getDefaultEnvironment().setSide(getEnvironmentType() == EnvType.CLIENT ? MixinEnvironment.Side.CLIENT : MixinEnvironment.Side.SERVER);

	EntrypointUtils.invoke("preLaunch", PreLaunchEntrypoint.class, PreLaunchEntrypoint::onPreLaunch);
}
 
Example #9
Source File: FabricServerLauncher.java    From fabric-loader with Apache License 2.0 4 votes vote down vote up
private static void setup(String... runArguments) throws IOException {
	// Pre-load "fabric-server-launcher.properties"
	File propertiesFile = new File("fabric-server-launcher.properties");
	Properties properties = new Properties();

	if (propertiesFile.exists()) {
		try (FileInputStream stream = new FileInputStream(propertiesFile)) {
			properties.load(stream);
		}
	}

	// Most popular Minecraft server hosting platforms do not allow
	// passing arbitrary arguments to the server .JAR. Meanwhile,
	// Mojang's default server filename is "server.jar" as of
	// a few versions... let's use this.
	if (!properties.containsKey("serverJar")) {
		properties.put("serverJar", "server.jar");
		try (FileOutputStream stream = new FileOutputStream(propertiesFile)) {
			properties.store(stream, null);
		}
	}

	File serverJar = new File((String) properties.get("serverJar"));

	if (!serverJar.exists()) {
		System.err.println("Could not find Minecraft server .JAR (" + properties.get("serverJar") + ")!");
		System.err.println();
		System.err.println("Fabric's server-side launcher expects the server .JAR to be provided.");
		System.err.println("You can edit its location in fabric-server-launcher.properties.");
		System.err.println();
		System.err.println("Without the official Minecraft server .JAR, Fabric Loader cannot launch.");
		throw new RuntimeException("Searched for '" + serverJar.getName() + "' but could not find it.");
	}

	System.setProperty("fabric.gameJarPath", serverJar.getAbsolutePath());
	try {
		URLClassLoader newClassLoader = new InjectingURLClassLoader(new URL[] { FabricServerLauncher.class.getProtectionDomain().getCodeSource().getLocation(), UrlUtil.asUrl(serverJar) }, parentLoader, "com.google.common.jimfs.");
		Thread.currentThread().setContextClassLoader(newClassLoader);
		launch(mainClass, newClassLoader, runArguments);
	} catch (Exception ex) {
		throw new RuntimeException(ex);
	}
}