Skip to content

Commit

Permalink
feat: handle variables with numerical suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus8448 committed Jun 4, 2024
1 parent d788811 commit 4b13f2a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
uses: actions/checkout@v4

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@v2
uses: gradle/actions/wrapper-validation@v3

- name: Setup JDK 17
uses: actions/setup-java@v4
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# Mojarn
Mixes official and file (yarn) mappings for better parameter names.

## Usage
To install mojarn, add the galacticraft repository to your `settings.gradle.kts` file:
```kotlin
pluginManagement {
repositories {
maven("https://maven.galacticraft.dev/repository/maven-releases/")
}
}
```

Then, add the following to your `build.gradle.kts` file:
```kotlin
plugins {
id("dev.galacticraft.mojarn") version("0.2.0+7")
}
```

Finally, use the mappings in your project:
```kotlin
dependencies {
mappings(mojarn.mappings("net.fabricmc:yarn:$minecraft+build.$yarn:v2"))
}
```
16 changes: 8 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ val mappingIoVersion = project.property("mapping-io.version").toString()
val junitVersion = project.property("junit.version").toString()

group = pluginGroup
version = buildString {
append(pluginVersion)
append('.')
append(System.getenv("GITHUB_RUN_NUMBER") ?: "0")
}
version = "$pluginVersion+${System.getenv("GITHUB_RUN_NUMBER") ?: "0"}"

description = pluginDescription

base.archivesName.set(pluginId)
Expand All @@ -56,11 +53,14 @@ java {
}

repositories {
mavenCentral()

maven("https://maven.fabricmc.net") {
name = "Fabric"
content {
includeGroup("net.fabricmc")
}
}

mavenCentral()
}

dependencies {
Expand Down Expand Up @@ -103,7 +103,7 @@ gradlePlugin {
id = "${pluginGroup}.${pluginId}"
displayName = pluginName
description = pluginDescription
implementationClass = "dev.galacticraft.mojarn.api.MojarnPlugin"
implementationClass = "dev.galacticraft.mojarn.impl.MojarnPlugin"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ plugin.group=dev.galacticraft
plugin.name=Mojarn
plugin.id=mojarn
plugin.description=Mixes official and yarn mappings for better argument names
plugin.version=0.1
plugin.version=0.2.0

loom.version=1.6.5
mapping-io.version=0.5.1
loom.version=1.6.12
mapping-io.version=0.6.1
junit.version=5.10.2
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=544c35d6bd849ae8a5ed0bcea39ba677dc40f49df7d1835561582da2009b961d
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionSha256Sum=a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package dev.galacticraft.mojarn.api;

import net.fabricmc.loom.api.mappings.layered.spec.FileMappingsSpecBuilder;

/**
* A builder for a Mojarn mappings spec.
*/
Expand Down Expand Up @@ -57,6 +59,7 @@ public interface MojarnMappingsSpecBuilder {
/**
* Whether the mapping file is an Enigma mappings file (forwarded to file mapping builder).
* @return This builder
* @see FileMappingsSpecBuilder#enigmaMappings()
*/
MojarnMappingsSpecBuilder fileIsEnigma();
}
73 changes: 44 additions & 29 deletions src/main/java/dev/galacticraft/mojarn/impl/MojarnMappingsLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,36 +155,45 @@ public void visit(MappingVisitor mappingVisitor) throws IOException {
}

String typeName = typeClass.getDstName(named);
if (yarn2official.containsKey(typeName)) {
// check if the argument name is the same as the type name
if (argName.equalsIgnoreCase(typeName)) {
String offDst = yarn2official.get(typeName);
argName = Character.toLowerCase(offDst.charAt(0)) + offDst.substring(1);
} else if (this.partialMatch) {
// check if the argument name contains part of the type name
boolean success = false;
// split the name ("CamelCase" -> ["Camel", "Case"])
for (String s : PATTERN.split(typeName)) {
if (s.equalsIgnoreCase(argName)) {
success = true;
String offDst = yarn2official.get(typeName);
argName = Character.toLowerCase(offDst.charAt(0)) + offDst.substring(1);
break;

if (typeName != null) {
// check if class ends in numeric suffix
if (!Character.isDigit(typeName.charAt(typeName.length() - 1))) {
// strip numeric suffix on argument (if it exists)
while (Character.isDigit(argName.charAt(argName.length() - 1))) {
argName = argName.substring(0, argName.length() - 1);
}
}

if (yarn2official.containsKey(typeName)) {
// check if the argument name is the same as the type name
if (argName.equalsIgnoreCase(typeName)) {
String offDst = yarn2official.get(typeName);
argName = Character.toLowerCase(offDst.charAt(0)) + offDst.substring(1);
} else if (this.partialMatch) {
// check if the argument name contains part of the type name
boolean success = false;
// split the name ("CamelCase" -> ["Camel", "Case"])
for (String s : PATTERN.split(typeName)) {
if (s.equalsIgnoreCase(argName)) {
success = true;
String offDst = yarn2official.get(typeName);
argName = Character.toLowerCase(offDst.charAt(0)) + offDst.substring(1);
break;
}
}
if (!success && this.skipDifferent) continue;
} else if (this.skipDifferent) {
continue;
}
if (!success && this.skipDifferent) continue;
} else if (this.skipDifferent) {
continue;
}
}
}
}

// avoid duplicate names
if (!names.containsKey(argName)) {
names.put(argName, 1);
} else {
argName += names.get(argName);
int dup = names.merge(argName, 0, (s, k) -> k+1);
if (dup != 0) {
names.put(argName, names.get(argName) + 1);
}

Expand All @@ -199,14 +208,20 @@ public void visit(MappingVisitor mappingVisitor) throws IOException {
// no type data, so it is just copied verbatim
if (this.mapVariables) {
for (MappingTree.MethodVarMapping var : yarnMethod.getVars()) {
String paramName = var.getDstName(named);
String varName = var.getDstName(named);
if (varName != null) {
// strip numeric suffix on variable (if it exists)
while (Character.isDigit(varName.charAt(varName.length() - 1))) {
varName = varName.substring(0, varName.length() - 1);
}

if (names.containsKey(paramName)) {
paramName += names.get(paramName);
}
if (names.containsKey(varName)) {
varName += names.merge(varName, 1, (s, k) -> k + 1);
}

mappingVisitor.visitMethodVar(var.getLvtRowIndex(), var.getLvIndex(), var.getStartOpIdx(), var.getEndOpIdx(), null);
mappingVisitor.visitDstName(MappedElementKind.METHOD_VAR, 0, paramName);
mappingVisitor.visitMethodVar(var.getLvtRowIndex(), var.getLvIndex(), var.getStartOpIdx(), var.getEndOpIdx(), null);
mappingVisitor.visitDstName(MappedElementKind.METHOD_VAR, 0, varName);
}
}
}
}
Expand All @@ -220,7 +235,7 @@ public void visit(MappingVisitor mappingVisitor) throws IOException {

@Nullable
private static String getClassName(@Nullable String fullName) {
return fullName == null ? null : fullName.substring(Math.max(fullName.lastIndexOf('/') + 1, fullName.lastIndexOf('$') + 1));
return fullName == null ? null : fullName.substring(Math.max(fullName.lastIndexOf('/'), fullName.lastIndexOf('$')) + 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
* SOFTWARE.
*/

package dev.galacticraft.mojarn.api;
package dev.galacticraft.mojarn.impl;

import dev.galacticraft.mojarn.impl.MojarnExtensionImpl;
import dev.galacticraft.mojarn.api.MojarnExtension;
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand Down

0 comments on commit 4b13f2a

Please sign in to comment.