/
ObjectFetcher.java
260 lines (227 loc) · 10.8 KB
/
ObjectFetcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.denizenscript.denizencore.objects;
import com.denizenscript.denizencore.objects.core.*;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.tags.TagContext;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.*;
public class ObjectFetcher {
@FunctionalInterface
public interface MatchesInterface {
boolean matches(String str);
}
public interface ValueOfInterface<T extends ObjectTag> {
T valueOf(String str, TagContext context);
}
public static class ObjectType<T extends ObjectTag> {
public Class<T> clazz;
public MatchesInterface matches;
public ValueOfInterface<T> valueOf;
public ObjectTagProcessor<T> tagProcessor;
public String prefix;
public boolean isAdjustable;
}
public static Map<String, ObjectType<? extends ObjectTag>> objectsByPrefix = new HashMap<>();
public static Map<Class<? extends ObjectTag>, ObjectType<? extends ObjectTag>> objectsByClass = new HashMap<>();
public static void registerCoreObjects() {
// Initialize the ObjectFetcher
registerWithObjectFetcher(CustomObjectTag.class, CustomObjectTag.tagProcessor); // custom@
registerWithObjectFetcher(DurationTag.class, DurationTag.tagProcessor); // d@
registerWithObjectFetcher(ElementTag.class, ElementTag.tagProcessor); // el@
registerWithObjectFetcher(ListTag.class, ListTag.tagProcessor); // li@
registerWithObjectFetcher(MapTag.class, MapTag.tagProcessor); // map@
registerWithObjectFetcher(QueueTag.class, QueueTag.tagProcessor); // q@
registerWithObjectFetcher(ScriptTag.class, ScriptTag.tagProcessor); // s@
}
public static MatchesInterface getMatchesFor(Class clazz) {
try {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
CallSite site = LambdaMetafactory.metafactory(lookup, "matches", // MatchesInterface#matches
MethodType.methodType(MatchesInterface.class), // Signature of invoke method
MethodType.methodType(Boolean.class, String.class).unwrap(), // signature of MatchesInterface#matches
lookup.findStatic(clazz, "matches", MethodType.methodType(Boolean.class, String.class).unwrap()), // signature of original matches method
MethodType.methodType(Boolean.class, String.class).unwrap()); // Signature of original matches again
return (MatchesInterface) site.getTarget().invoke();
}
catch (Throwable ex) {
System.err.println("Failed to get matches for " + clazz.getCanonicalName());
ex.printStackTrace();
Debug.echoError(ex);
return null;
}
}
public static ValueOfInterface getValueOfFor(Class clazz) {
try {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
CallSite site = LambdaMetafactory.metafactory(lookup, "valueOf", // ValueOfInterface#valueOf
MethodType.methodType(ValueOfInterface.class), // Signature of invoke method
MethodType.methodType(ObjectTag.class, String.class, TagContext.class), // signature of ValueOfInterface#valueOf
lookup.findStatic(clazz, "valueOf", MethodType.methodType(clazz, String.class, TagContext.class)), // signature of original valueOf method
MethodType.methodType(clazz, String.class, TagContext.class)); // Signature of original valueOf again
return (ValueOfInterface) site.getTarget().invoke();
}
catch (Throwable ex) {
System.err.println("Failed to get valueOf for " + clazz.getCanonicalName());
ex.printStackTrace();
Debug.echoError(ex);
return null;
}
}
@Deprecated
public static void registerWithObjectFetcher(Class<? extends ObjectTag> objectTag) {
registerWithObjectFetcher(objectTag, null);
}
public static <T extends ObjectTag> void registerWithObjectFetcher(Class<T> objectTag, ObjectTagProcessor<T> processor) {
ObjectType newType = new ObjectType();
newType.clazz = objectTag;
newType.tagProcessor = processor;
newType.isAdjustable = Adjustable.class.isAssignableFrom(objectTag);
objectsByClass.put(objectTag, newType);
try {
Method valueOfMethod = objectTag.getMethod("valueOf", String.class, TagContext.class);
if (valueOfMethod.isAnnotationPresent(Fetchable.class)) {
String identifier = valueOfMethod.getAnnotation(Fetchable.class).value();
objectsByPrefix.put(CoreUtilities.toLowerCase(identifier.trim()), newType);
newType.prefix = identifier;
}
else {
Debug.echoError("Type '" + objectTag.getSimpleName() + "' registered as an object type, but doesn't have a fetcher prefix.");
}
newType.matches = getMatchesFor(objectTag);
newType.valueOf = getValueOfFor(objectTag);
for (Method registerMethod: objectTag.getDeclaredMethods()) {
if (registerMethod.getName().equals("registerTags") && registerMethod.getParameterCount() == 0) {
registerMethod.invoke(null);
}
}
}
catch (Throwable e) {
Debug.echoError("Failed to initialize an object type(" + objectTag.getSimpleName() + "): ");
Debug.echoError(e);
}
}
public static boolean canFetch(String id) {
return objectsByPrefix.containsKey(CoreUtilities.toLowerCase(id));
}
public static Class getObjectClass(String id) {
if (canFetch(id)) {
return objectsByPrefix.get(CoreUtilities.toLowerCase(id)).clazz;
}
else {
return null;
}
}
public static boolean isObjectWithProperties(String input) {
return input.indexOf('[') != -1 && input.lastIndexOf(']') == input.length() - 1;
}
public static boolean checkMatch(Class<? extends ObjectTag> dClass, String value) {
if (value == null || dClass == null) {
return false;
}
int firstBracket = value.indexOf('[');
if (firstBracket != -1 && value.lastIndexOf(']') == value.length() - 1) {
value = value.substring(0, firstBracket);
}
try {
return objectsByClass.get(dClass).matches.matches(value);
}
catch (Exception e) {
Debug.echoError(e);
}
return false;
}
@Deprecated
public static <T extends ObjectTag> T getObjectFrom(Class<T> dClass, String value) {
return getObjectFrom(dClass, value, CoreUtilities.basicContext);
}
public static List<String> separateProperties(String input) {
if (!isObjectWithProperties(input)) {
return null;
}
ArrayList<String> output = new ArrayList<>(input.length() / 7);
int start = 0;
boolean needObject = true;
int brackets = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '[' && needObject) {
needObject = false;
output.add(input.substring(start, i));
start = i + 1;
}
else if (input.charAt(i) == '[') {
brackets++;
}
else if (input.charAt(i) == ']' && brackets > 0) {
brackets--;
}
else if ((input.charAt(i) == ';' || input.charAt(i) == ']') && brackets == 0) {
output.add((input.substring(start, i)));
start = i + 1;
}
}
return output;
}
public static <T extends ObjectTag> T getObjectFrom(Class<T> dClass, String value, TagContext context) {
return getObjectFrom((ObjectType<T>) objectsByClass.get(dClass), value, context);
}
public static <T extends ObjectTag> T getObjectFrom(ObjectType<T> type, String value, TagContext context) {
try {
List<String> matches = separateProperties(value);
boolean matched = matches != null && type.isAdjustable;
T gotten = type.valueOf.valueOf(matched ? matches.get(0) : value, context);
if (gotten != null && matched) {
for (int i = 1; i < matches.size(); i++) {
List<String> data = CoreUtilities.split(matches.get(i), '=', 2);
if (data.size() != 2) {
Debug.echoError("Invalid property string '" + matches.get(i) + "'!");
continue;
}
String description = data.get(1);
if (description.indexOf('&') != -1) {
description = CoreUtilities.replace(description, "&", "&");
description = CoreUtilities.replace(description, "&sc", ";");
}
((Adjustable) gotten).safeApplyProperty(new Mechanism(new ElementTag(data.get(0)), new ElementTag(description), context));
}
gotten = (T) gotten.fixAfterProperties();
}
return gotten;
}
catch (Exception e) {
Debug.echoError(e);
}
return null;
}
/**
* This function will return the most-valid ObjectTag for the input string.
* If the input lacks @ notation or is not a valid object, an ElementTag will be returned.
*
* @param value the input string.
* @return the most-valid ObjectTag available.
*/
public static ObjectTag pickObjectFor(String value) {
return pickObjectFor(value, DenizenCore.getImplementation().getEmptyScriptEntryData().getTagContext());
}
public static ObjectTag pickObjectFor(String value, TagContext context) {
if (value == null) {
return null;
}
if (CoreUtilities.contains(value, '@')) {
String type = value.split("@", 2)[0];
ObjectType<? extends ObjectTag> toFetch = objectsByPrefix.get(type);
if (toFetch != null) {
ObjectTag fetched = getObjectFrom(toFetch, value, context);
if (fetched != null) {
return fetched;
}
}
}
return new ElementTag(value);
}
}