mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
ScriptCore: Made creation of class instance safer
This commit is contained in:
@@ -300,8 +300,7 @@ internal class EntityEditor
|
|||||||
{
|
{
|
||||||
if (ImGui.Selectable(t.Name))
|
if (ImGui.Selectable(t.Name))
|
||||||
{
|
{
|
||||||
object instance = Activator.CreateInstance(t);
|
newValue = ActivatorExtension.CreateInstanceSafe(t);
|
||||||
newValue = instance;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace GlitchyEngine.Extensions;
|
||||||
|
|
||||||
|
public static class ActivatorExtension
|
||||||
|
{
|
||||||
|
/// <summary>Creates an instance of the specified type using that type's default constructor.</summary>
|
||||||
|
/// <param name="type">The type of object to create.</param>
|
||||||
|
/// <returns>A reference to the newly created object; or <see langword="null"/> if no instance could be created.</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using GlitchyEngine.Core;
|
using GlitchyEngine.Core;
|
||||||
|
using GlitchyEngine.Extensions;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
@@ -88,24 +89,7 @@ internal class DeserializationObject
|
|||||||
if (type == null)
|
if (type == null)
|
||||||
return context;
|
return context;
|
||||||
|
|
||||||
try
|
context._instance = ActivatorExtension.CreateInstanceSafe(type);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context._instance == null)
|
if (context._instance == null)
|
||||||
return context;
|
return context;
|
||||||
|
|||||||
Reference in New Issue
Block a user