ScriptGlue: Added Log.Exception

- Also pushed missing List Editor fix
This commit is contained in:
Simon Lübeß
2024-01-15 01:21:56 +01:00
parent 49db3a28c8
commit ceb4241216
6 changed files with 96 additions and 29 deletions
+11 -5
View File
@@ -806,21 +806,22 @@ static class ScriptEngine
return scriptClass;
}
internal static void HandleMonoException(MonoException* exception, ScriptInstance sourceInstance = null)
internal static void HandleMonoException(MonoException* exception, UUID entityId)
{
MonoExceptionHelper wrappedException = new MonoExceptionHelper(exception);
String entityInfo = scope .();
if (sourceInstance != null)
if (entityId != .Zero)
{
wrappedException.Instance = sourceInstance.EntityId;
wrappedException.Instance = entityId;
Result<Entity> sourceEntity = Context.GetEntityByID(sourceInstance.EntityId);
Result<Entity> sourceEntity = Context.GetEntityByID(entityId);
if (sourceEntity case .Ok(let e))
{
entityInfo.AppendF($" ({e.Name} | {sourceInstance.EntityId})");
entityInfo.AppendF($" ({e.Name} | {entityId})");
}
}
@@ -829,6 +830,11 @@ static class ScriptEngine
wrappedException.ReleaseRef();
}
internal static void HandleMonoException(MonoException* exception, ScriptInstance sourceInstance = null)
{
HandleMonoException(exception, sourceInstance?.EntityId ?? .Zero);
}
public static void ShowScriptEditor(Entity entity, ScriptComponent* scriptComponent)
{
if (scriptComponent.Instance == null)
+12 -2
View File
@@ -107,8 +107,10 @@ static class ScriptGlue
}
}
[RegisterCall("Log::LogMessage_Impl")]
static void Log(int32 logLevel, MonoString* message)
#region Log
[RegisterCall("ScriptGlue::Log_LogMessage")]
static void Log_LogMessage(int32 logLevel, MonoString* message)
{
char8* utfMessage = Mono.mono_string_to_utf8(message);
@@ -117,6 +119,14 @@ static class ScriptGlue
Mono.mono_free(utfMessage);
}
[RegisterCall("ScriptGlue::Log_LogException")]
static void Log_LogException(MonoException* exception, UUID entityId)
{
ScriptEngine.HandleMonoException(exception, entityId);
}
#endregion
#region Input
[RegisterCall("Input::IsKeyPressed")]
+40 -6
View File
@@ -73,7 +73,7 @@ internal class EntityEditor
}
catch (Exception e)
{
Log.Error(e);
Log.Exception(e);
}
return false;
@@ -558,7 +558,7 @@ internal class EntityEditor
}
catch (Exception e)
{
Log.Error(e);
Log.Exception(e);
}
}
}
@@ -712,6 +712,18 @@ internal class EntityEditor
ImGui.PopID();
}
ShowButtons(type, reference);
}
/// <summary>
/// Shows all buttons in the UI.
/// </summary>
private static void ShowButtons(Type type, object? reference)
{
ImGui.PushID("Buttons");
int i = 0;
// Iterate all methods
foreach (MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
@@ -735,11 +747,24 @@ internal class EntityEditor
}
else if (ImGui.Button(showButton.ButtonText))
{
method.Invoke(reference, null);
try
{
method.Invoke(reference, null);
}
catch (TargetInvocationException ex)
{
Log.Exception(ex.InnerException ?? ex);
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
ImGui.PopID();
}
ImGui.PopID();
}
struct ListPayload
@@ -764,8 +789,17 @@ internal class EntityEditor
Debug.Assert(list != null);
}
object? newElement = ActivatorExtension.CreateInstanceSafe(elementType);
list?.Add(newElement);
try
{
// Reference values can be null
object? newElement = elementType.IsByRef ? null : ActivatorExtension.CreateInstanceSafe(elementType);
list?.Add(newElement);
}
catch (Exception ex)
{
Log.Error($"Failed to add new element to list: {ex}");
}
}
void RemoveElement(IList? list, out object? newList)
@@ -882,7 +916,7 @@ internal class EntityEditor
ImGui.SameLine(ImGui.GetWindowContentRegionMax().X - removeButtonWidth);
ImGui.BeginDisabled(myList == null || myList.Count == 0);
ImGui.BeginDisabled(myList == null);
if (ImGui.SmallButton("-"))
{
+20 -14
View File
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System;
namespace GlitchyEngine;
@@ -10,7 +10,7 @@ public class Log
/// <summary>
/// The severity of the log message.
/// </summary>
private enum LogLevel
internal enum LogLevel
{
Trace = 0,
Debug,
@@ -27,7 +27,7 @@ public class Log
/// <param name="message">The message to log.</param>
public static void Trace(string message)
{
LogMessage_Impl(LogLevel.Trace, message);
ScriptGlue.Log_LogMessage(LogLevel.Trace, message);
}
/// <summary>
@@ -36,7 +36,7 @@ public class Log
/// <param name="message">The message to log.</param>
public static void Info(string message)
{
LogMessage_Impl(LogLevel.Info, message);
ScriptGlue.Log_LogMessage(LogLevel.Info, message);
}
/// <summary>
@@ -45,7 +45,7 @@ public class Log
/// <param name="message">The message to log.</param>
public static void Warning(string message)
{
LogMessage_Impl(LogLevel.Warning, message);
ScriptGlue.Log_LogMessage(LogLevel.Warning, message);
}
/// <summary>
@@ -54,7 +54,7 @@ public class Log
/// <param name="message">The message to log.</param>
public static void Error(string message)
{
LogMessage_Impl(LogLevel.Error, message);
ScriptGlue.Log_LogMessage(LogLevel.Error, message);
}
/// <summary>
@@ -63,7 +63,7 @@ public class Log
/// <param name="message">The message to log.</param>
public static void Critical(string message)
{
LogMessage_Impl(LogLevel.Critical, message);
ScriptGlue.Log_LogMessage(LogLevel.Critical, message);
}
/// <summary>
@@ -72,7 +72,7 @@ public class Log
/// <param name="obj">The object to serialize.</param>
public static void Trace(object obj)
{
LogMessage_Impl(LogLevel.Trace, obj.ToString());
ScriptGlue.Log_LogMessage(LogLevel.Trace, obj.ToString());
}
/// <summary>
@@ -81,7 +81,7 @@ public class Log
/// <param name="obj">The object to serialize.</param>
public static void Info(object obj)
{
LogMessage_Impl(LogLevel.Info, obj.ToString());
ScriptGlue.Log_LogMessage(LogLevel.Info, obj.ToString());
}
/// <summary>
@@ -90,7 +90,7 @@ public class Log
/// <param name="obj">The object to serialize.</param>
public static void Warning(object obj)
{
LogMessage_Impl(LogLevel.Warning, obj.ToString());
ScriptGlue.Log_LogMessage(LogLevel.Warning, obj.ToString());
}
/// <summary>
@@ -99,7 +99,7 @@ public class Log
/// <param name="obj">The object to serialize.</param>
public static void Error(object obj)
{
LogMessage_Impl(LogLevel.Error, obj.ToString());
ScriptGlue.Log_LogMessage(LogLevel.Error, obj.ToString());
}
/// <summary>
@@ -108,9 +108,15 @@ public class Log
/// <param name="obj">The object to serialize.</param>
public static void Critical(object obj)
{
LogMessage_Impl(LogLevel.Critical, obj.ToString());
ScriptGlue.Log_LogMessage(LogLevel.Critical, obj.ToString());
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern string LogMessage_Impl(LogLevel logLevel, string message);
/// <summary>
/// Logs the give exception.
/// </summary>
/// <param name="exception">The exception to log.</param>
public static void Exception(Exception exception)
{
ScriptGlue.Log_LogException(exception);
}
}
+11
View File
@@ -11,6 +11,17 @@ namespace GlitchyEngine;
/// </summary>
internal static class ScriptGlue
{
#region Log
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Log_LogMessage(Log.LogLevel logLevel, string message);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void Log_LogException(Exception exception);
#endregion
#region Entity
[MethodImpl(MethodImplOptions.InternalCall)]
+1 -1
View File
@@ -134,7 +134,7 @@ public class SerializedObject
}
catch (Exception e)
{
Log.Error(e);
Log.Exception(e);
// Don't attempt to use any other serializer after this error...
return true;
}