Skip to content

Commit

Permalink
Debug handling in Core instead of impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 28, 2022
1 parent 1ec9dc5 commit 4dc95cf
Show file tree
Hide file tree
Showing 30 changed files with 772 additions and 174 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/denizenscript/denizencore/DenizenCore.java
Expand Up @@ -17,6 +17,8 @@
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import com.denizenscript.denizencore.utilities.debugging.DebugSubmitter;
import com.denizenscript.denizencore.utilities.debugging.LogInterceptor;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable;
Expand Down Expand Up @@ -135,14 +137,13 @@ public static void init(DenizenImplementation implementation) {
currentTimeMonotonicMillis = CoreUtilities.monotonicMillis();
DenizenCore.implementation = implementation;
MAIN_THREAD = Thread.currentThread();
Debug.log("Initializing Denizen Core v" + VERSION +
", implementation for " + implementation.getImplementationName()
+ " version " + implementation.getImplementationVersion());
Debug.log("Initializing Denizen Core v" + VERSION + ", impl for " + implementation.getImplementationName() + " v" + implementation.getImplementationVersion());
ScriptRegistry._registerCoreTypes();
ScriptEvent.registerCoreEvents();
ObjectFetcher.registerCoreObjects();
TagManager.registerCoreTags();
commandRegistry.registerCoreCommands();
DebugSubmitter.init();
}

/**
Expand Down Expand Up @@ -184,8 +185,8 @@ public static void preloadScripts() {
ScriptHelper.reloadScripts();
}
catch (Exception ex) {
implementation.debugMessage("Error loading scripts:");
implementation.debugException(ex);
Debug.echoError("Error loading scripts:");
Debug.echoError(ex);
}
}

Expand All @@ -210,8 +211,8 @@ public static void postLoadScripts() {
ScriptsLoadedScriptEvent.instance.fire();
}
catch (Exception ex) {
implementation.debugMessage("Error loading scripts:");
implementation.debugException(ex);
Debug.echoError("Error loading scripts:");
Debug.echoError(ex);
}
}

Expand All @@ -237,7 +238,8 @@ public static void schedule(Schedulable sched) {

/** Returns true if called from the thread that DenizenCore understands to be the main thread, or false if on a different thread. */
public static boolean isMainThread() {
return Thread.currentThread().equals(MAIN_THREAD);
Thread curThread = Thread.currentThread();
return curThread.equals(MAIN_THREAD) || (curThread.equals(TagManager.tagThread));
}

