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; return scriptClass;
} }
internal static void HandleMonoException(MonoException* exception, ScriptInstance sourceInstance = null)
internal static void HandleMonoException(MonoException* exception, UUID entityId)
{ {
MonoExceptionHelper wrappedException = new MonoExceptionHelper(exception); MonoExceptionHelper wrappedException = new MonoExceptionHelper(exception);
String entityInfo = scope .(); 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)) 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(); wrappedException.ReleaseRef();
} }
internal static void HandleMonoException(MonoException* exception, ScriptInstance sourceInstance = null)
{
HandleMonoException(exception, sourceInstance?.EntityId ?? .Zero);
}
public static void ShowScriptEditor(Entity entity, ScriptComponent* scriptComponent) public static void ShowScriptEditor(Entity entity, ScriptComponent* scriptComponent)
{ {
if (scriptComponent.Instance == null) if (scriptComponent.Instance == null)
+12 -2
View File
@@ -107,8 +107,10 @@ static class ScriptGlue
} }
} }
[RegisterCall("Log::LogMessage_Impl")] #region Log
static void Log(int32 logLevel, MonoString* message)
[RegisterCall("ScriptGlue::Log_LogMessage")]
static void Log_LogMessage(int32 logLevel, MonoString* message)
{ {
char8* utfMessage = Mono.mono_string_to_utf8(message); char8* utfMessage = Mono.mono_string_to_utf8(message);
@@ -117,6 +119,14 @@ static class ScriptGlue
Mono.mono_free(utfMessage); Mono.mono_free(utfMessage);
} }
[RegisterCall("ScriptGlue::Log_LogException")]
static void Log_LogException(MonoException* exception, UUID entityId)
{
ScriptEngine.HandleMonoException(exception, entityId);
}
#endregion
#region Input #region Input
[RegisterCall("Input::IsKeyPressed")] [RegisterCall("Input::IsKeyPressed")]
+41 -7
View File
@@ -73,7 +73,7 @@ internal class EntityEditor
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e); Log.Exception(e);
} }
return false; return false;
@@ -558,7 +558,7 @@ internal class EntityEditor
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e); Log.Exception(e);
} }
} }
} }
@@ -711,7 +711,19 @@ internal class EntityEditor
ImGui.PopID(); 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 // Iterate all methods
foreach (MethodInfo method in type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) 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)) 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();
} }
ImGui.PopID();
} }
struct ListPayload struct ListPayload
@@ -763,9 +788,18 @@ internal class EntityEditor
Debug.Assert(list != null); Debug.Assert(list != null);
} }
try
{
// Reference values can be null
object? newElement = elementType.IsByRef ? null : ActivatorExtension.CreateInstanceSafe(elementType);
object? newElement = ActivatorExtension.CreateInstanceSafe(elementType); list?.Add(newElement);
list?.Add(newElement); }
catch (Exception ex)
{
Log.Error($"Failed to add new element to list: {ex}");
}
} }
void RemoveElement(IList? list, out object? newList) void RemoveElement(IList? list, out object? newList)
@@ -882,7 +916,7 @@ internal class EntityEditor
ImGui.SameLine(ImGui.GetWindowContentRegionMax().X - removeButtonWidth); ImGui.SameLine(ImGui.GetWindowContentRegionMax().X - removeButtonWidth);
ImGui.BeginDisabled(myList == null || myList.Count == 0); ImGui.BeginDisabled(myList == null);
if (ImGui.SmallButton("-")) if (ImGui.SmallButton("-"))
{ {
+20 -14
View File
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices; using System;
namespace GlitchyEngine; namespace GlitchyEngine;
@@ -10,7 +10,7 @@ public class Log
/// <summary> /// <summary>
/// The severity of the log message. /// The severity of the log message.
/// </summary> /// </summary>
private enum LogLevel internal enum LogLevel
{ {
Trace = 0, Trace = 0,
Debug, Debug,
@@ -27,7 +27,7 @@ public class Log
/// <param name="message">The message to log.</param> /// <param name="message">The message to log.</param>
public static void Trace(string message) public static void Trace(string message)
{ {
LogMessage_Impl(LogLevel.Trace, message); ScriptGlue.Log_LogMessage(LogLevel.Trace, message);
} }
/// <summary> /// <summary>
@@ -36,7 +36,7 @@ public class Log
/// <param name="message">The message to log.</param> /// <param name="message">The message to log.</param>
public static void Info(string message) public static void Info(string message)
{ {
LogMessage_Impl(LogLevel.Info, message); ScriptGlue.Log_LogMessage(LogLevel.Info, message);
} }
/// <summary> /// <summary>
@@ -45,7 +45,7 @@ public class Log
/// <param name="message">The message to log.</param> /// <param name="message">The message to log.</param>
public static void Warning(string message) public static void Warning(string message)
{ {
LogMessage_Impl(LogLevel.Warning, message); ScriptGlue.Log_LogMessage(LogLevel.Warning, message);
} }
/// <summary> /// <summary>
@@ -54,7 +54,7 @@ public class Log
/// <param name="message">The message to log.</param> /// <param name="message">The message to log.</param>
public static void Error(string message) public static void Error(string message)
{ {
LogMessage_Impl(LogLevel.Error, message); ScriptGlue.Log_LogMessage(LogLevel.Error, message);
} }
/// <summary> /// <summary>
@@ -63,7 +63,7 @@ public class Log
/// <param name="message">The message to log.</param> /// <param name="message">The message to log.</param>
public static void Critical(string message) public static void Critical(string message)
{ {
LogMessage_Impl(LogLevel.Critical, message); ScriptGlue.Log_LogMessage(LogLevel.Critical, message);
} }
/// <summary> /// <summary>
@@ -72,7 +72,7 @@ public class Log
/// <param name="obj">The object to serialize.</param> /// <param name="obj">The object to serialize.</param>
public static void Trace(object obj) public static void Trace(object obj)
{ {
LogMessage_Impl(LogLevel.Trace, obj.ToString()); ScriptGlue.Log_LogMessage(LogLevel.Trace, obj.ToString());
} }
/// <summary> /// <summary>
@@ -81,7 +81,7 @@ public class Log
/// <param name="obj">The object to serialize.</param> /// <param name="obj">The object to serialize.</param>
public static void Info(object obj) public static void Info(object obj)
{ {
LogMessage_Impl(LogLevel.Info, obj.ToString()); ScriptGlue.Log_LogMessage(LogLevel.Info, obj.ToString());
} }
/// <summary> /// <summary>
@@ -90,7 +90,7 @@ public class Log
/// <param name="obj">The object to serialize.</param> /// <param name="obj">The object to serialize.</param>
public static void Warning(object obj) public static void Warning(object obj)
{ {
LogMessage_Impl(LogLevel.Warning, obj.ToString()); ScriptGlue.Log_LogMessage(LogLevel.Warning, obj.ToString());
} }
/// <summary> /// <summary>
@@ -99,7 +99,7 @@ public class Log
/// <param name="obj">The object to serialize.</param> /// <param name="obj">The object to serialize.</param>
public static void Error(object obj) public static void Error(object obj)
{ {
LogMessage_Impl(LogLevel.Error, obj.ToString()); ScriptGlue.Log_LogMessage(LogLevel.Error, obj.ToString());
} }
/// <summary> /// <summary>
@@ -108,9 +108,15 @@ public class Log
/// <param name="obj">The object to serialize.</param> /// <param name="obj">The object to serialize.</param>
public static void Critical(object obj) public static void Critical(object obj)
{ {
LogMessage_Impl(LogLevel.Critical, obj.ToString()); ScriptGlue.Log_LogMessage(LogLevel.Critical, obj.ToString());
} }
[MethodImpl(MethodImplOptions.InternalCall)] /// <summary>
private static extern string LogMessage_Impl(LogLevel logLevel, string message); /// 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> /// </summary>
internal static class ScriptGlue 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 #region Entity
[MethodImpl(MethodImplOptions.InternalCall)] [MethodImpl(MethodImplOptions.InternalCall)]
+1 -1
View File
@@ -134,7 +134,7 @@ public class SerializedObject
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e); Log.Exception(e);
// Don't attempt to use any other serializer after this error... // Don't attempt to use any other serializer after this error...
return true; return true;
} }