|
23 | 23 | */ |
24 | 24 | package com.fox2code.foxloader.patching; |
25 | 25 |
|
| 26 | +import com.fox2code.foxloader.dependencies.DependencyHelper; |
| 27 | +import com.fox2code.foxloader.launcher.BuildConfig; |
26 | 28 | import org.objectweb.asm.ClassReader; |
27 | 29 | import org.objectweb.asm.ClassWriter; |
28 | 30 | import org.objectweb.asm.Opcodes; |
29 | 31 | import org.objectweb.asm.tree.*; |
30 | 32 |
|
31 | 33 | import java.io.*; |
32 | 34 | import java.nio.file.Files; |
| 35 | +import java.util.Locale; |
33 | 36 |
|
34 | 37 | // Allows us to do stuff java doesn't allow us to do. |
35 | 38 | public final class FoxLoaderPatches implements Opcodes { |
36 | 39 | private static final boolean DEBUG = false; |
37 | 40 |
|
38 | 41 | public static void main(String[] args) throws IOException { |
39 | 42 | patchFoxLoaderClassesDir(new File(args[0])); |
| 43 | + patchFoxLoaderResourcesDir(new File(args[1])); |
40 | 44 | } |
41 | 45 |
|
42 | 46 | public static void patchFoxLoaderClassesDir(File classesDir) throws IOException { |
43 | 47 | patchFoxClassLoader(classesDir); |
44 | 48 | } |
45 | 49 |
|
| 50 | + public static void patchFoxLoaderResourcesDir(File resourcesDir) throws IOException { |
| 51 | + generateDependenciesResources(resourcesDir); |
| 52 | + } |
| 53 | + |
46 | 54 | private static void patchFoxClassLoader(File classesDir) throws IOException { |
47 | 55 | File foxClassLoader = new File(classesDir, "com/fox2code/foxloader/launcher/FoxClassLoader.class"); |
48 | 56 | File source = foxClassLoader; |
@@ -108,4 +116,33 @@ private static void patchEarlyEnum(File classesDir, String className) throws IOE |
108 | 116 | TransformerUtils.checkBytecodeValidity(classData, true); |
109 | 117 | Files.write(enumClassFile.toPath(), classData); |
110 | 118 | } |
| 119 | + |
| 120 | + private static void generateDependenciesResources(File resourcesDir) throws IOException { |
| 121 | + File metaInf = new File(resourcesDir, "META-INF"); |
| 122 | + if (!metaInf.isDirectory() && !metaInf.mkdirs()) { |
| 123 | + throw new IOException("Cannot create META-INF directory"); |
| 124 | + } |
| 125 | + generateDependenciesResource(new File(metaInf, "DEPENDENCIES"), |
| 126 | + DependencyHelper.commonDependencies); |
| 127 | + // Add modern dependencies for Unimined support |
| 128 | + generateDependenciesResource(new File(metaInf, "DEPENDENCIES_MODERN"), |
| 129 | + DependencyHelper.commonDependenciesModernJava); |
| 130 | + // Also add modern dependencies for dependency bundle feature on Unimined |
| 131 | + for (String dependencyBundle : DependencyHelper.availableDependencyBundles) { |
| 132 | + generateDependenciesResource( |
| 133 | + new File(metaInf, "DEPENDENCIES_BUNDLE_" + |
| 134 | + (dependencyBundle.toUpperCase(Locale.ROOT))), |
| 135 | + DependencyHelper.getDependencyBundle(dependencyBundle)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static void generateDependenciesResource( |
| 140 | + File file, DependencyHelper.Dependency[] dependencies) throws IOException { |
| 141 | + try (PrintStream printStream = new PrintStream(file, "UTF-8")) { |
| 142 | + printStream.println("# Autogenerated by FoxLoader " + BuildConfig.FOXLOADER_VERSION); |
| 143 | + for (DependencyHelper.Dependency dependency : dependencies) { |
| 144 | + printStream.println(dependency.javaSupport + " " + dependency.name); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
111 | 148 | } |
0 commit comments