public static ApiEntry Create(ProgrammableBlockApi api, Whitelist whitelist, MemberInfo memberInfo) { switch (memberInfo) { case Type type: return(ForType(api, whitelist, type)); case ConstructorInfo constructorInfo: return(ForConstructor(api, whitelist, constructorInfo)); case EventInfo eventInfo: return(ForEvent(api, whitelist, eventInfo)); case FieldInfo fieldInfo: return(ForField(api, whitelist, fieldInfo)); case PropertyInfo propertyInfo: return(ForProperty(api, whitelist, propertyInfo)); case MethodInfo methodInfo: return(ForMethod(api, whitelist, methodInfo)); default: return(null); } }
static ApiEntry ForField(ProgrammableBlockApi api, Whitelist whitelist, FieldInfo fieldInfo) { if (fieldInfo.IsSpecialName || !fieldInfo.IsPublic && !fieldInfo.IsFamily && !fieldInfo.IsFamilyOrAssembly && !fieldInfo.IsFamilyOrAssembly) { return(null); } var basis = api.GetEntry(fieldInfo.DeclaringType); var xmlDocKey = $"F{basis.XmlDocKey.Substring(1)}.{fieldInfo.Name}"; return(new ApiEntry(api, fieldInfo, basis.AssemblyName, basis.NamespaceName, fieldInfo.Name, xmlDocKey, whitelist.IsWhitelisted(fieldInfo))); }
static ApiEntry ForEvent(ProgrammableBlockApi api, Whitelist whitelist, EventInfo eventInfo) { if (eventInfo.IsSpecialName || !(eventInfo.AddMethod?.IsPublic ?? false) && !(eventInfo.AddMethod?.IsFamily ?? false) && !(eventInfo.AddMethod?.IsFamilyOrAssembly ?? false) && !(eventInfo.AddMethod?.IsFamilyOrAssembly ?? false) && !(eventInfo.RemoveMethod?.IsPublic ?? false) && !(eventInfo.RemoveMethod?.IsFamily ?? false) && !(eventInfo.RemoveMethod?.IsFamilyOrAssembly ?? false) && !(eventInfo.RemoveMethod?.IsFamilyOrAssembly ?? false)) { return(null); } var basis = api.GetEntry(eventInfo.DeclaringType); var xmlDocKey = $"E{basis.XmlDocKey.Substring(1)}.{eventInfo.Name}"; return(new ApiEntry(api, eventInfo, basis.AssemblyName, basis.NamespaceName, eventInfo.Name, xmlDocKey, whitelist.IsWhitelisted(eventInfo))); }
static ApiEntry ForProperty(ProgrammableBlockApi api, Whitelist whitelist, PropertyInfo propertyInfo) { if (propertyInfo.IsSpecialName || !(propertyInfo.GetMethod?.IsPublic ?? false) && !(propertyInfo.GetMethod?.IsFamily ?? false) && !(propertyInfo.GetMethod?.IsFamilyOrAssembly ?? false) && !(propertyInfo.GetMethod?.IsFamilyOrAssembly ?? false) && !(propertyInfo.SetMethod?.IsPublic ?? false) && !(propertyInfo.SetMethod?.IsFamily ?? false) && !(propertyInfo.SetMethod?.IsFamilyOrAssembly ?? false) && !(propertyInfo.SetMethod?.IsFamilyOrAssembly ?? false)) { return(null); } var basis = api.GetEntry(propertyInfo.DeclaringType); var xmlDocKey = $"P{basis.XmlDocKey.Substring(1)}.{propertyInfo.Name}"; return(new ApiEntry(api, propertyInfo, basis.AssemblyName, basis.NamespaceName, propertyInfo.Name, xmlDocKey, whitelist.IsWhitelisted(propertyInfo))); }
static ApiEntry ForConstructor(ProgrammableBlockApi api, Whitelist whitelist, ConstructorInfo constructorInfo) { //if (constructorInfo.IsSpecialName || !constructorInfo.IsPublic && !constructorInfo.IsFamily && !constructorInfo.IsFamilyOrAssembly && !constructorInfo.IsFamilyOrAssembly) // return null; if (!constructorInfo.IsPublic && !constructorInfo.IsFamily && !constructorInfo.IsFamilyOrAssembly && !constructorInfo.IsFamilyOrAssembly) { return(null); } var basis = api.GetEntry(constructorInfo.DeclaringType); var xmlDocKey = $"C{basis.XmlDocKey.Substring(1)}.{constructorInfo.Name}"; var parameters = constructorInfo.GetParameters(); xmlDocKey += "(" + string.Join(",", parameters.Select(p => XmlDocParameterStr(api, p))) + ")"; return(new ApiEntry(api, constructorInfo, basis.AssemblyName, basis.NamespaceName, constructorInfo.Name, xmlDocKey, whitelist.IsWhitelisted(constructorInfo))); }
ApiEntry(ProgrammableBlockApi api, MemberInfo member, string assemblyName, string namespaceName, string name, string xmlDocKey, bool isWhitelisted) { Api = api ?? throw new ArgumentNullException(nameof(api)); Member = member ?? throw new ArgumentNullException(nameof(member)); AssemblyName = assemblyName ?? throw new ArgumentNullException(nameof(assemblyName)); NamespaceName = namespaceName ?? throw new ArgumentNullException(nameof(namespaceName)); Name = name ?? throw new ArgumentNullException(nameof(name)); XmlDocKey = xmlDocKey ?? throw new ArgumentNullException(nameof(xmlDocKey)); IsWhitelisted = isWhitelisted; FullName = $"{NamespaceName}.{Name}"; InheritedEntries = new ReadOnlyCollection <ApiEntry>(_inheritedEntries); InheritorEntries = new ReadOnlyCollection <ApiEntry>(_inheritorEntries); MemberEntries = new ReadOnlyCollection <ApiEntry>(_memberEntries); SuggestedFileName = ToSafeFileName(member.GetFullName()); }
static ApiEntry ForGenericType(ProgrammableBlockApi api, Whitelist whitelist, Type type) { var assemblyName = type.Assembly.GetName().Name; var namespaceName = type.IsGenericType ? type.GetGenericTypeDefinition().Namespace : type.Namespace; var name = type.IsGenericType ? type.GetGenericTypeDefinition().Name : type.Name; var separatorIndex = name.IndexOf('`'); if (separatorIndex >= 0) { name = name.Substring(0, separatorIndex); } var genericArguments = type.GetGenericArguments(); var whitelistKey = $"{namespaceName}.{name}"; var xmlDocKey = $"T:{whitelistKey}"; xmlDocKey += "{" + string.Join(",", genericArguments.Select(arg => arg.IsGenericParameter ? arg.Name : arg.FullName)) + "}"; return(new ApiEntry(api, type, assemblyName, namespaceName, name, xmlDocKey, whitelist.IsWhitelisted(type))); }
static ApiEntry ForType(ProgrammableBlockApi api, Whitelist whitelist, Type type) { if (!type.IsPublic && !type.IsNestedPublic && !type.IsNestedFamily && !type.IsNestedFamANDAssem && !type.IsNestedFamORAssem) { return(null); } if (type.IsGenericTypeDefinition || type.IsGenericType) { return(ForGenericType(api, whitelist, type)); } var assemblyName = type.Assembly.GetName().Name; var namespaceName = type.Namespace; var name = type.Name; var xmlDocKey = $"T:{namespaceName}.{name}"; return(new ApiEntry(api, type, assemblyName, namespaceName, name, xmlDocKey, whitelist.IsWhitelisted(type))); }
static string XmlDocParameterStr(ProgrammableBlockApi api, ParameterInfo parameterInfo) { var type = parameterInfo.ParameterType.IsByRef || parameterInfo.ParameterType.IsPointer ? parameterInfo.ParameterType.GetElementType() : parameterInfo.ParameterType; //if (type.IsGenericType && !type.IsGenericTypeDefinition) // type = type.GetGenericTypeDefinition(); if (parameterInfo.ParameterType.IsByRef) { if (type.FullName == null) { return(type.Name + "@"); } return(api.GetEntry(type).XmlDocKey.Substring(2) + "@"); } if (type.FullName == null) { return(type.Name); } return(api.GetEntry(type, true).XmlDocKey.Substring(2)); }
static ApiEntry ForMethod(ProgrammableBlockApi api, Whitelist whitelist, MethodInfo methodInfo) { if (methodInfo.IsSpecialName || !methodInfo.IsPublic && !methodInfo.IsFamily && !methodInfo.IsFamilyOrAssembly && !methodInfo.IsFamilyOrAssembly) { return(null); } if (methodInfo.IsGenericMethodDefinition || methodInfo.IsGenericMethod) { return(ForGenericMethod(api, whitelist, methodInfo)); } var basis = api.GetEntry(methodInfo.DeclaringType); var xmlDocKey = $"M{basis.XmlDocKey.Substring(1)}.{methodInfo.Name}"; var parameters = methodInfo.GetParameters(); if (parameters.Length > 0) { xmlDocKey += $"({string.Join(",", parameters.Select(p => XmlDocParameterStr(api, p)))})"; } return(new ApiEntry(api, methodInfo, basis.AssemblyName, basis.NamespaceName, methodInfo.Name, xmlDocKey, whitelist.IsWhitelisted(methodInfo))); }
static ApiEntry ForGenericMethod(ProgrammableBlockApi api, Whitelist whitelist, MethodInfo methodInfo) { var basis = api.GetEntry(methodInfo.DeclaringType); var name = methodInfo.Name; var separatorIndex = name.IndexOf('`'); if (separatorIndex >= 0) { name = name.Substring(0, separatorIndex); } var xmlDocKey = $"M{basis.XmlDocKey.Substring(1)}.{methodInfo.Name}"; var genericArguments = methodInfo.GetGenericArguments(); xmlDocKey += $"{{{string.Join(",", genericArguments.Select(arg => arg.Name))}}}"; var parameters = methodInfo.GetParameters(); if (parameters.Length > 0) { xmlDocKey += $"({string.Join(",", parameters.Select(p => XmlDocParameterStr(api, p)))})"; } return(new ApiEntry(api, methodInfo, basis.AssemblyName, basis.NamespaceName, name, xmlDocKey, whitelist.IsWhitelisted(methodInfo))); }
public static async Task <ProgrammableBlockApi> LoadAsync(string whitelistCacheFileName) { var api = new ProgrammableBlockApi(); await Task.Run(() => { var members = new List <MemberInfo>(); var spaceEngineers = new SpaceEngineers(); var installPath = Path.Combine(spaceEngineers.GetInstallPath(), "bin64"); MDKUtilityFramework.Load(installPath); var dllFiles = Directory.EnumerateFiles(installPath, "*.dll", SearchOption.TopDirectoryOnly) .ToList(); foreach (var file in dllFiles) { LoadAssembly(file); } //var assemblies = dllFiles.Select(LoadAssembly).Where(a => a != null).ToList(); var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var whitelist = Whitelist.Load(whitelistCacheFileName); api._whitelist = whitelist; foreach (var assembly in assemblies) { Visit(whitelist, assembly, members); } // Hack. I'm getting duplicated entries and atm I cannot be bothered to do a proper check // for why... var visitedMembers = new HashSet <MemberInfo>(); foreach (var assemblyGroup in members.GroupBy(m => m.GetAssembly())) { foreach (var typeGroup in assemblyGroup.GroupBy(m => m.DeclaringType)) { if (typeGroup.Key == null) { foreach (var type in typeGroup) { var entry = api.GetEntry(type); if (!api._entries.Contains(entry)) { api._entries.Add(entry); } } continue; } var typeEntry = api.GetEntry(typeGroup.Key); if (typeEntry != null) { if (!visitedMembers.Add(typeEntry.Member)) { continue; } if (!api._entries.Contains(typeEntry)) { api._entries.Add(typeEntry); } foreach (var member in typeGroup) { var entry = api.GetEntry(member); if (entry != null) { if (!visitedMembers.Add(member)) { continue; } api._entries.Add(entry); } } } } } foreach (var entry in api.Entries) { entry.ResolveLinks(); } }); return(api); }