Skip to content

Commit

Permalink
Add support for legacy Minecraft
Browse files Browse the repository at this point in the history
1.3.2 to 1.6.4 right now!
  • Loading branch information
ThexXTURBOXx committed Aug 7, 2023
1 parent cfe7418 commit 1233376
Show file tree
Hide file tree
Showing 33 changed files with 252 additions and 348 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ permissions:
contents: write
jobs:
build-and-deploy:
if: false # Disable for my fork!
concurrency: ci-${{ github.ref }}
runs-on: ubuntu-latest
steps:
Expand All @@ -16,4 +17,23 @@ jobs:
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: src/main/resources
folder: src/main/resources
build-jar:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'zulu'
cache: 'gradle'
- name: Build with Gradle
run: ./gradlew build
- name: Upload artifact
uses: actions/upload-artifact@v3
if: success()
with:
name: pipeblocker
path: legacy_forge/build/libs/pipeblocker-legacy-forge-*.jar
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ out/
*.ipr
*.iws
*.DS_Store
pipeblocker.jar
11 changes: 11 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ allprojects {
group = "info.mmpa.pipeblocker"
version = "1.2.0"

// Set the toolchain version to decouple the Java we run Gradle with from the Java used to compile and run the mod
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(7))
// Azul covers the most platforms for Java 7 toolchains, crucially including MacOS arm64
vendor.set(JvmVendorSpec.AZUL)
}
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}

val embed: Configuration by configurations.creating {
description = "Included in output JAR"
}
Expand Down
13 changes: 1 addition & 12 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@ plugins {
id("java")
}

// Set the toolchain version to decouple the Java we run Gradle with from the Java used to compile and run the mod
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
// Azul covers the most platforms for Java 8 toolchains, crucially including MacOS arm64
vendor.set(org.gradle.jvm.toolchain.JvmVendorSpec.AZUL)
}
}

repositories {
mavenCentral()
}

tasks.withType<Test> {
useJUnitPlatform()
}

// include the filter from the root src folder
Expand All @@ -30,6 +20,5 @@ dependencies {
testImplementation("org.apache.logging.log4j:log4j-api:2.0-beta9")
testImplementation("org.apache.logging.log4j:log4j-core:2.0-beta9")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
testImplementation("junit:junit:4.13.2")
}
52 changes: 38 additions & 14 deletions common/src/main/java/info/mmpa/pipeblocker/PipeBlocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
import java.net.URL;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class PipeBlocker {
private static PipeBlockerLogger LOGGER = new PipeBlockerStdoutLogger();

static {
PipeBlocker.chooseBestLogger();
}

private static final List<Pattern> allowedPatterns = new ArrayList<>();
private static final List<Pattern> rejectedPatterns = new ArrayList<>();
private static final List<Pattern> softAllowedPatterns = new ArrayList<>();
private static final HashMap<Class<?>, CheckStatus> cache = new HashMap<>();

private static final Set<Class<?>> REJECTED_CLASSES = Collections.synchronizedSet(new HashSet<>());
private static final Set<Class<?>> REJECTED_CLASSES = Collections.synchronizedSet(new HashSet<Class<?>>());

private static int numEntriesLoaded = 0;

Expand Down Expand Up @@ -73,8 +76,26 @@ private static void processFilter(InputStream filterStream) throws IOException {
}
}

public 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();
}

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

public static void useStdOut() {
Expand Down Expand Up @@ -122,15 +143,15 @@ private static void processLine(String line) {
}
}

