Skip to content

Commit c4bba0e

Browse files
committed
Auto-generate dependencies info resources (Close #41)
1 parent f9028fd commit c4bba0e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

loader/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ tasks.compileJava.dependsOn(":patching:jar")
9999

100100
File classDir = new File(project.rootProject.rootDir, "loader" + File.separator +
101101
"build" + File.separator + "classes" + File.separator + "java" + File.separator + "main")
102+
File resourcesDir = new File(project.rootProject.rootDir, "loader" + File.separator +
103+
"build" + File.separator + "resources" + File.separator + File.separator + "main")
102104

103105
tasks.register("transformFoxLoader", JavaExec) {
104106
classpath = project(":patching").sourceSets.main.runtimeClasspath
105107
mainClass = "com.fox2code.foxloader.patching.FoxLoaderPatches"
106-
args = [classDir.getAbsolutePath()]
108+
args = [classDir.getAbsolutePath(), resourcesDir.getAbsolutePath()]
107109
dependsOn(tasks.compileJava)
110+
dependsOn(tasks.processResources)
108111
}
109112

110113
tasks.classes.dependsOn(tasks.transformFoxLoader)

patching/src/main/java/com/fox2code/foxloader/patching/FoxLoaderPatches.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,34 @@
2323
*/
2424
package com.fox2code.foxloader.patching;
2525

26+
import com.fox2code.foxloader.dependencies.DependencyHelper;
27+
import com.fox2code.foxloader.launcher.BuildConfig;
2628
import org.objectweb.asm.ClassReader;
2729
import org.objectweb.asm.ClassWriter;
2830
import org.objectweb.asm.Opcodes;
2931
import org.objectweb.asm.tree.*;
3032

3133
import java.io.*;
3234
import java.nio.file.Files;
35+
import java.util.Locale;
3336

3437
// Allows us to do stuff java doesn't allow us to do.
3538
public final class FoxLoaderPatches implements Opcodes {
3639
private static final boolean DEBUG = false;
3740

3841
public static void main(String[] args) throws IOException {
3942
patchFoxLoaderClassesDir(new File(args[0]));
43+
patchFoxLoaderResourcesDir(new File(args[1]));
4044
}
4145

4246
public static void patchFoxLoaderClassesDir(File classesDir) throws IOException {
4347
patchFoxClassLoader(classesDir);
4448
}
4549

50+
public static void patchFoxLoaderResourcesDir(File resourcesDir) throws IOException {
51+
generateDependenciesResources(resourcesDir);
52+
}
53+
4654
private static void patchFoxClassLoader(File classesDir) throws IOException {
4755
File foxClassLoader = new File(classesDir, "com/fox2code/foxloader/launcher/FoxClassLoader.class");
4856
File source = foxClassLoader;
@@ -108,4 +116,33 @@ private static void patchEarlyEnum(File classesDir, String className) throws IOE
108116
TransformerUtils.checkBytecodeValidity(classData, true);
109117
Files.write(enumClassFile.toPath(), classData);
110118
}
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+
}
111148
}

0 commit comments

Comments
 (0)