/
ScriptHelper.java
228 lines (205 loc) · 9.34 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
228
package com.denizenscript.denizencore.scripts;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import com.denizenscript.denizencore.DenizenCore;
import java.io.*;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
/**
* 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;
Debug.echoError("Could not load scripts!");
DenizenCore.implementation.debugException(e);
_yamlScripts = YamlConfiguration.load("scripts_failed_to_load:\n type: data\n");
}
ScriptRegistry.buildCoreYamlScriptContainers(_yamlScripts);
}
public 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 (StringHolder str : config.getKeys(false)) {
String up = str.str.toUpperCase();
if (list.contains(up)) {
hadError = true;
Debug.echoError("There is more than one script named '" + up + "'!");
}
else {
list.add(up);
}
}
}
private static HashMap<String, String> scriptSources = new HashMap<>();
private static HashMap<String, String> scriptOriginalNames = new HashMap<>();
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");
boolean hasAnyScript = false;
for (int lineNum = 0; lineNum < lines.length; lineNum++) {
String trimmedLine = lines[lineNum].trim();
String trimStart = lines[lineNum].replaceAll("^[\\s]+", "");
if (trackSources && !trimmedLine.startsWith("#") && trimStart.length() == lines[lineNum].length() && trimmedLine.endsWith(":") && trimmedLine.length() > 1) {
String name = trimmedLine.substring(0, trimmedLine.length() - 1).replace('\"', '\'').replace("'", "");
scriptSources.put(name.toUpperCase(), filename);
scriptOriginalNames.put(name.toUpperCase(), name);
result.append(name.toUpperCase()).append(":\n");
hasAnyScript = true;
}
else if (!trimmedLine.startsWith("#")) {
if (trackSources && !hasAnyScript && trimmedLine.endsWith(":")) {
Debug.echoError("Script '" + filename + "' is broken: script container title has spaces in front.");
hasAnyScript = true;
}
if ((trimmedLine.startsWith("}") || trimmedLine.startsWith("{") || trimmedLine.startsWith("else")) && !trimmedLine.endsWith(":")) {
result.append(' ').append(lines[lineNum].replace('\0', ' ')
.replace(": ", "<&co>").replace("#", "<&ns>")).append("\n");
}
else {
String curLine = lines[lineNum].replace('\0', ' ');
boolean endsColon = trimmedLine.endsWith(":");
boolean startsDash = trimmedLine.startsWith("-");
if (!endsColon && startsDash) {
curLine = curLine.replace(": ", "<&co> ");
curLine = curLine.replace("#", "<&ns>");
}
else if (endsColon && !startsDash) {
if (curLine.contains(".")) {
curLine = curLine.replace("&", "&").replace(".", "&dot");
}
}
else if (!startsDash && trimmedLine.contains(": &")) {
int colon = curLine.indexOf(':');
curLine = curLine.substring(0, colon) + ": \"" + curLine.substring(colon + 2).replace("\"", "<&dq>") + "\"";
}
if (trimmedLine.startsWith("- ") && !trimmedLine.startsWith("- \"") && !trimmedLine.startsWith("- '")) {
int dashIndex = curLine.indexOf('-');
curLine = curLine.substring(0, dashIndex + 1) + " " + ScriptBuilder.LINE_PREFIX_CHAR + (lineNum + 1) + ScriptBuilder.LINE_PREFIX_CHAR + curLine.substring(dashIndex + 1);
}
result.append(curLine).append("\n");
}
}
else {
result.append("\n");
}
}
result.append("\n");
return result.toString();
}
public static CharsetDecoder encoding = null;
public static String convertStreamToString(InputStream is) {
return convertStreamToString(is, false);
}
public static String convertStreamToString(InputStream is, boolean defaultUTF8) {
Scanner s;
if (encoding == null && !defaultUTF8) {
s = new Scanner(is);
}
else {
s = new Scanner(new InputStreamReader(is, encoding == null ? StandardCharsets.UTF_8.newDecoder() : encoding));
}
s.useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
public static YamlConfiguration loadConfig(String filename, InputStream resource) throws IOException {
try {
String script = clearComments(filename, convertStreamToString(resource, filename.endsWith(".dsc")), true);
return YamlConfiguration.load(script);
}
finally {
resource.close();
}
}
private static String _concatenateCoreScripts() {
scriptSources.clear();
try {
File file = DenizenCore.implementation.getScriptFolder();
// Check if the directory exists
if (!file.exists()) {
Debug.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<>(files.size() * 2);
YamlConfiguration yaml;
for (File f : files) {
String fileName = f.getAbsolutePath().substring(file.getAbsolutePath().length());
if (Debug.showLoading) {
Debug.log("Processing '" + fileName + "'... ");
}
try {
yaml = loadConfig(f.getAbsolutePath(), new FileInputStream(f));
String saved = yaml != null ? yaml.saveToString(false) : null;
if (saved != null && saved.length() > 0) {
handleListing(yaml, scriptNames);
sb.append(saved).append("\r\n");
}
else {
Debug.echoError("Error parsing " + fileName + "! This script has been skipped. No internal error - is the file empty?");
hadError = true;
}
}
catch (Exception e) {
Debug.echoError("Error parsing " + fileName + "!");
hadError = true;
Debug.echoError(e);
}
}
if (Debug.showLoading) {
Debug.echoApproval("All scripts loaded!");
}
return sb.toString();
}
else {
Debug.log("No scripts in /plugins/Denizen/scripts/ to load!");
hadError = true;
}
}
catch (Exception e) {
Debug.echoError("No script folder found in " + DenizenCore.implementation.getScriptFolder());
hadError = true;
Debug.echoError(e);
}
return "";
}
}