aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/loader/JARLoadingTweaker.java
blob: abd4712b8672db389f6bd77478b6e55b00f1de13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2024 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.loader;

import lombok.SneakyThrows;
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * Tweaker used to load library JARs conditionally. See the subclasses for concrete implementations.
 */
public abstract class JARLoadingTweaker implements ITweaker {

	/**
	 * @return a path pointing to a folder filled with JAR files
	 */
	protected abstract Path getFilesToLoad();

	protected abstract String getTestClass();

	@SneakyThrows
	protected @Nullable Path getShadowedElement(String path) {
		URI uri = Objects.requireNonNull(getClass().getResource(path)).toURI();
		if ("jar".equals(uri.getScheme())) {
			FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap());
			closeFileSystemLater(fs);
			return fs.getPath(path);
		} else {
			return Paths.get(uri);
		}
	}

	private List<FileSystem> toClose = new ArrayList<>();

	protected void closeFileSystemLater(FileSystem fileSystem) {
		toClose.add(fileSystem);
	}

	@Override
	public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {

	}

	protected void performLoading(LaunchClassLoader classLoader) {
		try {
			if (Launch.blackboard.get("fml.deobfuscatedEnvironment") == Boolean.TRUE) {
				System.out.println("Skipping JAR loading in development environment.");
				return;
			}

			Path p = getFilesToLoad();

			System.out.println("Loading a JAR from " + p.toAbsolutePath());
			Path tempDirectory = Files.createTempDirectory("notenoughupdates-extracted-" + getClass().getSimpleName());
			System.out.println("Using temporary directory " + tempDirectory + " to store extracted jars.");
			tempDirectory.toFile().deleteOnExit();
			try (Stream<Path> libraries = Files.walk(p, 1)) {
				libraries.filter(it -> it.getFileName().toString().toLowerCase(Locale.ROOT).endsWith(".jar"))
								 .forEach(it -> {
									 try {
										 Path extractedPath = tempDirectory.resolve(it.getFileName().toString());
										 extractedPath.toFile().deleteOnExit();
										 Files.copy(it, extractedPath);
										 ClassLoaderExtUtil.addClassSourceTwice(classLoader, extractedPath.toUri().toURL());
									 } catch (Exception e) {
										 throw new RuntimeException(e);
									 }
								 });
			}
			classLoader.loadClass(getTestClass());
			System.out.println("Could successfully load a class from loaded library.");
		} catch (Throwable e) {
			System.err.println("Failed to load a JAR into NEU. This is most likely a bad thing.");
			e.printStackTrace();
		} finally {
			Iterator<FileSystem> iterator = toClose.iterator();
			while (iterator.hasNext())
				try {
					iterator.next().close();
				} catch (Throwable e) {
					e.printStackTrace();
				} finally {
					iterator.remove();
				}
		}

	}

	@Override
	public String getLaunchTarget() {
		return null;
	}

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