Skip to content

Commit

Permalink
Minor efficiency changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Aug 12, 2020
1 parent 8f689c9 commit 7b9dc59
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 123 deletions.
130 changes: 75 additions & 55 deletions GalaxiAPI/src/net/ME1312/Galaxi/Library/Log/LogStream.java
Expand Up @@ -75,52 +75,56 @@ private void submit(String str) {
Logger.messages.add(new NamedContainer<LogStream, String>(this, str));
}

@SuppressWarnings({"unchecked", "StringConcatenationInsideStringBufferAppend"})
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
private String convert(TextElement original) {
StringBuilder message = new StringBuilder();
ConsoleTextElement element = new ConsoleTextElement(original.toRaw());
try {
for (TextElement e : Util.<List<TextElement>>reflect(TextElement.class.getDeclaredField("before"), element)) message.append(convert(e));
} catch (Throwable e) {
getLogger().error.println(e);
}
if (original != null) {
ConsoleTextElement element = new ConsoleTextElement(original.toRaw());
try {
for (TextElement e : Util.<List<TextElement>>reflect(TextElement.class.getDeclaredField("before"), element)) message.append(convert(e));
} catch (Throwable e) {
getLogger().error.println(e);
}

if (element.bold()) message.append("\u001B[1m");
if (element.italic()) message.append("\u001B[3m");
if (element.underline()) message.append("\u001B[4m");
if (element.strikethrough()) message.append("\u001B[9m");
if (element.color() != null) {
int red = element.color().getRed();
int green = element.color().getGreen();
int blue = element.color().getBlue();
float alpha = element.color().getAlpha() / 255f;
if (element.bold()) message.append("\u001B[1m");
if (element.italic()) message.append("\u001B[3m");
if (element.underline()) message.append("\u001B[4m");
if (element.strikethrough()) message.append("\u001B[9m");
if (element.color() != null) {
int red = element.color().getRed();
int green = element.color().getGreen();
int blue = element.color().getBlue();
float alpha = element.color().getAlpha() / 255f;

red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);
red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);

message.append("\u001B[38;2;" + red + ";" + green + ";" + blue + "m");
}
if (element.backgroundColor() != null) {
int red = element.backgroundColor().getRed();
int green = element.backgroundColor().getGreen();
int blue = element.backgroundColor().getBlue();
float alpha = element.backgroundColor().getAlpha() / 255f;
message.append("\u001B[38;2;" + red + ";" + green + ";" + blue + "m");
}
if (element.backgroundColor() != null) {
int red = element.backgroundColor().getRed();
int green = element.backgroundColor().getGreen();
int blue = element.backgroundColor().getBlue();
float alpha = element.backgroundColor().getAlpha() / 255f;

red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);
red = Math.round(alpha * red);
green = Math.round(alpha * green);
blue = Math.round(alpha * blue);

message.append("\u001B[48;2;" + red + ";" + green + ";" + blue + "m");
}
if (element.onClick() != null) message.append("\033]99900;" + element.onClick().toString() + "\007");
message.append(element.message());
message.append("\u001B[m");
message.append("\u001B[48;2;" + red + ";" + green + ";" + blue + "m");
}
if (element.onClick() != null) message.append("\033]99900;" + element.onClick().toString() + "\007");
message.append(element.message());
message.append("\u001B[m");

try {
for (TextElement e : Util.<List<TextElement>>reflect(TextElement.class.getDeclaredField("after"), element)) message.append(convert(e));
} catch (Throwable e) {
getLogger().error.println(e);
try {
for (TextElement e : Util.<List<TextElement>>reflect(TextElement.class.getDeclaredField("after"), element)) message.append(convert(e));
} catch (Throwable e) {
getLogger().error.println(e);
}
} else {
message.append("null");
}

