Skip to content

Commit

Permalink
Fix a few legacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Aug 7, 2023
1 parent 433aa00 commit cfe7418
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 24 deletions.
15 changes: 14 additions & 1 deletion common/src/main/java/info/mmpa/pipeblocker/PipeBlocker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package info.mmpa.pipeblocker;

import info.mmpa.pipeblocker.logger.PipeBlockerJavaLogger;
import info.mmpa.pipeblocker.logger.PipeBlockerLog4jLogger;
import info.mmpa.pipeblocker.logger.PipeBlockerLogger;
import info.mmpa.pipeblocker.logger.PipeBlockerStdoutLogger;
Expand Down Expand Up @@ -72,13 +73,25 @@ private static void processFilter(InputStream filterStream) throws IOException {
}
}

public static void chooseBestLogger() {
LOGGER = PipeBlockerLogger.detectLogger();
}

public static void useStdOut() {
LOGGER = new PipeBlockerStdoutLogger();
}

public static void useLog4j() {
LOGGER = new PipeBlockerLog4jLogger();
}

public static void useJavaLogger() {
LOGGER = new PipeBlockerJavaLogger();
}

private static void processLine(String line) {
// ignore blank and comments
if(line.length() == 0 || line.charAt(0) == '#')
if(line.isEmpty() || line.charAt(0) == '#')
return;
// process glob lines
String type = null;
Expand Down
20 changes: 12 additions & 8 deletions common/src/main/java/info/mmpa/pipeblocker/java8/FilterSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import sun.misc.ObjectInputFilter;

public class FilterSetter {
private static boolean applied = false;
public static void apply() {
ObjectInputFilter.Config.setSerialFilter(filterInfo -> {
switch (PipeBlocker.check(filterInfo.serialClass())) {
case UNDECIDED: return sun.misc.ObjectInputFilter.Status.UNDECIDED;
case ALLOWED: return sun.misc.ObjectInputFilter.Status.ALLOWED;
case REJECTED: return sun.misc.ObjectInputFilter.Status.REJECTED;
}
throw new AssertionError("unknown check status");
});
if (!applied) {
ObjectInputFilter.Config.setSerialFilter(filterInfo -> {
switch (PipeBlocker.check(filterInfo.serialClass())) {
case UNDECIDED: return ObjectInputFilter.Status.UNDECIDED;
case ALLOWED: return ObjectInputFilter.Status.ALLOWED;
case REJECTED: return ObjectInputFilter.Status.REJECTED;
}
throw new AssertionError("unknown check status");
});
applied = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package info.mmpa.pipeblocker.logger;

import java.lang.reflect.Method;
import java.util.logging.Logger;

public class PipeBlockerJavaLogger implements PipeBlockerLogger {
private final Logger logger = Logger.getLogger("PipeBlocker");

public PipeBlockerJavaLogger() {
// Legacy FML logger injection
try {
Class<?> fmlLog = Class.forName("cpw.mods.fml.common.FMLLog");
Method getLogger = fmlLog.getMethod("getLogger");
getLogger.setAccessible(true);
logger.setParent((Logger) getLogger.invoke(null));
} catch (Throwable ignored) {
}
}

@Override
public void info(String msg) {
logger.info(msg);
}

@Override
public void error(String msg) {
logger.severe(msg);
}

@Override
public void debug(String msg) {
logger.finest(msg);
}

@Override
public void warn(String msg) {
logger.warning(msg);
}

@Override
public void fatal(String msg) {
logger.severe(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,22 @@ public interface PipeBlockerLogger {
void warn(String msg);
void fatal(String msg);
void error(String msg);

static PipeBlockerLogger detectLogger() {
try {
Class.forName("org.apache.logging.log4j.Logger");
Class.forName("org.apache.logging.log4j.LogManager");
return new PipeBlockerLog4jLogger();
} catch (ClassNotFoundException ignored) {
}

try {
Class.forName("java.util.logging.Logger");
return new PipeBlockerJavaLogger();
} catch (ClassNotFoundException ignored) {
}

// Should never reach here
return new PipeBlockerStdoutLogger();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class PipeBlockerInitializer implements PreLaunchEntrypoint {
@Override
public void onPreLaunch() {
PipeBlocker.useLog4j();
PipeBlocker.chooseBestLogger();
PipeBlocker.apply();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package info.mmpa.pipeblocker;

import info.mmpa.pipeblocker.logger.PipeBlockerLogger;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

@Mod(modid = "pipeblocker", name = "PipeBlocker", version = Tags.VERSION, acceptableRemoteVersions = "*")
public class PipeBlockerMod {

public static final Logger LOGGER = LogManager.getLogger("PipeBlocker");
public static final PipeBlockerLogger LOGGER = PipeBlockerLogger.detectLogger();

@Mod.EventHandler
@SuppressWarnings("unused")
public void init(FMLInitializationEvent ev) {
PipeBlocker.useLog4j();
PipeBlocker.chooseBestLogger();
}
}
16 changes: 10 additions & 6 deletions java9/src/main/java/info/mmpa/pipeblocker/java9/FilterSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import info.mmpa.pipeblocker.PipeBlocker;

public class FilterSetter {
public static void apply () {
ObjectInputFilter.Config.setSerialFilter(filterInfo -> {
switch (PipeBlocker.check(filterInfo.serialClass())) {
private static boolean applied = false;
public static void apply() {
if (!applied) {
ObjectInputFilter.Config.setSerialFilter(filterInfo -> {
switch (PipeBlocker.check(filterInfo.serialClass())) {
case UNDECIDED: return ObjectInputFilter.Status.UNDECIDED;
case ALLOWED: return ObjectInputFilter.Status.ALLOWED;
case REJECTED: return ObjectInputFilter.Status.REJECTED;
}
throw new AssertionError("unknown check status");
});
}
throw new AssertionError("unknown check status");
});
applied = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package info.mmpa.pipeblocker;

import org.apache.logging.log4j.*;

import java.lang.instrument.*;

public class PipeBlockerAgent {
public static void premain(String args, Instrumentation instrumentation){
PipeBlocker.chooseBestLogger();
PipeBlocker.apply();
System.out.println("PipeBlocker Java agent loaded.");
}
Expand Down

0 comments on commit cfe7418

Please sign in to comment.