Better engine exception for scripting

This commit is contained in:
Simon Lübeß
2025-07-30 18:58:26 +02:00
parent 72e87ee9ad
commit ed644b5b41
6 changed files with 74 additions and 36 deletions
+13 -19
View File
@@ -17,24 +17,16 @@ internal enum EngineResult
EntityNotFound = -4, // The entity doesn't exist or was deleted.
EntityDoesntHaveComponent = -5, // The entity has no component of type {typeof(T)} or it was deleted.
AssetNotFound = -6, // No asset exists for AssetHandle \"{assetId}\".
CustomError = 1 << 31
}
public class EngineException(string message) : Exception(message);
public class EntityNotFoundException : Exception
{
}
public class EntityNotFoundException(string? message = null) : Exception(message);
public class ComponentNotFoundException : Exception
{
}
public class ComponentNotFoundException(string? message = null) : Exception(message);
public class AssetNotFoundException : Exception
{
}
public class AssetNotFoundException(string? message = null) : Exception(message);
internal static class EngineErrors
{
@@ -43,22 +35,24 @@ internal static class EngineErrors
if (result >= 0)
return;
string engineMessage = ScriptGlue.GetLastExceptionMessage();
switch (result)
{
case EngineResult.Error:
throw new EngineException($"Unspecified engine excepiton in {callerName}");
throw new EngineException(engineMessage ?? $"Unspecified engine excepiton in {callerName}");
case EngineResult.NotImplemented:
throw new NotImplementedException($"Function {callerName} is not implemented in the engine.");
throw new NotImplementedException(engineMessage ?? $"Function {callerName} is not implemented in the engine.");
case EngineResult.ArgumentError:
throw new ArgumentException($"An argument passed to {callerName} is invalid.");
throw new ArgumentException(engineMessage ?? $"An argument passed to {callerName} is invalid.");
case EngineResult.EntityNotFound:
throw new EntityNotFoundException();
throw new EntityNotFoundException(engineMessage);
case EngineResult.EntityDoesntHaveComponent:
throw new ComponentNotFoundException();
throw new ComponentNotFoundException(engineMessage);
case EngineResult.AssetNotFound:
throw new AssetNotFoundException();
throw new AssetNotFoundException(engineMessage);
default:
throw new EngineException($"Unknown engine excepiton in {callerName}");
throw new EngineException(engineMessage ?? $"Unknown engine excepiton in {callerName}");
}
}
}
+5 -3
View File
@@ -319,7 +319,7 @@ internal static unsafe partial class ScriptGlue
struct ComponentFunctionPointers
{
public delegate* unmanaged[Cdecl]<UUID, void> AddComponent;
public delegate* unmanaged[Cdecl]<UUID, bool> HasComponent;
public delegate* unmanaged[Cdecl]<UUID, EngineResult> HasComponent;
public delegate* unmanaged[Cdecl]<UUID, void> RemoveComponent;
}
@@ -328,7 +328,7 @@ internal static unsafe partial class ScriptGlue
[UnmanagedCallersOnly]
public static void RegisterComponentType(byte* fullComponentTypeName,
delegate* unmanaged[Cdecl]<UUID, void> addComponent,
delegate* unmanaged[Cdecl]<UUID, bool> hasComponent,
delegate* unmanaged[Cdecl]<UUID, EngineResult> hasComponent,
delegate* unmanaged[Cdecl]<UUID, void> removeComponent)
{
string? beefComponentTypeName = Marshal.PtrToStringUTF8((IntPtr)fullComponentTypeName);
@@ -421,7 +421,9 @@ internal static unsafe partial class ScriptGlue
public static bool Entity_HasComponent(UUID entityId, Type componentType)
{
return ComponentTypeFunctions[componentType].HasComponent(entityId);
EngineResult returnValue = ComponentTypeFunctions[componentType].HasComponent(entityId);
EngineErrors.ThrowIfError(returnValue);
return returnValue == EngineResult.Ok;
}
public static void Entity_RemoveComponent(UUID entityId, Type componentType)