/
ScriptHelper.java
227 lines (192 loc) · 8.37 KB
/
ScriptHelper.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
package net.aufdemrand.denizencore.scripts;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Reloads and retrieves information from the scripts folder.
*/
public class ScriptHelper {
public static YamlConfiguration _yamlScripts = null;
public static void reloadScripts() {
String concatenated = _concatenateCoreScripts();
try {
_yamlScripts = YamlConfiguration.load(concatenated);
}
catch (Exception e) {
hadError = true;
DenizenCore.getImplementation().debugError("Could not load scripts!");
DenizenCore.getImplementation().debugException(e);
_yamlScripts = YamlConfiguration.load("scripts_failed_to_load:\n type: yaml data\n");
}
DenizenCore.getImplementation().buildCoreContainers(_yamlScripts);
}
public static YamlConfiguration _gs() {
return getScripts();
}
private static YamlConfiguration getScripts() {
if (_yamlScripts == null)
reloadScripts();
return _yamlScripts;
}
/**
* Console will be alerted if error was had during reload
*/
private static boolean hadError = false;
public static boolean hadError() {
return hadError;
}
public static void resetError() {
hadError = false;
}
public static void setHadError() {
hadError = true;
}
static void handleListing(YamlConfiguration config, List<String> list) {
for (String str: config.getKeys(false)) {
String up = str.toUpperCase();
if (list.contains(up)) {
hadError = true;
DenizenCore.getImplementation().debugError("There is more than one script named '" + up + "'!");
}
else {
list.add(up);
}
}
}
private static HashMap<String, String> scriptSources = new HashMap<String, String>();
private static HashMap<String, String> scriptOriginalNames = new HashMap<String, String>();
public static String getSource(String script) {
return scriptSources.get(script.toUpperCase());
}
public static String getOriginalName(String script) {
return scriptOriginalNames.get(script.toUpperCase());
}
public static String ClearComments(String filename, String input, boolean trackSources) {
StringBuilder result = new StringBuilder(input.length());
String[] lines = input.replace("\t", " ").replace("\r", "").split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i].trim();
String trimStart = lines[i].replaceAll("^[\\s\\t]+", "");
if (trackSources) {
if (trimStart.length() == lines[i].length() && line.endsWith(":") && line.length() > 1) {
String name = line.substring(0, line.length() - 1).replace('\"', '\'').replace("'", "");
scriptSources.put(name.toUpperCase(), filename);
scriptOriginalNames.put(name.toUpperCase(), name);
}
}
if (!line.startsWith("#")) {
if ((line.startsWith("}") || line.startsWith("{") || line.startsWith("else")) && !line.endsWith(":")) {
result.append(' ').append(lines[i].replace('\0', ' ')
.replace(": ", "<&co>").replace("#", "<&ns>")).append("\n");
}
else {
String liner = lines[i].replace('\0', ' ');
if (!line.endsWith(":") && line.startsWith("-")) {
liner = liner.replace(": ", "<&co> ");
liner = liner.replace("#", "<&ns>");
}
result.append(liner.replace('\0', ' ')).append("\n");
}
}
else {
result.append("\n");
}
}
result.append("\n");
return result.toString();
}
public static String convertStreamToString(InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
public static YamlConfiguration loadConfig(String filename, InputStream resource) throws IOException {
try {
String script = ClearComments(filename, convertStreamToString(resource), true);
return YamlConfiguration.load(script);
}
finally {
resource.close();
}
}
private static String _concatenateCoreScripts() {
scriptSources.clear();
try {
File file = null;
file = DenizenCore.getImplementation().getScriptFolder();
// Check if the directory exists
if(!file.exists()) {
dB.echoError("No script folder found, please create one.");
hadError = true;
return "";
}
// Get files using script directory
List<File> files = CoreUtilities.listDScriptFiles(file);
if (files.size() > 0) {
StringBuilder sb = new StringBuilder();
List<String> scriptNames = new ArrayList<String>();
YamlConfiguration yaml;
dB.log("Processing outside scripts... ");
for (YamlConfiguration outsideConfig : DenizenCore.getImplementation().getOutsideScripts()) {
try {
dB.log("Processing unnamed script...");
sb.append(outsideConfig.saveToString()).append("\r\n");
}
catch (Exception e) {
dB.echoError("Woah! Error parsing outside scripts!");
hadError = true;
}
}
for (File f : files) {
String fileName = f.getAbsolutePath().substring(file.getAbsolutePath().length());
dB.log("Processing '" + fileName + "'... ");
try {
yaml = loadConfig(f.getAbsolutePath(), new FileInputStream(f));
String saved = yaml != null ? yaml.saveToString() : null;
if (saved != null && saved.length() > 0) {
handleListing(yaml, scriptNames);
sb.append(saved).append("\r\n");
}
else {
dB.echoError("Woah! Error parsing " + fileName + "! This script has been skipped. No internal error - is the file empty?");
hadError = true;
}
} catch (Exception e) {
dB.echoError("Woah! Error parsing " + fileName + "!");
hadError = true;
dB.echoError(e);
}
}
dB.echoApproval("All scripts loaded!");
return yamlKeysToUpperCase(sb.toString());
} else {
dB.echoError("Woah! No scripts in /plugins/Denizen/scripts/ to load!");
hadError = true;
}
} catch (Exception e) {
dB.echoError("Woah! No script folder found in /plugins/Denizen/scripts/");
hadError = true;
dB.echoError(e);
}
return "";
}
static Pattern pattern = Pattern.compile("(^.*?[^\\s](:\\s))", Pattern.MULTILINE);
/**
* Changes YAML 'keys' to all Upper Case to de-sensitize case sensitivity when
* reading and parsing scripts.
*/
private static String yamlKeysToUpperCase(String string) {
StringBuffer sb = new StringBuffer();
Matcher matcher = pattern.matcher(string);
while (matcher.find())
matcher.appendReplacement(sb, matcher.group().toUpperCase());
matcher.appendTail(sb);
return sb.toString();
}
}