private static Stream<Class<?>> inheritanceStream(Class<?> clz) {
private static Set<Class<?>> inheritanceStream(Class<?> clz) {
if(clz == null)
return Stream.empty();
Stream.Builder<Class<?>> streamBuilder = Stream.builder();
return Collections.emptySet();
Set<Class<?>> streamBuilder = new HashSet<>();
while(clz != null) {
streamBuilder.add(clz);
clz = clz.getSuperclass();
}
return streamBuilder.build();
return streamBuilder;
}

private static boolean isMatchingName(String name, List<Pattern> patterns) {
Expand All @@ -142,10 +163,16 @@ private static boolean isMatchingName(String name, List<Pattern> patterns) {
return false;
}

private static boolean checkPatterns(Class<?> clazz, List<Pattern> patterns) {
Set<Class<?>> set = inheritanceStream(clazz);
for (Class<?> c : set) if (PipeBlocker.isMatchingName(clazz.getCanonicalName(), patterns)) return true;
return false;
}

private static FilterMatchType matchClass(Class<?> clazz) {
if (inheritanceStream(clazz).map(Class::getCanonicalName).anyMatch(n -> PipeBlocker.isMatchingName(n, rejectedPatterns)))
if (checkPatterns(clazz, rejectedPatterns))
return FilterMatchType.REJECT;
if (inheritanceStream(clazz).map(Class::getCanonicalName).anyMatch(n -> PipeBlocker.isMatchingName(n, allowedPatterns)))
if (checkPatterns(clazz, allowedPatterns))
return FilterMatchType.ALLOW;
if (PipeBlocker.isMatchingName(clazz.getCanonicalName(), softAllowedPatterns))
return FilterMatchType.SOFT_ALLOW;
Expand Down Expand Up @@ -188,7 +215,6 @@ public static CheckStatus check(Class<?> clazz) {
return status;
}


public static void apply() {
if (initialized) {
throw new RuntimeException("PipeBlocker is already initialized!");
Expand All @@ -212,10 +238,8 @@ public static void apply() {
loadFilter();
String javaVersion = System.getProperties().getProperty("java.specification.version");
String className;
if ("1.8".equals(javaVersion)) {
className = "info.mmpa.pipeblocker.java8.FilterSetter";
} else if (javaVersion.chars().allMatch(Character::isDigit) && Integer.parseInt(javaVersion) > 8) {
className = "info.mmpa.pipeblocker.java9.FilterSetter";
if ("1.8".equals(javaVersion) || "1.7".equals(javaVersion)) {
className = "info.mmpa.pipeblocker.java7.FilterSetter";
} else {
System.err.println("Unsupported java version: " + javaVersion);
throw new RuntimeException("Unsupported java version: " + javaVersion);
Expand Down
24 changes: 24 additions & 0 deletions common/src/main/java/info/mmpa/pipeblocker/java7/FilterSetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package info.mmpa.pipeblocker.java7;

import info.mmpa.pipeblocker.PipeBlocker;
import sun.misc.ObjectInputFilter;

public class FilterSetter {
private static boolean applied = false;
public static void apply() {
if (!applied) {
ObjectInputFilter.Config.setSerialFilter(new ObjectInputFilter() {
@Override
public Status checkInput(FilterInfo 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;
}
}
}
21 changes: 0 additions & 21 deletions common/src/main/java/info/mmpa/pipeblocker/java8/FilterSetter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,4 @@ 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
@@ -1,17 +1,17 @@
package info.mmpa.pipeblocker.test;

import info.mmpa.pipeblocker.PipeBlocker;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.Assert.*;

public class ObjectStreamTest {
@BeforeAll
@BeforeClass
public static void applyFilter() {
PipeBlocker.apply();
}
Expand Down
8 changes: 0 additions & 8 deletions dummy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,3 @@
plugins {
id("java-library")
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
// Azul covers the most platforms for Java 8 toolchains, crucially including MacOS arm64
vendor.set(JvmVendorSpec.AZUL)
}
}
23 changes: 23 additions & 0 deletions dummy/src/main/java/cpw/mods/fml/common/Mod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cpw.mods.fml.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Mod {
String modid();

String name() default "";

String version() default "";

String acceptableRemoteVersions() default "";

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface EventHandler {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cpw.mods.fml.common.event;

public class FMLInitializationEvent {
public FMLInitializationEvent(Object... data) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.minecraft.launchwrapper;

public interface IClassTransformer {

byte[] transform(String name, String transformedName, byte[] basicClass);

}
47 changes: 0 additions & 47 deletions fabric/build.gradle.kts

This file was deleted.

Loading

0 comments on commit 1233376

Please sign in to comment.