diff --git a/Upsilon/BaseTypes/UserData/UpsilonUserDataAttribute.cs b/Upsilon/BaseTypes/UserData/UpsilonUserDataAttribute.cs new file mode 100644 index 0000000..24a371c --- /dev/null +++ b/Upsilon/BaseTypes/UserData/UpsilonUserDataAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace Upsilon.BaseTypes.UserData +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum, AllowMultiple = false, Inherited = true)] + public class UpsilonUserDataAttribute : Attribute + { + public UpsilonUserDataAttribute(string name) + { + Name = name; + } + + public string Name { get; } + } +} \ No newline at end of file diff --git a/Upsilon/BaseTypes/UserData/UserDataTypeHandler.cs b/Upsilon/BaseTypes/UserData/UserDataTypeHandler.cs index 45cfbc1..ce7f261 100644 --- a/Upsilon/BaseTypes/UserData/UserDataTypeHandler.cs +++ b/Upsilon/BaseTypes/UserData/UserDataTypeHandler.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Reflection; using Upsilon.BoundTypes; +using Upsilon.StandardLibraries; namespace Upsilon.BaseTypes.UserData { @@ -8,25 +10,47 @@ namespace Upsilon.BaseTypes.UserData { private static readonly Dictionary Types = new Dictionary(); - public static void LoadType(System.Type t) + public static void LoadType(System.Type t, string name) { var info = new UserDataType(t); Types.Add(t, info); UserDataBoundTypeDefinition boundType; if (t.IsEnum) { - boundType = new UserDataBoundEnumDefinition(t); + boundType = new UserDataBoundEnumDefinition(t, name); } else { - boundType = UserDataBoundTypeDefinition.Create(t); + boundType = UserDataBoundTypeDefinition.Create(t, name); } BoundTypeHandler.LoadUserDataTypeDefinition(boundType); } + 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); + if (type.IsEnum) + { + var def = Activator.CreateInstance(type); + StaticScope.RegisterStaticVariable(attr.Name, def); + } + } + } + } + public static void LoadType() { - LoadType(typeof(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) diff --git a/Upsilon/BoundTypes/BoundTypeHandler.cs b/Upsilon/BoundTypes/BoundTypeHandler.cs index d63a05e..3466e6c 100644 --- a/Upsilon/BoundTypes/BoundTypeHandler.cs +++ b/Upsilon/BoundTypes/BoundTypeHandler.cs @@ -39,6 +39,7 @@ namespace Upsilon.BoundTypes } return null; } + public static BoundTypeDefinition GetTypeDefinition(System.Type type) { return _typeDefinitions.Values.FirstOrDefault(x => x.ValidInternalTypes.Contains(type)); diff --git a/Upsilon/BoundTypes/UserDataBoundTypeDefinition.cs b/Upsilon/BoundTypes/UserDataBoundTypeDefinition.cs index 15e62fe..1af8849 100644 --- a/Upsilon/BoundTypes/UserDataBoundTypeDefinition.cs +++ b/Upsilon/BoundTypes/UserDataBoundTypeDefinition.cs @@ -10,7 +10,7 @@ namespace Upsilon.BoundTypes { public class UserDataBoundTypeDefinition : BoundTypeDefinition { - public string Name { get; } + public string Name { get; protected set; } public Dictionary Properties { get; protected set; } internal UserDataBoundTypeDefinition(System.Type backingType) @@ -28,11 +28,12 @@ namespace Upsilon.BoundTypes - public static UserDataBoundTypeDefinition Create(System.Type backingType) + public static UserDataBoundTypeDefinition Create(System.Type backingType, string name) { var obj = new UserDataBoundTypeDefinition(backingType) { - Properties = new Dictionary() + Properties = new Dictionary(), + Name = name }; var fields = backingType.GetFields().Select(x => new UserDataBoundProperty() { @@ -60,15 +61,15 @@ namespace Upsilon.BoundTypes { if (backingMethod.IsSpecialName) continue; - var name = backingMethod.Name; + var methodName = backingMethod.Name; var attribute = backingMethod.GetCustomAttribute(typeof(ScriptFunctionAttribute)); if (attribute is ScriptFunctionAttribute sfa ) { - name = sfa.Name; + methodName = sfa.Name; } methods.Add(new UserDataBoundMethod() { - Name = name, + Name = methodName, Type = Type.Function, ResultType = backingMethod.ReturnType.GetScriptType() }); @@ -88,7 +89,7 @@ namespace Upsilon.BoundTypes public class UserDataBoundEnumDefinition : UserDataBoundTypeDefinition { - public UserDataBoundEnumDefinition(System.Type enumType) : base(enumType) + public UserDataBoundEnumDefinition(System.Type enumType, string name) : base(enumType) { if (!enumType.IsEnum) throw new Exception("Trying to bind an enum with a type that's not an enum"); @@ -97,14 +98,16 @@ namespace Upsilon.BoundTypes var enumUnderlyingType = Enum.GetUnderlyingType(enumType); var enumValues = Enum.GetValues(enumType); + Name = name; + for (var i=0; i < enumValues.Length; i++) { var value = enumValues.GetValue(i); - var name = value.ToString().ToLowerInvariant(); - Properties.Add(name, new UserDataBoundProperty() + var valueName = value.ToString().ToLowerInvariant(); + Properties.Add(valueName, new UserDataBoundProperty() { - Name = name, + Name = valueName, ActualType = enumUnderlyingType.ToString(), Type = Type.Number });