From ed3884e30e16dc689fa170050ef05ad28804fc52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 31 Dec 2023 17:05:27 +0100 Subject: [PATCH] ScriptCore: Made creation of class instance safer --- ScriptCore/Editor/EntityEditor.cs | 3 +- ScriptCore/Extensions/ActivatorExtension.cs | 31 +++++++++++++++++++ .../Serialization/DeserializationObject.cs | 20 ++---------- 3 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 ScriptCore/Extensions/ActivatorExtension.cs diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index ec20147..458090f 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -300,8 +300,7 @@ internal class EntityEditor { if (ImGui.Selectable(t.Name)) { - object instance = Activator.CreateInstance(t); - newValue = instance; + newValue = ActivatorExtension.CreateInstanceSafe(t); } } diff --git a/ScriptCore/Extensions/ActivatorExtension.cs b/ScriptCore/Extensions/ActivatorExtension.cs new file mode 100644 index 0000000..220623f --- /dev/null +++ b/ScriptCore/Extensions/ActivatorExtension.cs @@ -0,0 +1,31 @@ +using System; + +namespace GlitchyEngine.Extensions; + +public static class ActivatorExtension +{ + /// Creates an instance of the specified type using that type's default constructor. + /// The type of object to create. + /// A reference to the newly created object; or if no instance could be created. + public static object CreateInstanceSafe(Type type) + { + try + { + return Activator.CreateInstance(type, true); + } + catch (MissingMethodException e) + { + Log.Error($"Failed to create instance of type \"{type}\": The type doesn't contain a constructor with zero parameters.\nMake sure the type has a constructor that takes no arguments (It can be private!).\n{e}"); + } + catch (MethodAccessException e) + { + Log.Error($"Failed to create instance of type \"{type}\": The default constructor is not accessible.\n{e}"); + } + catch (Exception e) + { + Log.Error($"Failed to create instance of type \"{type}\": {e}"); + } + + return null; + } +} diff --git a/ScriptCore/Serialization/DeserializationObject.cs b/ScriptCore/Serialization/DeserializationObject.cs index c4aaf31..e1e27a5 100644 --- a/ScriptCore/Serialization/DeserializationObject.cs +++ b/ScriptCore/Serialization/DeserializationObject.cs @@ -1,4 +1,5 @@ using GlitchyEngine.Core; +using GlitchyEngine.Extensions; using System; using System.Collections.Generic; using System.Diagnostics; @@ -88,24 +89,7 @@ internal class DeserializationObject if (type == null) return context; - try - { - context._instance = Activator.CreateInstance(type, true); - } - catch (MissingMethodException e) - { - Log.Error($"Failed to create instance of type \"{type}\": The type doesn't contain a constructor with zero parameters.\nMake sure the type has a constructor that takes no arguments (It can be private!).\n{e}"); - } - catch (MethodAccessException e) - { - Log.Error($"Failed to create instance of type \"{type}\": The default constructor is not accessible.\n{e}"); - } - catch (Exception e) - { - Log.Error(e); - - return context; - } + context._instance = ActivatorExtension.CreateInstanceSafe(type); if (context._instance == null) return context;