69 lines
2.5 KiB
C#
69 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Upsilon.BoundTypes;
|
|
using Upsilon.Exceptions;
|
|
using Upsilon.StandardLibraries;
|
|
using Upsilon.Text;
|
|
|
|
namespace Upsilon.BaseTypes.UserData
|
|
{
|
|
public static class UserDataTypeHandler
|
|
{
|
|
private static readonly ConcurrentDictionary<System.Type, UserDataType> Types
|
|
= new ConcurrentDictionary<System.Type, UserDataType>();
|
|
|
|
public static void LoadType(System.Type t, string name)
|
|
{
|
|
var boundType = t.IsEnum
|
|
? new UserDataBoundEnumDefinition(t, name)
|
|
: UserDataBoundTypeDefinition.Create(t, name);
|
|
BoundTypeHandler.LoadUserDataTypeDefinition(boundType);
|
|
var info = new UserDataType(t);
|
|
Types.AddOrUpdate(t, info, (type, dataType) => dataType);
|
|
}
|
|
|
|
public static void LoadAssembly(Assembly assembly)
|
|
{
|
|
var types = assembly.GetTypes();
|
|
foreach (var type in types)
|
|
{
|
|
var attr = (UpsilonUserDataAttribute)type.GetCustomAttribute(typeof(UpsilonUserDataAttribute));
|
|
if (attr != null)
|
|
{
|
|
var name = attr.Name.ToLowerInvariant();
|
|
LoadType(type, name);
|
|
}
|
|
|
|
var createStaticAttribute =
|
|
(UpsilonCreateStaticAttribute) type.GetCustomAttribute(typeof(UpsilonCreateStaticAttribute));
|
|
if (createStaticAttribute != null)
|
|
{
|
|
var name = createStaticAttribute.Name.ToLowerInvariant();
|
|
StaticScope.RegisterStaticVariable(createStaticAttribute.Name, Activator.CreateInstance(type));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void LoadType<T>()
|
|
{
|
|
var attr = (UpsilonUserDataAttribute)typeof(T).GetCustomAttribute(typeof(UpsilonUserDataAttribute));
|
|
var name = typeof(T).Name;
|
|
if (attr != null) name = attr.Name;
|
|
LoadType(typeof(T), name);
|
|
}
|
|
|
|
internal static UserDataType GetTypeInfo(System.Type t)
|
|
{
|
|
if (t.IsGenericType)
|
|
t = t.GetGenericTypeDefinition();
|
|
if (Types.TryGetValue(t, out var result))
|
|
return result;
|
|
else
|
|
{
|
|
throw new Exception($"Can't use type '{t.FullName}' as script type, it's not registered for use.");
|
|
}
|
|
}
|
|
}
|
|
} |