Skip to content

Commit f861d05

Browse files
author
Devan-Kerman
committed
minor tweaks
1 parent 8b4ce76 commit f861d05

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id 'fabric-loom' version '0.2.6-SNAPSHOT'
2+
id 'fabric-loom' version '0.2.7-SNAPSHOT'
33
id 'maven-publish'
44
}
55

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ minecraft_version=1.15.2
66
yarn_mappings=1.15.2+build.15
77
loader_version=0.8.2+build.194
88
# Mod Properties
9-
mod_version=1.2
9+
mod_version=1.4
1010
maven_group=net.devtech
1111
archives_base_name=nanoevents
1212
# Dependencies

src/main/java/net/devtech/nanoevents/Finder.java

+27-17
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,13 @@ public static Map<Id, List<String>> parseListeners() {
2727
Map<Id, List<String>> listeners = new HashMap<>();
2828
forVal("nano:lst", (m, c) -> {
2929
Path path = m.getPath(c);
30-
try {
31-
Properties properties = new Properties();
32-
properties.load(Files.newBufferedReader(path));
33-
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
34-
String listener = (String) entry.getKey();
35-
String eventId = (String) entry.getValue();
36-
int colonIndex = eventId.indexOf(':');
37-
if (colonIndex == -1) {
38-
LOGGER.severe("Invalid identifier: " + eventId + " in " + c + " in " + m.getMetadata().getId());
39-
} else {
40-
listeners.computeIfAbsent(new Id(eventId.substring(0, colonIndex), eventId.substring(colonIndex + 1)), i -> new ArrayList<>()).add(listener);
41-
}
30+
if (Files.isDirectory(path)) {
31+
try {
32+
Files.list(path).forEach(v -> parse(listeners, v, m, v.toString()));
33+
} catch (IOException e) {
34+
throw new RuntimeException(e);
4235
}
43-
} catch (IOException e) {
44-
LOGGER.severe("error in reading listneers in " + path + " in mod " + m.getMetadata().getId());
45-
e.printStackTrace();
46-
}
36+
} else parse(listeners, path, m, c);
4737
});
4838
return listeners;
4939
}
@@ -55,7 +45,7 @@ private static void forVal(String val, BiConsumer<ModContainer, String> consumer
5545
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
5646
ModMetadata metadata = mod.getMetadata();
5747
CustomValue value = metadata.getCustomValue(val);
58-
if(value != null) {
48+
if (value != null) {
5949
if (value.getType() == CustomValue.CvType.STRING) consumer.accept(mod, value.getAsString());
6050
else if (value.getType() == CustomValue.CvType.ARRAY) for (CustomValue customValue : value.getAsArray()) {
6151
if (customValue.getType() == CustomValue.CvType.STRING) consumer.accept(mod, customValue.getAsString());
@@ -65,6 +55,26 @@ else if (value.getType() == CustomValue.CvType.ARRAY) for (CustomValue customVal
6555
}
6656
}
6757

58+
private static void parse(Map<Id, List<String>> listeners, Path path, ModContainer mod, String file) {
59+
try {
60+
Properties properties = new Properties();
61+
properties.load(Files.newBufferedReader(path));
62+
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
63+
String listener = (String) entry.getKey();
64+
String eventId = (String) entry.getValue();
65+
int colonIndex = eventId.indexOf(':');
66+
if (colonIndex == -1) {
67+
LOGGER.severe("Invalid identifier: " + eventId + " in " + file + " in " + mod.getMetadata().getId());
68+
} else {
69+
listeners.computeIfAbsent(new Id(eventId.substring(0, colonIndex), eventId.substring(colonIndex + 1)), i -> new ArrayList<>()).add(listener);
70+
}
71+
}
72+
} catch (IOException e) {
73+
LOGGER.severe("error in reading listneers in " + path + " in mod " + mod.getMetadata().getId());
74+
e.printStackTrace();
75+
}
76+
}
77+
6878
/**
6979
* find all the events declared in each mod
7080
*/

src/main/java/net/devtech/nanoevents/asm/NanoTransformer.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.FileOutputStream;
1616
import java.io.IOException;
1717
import java.util.Collection;
18+
import java.util.Collections;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.logging.Logger;
@@ -28,9 +29,9 @@ public class NanoTransformer implements Runnable {
2829
private static final String LISTENER_INVOKER_TYPE = 'L' + Type.getInternalName(ListenerInvoker.class) + ';';
2930

3031
private static final Logger LOGGER = Logger.getLogger("NanoTransformer");
32+
3133
static {
32-
if(DEBUG_TRANSFORMER)
33-
LOGGER.info("Transformer debugging is enabled!");
34+
if (DEBUG_TRANSFORMER) LOGGER.info("Transformer debugging is enabled!");
3435
}
3536

3637
@Override
@@ -40,7 +41,11 @@ public void run() {
4041
Id id = evt.getId();
4142
List<String> listeners = NanoEvents.LISTENERS.get(id);
4243
String invokerType = invoker.replace('.', '/');
43-
ClassTinkerers.addTransformation(invokerType, node -> transformClass(listeners, node, id, find(listeners, node, id)));
44+
ClassTinkerers.addTransformation(invokerType, node -> {
45+
List<String> list = listeners;
46+
if (list == null) list = Collections.emptyList();
47+
transformClass(list, node, id, find(list, node, id));
48+
});
4449
}
4550
}
4651

@@ -64,21 +69,21 @@ public static void transformClass(List<String> listeners, ClassNode node, Id id,
6469
MethodNode listenerInvoker = nodes.get("listener_invoker");
6570

6671
String desc = listenerInvoker == null ? invokerMethod.desc : listenerInvoker.desc;
67-
if(single == null) {
72+
if (single == null) {
6873
String name = listenerInvoker == null ? invokerMethod.name : listenerInvoker.name;
6974
transform(listeners, invokerMethod.instructions, name, desc, node.name);
7075
}
7176

7277
// single transformation
73-
if(single != null) {
78+
if (single != null) {
7479
String name = listenerInvoker == null ? single.name : listenerInvoker.name;
7580
invokerMethod.instructions = replace(single.instructions, name, node.name, desc, listeners.get(0));
7681
}
7782

78-
if(DEBUG_TRANSFORMER) {
83+
if (DEBUG_TRANSFORMER) {
7984
File file = new File("nano_debug/" + node.name + ".class");
8085
File parent = file.getParentFile();
81-
if(!parent.exists()) parent.mkdirs();
86+
if (!parent.exists()) parent.mkdirs();
8287
try (FileOutputStream output = new FileOutputStream(file)) {
8388
ClassWriter writer = new ClassWriter(0);
8489
node.accept(writer);
@@ -141,7 +146,6 @@ private static void transform(Collection<String> listeners, InsnList insns, Stri
141146
}
142147

143148

144-
145149
/**
146150
* replace all of shallow recursive call with a listener reference in a newly copied list
147151
*
@@ -159,14 +163,14 @@ private static InsnList replace(InsnList list, String nodeName, String nodeOwner
159163
// check if method call is the right one
160164
if (replacementNode.name.equals(nodeName) && replacementNode.owner.equals(nodeOwner) && replacementNode.desc.equals(nodeDescriptor)) {
161165
// parse the listener reference
162-
int classIndex = listenerReference.indexOf('#');
166+
int classIndex = listenerReference.indexOf("::");
163167
if (classIndex == -1) {
164168
LOGGER.severe("Bad method signature " + listenerReference);
165169
replacementNode.owner = "null";
166170
replacementNode.name = "READ_THE_LOGS";
167171
} else {
168172
replacementNode.owner = listenerReference.substring(0, classIndex).replace('.', '/');
169-
replacementNode.name = listenerReference.substring(classIndex + 1);
173+
replacementNode.name = listenerReference.substring(classIndex + 2);
170174
}
171175
}
172176
}

0 commit comments

Comments
 (0)