Skip to content

Commit 0c4a2e2

Browse files
committed
Bug fixes
- Fixed class mappings not being created - Fixed libraries being included both in plugin zip and Knit jar - Improved build.gradle and plugin.xml - Fixed renaming constructor parameters not working - Added support for classes in the default package - Improved instructions
1 parent 86f9d5f commit 0c4a2e2

File tree

5 files changed

+104
-41
lines changed

5 files changed

+104
-41
lines changed

build.gradle

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.dimdev'
7-
version '1.0.1'
7+
version '1.0.2'
88

99
sourceCompatibility = 1.8
1010

@@ -21,10 +21,14 @@ dependencies {
2121
compile 'cuchaz:enigma:0.12.2.62:lib'
2222
}
2323

24-
jar {
25-
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
26-
}
27-
2824
intellij {
2925
version '2018.3'
3026
}
27+
28+
processResources {
29+
filesMatching('plugin.xml') {
30+
expand 'version': project.version
31+
}
32+
33+
inputs.property 'version', project.version
34+
}

src/main/java/org/dimdev/knit/KnitRefactoringListenerProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RefactoringElementListener getListener(PsiElement element) {
2222
if (element instanceof PsiClass) {
2323
oldName = RenameHandler.getClassName((PsiClass) element);
2424
} else if (element instanceof PsiMethod) {
25-
oldName = ((PsiMethod) element).getName();
25+
oldName = ((PsiMethod) element).isConstructor() ? "<init>" : ((PsiMethod) element).getName();
2626
} else if (element instanceof PsiField) {
2727
oldName = ((PsiField) element).getName();
2828
} else {

src/main/java/org/dimdev/knit/MappingsService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,4 @@ public void clearMappings() {
5252
public boolean hasMappings() {
5353
return mappings != null;
5454
}
55-
56-
static { // TODO: this should probably go somewhere else
57-
58-
}
5955
}

src/main/java/org/dimdev/knit/RenameHandler.java

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import com.intellij.openapi.components.ServiceManager;
66
import com.intellij.psi.*;
77
import cuchaz.enigma.mapping.*;
8+
import cuchaz.enigma.throwables.MappingConflict;
89

910
public class RenameHandler {
11+
public static final String NO_PACKAGE_PREFIX = "nopackage/";
12+
public static final int MAX_OBFUSCATED_NAME_LENGTH = 3;
1013
private final MappingsService mappingsService = ServiceManager.getService(MappingsService.class);
1114

1215
public void handleRename(String oldName, PsiElement element) {
@@ -15,49 +18,61 @@ public void handleRename(String oldName, PsiElement element) {
1518
}
1619

1720
if (element instanceof PsiClass) {
18-
ClassMapping mapping = getClassMapping(oldName);
21+
PsiClass clazz = (PsiClass) element;
22+
23+
ClassMapping mapping = getOrCreateClassMapping(oldName);
1924
if (mapping != null) {
20-
mappingsService.getMappings().setClassDeobfName(mapping, getClassName((PsiClass) element));
25+
mappingsService.getMappings().setClassDeobfName(mapping, getClassName(clazz));
2126
}
2227
}
2328

2429
if (element instanceof PsiField) {
25-
ClassMapping classMapping = getClassMapping(getClassName(((PsiField) element).getContainingClass()));
26-
FieldMapping fieldMapping = getOrCreateFieldMapping((PsiField) element, oldName);
27-
classMapping.setFieldName(fieldMapping.getObfName(), fieldMapping.getObfDesc(), ((PsiField) element).getName());
30+
PsiField field = (PsiField) element;
31+
32+
ClassMapping classMapping = getOrCreateClassMapping(getClassName(field.getContainingClass()));
33+
FieldMapping fieldMapping = getOrCreateFieldMapping(field, oldName);
34+
classMapping.setFieldName(fieldMapping.getObfName(), fieldMapping.getObfDesc(), field.getName());
2835
}
2936

3037
if (element instanceof PsiMethod) {
31-
ClassMapping classMapping = getClassMapping(getClassName(((PsiMethod) element).getContainingClass()));
32-
MethodMapping methodMapping = getOrCreateMethodMapping((PsiMethod) element, oldName);
33-
classMapping.setMethodName(methodMapping.getObfName(), methodMapping.getObfDesc(), ((PsiMethod) element).getName());
38+
PsiMethod method = (PsiMethod) element;
39+
40+
if (method.isConstructor()) {
41+
return;
42+
}
43+
44+
ClassMapping classMapping = getOrCreateClassMapping(getClassName(method.getContainingClass()));
45+
MethodMapping methodMapping = getOrCreateMethodMapping(method, oldName);
46+
classMapping.setMethodName(methodMapping.getObfName(), methodMapping.getObfDesc(), method.getName());
3447
}
3548

3649
if (element instanceof PsiParameter) {
37-
PsiElement declarationScope = ((PsiParameter) element).getDeclarationScope();
50+
PsiParameter parameter = (PsiParameter) element;
51+
52+
PsiElement declarationScope = parameter.getDeclarationScope();
3853
if (declarationScope instanceof PsiMethod) {
3954
MethodMapping mapping = getOrCreateMethodMapping((PsiMethod) declarationScope, ((PsiMethod) declarationScope).getName());
4055

4156
if (mapping != null) {
4257
int index = ((PsiMethod) declarationScope).hasModifier(JvmModifier.STATIC) ? 0 : 1;
4358
boolean found = false;
44-
for (PsiParameter parameter : ((PsiMethod) declarationScope).getParameterList().getParameters()) {
45-
if (parameter.equals(element)) {
59+
for (PsiParameter currentParameter : ((PsiMethod) declarationScope).getParameterList().getParameters()) {
60+
if (currentParameter.equals(parameter)) {
4661
found = true;
4762
break;
4863
}
49-
index += parameter.getType().equals(PsiType.LONG) || parameter.getType().equals(PsiType.DOUBLE) ? 2 : 1;
64+
index += currentParameter.getType().equals(PsiType.LONG) || currentParameter.getType().equals(PsiType.DOUBLE) ? 2 : 1;
5065
}
5166

5267
if (found) {
53-
mapping.setLocalVariableName(index, ((PsiParameter) element).getName());
68+
mapping.setLocalVariableName(index, parameter.getName());
5469
}
5570
}
5671
}
5772
}
5873
}
5974

60-
private ClassMapping getClassMapping(String className) {
75+
private ClassMapping getOrCreateClassMapping(String className) {
6176
// Try getting deobfuscated class mapping
6277
ClassMapping mapping = mappingsService.getMappings().getClassByDeobf(className);
6378

@@ -66,12 +81,27 @@ private ClassMapping getClassMapping(String className) {
6681
mapping = mappingsService.getMappings().getClassByObf(className);
6782
}
6883

84+
// If that doesn't work either, create a new class mapping
85+
if (mapping == null && className.length() <= MAX_OBFUSCATED_NAME_LENGTH) {
86+
try {
87+
mapping = new ClassMapping(className);
88+
mappingsService.getMappings().addClassMapping(mapping);
89+
} catch (MappingConflict e) {
90+
throw new IllegalStateException("Both getClassByDeobf and getClassByObf returned null yet addClassMapping " +
91+
"threw a MappingConflict exception", e);
92+
}
93+
}
94+
6995
return mapping;
7096
}
7197

7298
private MethodMapping getOrCreateMethodMapping(PsiMethod method, String actualName) {
99+
if (method.isConstructor()) {
100+
actualName = "<init>";
101+
}
102+
73103
// Get mapping for containing class
74-
ClassMapping classMapping = getClassMapping(getClassName(method.getContainingClass()));
104+
ClassMapping classMapping = getOrCreateClassMapping(getClassName(method.getContainingClass()));
75105
if (classMapping == null) {
76106
return null;
77107
}
@@ -86,7 +116,7 @@ private MethodMapping getOrCreateMethodMapping(PsiMethod method, String actualNa
86116
}
87117

88118
// If that doesn't work either, create a new method mapping
89-
if (mapping == null) {
119+
if (mapping == null && actualName.length() <= MAX_OBFUSCATED_NAME_LENGTH) {
90120
mapping = new MethodMapping(actualName, descriptor);
91121
classMapping.addMethodMapping(mapping);
92122
}
@@ -96,7 +126,7 @@ private MethodMapping getOrCreateMethodMapping(PsiMethod method, String actualNa
96126

97127
private FieldMapping getOrCreateFieldMapping(PsiField field, String actualName) {
98128
// Get mapping for containing class
99-
ClassMapping classMapping = getClassMapping(getClassName(field.getContainingClass()));
129+
ClassMapping classMapping = getOrCreateClassMapping(getClassName(field.getContainingClass()));
100130
if (classMapping == null) {
101131
return null;
102132
}
@@ -111,7 +141,7 @@ private FieldMapping getOrCreateFieldMapping(PsiField field, String actualName)
111141
}
112142

113143
// If that doesn't work either, create a new field mapping
114-
if (mapping == null) {
144+
if (mapping == null && actualName.length() <= MAX_OBFUSCATED_NAME_LENGTH) {
115145
mapping = new FieldMapping(actualName, descriptor, actualName, Mappings.EntryModifier.UNCHANGED);
116146
classMapping.addFieldMapping(mapping);
117147
}
@@ -134,7 +164,12 @@ public TypeDescriptor obfuscateDescriptor(TypeDescriptor descriptor) {
134164
}
135165

136166
public static String getClassName(PsiClass element) {
137-
return JVMNameUtil.getClassVMName(element).replace('.', '/');
167+
String className = JVMNameUtil.getClassVMName(element).replace('.', '/');
168+
169+
if (className.startsWith(NO_PACKAGE_PREFIX)) {
170+
className = className.substring(NO_PACKAGE_PREFIX.length());
171+
}
172+
return className;
138173
}
139174

140175
public static String getDescriptor(PsiType type) {

src/main/resources/META-INF/plugin.xml

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
11
<idea-plugin>
22
<id>org.dimdev.knit</id>
33
<name>Knit</name>
4-
<version>1.0</version>
4+
<version>${version}</version>
55
<vendor url="https://www.dimdev.org">Dimensional Development</vendor>
66

77
<description>
88
<![CDATA[
9-
<p>Adds support for editing Enigma mappings directly in IntelliJ.<br/></p>
9+
<p>Adds support for editing Enigma mappings directly in IntelliJ.</p>
1010
11-
<b>Usage:</b>
11+
<h2>Instructions</h2>
12+
13+
<h3>Setting up Minecraft sources</h3>
1214
<ol>
13-
<li>Set up a gradle project with this build.gradle (last updated for 18w50a): https://paste.dimdev.org/raw/yoxamudixi</li>
15+
<li>Set up a gradle project with <a href="https://paste.dimdev.org/raw/yoxamudixi">this build.gradle (last updated for 18w50a)</a>.</li>
1416
<li>Export source code from enigma to src/main/java</li>
1517
<li>Open the project in IntelliJ with the plugin installed</li>
16-
<li>Click 'Refactor > Enable Remapping' and select the Enigma 'mappings' folder</li>
18+
<li>Move all classes in the default package to a new package named "nopackage" (can't be named something else)</li>
19+
<li>Do a regex find-replace (Ctrl-Shift-R, check "Regex") and replace "\n\nimport" with "\n\nimport nopackage;\nimport"</li>
20+
</ol>
21+
22+
<h3>Enabling remapping</h3>
23+
<ol>
24+
<li>Click "Refactor | Enable Remapping" and select the Enigma "mappings" folder</li>
1725
<li>Rename classes, fields, methods, and parameters through Shift-F6</li>
18-
<li>Save every once in a while using 'Refactor > Save Mappings'</li>
26+
<li>Save every once in a while using "Refactor | Save Mappings"</li>
1927
</ol>
2028
21-
<p><i>Note:</i> You may want to disable IntelliJ's "search and strings and comments" feature. It slows things down
22-
and finds lots of false matches when renaming one letter names.</p>
29+
<p><i>Note 1:</i> You may want to disable IntelliJ's "search and strings and comments" feature since it can lead to false matches
30+
when renaming obfuscated names. To do this, you can press Shift-F6 twice when renaming the first name and unchecking all boxes.
31+
It will stay disabled for the current project until you manually re-enable it.</p>
32+
33+
<p><i>Note 2:</i> Undo is not yet supported. If you want to change a name after renaming it, just rename it again. If you accidentally
34+
undo, just redo and change the name.</p>
35+
36+
<h2>Source code</h2>
37+
38+
<a href="https://github.com/DimensionalDevelopment/knit">https://github.com/DimensionalDevelopment/knit</a></p>
2339
]]>
2440
</description>
2541

2642
<change-notes>
27-
1.0.1 - Updated Enigma version
28-
1.0 - Initial release
43+
<![CDATA[
44+
<b>1.0.2</b>
45+
<ul>
46+
<li>Fixed class mappings not being created</li>
47+
<li>Fixed libraries being included both in plugin zip and Knit jar</li>
48+
<li>Fixed renaming constructor parameters not working</li>
49+
<li>Added support for classes in default package</li>
50+
<li>Improved instructions</li>
51+
</ul>
52+
<b>1.0.1</b>
53+
<ul>
54+
<li>Updated Enigma version</li>
55+
</ul>
56+
<b>1.0</b>
57+
<li>Initial release</li>
58+
]]>
2959
</change-notes>
3060

3161
<idea-version since-build="173.0"/>
@@ -40,9 +70,7 @@
4070
<action id="Knit.EnableRemapping" class="org.dimdev.knit.EnableRemappingAction" text="Enable Remapping" description="Enable or disable remapping mode">
4171
<add-to-group group-id="RefactoringMenu"/>
4272
</action>
43-
</actions>
4473

45-
<actions>
4674
<action id="Knit.SaveMappings" class="org.dimdev.knit.SaveMappingsAction" text="Save Mappings" description="Save mappings to directory they were loaded from">
4775
<add-to-group group-id="RefactoringMenu"/>
4876
</action>

0 commit comments

Comments
 (0)