Skip to content

Commit

Permalink
Feature creep commit
Browse files Browse the repository at this point in the history
This commit has 3 new features in it:
-> Internal Templates
-> The new Purpur Template
-> Changes to External Logging
  • Loading branch information
ME1312 committed Jun 25, 2021
1 parent ff709ba commit 1533987
Show file tree
Hide file tree
Showing 34 changed files with 258 additions and 126 deletions.
1 change: 1 addition & 0 deletions SubServers.Bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<mkdir dir="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates" />
<zip basedir="${basedir}/../SubServers.Creator" destfile="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates/forge.zip" includes="Forge/**" />
<zip basedir="${basedir}/../SubServers.Creator" destfile="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates/paper.zip" includes="Paper/**" />
<zip basedir="${basedir}/../SubServers.Creator" destfile="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates/purpur.zip" includes="Purpur/**" />
<zip basedir="${basedir}/../SubServers.Creator" destfile="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates/spigot.zip" includes="Spigot/**" />
<zip basedir="${basedir}/../SubServers.Creator" destfile="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates/sponge.zip" includes="Sponge/**" />
<zip basedir="${basedir}/../SubServers.Creator" destfile="${project.build.directory}/classes/net/ME1312/SubServers/Bungee/Library/Files/Templates/vanilla.zip" includes="Vanilla/**" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void reload() {
try {
if (file.isDirectory() && !file.getName().endsWith(".x")) {
ObjectMap<String> config = (new UniversalFile(file, "template.yml").exists())?new YAMLConfig(new UniversalFile(file, "template.yml")).get().getMap("Template", new ObjectMap<String>()):new ObjectMap<String>();
ServerTemplate template = loadTemplate(file.getName(), config.getBoolean("Enabled", true), config.getRawString("Icon", "::NULL::"), file, config.getMap("Build", new ObjectMap<String>()), config.getMap("Settings", new ObjectMap<String>()));
ServerTemplate template = loadTemplate(file.getName(), config.getBoolean("Enabled", true), config.getBoolean("Internal", false), config.getRawString("Icon", "::NULL::"), file, config.getMap("Build", new ObjectMap<String>()), config.getMap("Settings", new ObjectMap<String>()));
templatesR.put(file.getName().toLowerCase(), template);
if (config.getKeys().contains("Display")) template.setDisplayName(config.getString("Display"));
}
Expand All @@ -82,7 +82,6 @@ public void reload() {
}

if (host.available && !Util.getDespiteException(() -> Util.reflect(SubProxy.class.getDeclaredField("reloading"), host.plugin), false)) {
host.queue(new PacketExConfigureHost(host.plugin, host));
host.queue(new PacketExUploadTemplates(host.plugin));
if (enableRT == null || enableRT) host.queue(new PacketExDownloadTemplates(host.plugin, host));
}
Expand Down Expand Up @@ -115,7 +114,6 @@ public boolean create(UUID player, String name, ServerTemplate template, Version
logger.start();
host.queue(new PacketExCreateServer(player, name, template, version, port, logger.getExternalAddress(), data -> {
finish(player, null, name, template, version, fport, prefix, origin, data, callback);
logger.stop();
this.thread.remove(name.toLowerCase());
}));
return true;
Expand Down Expand Up @@ -156,7 +154,6 @@ public boolean update(UUID player, SubServer server, ServerTemplate template, Ve
((ExternalSubServer) server).updating(false);
if (callback != null) callback.run(s != null);
});
logger.stop();
this.thread.remove(name.toLowerCase());
}));
return true;
Expand Down Expand Up @@ -334,14 +331,26 @@ public List<Integer> getReservedPorts() {
@Override
public Map<String, ServerTemplate> getTemplates() {
TreeMap<String, ServerTemplate> map = new TreeMap<String, ServerTemplate>();
if (enableRT != null && enableRT) map.putAll(templatesR);
map.putAll(templates);
if (enableRT != null && enableRT) for (Map.Entry<String, ServerTemplate> template : templatesR.entrySet()) {
if (!template.getValue().isInternal()) map.put(template.getKey(), template.getValue());
}
for (Map.Entry<String, ServerTemplate> template : templates.entrySet()) {
if (!template.getValue().isInternal()) map.put(template.getKey(), template.getValue());
}
return map;
}

@Override
public ServerTemplate getTemplate(String name) {
if (Util.isNull(name)) throw new NullPointerException();
return getTemplates().get(name.toLowerCase());
name = name.toLowerCase();

ServerTemplate template = templates.getOrDefault(name, null);
if (template == null && enableRT != null && enableRT) template = templatesR.getOrDefault(name, null);
if (template == null || template.isInternal()) {
return null;
} else {
return template;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -72,29 +74,17 @@ public void start() {
}

@SuppressWarnings("deprecation")
private void log(String line) {
private void log(String type, String msg) {
if (started) {
String msg = line;
Level level;

// REGEX Formatting
String type = "";
Matcher matcher = Pattern.compile("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARNING|WARN|ERROR|ERR|SEVERE)\\]?:?(?:\\s*>)?\\s*)").matcher(msg.replaceAll("\u001B\\[[;\\d]*m", ""));
while (matcher.find()) {
type = matcher.group(3).toUpperCase();
}

msg = msg.substring(msg.length() - msg.replaceAll("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARNING|WARN|ERROR|ERR|SEVERE)\\]?:?(?:\\s*>)?\\s*)", "").length());

// Determine LOG LEVEL
switch (type) {
case "WARNING":
case "WARN":
level = Level.WARNING;
break;
case "SEVERE":
case "ERROR":
case "ERR":
level = Level.SEVERE;
break;
default:
Expand All @@ -117,7 +107,7 @@ private void log(String line) {

// Log to FILE
if (writer != null) {
writer.println(line);
writer.println('[' + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()) + "] [" + level + "] > " + msg);
writer.flush();
}
}
Expand Down Expand Up @@ -147,7 +137,6 @@ public void unregisterFilter(SubLogFilter filter) {
@Override
public void stop() {
if (started) {
PacketInExLogMessage.unregister(id);
id = null;
started = false;
List<SubLogFilter> filters = new ArrayList<SubLogFilter>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,24 @@ private ObjectMap<String> build(File dir, ServerTemplate template, List<ServerTe
if (templates.get(other.toLowerCase()).isEnabled()) {
if (version != null || !templates.get(other.toLowerCase()).requiresVersion()) {
if (update == null || templates.get(other.toLowerCase()).canUpdate()) {
ObjectMap<String> config = build(dir, templates.get(other.toLowerCase()), history);
if (config == null) {
throw new SubCreatorException();
} else {
server.setAll(config);
}
server.setAll(this.build(dir, templates.get(other.toLowerCase()), history));
} else {
Logger.get(prefix).info("Skipping template that cannot be run in update mode: " + other);
Logger.get(prefix).warning("Skipping template that cannot be run in update mode: " + other);
}
} else {
Logger.get(prefix).info("Skipping template that requires extra versioning information: " + other);
Logger.get(prefix).warning("Skipping template that requires extra versioning information: " + other);
}
} else {
Logger.get(prefix).info("Skipping disabled template: " + other);
Logger.get(prefix).warning("Skipping disabled template: " + other);
}
} else {
Logger.get(prefix).info("Skipping missing template: " + other);
Logger.get(prefix).warning("Skipping missing template: " + other);
}
}
server.setAll(template.getConfigOptions());
try {
Logger.get(prefix).info("Loading" + ((template.isDynamic())?" Dynamic":"") + " Template: " + template.getDisplayName());
if (template.getBuildOptions().getBoolean("Update-Files", false)) updateDirectory(template.getDirectory(), dir);
else Util.copyDirectory(template.getDirectory(), dir);
updateDirectory(template.getDirectory(), dir, template.getBuildOptions().getBoolean("Update-Files", false));

for (ObjectMapValue<String> replacement : template.getBuildOptions().getMap("Replacements", new ObjectMap<>()).getValues()) if (!replacement.isNull()) {
replacements.put(replacement.getHandle().toLowerCase().replace('-', '_').replace(' ', '_'), replacement.asRawString());
Expand Down Expand Up @@ -235,7 +229,6 @@ public void run() {
declaration.run();
File dir = (update != null)?new File(update.getFullPath()):new File(host.getPath(),
(template.getConfigOptions().contains("Directory"))?new ReplacementScanner(replacements).replace(template.getConfigOptions().getRawString("Directory")).toString():name);
dir.mkdirs();

ObjectMap<String> server = new ObjectMap<String>();
ObjectMap<String> config;
Expand Down Expand Up @@ -366,7 +359,7 @@ public void reload() {
try {
if (file.isDirectory() && !file.getName().endsWith(".x")) {
ObjectMap<String> config = (new UniversalFile(file, "template.yml").exists()) ? new YAMLConfig(new UniversalFile(file, "template.yml")).get().getMap("Template", new ObjectMap<String>()) : new ObjectMap<String>();
ServerTemplate template = loadTemplate(file.getName(), config.getBoolean("Enabled", true), config.getRawString("Icon", "::NULL::"), file, config.getMap("Build", new ObjectMap<String>()), config.getMap("Settings", new ObjectMap<String>()));
ServerTemplate template = loadTemplate(file.getName(), config.getBoolean("Enabled", true), config.getBoolean("Internal", false), config.getRawString("Icon", "::NULL::"), file, config.getMap("Build", new ObjectMap<String>()), config.getMap("Settings", new ObjectMap<String>()));
templates.put(file.getName().toLowerCase(), template);
if (config.getKeys().contains("Display")) template.setDisplayName(config.getString("Display"));
}
Expand Down Expand Up @@ -550,13 +543,23 @@ public List<Integer> getReservedPorts() {

@Override
public Map<String, ServerTemplate> getTemplates() {
return new TreeMap<String, ServerTemplate>(templates);
TreeMap<String, ServerTemplate> map = new TreeMap<String, ServerTemplate>();
for (Map.Entry<String, ServerTemplate> template : templates.entrySet()) {
if (!template.getValue().isInternal()) map.put(template.getKey(), template.getValue());
}
return map;
}

@Override
public ServerTemplate getTemplate(String name) {
if (Util.isNull(name)) throw new NullPointerException();
return getTemplates().get(name.toLowerCase());

ServerTemplate template = templates.getOrDefault(name.toLowerCase(), null);
if (template == null || template.isInternal()) {
return null;
} else {
return template;
}
}

private static Pair<YAMLSection, Map<String, Object>> subdata = null;
Expand Down Expand Up @@ -598,23 +601,21 @@ private void generateClient(File dir, ServerType type, String name) throws IOExc
}
}

private void updateDirectory(File from, File to) {
if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) {
if (!to.exists()) {
to.mkdirs();
}

private void updateDirectory(File from, File to, boolean overwrite) {
if (!to.exists()) {
Util.copyDirectory(from, to);
} else if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) {
String files[] = from.list();

for (String file : files) {
File srcFile = new File(from, file);
File destFile = new File(to, file);

updateDirectory(srcFile, destFile);
updateDirectory(srcFile, destFile, overwrite);
}
} else {
try {
if (!to.exists() || from.length() != to.length() || !Arrays.equals(generateSHA256(to), generateSHA256(from))) {
if (overwrite && (from.length() != to.length() || !Arrays.equals(generateSHA256(to), generateSHA256(from)))) {
if (to.exists()) {
if (to.isDirectory()) Util.deleteDirectory(to);
else to.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,27 @@ private void start(InputStream in, boolean isErr) {
}
}

private static final String PATTERN = "^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(DEBUG|MESSAGE|MSG|" + Pattern.quote(Level.INFO.getLocalizedName()) + "|INFO|" + Pattern.quote(Level.WARNING.getLocalizedName()) + "|WARNING|WARN|ERROR|ERR|" + Pattern.quote(Level.SEVERE.getLocalizedName()) + "|SEVERE)\\]?:?(?:\\s*>)?\\s*)";
private void log(String line) {
if (!line.startsWith(">")) {
String msg = line;
Level level;

// REGEX Formatting
String type = "";
Matcher matcher = Pattern.compile("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARNING|WARN|ERROR|ERR|SEVERE)\\]?:?(?:\\s*>)?\\s*)").matcher(msg.replaceAll("\u001B\\[[;\\d]*m", ""));
Matcher matcher = Pattern.compile(PATTERN).matcher(msg.replaceAll("\u001B\\[[;\\d]*m", ""));
while (matcher.find()) {
type = matcher.group(3).toUpperCase();
}

msg = msg.replaceAll("^((?:\\s*\\[?([0-9]{2}:[0-9]{2}:[0-9]{2})]?)?[\\s\\/\\\\\\|]*(?:\\[|\\[.*\\/)?(MESSAGE|INFO|WARNING|WARN|ERROR|ERR|SEVERE)\\]?:?(?:\\s*>)?\\s*)", "");
msg = msg.replaceAll(PATTERN, "");

// Determine LOG LEVEL
switch (type) {
if (type.equalsIgnoreCase(Level.WARNING.getLocalizedName())) {
level = Level.WARNING;
} else if (type.equalsIgnoreCase(Level.SEVERE.getLocalizedName())) {
level = Level.SEVERE;
} else switch (type) {
case "WARNING":
case "WARN":
level = Level.WARNING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class ServerTemplate {
private String name;
private String nick = null;
private boolean enabled;
private boolean internal;
private String icon;
private File directory;
private ServerType type;
Expand All @@ -39,14 +40,15 @@ public static class ServerTemplate {
* @param options Configuration Options
*/
public ServerTemplate(String name, boolean enabled, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options) {
this(name, enabled, icon, directory, build, options, true);
this(name, enabled, false, icon, directory, build, options, true);
}

private ServerTemplate(String name, boolean enabled, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options, boolean dynamic) {
private ServerTemplate(String name, boolean enabled, boolean internal, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options, boolean dynamic) {
if (Util.isNull(name, enabled, directory, build, options)) throw new NullPointerException();
if (name.contains(" ")) throw new InvalidTemplateException("Template names cannot have spaces: " + name);
this.name = name;
this.enabled = enabled;
this.internal = internal;
this.icon = icon;
this.directory = directory;
this.type = (build.contains("Server-Type"))?ServerType.valueOf(build.getRawString("Server-Type").toUpperCase()):ServerType.CUSTOM;
Expand Down Expand Up @@ -104,6 +106,15 @@ public void setEnabled(boolean value) {
enabled = value;
}

/**
* Get if this Template is for Internal use only
*
* @return Internal Status
*/
public boolean isInternal() {
return internal;
}

/**
* Get the Item Icon for this Template
*
Expand Down Expand Up @@ -161,7 +172,7 @@ public boolean canUpdate() {
/**
* Get whether this Template was generated by a SubCreator instance
*
* @return Custom Status
* @return Dynamic Status
*/
public boolean isDynamic() {
return dynamic;
Expand Down Expand Up @@ -512,13 +523,14 @@ public static List<InetSocketAddress> getAllReservedAddresses() {
*
* @param name Template Name
* @param enabled Template Enabled Status
* @param internal Template Internal Status
* @param icon Template Item Icon Name
* @param directory Template Directory
* @param build Build Options
* @param options Configuration Options
*/
protected ServerTemplate loadTemplate(String name, boolean enabled, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options) {
return new ServerTemplate(name, enabled, icon, directory, build, options, false);
protected ServerTemplate loadTemplate(String name, boolean enabled, boolean internal, String icon, File directory, ObjectMap<String> build, ObjectMap<String> options) {
return new ServerTemplate(name, enabled, internal, icon, directory, build, options, false);
}

/**
Expand Down
Loading

0 comments on commit 1533987

Please sign in to comment.