Document processCrashContent, add List support

This commit is contained in:
JeremyStar™ 2024-07-31 15:03:00 +02:00
parent c984974252
commit 1a56f42287
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -25,6 +25,7 @@ import de.staropensource.sosengine.base.classes.Placeholder;
import de.staropensource.sosengine.base.events.EngineCrashEvent;
import de.staropensource.sosengine.base.events.EngineSoftCrashEvent;
import de.staropensource.sosengine.base.internal.placeholders.crashhandler.*;
import de.staropensource.sosengine.base.types.EngineState;
import de.staropensource.sosengine.base.types.logging.LogLevel;
import de.staropensource.sosengine.base.utility.PlaceholderEngine;
import lombok.Getter;
@ -180,49 +181,79 @@ Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party su
/*
* Note: This entire method causes a compilation warning as we are using "unchecked or unsecure operations".
* We can safely ignore this as this method
* 1. checks data types as best as it can,
* 2. only works on a String and not on a File or something which could cause damage, and
* 1. checks data types before using them,
* 2. only works on Strings, Lists and LinkedHashMaps and not on Files or something which could cause damage, and
* 3. we can trust our own engine and possibly subsystems not doing shit in here.
* As a subsystem developer you'll likely want useful crash information.
*
* But hey, if someone breaks this method (which may be possible idk didn't test it) then congrats!
*/
private static synchronized @NotNull String processCrashContent(@NotNull LinkedHashMap<Object, Object> map, int indentationSize) {
private static @NotNull String processCrashContent(@NotNull LinkedHashMap<Object, Object> map, int indentationSize) {
StringBuilder content = new StringBuilder();
for (Object key : map.keySet()) {
// Ensure key is of type String
if (!(key instanceof String))
// Invalid content key, skip
continue;
if (!(map.get(key) instanceof LinkedHashMap<?, ?> || map.get(key) instanceof String))
// Ensure value is of type String, List or LinkedHashMap
if (!(map.get(key) instanceof String
|| map.get(key) instanceof List<?>
|| map.get(key) instanceof LinkedHashMap<?, ?>))
// Invalid content value, skip
continue;
/*
* I'm too lazy to describe what you are seeing down below,
* so uuhhh just try breaking it and see what happens.
*/
// Add newline
if (!content.isEmpty())
content.append("\n");
if (map.get(key) == null) {
if (indentationSize == 0) content.append("\n");
else content.append(" ".repeat(indentationSize)).append("-> ");
content.append(key);
} else if (map.get(key) instanceof String) {
if (indentationSize == 0) content.append("\n");
else content.append(" ".repeat(indentationSize)).append("-> ");
content.append(key).append(": ").append(map.get(key));
} else {
if (indentationSize == 0) content.append("\n");
else content.append(" ".repeat(indentationSize)).append("-> ");
content.append(key).append("\n");
// Indent key
if (indentationSize == 0)
content.append("\n");
else
content
.append(" ".repeat(indentationSize))
.append("-> ");
//noinspection unchecked
content.append(processCrashContent((LinkedHashMap<Object, Object>) map.get(key), indentationSize + 1));
if (map.get(key) == null)
// Append key name, there's no content
// Format: %key%
content.append(key);
else if (map.get(key) instanceof String)
// Append key and it's value
// Format: %key%: %value%
content
.append(key)
.append(": ")
.append(map.get(key));
else if (map.get(key) instanceof List<?>) {
// Append key and list the list's contents
content.append(key);
for (Object item : (List<?>) map.get(key)) {
if (item instanceof String)
item = ((String) item)
.replace("\\", "\\\\")
.replace("\n", "\\n")
.replace("\"", "\\\"");
content
.append("\n")
.append(" ".repeat(indentationSize))
.append(" -> ")
.append(item);
}
} else
// So this one processes a map recursively
// Format:
// -> %parent_key%
// -> %child_key0%: %child_value0%
// -> %child_key1%
// -> %nested_key0%: %nested_value0%
// -> %nested_key1%: %nested_value1%
// -> %child_key2%: %child_value1%
//noinspection unchecked
content.append(key).append("\n").append(processCrashContent((LinkedHashMap<Object, Object>) map.get(key), indentationSize + 1));
}
return content.toString();
@ -234,7 +265,7 @@ Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party su
* @return crash content string
* @since v1-alpha0
*/
public static synchronized String processCrashContent() {
public static String processCrashContent() {
return processCrashContent(crashContent, 0);
}
}