Skip to content

Commit

Permalink
Add preliminary plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed May 29, 2023
1 parent 6b6600f commit 36bf998
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 107 deletions.
69 changes: 38 additions & 31 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,49 @@ plugins {
id 'checkstyle'
}

checkstyle {
configFile = file('checkstyle.xml')
}
allprojects {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
apply plugin: 'com.diffplug.spotless'
apply plugin: 'checkstyle'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

def ENV = System.getenv()
version += (ENV.GITHUB_ACTIONS ? '' : '+local')
repositories {
mavenCentral()
maven {
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
}

repositories {
mavenCentral()
maven {
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
checkstyle {
configFile = rootProject.file('checkstyle.xml')
}

spotless {
java {
licenseHeaderFile(rootProject.file('HEADER'))
}
}

java {
withSourcesJar()
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = 'UTF-8'

if (JavaVersion.current().isJava9Compatible()) {
it.options.release = 8
}
}
}

def ENV = System.getenv()
version += (ENV.GITHUB_ACTIONS ? '' : '+local')

configurations {
ship
implementation.extendsFrom ship
Expand All @@ -42,12 +67,6 @@ dependencies {
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junit_jupiter_version}"
}

spotless {
java {
licenseHeaderFile(rootProject.file('HEADER'))
}
}

jar {
manifest {
attributes 'Implementation-Title': 'Stitch',
Expand All @@ -57,22 +76,10 @@ jar {
}

shadowJar {
configurations = [project.configurations.ship]
configurations = [project.configurations.ship]
archiveClassifier = 'all'
}

java {
withSourcesJar()
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = 'UTF-8'

if (JavaVersion.current().isJava9Compatible()) {
it.options.release = 8
}
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
30 changes: 30 additions & 0 deletions minecraft-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
archivesBaseName = 'stitch-minecraft-plugin'
version = rootProject.version
group = rootProject.group

dependencies {
implementation project(':')
}

def ENV = System.getenv()

publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}

repositories {
if (ENV.MAVEN_URL) {
repositories.maven {
name 'fabric'
url ENV.MAVEN_URL
credentials {
username ENV.MAVEN_USERNAME
password ENV.MAVEN_PASSWORD
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.stitch.plugins;

import java.util.regex.Pattern;

import net.fabricmc.stitch.plugin.StitchPlugin;
import net.fabricmc.stitch.representation.ClassStorage;
import net.fabricmc.stitch.representation.JarClassEntry;
import net.fabricmc.stitch.representation.JarFieldEntry;
import net.fabricmc.stitch.representation.JarMethodEntry;

public class MinecraftPlugin implements StitchPlugin {
// Minecraft classes without a package are obfuscated.
private static Pattern classObfuscationPattern = Pattern.compile("^[^/]*$");

@Override
public int needsIntermediaryName(ClassStorage storage, JarClassEntry cls) {
return StitchPlugin.super.needsIntermediaryName(storage, cls) > 0
&& classObfuscationPattern.matcher(cls.getName()).matches() ? 2 : -2;
}

@Override
public int needsIntermediaryName(ClassStorage storage, JarClassEntry cls, JarFieldEntry fld) {
String name = fld.getName();

return name.length() <= 2 || (name.length() == 3 && name.charAt(2) == '_') ? 2 : -2;
}

@Override
public int needsIntermediaryName(ClassStorage storage, JarClassEntry cls, JarMethodEntry mth) {
String name = mth.getName();

return (name.length() <= 2 || (name.length() == 3 && name.charAt(2) == '_'))
&& StitchPlugin.super.needsIntermediaryName(storage, cls, mth) > 0 ? 2 : -2;
}

@Override
public String getIntermediaryTargetPackage() {
return "net/minecraft/";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
net.fabricmc.stitch.plugin.StitchPlugin
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pluginManagement {
}

rootProject.name = 'stitch'

include ':minecraft-plugin'
2 changes: 2 additions & 0 deletions src/main/java/net/fabricmc/stitch/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.fabricmc.stitch.commands.CommandRewriteIntermediary;
import net.fabricmc.stitch.commands.CommandUpdateIntermediary;
import net.fabricmc.stitch.commands.CommandValidateRecords;
import net.fabricmc.stitch.plugin.PluginLoader;

public class Main {
private static final Map<String, Command> COMMAND_MAP = new TreeMap<>();
Expand Down Expand Up @@ -72,6 +73,7 @@ public static void main(String[] args) {
System.arraycopy(args, 1, argsCommand, 0, argsCommand.length);
}

PluginLoader.loadPlugins();
COMMAND_MAP.get(args[0].toLowerCase(Locale.ROOT)).run(argsCommand);
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CommandGenerateIntermediary() {

@Override
public String getHelpString() {
return "<input-jar> <mapping-name> [-t|--target-namespace <namespace>] [-p|--obfuscation-pattern <regex pattern>]...";
return "<input-jar> <mapping-name> [-t|--target-namespace <namespace>] [--write-all]";
}

@Override
Expand All @@ -52,7 +52,6 @@ public void run(String[] args) throws Exception {
}

GenState state = new GenState();
boolean clearedPatterns = false;

for (int i = 2; i < args.length; i++) {
switch (args[i].toLowerCase(Locale.ROOT)) {
Expand All @@ -61,15 +60,8 @@ public void run(String[] args) throws Exception {
state.setTargetPackage(args[i + 1]);
i++;
break;
case "-p":
case "--obfuscation-pattern":
if (!clearedPatterns) {
state.clearObfuscatedPatterns();
}

clearedPatterns = true;
state.addObfuscatedPattern(args[i + 1]);
i++;
case "--write-all":
state.setWriteAll(true);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CommandRewriteIntermediary() {

@Override
public String getHelpString() {
return "<jar> <old-mapping-file> <new-mapping-file> [--writeAll]";
return "<jar> <old-mapping-file> <new-mapping-file> [--write-all]";
}

@Override
Expand All @@ -55,7 +55,7 @@ public void run(String[] args) throws Exception {

for (int i = 3; i < args.length; i++) {
switch (args[i].toLowerCase(Locale.ROOT)) {
case "--writeall":
case "--write-all":
state.setWriteAll(true);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CommandUpdateIntermediary() {

@Override
public String getHelpString() {
return "<old-jar> <new-jar> <old-mapping-file> <new-mapping-file> <match-file> [-t|--target-namespace <namespace>] [-p|--obfuscation-pattern <regex pattern>]";
return "<old-jar> <new-jar> <old-mapping-file> <new-mapping-file> <match-file> [-t|--target-namespace <namespace>] [--write-all]";
}

@Override
Expand Down Expand Up @@ -62,7 +62,6 @@ public void run(String[] args) throws Exception {
}

GenState state = new GenState();
boolean clearedPatterns = false;

for (int i = 5; i < args.length; i++) {
switch (args[i].toLowerCase(Locale.ROOT)) {
Expand All @@ -71,15 +70,8 @@ public void run(String[] args) throws Exception {
state.setTargetPackage(args[i + 1]);
i++;
break;
case "-p":
case "--obfuscation-pattern":
if (!clearedPatterns) {
state.clearObfuscatedPatterns();
}

clearedPatterns = true;
state.addObfuscatedPattern(args[i + 1]);
i++;
case "--write-all":
state.setWriteAll(true);
break;
}
}
Expand Down
Loading

0 comments on commit 36bf998

Please sign in to comment.