147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using PorygonSharp.Attributes;
|
|
using PorygonSharp.EvalValues;
|
|
using PorygonSharp.Utilities;
|
|
|
|
namespace PorygonSharp.UserData
|
|
{
|
|
public static class UserDataHandler
|
|
{
|
|
private static readonly ConcurrentDictionary<Type, UserData> UserDataLookup = new ConcurrentDictionary<Type, UserData>();
|
|
internal static readonly ConcurrentDictionary<uint, UserData> ReverseLookup = new ConcurrentDictionary<uint, UserData>();
|
|
|
|
|
|
public static void RegisterAssembly(Assembly assembly)
|
|
{
|
|
foreach (var type in assembly.GetTypes())
|
|
{
|
|
var attr = type.GetCustomAttribute<PorygonUserdataAttribute>();
|
|
if (attr != null)
|
|
{
|
|
RegisterType(attr.Identifier, type);
|
|
}
|
|
|
|
var enumAttr = type.GetCustomAttribute<PorygonEnumAttribute>();
|
|
if (enumAttr != null)
|
|
{
|
|
StaticScope.RegisterStaticEnum(enumAttr.Name, type);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void RegisterType(string name, Type type)
|
|
{
|
|
var hash = name.ScriptHash();
|
|
lock (UserDataLookup)
|
|
{
|
|
if (UserDataLookup.ContainsKey(type))
|
|
return;
|
|
RegisterUserDataType(name);
|
|
var ud = new UserData(hash, type);
|
|
UserDataLookup.TryAdd(type, ud);
|
|
ReverseLookup.TryAdd(hash, ud);
|
|
ud.RegisterFields();
|
|
}
|
|
}
|
|
|
|
public static void RegisterEnumType(string name, Type type)
|
|
{
|
|
var hash = name.ScriptHash();
|
|
lock (UserDataLookup)
|
|
{
|
|
if (UserDataLookup.ContainsKey(type))
|
|
return;
|
|
RegisterUserDataType(name);
|
|
var ud = new UserData(hash, type);
|
|
UserDataLookup.TryAdd(type, ud);
|
|
ReverseLookup.TryAdd(hash, ud);
|
|
}
|
|
var values = Enum.GetValues(type);
|
|
foreach (var value in values)
|
|
{
|
|
var getter = new UserDataField.GetterDelegate(ptr => EvalValueCreator.CreateIntegerEvalValue(Convert.ToInt64(value)));
|
|
var fieldData = new UserDataField(getter, null);
|
|
var valueName = Enum.GetName(type, value);
|
|
var fieldName = valueName.ScriptHash();
|
|
var t = ScriptType.ScriptTypeHandler.CreateNumericScriptType(true, false);
|
|
UserData.RegisterUserDataField(hash, fieldName,
|
|
UserData.CreateUserDataField(t, fieldData.GetGetterPointer(), IntPtr.Zero));
|
|
ReverseLookup[hash].Fields[fieldName] = fieldData;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static uint GetTypeId(Type t)
|
|
{
|
|
if (UserDataLookup.TryGetValue(t, out var i))
|
|
return i.Id;
|
|
var attr = t.GetCustomAttribute<PorygonUserdataAttribute>();
|
|
if (attr == null)
|
|
return 0;
|
|
var id = attr.Identifier.ScriptHash();
|
|
if (ReverseLookup.ContainsKey(id))
|
|
return id;
|
|
return 0;
|
|
}
|
|
|
|
public static bool IsTypeRegistered(Type t)
|
|
{
|
|
if (UserDataLookup.ContainsKey(t)) return true;
|
|
var attr = t.GetCustomAttribute<PorygonUserdataAttribute>();
|
|
if (attr == null)
|
|
return false;
|
|
var id = attr.Identifier.ScriptHash();
|
|
return ReverseLookup.ContainsKey(id);
|
|
}
|
|
|
|
public static bool TryResolveType(Type t)
|
|
{
|
|
var cur = t;
|
|
while (cur != null)
|
|
{
|
|
if (IsTypeRegistered(cur))
|
|
{
|
|
UserDataLookup.TryAdd(t, UserDataLookup[cur]);
|
|
return true;
|
|
}
|
|
cur = cur.BaseType;
|
|
}
|
|
var interfaces = t.GetInterfaces();
|
|
foreach (var i in interfaces)
|
|
{
|
|
if (!IsTypeRegistered(i))
|
|
continue;
|
|
UserDataLookup.TryAdd(t, UserDataLookup[i]);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static int GetUserDataFieldCount(Type t)
|
|
{
|
|
var hash = GetTypeId(t);
|
|
return GetUserDataFieldCount(hash);
|
|
}
|
|
|
|
internal static IntPtr CreateUserDataType(Type t)
|
|
{
|
|
var hash = GetTypeId(t);
|
|
return CreateUserDataType(hash);
|
|
}
|
|
|
|
[DllImport("PorygonLang", EntryPoint = "RegisterUserDataType", CallingConvention = CallingConvention.Cdecl)]
|
|
private static extern void RegisterUserDataType([MarshalAs(UnmanagedType.LPWStr)]string name);
|
|
|
|
[DllImport("PorygonLang", EntryPoint = "GetUserDataFieldCount", CallingConvention = CallingConvention.Cdecl)]
|
|
private static extern int GetUserDataFieldCount(uint hashId);
|
|
|
|
[DllImport("PorygonLang", EntryPoint = "CreateUserDataType", CallingConvention = CallingConvention.Cdecl)]
|
|
private static extern IntPtr CreateUserDataType(uint hashId);
|
|
|
|
}
|
|
} |