JsonEvents.java
4.51 KB
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
package com.mumfrey.liteloader.transformers.event.json;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import com.mumfrey.liteloader.core.runtime.Obf;
/**
* Serialisable class which represents a set of event injection definitions. Instances of this class are
* created by deserialising with JSON. The JSON string should be passed to the static {@link #parse} method
* which returns an instance of the class.
*
* After parsing, the events defined here can be injected into an event transformer instance by calling the
* {@link #register} method
*
* @author Adam Mummery-Smith
*/
public class JsonEvents implements Serializable
{
private static final long serialVersionUID = 1L;
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
/**
* Tokens are an instruction to the parser to look up a value rather than using a literal
*/
private static final Pattern tokenPattern = Pattern.compile("^\\$\\{([a-zA-Z0-9_\\-\\.\\$]+)\\}$");
/**
* Serialised obfusctation entries
*/
@SerializedName("obfuscation")
private JsonObfuscationTable obfuscation;
/**
* Serialised method descriptors
*/
@SerializedName("descriptors")
private List<JsonDescriptor> descriptors;
/**
* Serialised events
*/
@SerializedName("events")
private List<JsonEvent> events;
/**
* List of accessor interfaces
*/
@SerializedName("accessors")
private List<String> accessors;
/**
* Parsed method descriptors
*/
private transient JsonMethods methods;
/**
* Parsed accessors
*/
private transient List<String> accessorInterfaces = new ArrayList<String>();
/**
* Attempts to parse the information in this object
*/
private void parse()
{
if (this.events == null || this.events.isEmpty())
{
throw new InvalidEventJsonException("No events were defined in the supplied JSON");
}
try
{
// Parse the obfuscation table
this.obfuscation.parse();
// Parse the descriptor list
this.methods = new JsonMethods(this.obfuscation, this.descriptors);
// Parse the events
for (JsonEvent event : this.events)
{
event.parse(this.methods);
}
if (this.accessors != null)
{
for (String accessor : this.accessors)
{
if (accessor != null)
{
Obf accessorName = this.obfuscation.parseClass(accessor);
this.accessorInterfaces.add(accessorName.name);
}
}
}
}
catch (InvalidEventJsonException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new InvalidEventJsonException("An error occurred whilst parsing the event definition: " + ex.getClass().getSimpleName() + ": " + ex.getMessage(), ex);
}
}
/**
* Parse a token name, returns the token name as a string if the token is valid, or null if the token is not valid
*
* @param token
*/
static String parseToken(String token)
{
token = token.replace(" ", "").trim();
Matcher tokenPatternMatcher = JsonEvents.tokenPattern.matcher(token);
if (tokenPatternMatcher.matches())
{
return tokenPatternMatcher.group(1);
}
return null;
}
/**
* Called to register all events defined in this object into the specified transformer
*
* @param transformer
*/
public void register(ModEventInjectionTransformer transformer)
{
for (JsonEvent event : this.events)
{
event.register(transformer);
}
for (String interfaceName : this.accessorInterfaces)
{
transformer.registerAccessor(interfaceName);
}
}
// public String toJson()
// {
// return JsonEvents.gson.toJson(this);
// }
/**
* Parse a new JsonEvents object from the supplied JSON string
*
* @param json
* @return new JsonEvents instance
* @throws InvalidEventJsonException if the JSON ins invalid
*/
public static JsonEvents parse(String json) throws InvalidEventJsonException
{
try
{
JsonEvents newJsonEvents = JsonEvents.gson.fromJson(json, JsonEvents.class);
newJsonEvents.parse();
return newJsonEvents;
}
catch (InvalidEventJsonException ex)
{
throw ex;
}
catch (Throwable th)
{
throw new InvalidEventJsonException("An error occurred whilst parsing the event definition: " + th.getClass().getSimpleName() + ": " + th.getMessage(), th);
}
}
}