Skip to content

Commit

Permalink
Only initialise JavaPatcher if it is actually being used
Browse files Browse the repository at this point in the history
  • Loading branch information
LunNova committed Sep 3, 2017
1 parent 87b923b commit 46b9976
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.minimallycorrect.modpatcher.api;

import LZMA.LzmaInputStream;
import lombok.SneakyThrows;
import lombok.val;
import net.minecraft.launchwrapper.IClassNameTransformer;
Expand All @@ -10,7 +9,6 @@
import java.io.*;
import java.lang.reflect.*;
import java.nio.file.FileSystem;
import java.nio.file.*;
import java.util.*;

public enum LaunchClassLoaderUtil {
Expand Down Expand Up @@ -192,7 +190,7 @@ public static void cacheSrgBytes(String transformedName, byte[] bytes) {

byte[] old = cachedSrgClasses.put(transformedName, bytes);
if (old != null && !Arrays.equals(bytes, old)) {
ModPatcherTransformer.pool.dropCache(transformedName, false);
((ClassLoaderPool) ModPatcherTransformer.getPatcher().getClassPool()).dropCache(transformedName, false);
if (shouldWarnInconsistentTransformation())
PatcherLog.warn(null, new Error("Inconsistent transformation results. Tried to cache different bytes for class " + transformedName + " to previous result after transformation."));
}
Expand Down Expand Up @@ -254,6 +252,6 @@ static void removeRedundantExclusions(Set<String> transformerExceptions) {

public static void releaseSrgBytes(String transformedName) {
cachedSrgClasses.remove(transformedName);
ModPatcherTransformer.pool.dropCache(transformedName, true);
((ClassLoaderPool) ModPatcherTransformer.getPatcher().getClassPool()).dropCache(transformedName, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import java.nio.file.*;

public class ModPatcherTransformer {
public static final ClassLoaderPool pool;
private static final String MOD_PATCHES_DIRECTORY = "./ModPatches/";
private static final Patcher patcher;
private static final String ALREADY_LOADED_PROPERTY_NAME = "nallar.ModPatcher.alreadyLoaded";
private static final String DUMP_PROPERTY_NAME = "nallar.ModPatcher.dump";
private static final boolean DUMP = !System.getProperty(DUMP_PROPERTY_NAME, "").isEmpty();
private static boolean classLoaderInitialised = false;
private static boolean haveTransformedClasses;
private static Patcher patcher;
private static MixinApplicator mixinApplicator;

static {
Expand All @@ -28,12 +28,10 @@ public class ModPatcherTransformer {
checkForMultipleClassLoads();

try {
patcher = new Patcher(pool = new ClassLoaderPool(), Patches.class, new MCPMappings());

// TODO - issue #2. Determine layout/config file structure
recursivelyAddXmlFiles(new File(MOD_PATCHES_DIRECTORY), patcher);
recursivelyAddXmlFiles(new File(MOD_PATCHES_DIRECTORY));
} catch (Throwable t) {
throw logError("Failed to create Patcher", t);
throw logError("Failed to load patches from " + MOD_PATCHES_DIRECTORY, t);
}
}

Expand All @@ -53,23 +51,29 @@ private static void checkForMultipleClassLoads() {
}

public static Patcher getPatcher() {
if (patcher == null) {
if (haveTransformedClasses)
throw new IllegalStateException("Too late to initialise patcher");
patcher = new Patcher(new ClassLoaderPool(), Patches.class, new MCPMappings());
}

return patcher;
}

@SuppressWarnings("deprecation")
private static void recursivelyAddXmlFiles(File directory, Patcher patcher) {
private static void recursivelyAddXmlFiles(File directory) {
File[] files = directory.listFiles();
if (files == null)
return;

try {
for (File f : files) {
if (f.isDirectory()) {
recursivelyAddXmlFiles(f, patcher);
recursivelyAddXmlFiles(f);
} else if (f.getName().endsWith(".xml")) {
patcher.readPatchesFromXmlInputStream(new FileInputStream(f));
getPatcher().readPatchesFromXmlInputStream(new FileInputStream(f));
} else if (f.getName().endsWith(".json")) {
patcher.readPatchesFromJsonInputStream(new FileInputStream(f));
getPatcher().readPatchesFromJsonInputStream(new FileInputStream(f));
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -105,6 +109,8 @@ static MixinApplicator getMixinApplicator() {
MixinApplicator mixinApplicator = ModPatcherTransformer.mixinApplicator;

if (mixinApplicator == null) {
if (haveTransformedClasses)
throw new IllegalStateException("Too late to initialise mixin applicator");
ModPatcherTransformer.mixinApplicator = mixinApplicator = new MixinApplicator();
mixinApplicator.setApplicationType(ApplicationType.FINAL_PATCH);
mixinApplicator.setNoMixinIsError(true);
Expand All @@ -116,7 +122,6 @@ static MixinApplicator getMixinApplicator() {

private static class ClassTransformer implements IClassTransformer {
static IClassTransformer INSTANCE = new ClassTransformer();
private boolean init;

private static void dumpIfEnabled(String name, byte[] data) {
if (!DUMP || !name.contains("net.minecraft"))
Expand All @@ -133,26 +138,25 @@ private static void dumpIfEnabled(String name, byte[] data) {

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (!init) {
init = true;
patcher.logDebugInfo();
}
haveTransformedClasses = true;

dumpIfEnabled(transformedName + "_unpatched", bytes);

final byte[] originalBytes = bytes;
val mixinApplicator = ModPatcherTransformer.mixinApplicator;
if (mixinApplicator != null) {
if (mixinApplicator != null)
bytes = mixinApplicator.getMixinTransformer().transformClass(() -> originalBytes, transformedName).get();
}

LaunchClassLoaderUtil.cacheSrgBytes(transformedName, bytes);
try {
bytes = patcher.patch(transformedName, bytes);
} catch (Throwable t) {
PatcherLog.error("Failed to patch " + transformedName, t);
} finally {
LaunchClassLoaderUtil.releaseSrgBytes(transformedName);
val patcher = ModPatcherTransformer.patcher;
if (patcher != null) {
LaunchClassLoaderUtil.cacheSrgBytes(transformedName, bytes);
try {
bytes = patcher.patch(transformedName, bytes);
} catch (Throwable t) {
PatcherLog.error("Failed to patch " + transformedName, t);
} finally {
LaunchClassLoaderUtil.releaseSrgBytes(transformedName);
}
}

if (originalBytes != bytes)
Expand Down

0 comments on commit 46b9976

Please sign in to comment.