return message.toString();
Expand All @@ -133,9 +137,13 @@ private String convert(TextElement original) {
*/
public void print(Object obj) {
if (obj == null) {
print("null");
submit("null");
} else if (obj instanceof Throwable) {
print((Throwable) obj);
} else if (obj instanceof TextElement) {
print((TextElement) obj);
} else {
print(obj.toString());
submit(obj.toString());
}
}

Expand All @@ -146,11 +154,11 @@ public void print(Object obj) {
*/
public void print(Throwable err) {
if (err == null) {
print("null");
submit("null");
} else {
StringWriter sw = new StringWriter();
err.printStackTrace(new PrintWriter(sw));
print(sw.toString());
submit(sw.toString());
}
}

Expand All @@ -160,11 +168,7 @@ public void print(Throwable err) {
* @param element Text Element
*/
public void print(TextElement element) {
if (element == null) {
submit("null");
} else {
submit(convert(element));
}
submit(convert(element));
}

/**
Expand All @@ -186,7 +190,11 @@ public void print(String str) {
* @param str Character Array
*/
public void print(char[] str) {
print(new String(str));
if (str == null) {
submit("null");
} else {
submit(new String(str));
}
}

/**
Expand All @@ -202,7 +210,7 @@ public void print(char c) {
* Print an empty line
*/
public void println() {
print("\r\n");
submit("\r\n");
}

/**
Expand All @@ -213,9 +221,13 @@ public void println() {
public void println(Object... obj) {
for (Object OBJ : obj) {
if (OBJ == null) {
print("null\n");
submit("null\n");
} else if (OBJ instanceof Throwable) {
println((Throwable) OBJ);
} else if (OBJ instanceof TextElement) {
println((TextElement) OBJ);
} else {
print(OBJ.toString() + '\n');
submit(OBJ.toString() + '\n');
}
}
}
Expand All @@ -228,11 +240,11 @@ public void println(Object... obj) {
public void println(Throwable... err) {
for (Throwable ERR : err) {
if (ERR == null) {
print("null\n");
submit("null\n");
} else {
StringWriter sw = new StringWriter();
ERR.printStackTrace(new PrintWriter(sw));
print(sw.toString() + '\n');
submit(sw.toString() + '\n');
}
}
}
Expand All @@ -255,7 +267,11 @@ public void println(TextElement... element) {
*/
public void println(String... str) {
for (String STR : str) {
print(STR + '\n');
if (STR == null) {
submit("null\n");
} else {
submit(STR + '\n');
}
}
}

Expand All @@ -266,7 +282,11 @@ public void println(String... str) {
*/
public void println(char[]... str) {
for (char[] STR : str) {
print(new String(STR) + '\n');
if (STR == null) {
submit("null\n");
} else {
submit(new String(STR) + '\n');
}
}
}

Expand Down
28 changes: 6 additions & 22 deletions GalaxiAPI/src/net/ME1312/Galaxi/Library/Log/LogTranslator.java
Expand Up @@ -4,6 +4,7 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

Expand Down Expand Up @@ -43,29 +44,12 @@ public void publish(LogRecord record) {
}

if (stream != null) {
String message = record.getMessage();
int i = 0;
if (record.getParameters() != null) for (Object obj : record.getParameters()) {// Parse Parameters
String value = convert(obj);
String message = (record.getParameters() == null)? record.getMessage() : MessageFormat.format(record.getMessage(), record.getParameters());

boolean logged = false;
if (message.contains("{" + i + "}")) { // Write Parameters
message = message.replace("{" + i + "}", value);
logged = true;
} if (message.contains("{}")) {
message = message.replaceFirst("\\{}", value);
logged = true;
} if (!logged) {
message += "\n" + value;
}
i++;
}
if (record.getThrown() != null) {
message += "\n" + convert(record.getThrown());
}

message = message.replace("\r", ""); // Remove carriage returns (has special meaning in the Galaxi)
for (String m : ((message.contains("\n"))?message.split("\\n"):new String[]{ message })) stream.println(m); // Properly format new lines (if they exist)
message = message.replace("\r", ""); // Remove carriage returns (they have special meaning in Galaxi)
stream.println(message);
if (record.getThrown() != null)
stream.println(record.getThrown());
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions GalaxiAPI/src/net/ME1312/Galaxi/Library/Log/Logger.java
Expand Up @@ -179,15 +179,11 @@ private static void log(boolean COLOR_LEVELS) {
case '\r':
terminate_with_prefix = true;
break;
case '\t':
c = ' ';
message.append(" ");
default:
switch (c) {
case '\t':
message.append(" ");
break;
default:
message.appendCodePoint(c);
break;
}
message.appendCodePoint(c);
terminate = false;
terminate_with_prefix = false;
break;
Expand Down
11 changes: 5 additions & 6 deletions GalaxiAPI/src/net/ME1312/Galaxi/Plugin/PluginInfo.java
Expand Up @@ -405,14 +405,13 @@ public List<String> getPlatformStack() {
engine.getName() + " v" + engine.getVersion().toExtendedString() + ((engine.getSignature() != null)?" (" + engine.getSignature() + ')':"") + ((engine == app)?" [Standalone]"+((engine == this)?"":","):",")
));

if (engine != this) {
if (engine != app)
stack.add(app.getName() + " v" + app.getVersion().toExtendedString() + ((app.getSignature() != null)?" (" + app.getSignature() + ')':"") + ((app == this)?"":","));
if (app != this) {
for (PluginInfo plugin : scanDependencies()) {
stack.add(plugin.getDisplayName() + " v" + plugin.getVersion().toExtendedString() + ((plugin.getSignature() != null)?" (" + plugin.getSignature() + ')':"") + ((plugin.getState() != null)?" [" + plugin.getState() + ']':"") + ',');
}
stack.add(this.getDisplayName() + " v" + this.getVersion().toExtendedString() + ((this.getSignature() != null)?" (" + this.getSignature() + ')':"") + ((this.getState() != null)?" [" + this.getState() + ']':""));
if (app != this) {
for (PluginInfo plugin : scanDependencies()) {
stack.add(plugin.getDisplayName() + " v" + plugin.getVersion().toExtendedString() + ((plugin.getSignature() != null)?" (" + plugin.getSignature() + ')':"") + ((plugin.getState() != null)?" [" + plugin.getState() + ']':"") + ',');
}
stack.add(this.getDisplayName() + " v" + this.getVersion().toExtendedString() + ((this.getSignature() != null)?" (" + this.getSignature() + ')':"") + ((this.getState() != null)?" [" + this.getState() + ']':""));
}
return stack;
}
Expand Down
Expand Up @@ -37,7 +37,7 @@
/**
* Galaxi Engine Main Class
*/
@App(name = "GalaxiEngine", version = "3.3.0a", authors = "ME1312", description = "An engine for command line Java applications", website = "https://github.com/ME1312/GalaxiEngine")
@App(name = "GalaxiEngine", version = "3.3.1a", authors = "ME1312", description = "An engine for command line Java applications", website = "https://github.com/ME1312/GalaxiEngine")
public class GalaxiEngine extends Galaxi {
private final PluginManager pluginManager = new PluginManager(this);

Expand Down
Expand Up @@ -32,10 +32,11 @@
*/
public class ConsoleReader {
private Container<Boolean> running;
private Container<Boolean> jstatus;
private LineReader jline;
private Parser parser;
private Thread thread;
private OutputStream window;
private Container<OutputStream> window;
private Callback<String> chat = null;
private GalaxiEngine engine;

Expand All @@ -52,6 +53,7 @@ public ConsoleReader(GalaxiEngine engine, Container<Boolean> status) throws Exce
TerminalBuilder jtb = TerminalBuilder.builder();
if (!USE_JLINE.def()) jtb.dumb(true);
if (!USE_ANSI.def()) jtb.jansi(false);
this.jstatus = new Container<>(false);
this.jline = LineReaderBuilder.builder()
.appName(engine.getAppInfo().getName())
.terminal(jtb.build())
Expand All @@ -62,8 +64,9 @@ public ConsoleReader(GalaxiEngine engine, Container<Boolean> status) throws Exce
.completer((reader, line, list) -> {
for (String s : ConsoleReader.this.complete(ConsoleCommandSender.get(), (ParsedCommand) line)) list.add(new Candidate(s));
}).build();
window = new Container<>(null);
thread = new Thread(this::read, Galaxi.getInstance().getEngineInfo().getName() + "::Console_Reader");
Util.reflect(SystemLogger.class.getDeclaredMethod("start", LineReader.class), null, jline);
Util.reflect(SystemLogger.class.getDeclaredMethod("start", Container.class, LineReader.class, Container.class), null, window, jline, jstatus);
try {
if (SHOW_CONSOLE_WINDOW.usr().equalsIgnoreCase("true") || (SHOW_CONSOLE_WINDOW.usr().length() <= 0 && SHOW_CONSOLE_WINDOW.get() && System.console() == null)) {
openConsoleWindow(!(SHOW_CONSOLE_WINDOW.usr().equalsIgnoreCase("true") && System.console() != null));
Expand All @@ -89,15 +92,16 @@ public void setChatListener(Callback<String> listener) {
*/
public void openConsoleWindow(boolean exit) {
if (!GraphicsEnvironment.isHeadless())
window = Util.getDespiteException(() -> (OutputStream) Class.forName("net.ME1312.Galaxi.Engine.Standalone.ConsoleWindow").getConstructor(ConsoleReader.class, boolean.class).newInstance(this, exit), null);
window.set(Util.getDespiteException(() -> (OutputStream) Class.forName("net.ME1312.Galaxi.Engine.Standalone.ConsoleWindow").getConstructor(ConsoleReader.class, boolean.class).newInstance(this, exit), null));
}

/**
* Close the Console Window
*/
public void closeConsoleWindow() {
if (window != null) Util.isException(() -> window.close());
window = null;
OutputStream window;
if ((window = this.window.get()) != null) Util.isException(window::close);
this.window.set(null);
}

/**
Expand Down Expand Up @@ -154,12 +158,15 @@ public List<String> complete(CommandSender sender, ParsedCommand command) {
private void read() {
try {
boolean interrupted = false;
jstatus.set(true);
do {
try {
String line;
while (running.get() && (line = jline.readLine((USE_JLINE.def())?">":"")) != null) {
if (!running.get() || line.replaceAll("\\s", "").length() == 0) continue;
jstatus.set(false);
input(line);
jstatus.set(true);
}
} catch (UserInterruptException e) {
if (!interrupted) {
Expand All @@ -175,6 +182,7 @@ private void read() {
} catch (Exception e) {
engine.getAppInfo().getLogger().error.println(e);
}
jstatus.set(false);
}
private void input(String line) {
if (Util.isNull(line)) throw new NullPointerException();
Expand Down

0 comments on commit 7b9dc59

Please sign in to comment.