/** Runs the task immediately if called on main thread, or later if called off-thread. */
Expand Down Expand Up @@ -269,6 +271,7 @@ static void oncePerSecond() {
* @param ms_elapsed how many MS have actually elapsed. (50 on a standard engine).
*/
public static void tick(int ms_elapsed) {
DebugInternals.onTick();
serverTimeMillis += ms_elapsed;
currentTimeMillis = System.currentTimeMillis();
currentTimeMonotonicMillis = CoreUtilities.monotonicMillis();
Expand Down
Expand Up @@ -8,11 +8,8 @@
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import com.denizenscript.denizencore.utilities.debugging.Debug;

import java.io.File;
import java.util.function.Consumer;

/**
* Interface representing all the information that an implementation must provide to the engine.
Expand All @@ -30,71 +27,6 @@ public interface DenizenImplementation {
*/
String getImplementationVersion();

/**
* Output a debug message to console.
*/
void debugMessage(String message);

/**
* Output a debug message to console.
*/
void debugMessage(String caller, String message);

/**
* Output a debug message to console.
*/
void debugMessage(Debug.DebugElement element, String message);

/**
* Output an exception to console.
*/
void debugException(Throwable ex);

/**
* Output an error to console.
*/
void debugError(String addedContext, String error);

/**
* Output an error to console, specific to a script queue.
*/
void debugError(ScriptEntry queue, String addedContext, String error);

/**
* Output an error to console, specific to a script queue.
*/
void debugError(ScriptEntry queue, Throwable error);

/**
* Output a command information report.
*/
void debugReport(Debuggable caller, String name, String message);

/**
* Output a command information report.
*/
void debugReport(Debuggable caller, String name, Object... values);

/**
* Output an 'Okay!' message.
*/
void debugApproval(String message);

/**
* Outputs a message specific to a debuggable object.
*/
void debugEntry(Debuggable entry, String message);

/**
* Outputs a message specific to a debuggable object.
*/
void debugEntry(Debuggable entry, Debug.DebugElement element, String message);

/**
* Outputs a message specific to a debuggable object.
*/
void debugEntry(Debuggable entry, Debug.DebugElement element);

/**
* Return the name of the implementation.
* EG, "Gamey Game".
Expand Down Expand Up @@ -135,10 +67,6 @@ public interface DenizenImplementation {

boolean needsHandleArgPrefix(String prefix);

boolean shouldDebug(Debuggable debug);

void debugQueueExecute(ScriptEntry entry, String queue, String execute);

boolean canWriteToFile(File f);

String getRandomColor();
Expand All @@ -149,19 +77,19 @@ public interface DenizenImplementation {

String queueHeaderInfo(ScriptEntry entry);

void submitRecording(Consumer<String> processResult);

FlaggableObject simpleWordToFlaggable(String word, ScriptEntry entry);

ObjectTag getSpecialDef(String def, ScriptQueue queue);

boolean setSpecialDef(String def, ScriptQueue queue, ObjectTag value);

String getTextColor();
void saveClassToLoader(Class<?> clazz);

String getEmphasisColor();
void addExtraErrorHeaders(StringBuilder headerBuilder, ScriptEntry source);

void saveClassToLoader(Class<?> clazz);
String applyDebugColors(String uncolored);

void doFinalDebugOutput(String rawText);

boolean isSafeThread();
String stripColor(String message);
}
Expand Up @@ -85,10 +85,6 @@ public static List<String> trimEvents(List<String> original) {
List<String> event = new ArrayList<>(original);
List<String> parsed = new ArrayList<>();

if (Debug.showEventsTrimming) {
Debug.echoApproval("Trimming world events '" + event + '\'');
}

// Remove any duplicate event names
for (int i = 0; i < event.size(); i++) {
for (int x = 0; x < event.size(); x++) {
Expand Down
Expand Up @@ -20,6 +20,7 @@
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import com.denizenscript.denizencore.utilities.scheduling.OneTimeSchedulable;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import com.denizenscript.denizencore.DenizenCore;
Expand Down Expand Up @@ -81,7 +82,7 @@ public static void registerScriptEvent(Class<? extends ScriptEvent> eventClass)
public static List<String> notNameParts = new ArrayList<>(Collections.singleton("ScriptEvent"));

public static void registerScriptEvent(ScriptEvent event) {
String name = event.getClass().getSimpleName();
String name = DebugInternals.getClassNameOpti(event.getClass());
for (String suffix : notNameParts) {
if (name.endsWith(suffix)) {
name = name.substring(0, name.length() - suffix.length());
Expand Down Expand Up @@ -392,7 +393,7 @@ private static void reloadPreClear() {

private static void loadSinglePath(StringHolder evt1, ScriptContainer container) {
if (CoreUtilities.contains(evt1.str, '@')) {
Debug.echoError("Script '<Y>" + container.getName() + "<W>' has event '<Y>" + evt1.str.replace("@", "<R>@<Y>")
Debug.echoError("Script '<Y>" + container.getName() + "<W>' has event '<Y>" + evt1.str.replace("@", "<LR>@<Y>")
+ "<W>' which contains object notation, which is deprecated for use in world events. Please remove it.");
}
String evt;
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;

public class ServerGeneratesExceptionScriptEvent extends ScriptEvent {

Expand Down Expand Up @@ -58,7 +59,7 @@ public ObjectTag getContext(String name) {
switch (name) {
case "message": return new ElementTag(exception.getMessage());
case "full_trace": return new ElementTag(fullTrace);
case "type": return new ElementTag(exception.getClass().getSimpleName());
case "type": return new ElementTag(DebugInternals.getClassNameOpti(exception.getClass()));
case "queue":
if (queue != null) {
return new QueueTag(queue);
Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.EnumHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;

public class Argument implements Cloneable {

Expand Down Expand Up @@ -314,7 +315,7 @@ public ElementTag asElement() {
public <T extends ObjectTag> T asType(Class<T> clazz) {
T arg = object.asType(clazz, scriptEntry.context);
if (arg == null) {
Debug.echoError("Cannot process argument '" + object + "' as type '" + clazz.getSimpleName() + "' (conversion returned null).");
Debug.echoError("Cannot process argument '" + object + "' as type '" + DebugInternals.getClassNameOpti(clazz) + "' (conversion returned null).");
return null;
}
arg.setPrefix(prefix);
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.debugging.Debug;

import java.util.*;
Expand Down Expand Up @@ -82,11 +83,9 @@ else if (currentQuote == c) {
if (start < len) {
matchList.add(stringArgs.substring(start));
}

if (Debug.showScriptBuilder) {
if (CoreConfiguration.debugScriptBuilder) {
Debug.log("Constructed args: " + Arrays.toString(matchList.toArray()));
}

return matchList.toArray(new String[0]);
}

Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;

public class Mechanism {

Expand Down Expand Up @@ -121,7 +122,7 @@ public boolean requireEnum(String error, Class<? extends Enum> clazz) {
return true;
}
if (error == null) {
echoError("Invalid " + clazz.getSimpleName() + ". Must specify a valid name.");
echoError("Invalid " + DebugInternals.getClassNameOpti(clazz) + ". Must specify a valid name.");
}
else {
echoError(error);
Expand Down Expand Up @@ -150,8 +151,7 @@ public <T extends ObjectTag> boolean requireObject(String error, Class<T> type)
return true;
}
if (error == null) {
// TODO: Remove getSimpleName(), or simplify somehow.
echoError("Invalid " + type.getSimpleName() + " specified.");
echoError("Invalid " + DebugInternals.getClassNameOpti(type) + " specified.");
}
else {
echoError(error);
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;

import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
Expand Down Expand Up @@ -212,7 +213,7 @@ public static void registerWithObjectFetcher(Class<? extends ObjectTag> objectTa
}

public static <T extends ObjectTag> ObjectType<T> registerWithObjectFetcher(Class<T> objectTag, ObjectTagProcessor<T> processor) {
String className = objectTag.getSimpleName();
String className = DebugInternals.getClassNameOpti(objectTag);
String shortName = null;
if (className.endsWith("Tag")) {
shortName = className.substring(0, className.length() - "Tag".length());
Expand Down Expand Up @@ -245,7 +246,7 @@ public static <T extends ObjectTag> ObjectType<T> registerWithObjectFetcher(Clas
newType.prefix = identifier;
}
else {
Debug.echoError("Type '" + objectTag.getSimpleName() + "' registered as an object type, but doesn't have a fetcher prefix.");
Debug.echoError("Type '" + DebugInternals.getClassNameOpti(objectTag) + "' registered as an object type, but doesn't have a fetcher prefix.");
}
newType.matches = getMatchesFor(objectTag);
newType.valueOf = getValueOfFor(objectTag);
Expand All @@ -256,7 +257,7 @@ public static <T extends ObjectTag> ObjectType<T> registerWithObjectFetcher(Clas
}
}
catch (Throwable ex) {
Debug.echoError("Failed to initialize an object type(" + objectTag.getSimpleName() + "): ");
Debug.echoError("Failed to initialize an object type(" + DebugInternals.getClassNameOpti(objectTag) + "): ");
Debug.echoError(ex);
}
return newType;
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.*;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -159,7 +160,7 @@ public static void registerTags() {
// Returns the simple/short class name of the reflected object, such as "JavaReflectedObjectTag".
// -->
tagProcessor.registerStaticTag(ElementTag.class, "simple_class_name", (attribute, object) -> {
return new ElementTag(object.object.getClass().getSimpleName());
return new ElementTag(DebugInternals.getClassNameOpti(object.object.getClass()));
});

// <--[tag]
Expand Down
Expand Up @@ -11,6 +11,7 @@
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.utilities.*;
import com.denizenscript.denizencore.utilities.debugging.DebugInternals;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
Expand Down Expand Up @@ -576,11 +577,11 @@ public <T extends ObjectTag> List<T> filter(Class<T> dClass, TagContext context,
results.add(object);
}
else if (showFailure) {
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + dClass.getSimpleName() + "' (conversion returned null).");
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + DebugInternals.getClassNameOpti(dClass) + "' (conversion returned null).");
}
}
else if (showFailure) {
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + dClass.getSimpleName() + "' (does not match expected type).");
Debug.echoError("Cannot process list-entry '" + obj + "' as type '" + DebugInternals.getClassNameOpti(dClass) + "' (does not match expected type).");
}
}
catch (Exception e) {
Expand Down

0 comments on commit 4dc95cf

Please sign in to comment.