Skip to content

Commit

Permalink
Add: remap task after build
Browse files Browse the repository at this point in the history
  • Loading branch information
xfl03 committed Jan 26, 2023
1 parent c076217 commit 48beebe
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 7 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions LocatorLoader/build.gradle → 2Loader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ dependencies {
implementation("org.ow2.asm:asm:9.3")
implementation("org.ow2.asm:asm-tree:9.3")
implementation("org.ow2.asm:asm-commons:9.3")
implementation "cpw.mods:modlauncher:8.1.3"
implementation "cpw.mods:modlauncher:10.0.8"
implementation "cpw.mods:securejarhandler:2.1.6"
implementation 'org.jetbrains:annotations:23.1.0'
implementation "org.apache.logging.log4j:log4j-api:2.19.0"
}

jar {
from('../Locator/build/libs/') {
from('../1Locator/build/libs/') {
include '*.jar'
}
from('../build/libs/') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public void initialize(IEnvironment environment) {
return Collections.singletonList(new CrashReportExtenderTransformer());
}

public void beginScanning(IEnvironment environment) {
//ModLauncher 8
//it will be renamed to beginScanning in Gradle Build
public void beginScanningLegacy(IEnvironment environment) {
}

private Path modPath;
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ This mod implemented a powerful ASM-based remapping and custom jar detection fea

Please use Java 17+ in development or building environment.

### Loading
**Build(Gradle `Task`) -> Loader(ModLauncher `ITransformerService`) -> Locator(ForgeSPI `IModLocator`) -> Base(Forge `Mod`)**

In every `->`(3 times), our mod use ASM to operate bytecode in class file.

### Build
```shell
./gradlew build
./gradlew clean build remap
```
Mod file is located at `LocatorLoader/build/libs`.
Mod file is located at `build/libs/MoreCrashInfo-x.x.x.jar`.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'eclipse'
id 'maven-publish'
id 'net.minecraftforge.gradle' version '5.1.+'
id 'morecrashinfo'
}

version = "2.3.0"
Expand Down
18 changes: 18 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id 'java'
}

repositories {
mavenCentral()
}

configurations.configureEach {
transitive = false
}

dependencies {
gradleApi()
implementation("org.ow2.asm:asm:9.3")
implementation("org.ow2.asm:asm-tree:9.3")
implementation("org.ow2.asm:asm-commons:9.3")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.xfl03.morecrashinfo.gradle;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class GradlePlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
//Is not root project
if (!project.getRootProject().equals(project)) {
return;
}

//Only apply to root project
project.getTasks().create("remap", RemapTask.class, task -> task.rootProject = project.getRootProject());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package me.xfl03.morecrashinfo.gradle;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.ClassRemapper;
import org.objectweb.asm.commons.Remapper;
import org.objectweb.asm.commons.SimpleRemapper;
import org.objectweb.asm.tree.ClassNode;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class JarRemapper {
private final Remapper remapper;

public JarRemapper() {
Map<String, String> mapping = new HashMap<>();
mapping.put("me/xfl03/morecrashinfo/modlauncher/TransformerService.beginScanningLegacy(Lcpw/mods/modlauncher/api/IEnvironment;)V", "beginScanning");
remapper = new SimpleRemapper(mapping);
}

public byte[] process(InputStream is) throws Exception {
ClassReader cr = new ClassReader(is);
ClassNode cn = new ClassNode();
ClassRemapper cv = new ClassRemapper(cn, remapper);
cr.accept(cv, 0);
ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
System.out.printf("Remapped %s\n", cn.name);
return cw.toByteArray();
}

public void processJar(Path inFile, Path outFile) {
try (ZipFile zf = new ZipFile(inFile.toFile())) {
ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(outFile));
for (Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements(); ) {
ZipEntry ze = e.nextElement();
String name = ze.getName();
zos.putNextEntry(new ZipEntry(name));
InputStream is = zf.getInputStream(ze);
if (name.endsWith(".class")) {
//Remap class file
zos.write(process(is));
} else {
//Copy other file
byte[] cache = new byte[1024];
int len;
while ((len = is.read(cache)) > 0) {
zos.write(cache, 0, len);
}
}
zos.closeEntry();
}
zos.close();
System.out.printf("Remapped %s to %s\n", inFile, outFile);
} catch (Exception e) {
System.err.printf("Error remapping jar %s to %s\n", inFile, outFile);
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.xfl03.morecrashinfo.gradle;

import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.util.Arrays;

public class RemapTask extends DefaultTask {
public Project rootProject;
public JarRemapper remapper = new JarRemapper();

@TaskAction
public void run() {
File loaderLibs = rootProject.file("2Loader/build/libs");
File[] files = loaderLibs.listFiles();
if (files == null) {
System.err.println("Cannot find build libs.");
return;
}
Arrays.stream(files).forEach(this::remap);
}

private void remap(File inFile) {
File rootLibs = rootProject.file("build/libs");
remapper.processJar(inFile.toPath(), rootLibs.toPath().resolve(inFile.getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implementation-class=me.xfl03.morecrashinfo.gradle.GradlePlugin
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pluginManagement {
}
}
//include "CrashMaker"
include "Locator"
include "LocatorLoader"
include "1Locator"
include "2Loader"

0 comments on commit 48beebe

Please sign in to comment.