mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System;
|
|
using Robust.Shared.ContentPack;
|
|
using Robust.Shared.IoC;
|
|
|
|
namespace Robust.Shared.Sandboxing
|
|
{
|
|
[NotContentImplementable]
|
|
public interface ISandboxHelper
|
|
{
|
|
/// <summary>
|
|
/// Effectively equivalent to <see cref="Activator.CreateInstance(Type)"/> but safe for content use.
|
|
/// </summary>
|
|
/// <exception cref="SandboxArgumentException">
|
|
/// Thrown if <paramref name="type"/> is not defined in content.
|
|
/// </exception>
|
|
/// <seealso cref="IDynamicTypeFactory.CreateInstance(System.Type, bool, bool)"/>
|
|
object CreateInstance(Type type);
|
|
}
|
|
|
|
internal sealed class SandboxHelper : ISandboxHelper
|
|
{
|
|
[Dependency] private readonly IModLoader _modLoader = default!;
|
|
|
|
public object CreateInstance(Type type)
|
|
{
|
|
if (!_modLoader.IsContentTypeAccessAllowed(type))
|
|
{
|
|
throw new SandboxArgumentException("Creating non-content types is not allowed.");
|
|
}
|
|
|
|
return Activator.CreateInstance(type)!;
|
|
}
|
|
}
|
|
}
|