From ceb42412165ef3976712e96719718806df7db38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Mon, 15 Jan 2024 01:21:56 +0100 Subject: [PATCH] ScriptGlue: Added Log.Exception - Also pushed missing List Editor fix --- GlitchyEngine/src/Scripting/ScriptEngine.bf | 16 +++++-- GlitchyEngine/src/Scripting/ScriptGlue.bf | 14 +++++- ScriptCore/Editor/EntityEditor.cs | 48 +++++++++++++++++--- ScriptCore/Log.cs | 34 ++++++++------ ScriptCore/ScriptGlue.cs | 11 +++++ ScriptCore/Serialization/SerializedObject.cs | 2 +- 6 files changed, 96 insertions(+), 29 deletions(-) diff --git a/GlitchyEngine/src/Scripting/ScriptEngine.bf b/GlitchyEngine/src/Scripting/ScriptEngine.bf index 87f78b4..2d09e65 100644 --- a/GlitchyEngine/src/Scripting/ScriptEngine.bf +++ b/GlitchyEngine/src/Scripting/ScriptEngine.bf @@ -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 sourceEntity = Context.GetEntityByID(sourceInstance.EntityId); + Result 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) diff --git a/GlitchyEngine/src/Scripting/ScriptGlue.bf b/GlitchyEngine/src/Scripting/ScriptGlue.bf index f974daa..aca39a8 100644 --- a/GlitchyEngine/src/Scripting/ScriptGlue.bf +++ b/GlitchyEngine/src/Scripting/ScriptGlue.bf @@ -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")] diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index 370ba21..7625f3b 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -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); } } } @@ -711,7 +711,19 @@ internal class EntityEditor ImGui.PopID(); } - + + ShowButtons(type, reference); + } + + /// + /// Shows all buttons in the UI. + /// + 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 @@ -763,9 +788,18 @@ internal class EntityEditor 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) @@ -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("-")) { diff --git a/ScriptCore/Log.cs b/ScriptCore/Log.cs index e3e57f4..dc3180f 100644 --- a/ScriptCore/Log.cs +++ b/ScriptCore/Log.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using System; namespace GlitchyEngine; @@ -10,7 +10,7 @@ public class Log /// /// The severity of the log message. /// - private enum LogLevel + internal enum LogLevel { Trace = 0, Debug, @@ -27,7 +27,7 @@ public class Log /// The message to log. public static void Trace(string message) { - LogMessage_Impl(LogLevel.Trace, message); + ScriptGlue.Log_LogMessage(LogLevel.Trace, message); } /// @@ -36,7 +36,7 @@ public class Log /// The message to log. public static void Info(string message) { - LogMessage_Impl(LogLevel.Info, message); + ScriptGlue.Log_LogMessage(LogLevel.Info, message); } /// @@ -45,7 +45,7 @@ public class Log /// The message to log. public static void Warning(string message) { - LogMessage_Impl(LogLevel.Warning, message); + ScriptGlue.Log_LogMessage(LogLevel.Warning, message); } /// @@ -54,7 +54,7 @@ public class Log /// The message to log. public static void Error(string message) { - LogMessage_Impl(LogLevel.Error, message); + ScriptGlue.Log_LogMessage(LogLevel.Error, message); } /// @@ -63,7 +63,7 @@ public class Log /// The message to log. public static void Critical(string message) { - LogMessage_Impl(LogLevel.Critical, message); + ScriptGlue.Log_LogMessage(LogLevel.Critical, message); } /// @@ -72,7 +72,7 @@ public class Log /// The object to serialize. public static void Trace(object obj) { - LogMessage_Impl(LogLevel.Trace, obj.ToString()); + ScriptGlue.Log_LogMessage(LogLevel.Trace, obj.ToString()); } /// @@ -81,7 +81,7 @@ public class Log /// The object to serialize. public static void Info(object obj) { - LogMessage_Impl(LogLevel.Info, obj.ToString()); + ScriptGlue.Log_LogMessage(LogLevel.Info, obj.ToString()); } /// @@ -90,7 +90,7 @@ public class Log /// The object to serialize. public static void Warning(object obj) { - LogMessage_Impl(LogLevel.Warning, obj.ToString()); + ScriptGlue.Log_LogMessage(LogLevel.Warning, obj.ToString()); } /// @@ -99,7 +99,7 @@ public class Log /// The object to serialize. public static void Error(object obj) { - LogMessage_Impl(LogLevel.Error, obj.ToString()); + ScriptGlue.Log_LogMessage(LogLevel.Error, obj.ToString()); } /// @@ -108,9 +108,15 @@ public class Log /// The object to serialize. 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); + /// + /// Logs the give exception. + /// + /// The exception to log. + public static void Exception(Exception exception) + { + ScriptGlue.Log_LogException(exception); + } } diff --git a/ScriptCore/ScriptGlue.cs b/ScriptCore/ScriptGlue.cs index 996d222..e0b9f83 100644 --- a/ScriptCore/ScriptGlue.cs +++ b/ScriptCore/ScriptGlue.cs @@ -11,6 +11,17 @@ namespace GlitchyEngine; /// 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)] diff --git a/ScriptCore/Serialization/SerializedObject.cs b/ScriptCore/Serialization/SerializedObject.cs index a42a7d3..a0e8a99 100644 --- a/ScriptCore/Serialization/SerializedObject.cs +++ b/ScriptCore/Serialization/SerializedObject.cs @@ -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; }