public static IEnumerable <IVectorPlusBehaviourModule> ExtractModules(byte[] zipData, ILogger logger = null) { var modules = new List <IVectorPlusBehaviourModule>(); var tempZipFile = Path.GetTempFileName(); var tempDirectory = CreateTemporaryDirectory(); try { // extract the ZIP to a temporary folder so all DLLs available to each other File.WriteAllBytes(tempZipFile, zipData); ZipFile.ExtractToDirectory(tempZipFile, tempDirectory); // step through each file in the directory foreach (var path in Directory.EnumerateFiles(tempDirectory)) { // only DLLs if (Path.GetExtension(path.ToLower()) == ".dll") { // Load the DLL, search for modules var loadContext = new ModuleLoadContext(path); var assembly = loadContext.LoadFromAssemblyPath(path); foreach (Type type in assembly.GetExportedTypes()) { var ok1 = type.GetInterfaces().Any(i => typeof(IVectorPlusBehaviourModule).IsAssignableFrom(i)); var ok2 = type.GetInterfaces().Any(i => i.FullName == typeof(IVectorPlusBehaviourModule).FullName); if (ok2) { IVectorPlusBehaviourModule module = (IVectorPlusBehaviourModule)Activator.CreateInstance(type); modules.Add(module); } } } } } catch (Exception e) { logger?.LogWarning(e, "Could not load DLL(s) from zip bytes."); throw e; } finally { if (File.Exists(tempZipFile)) { File.Delete(tempZipFile); } if (Directory.Exists(tempDirectory)) { Directory.Delete(tempDirectory, true); } } return(modules); }
public static ModuleConfig From(IVectorPlusBehaviourModule module, byte[] zip, bool enabled) { return(new ModuleConfig() { UniqueReference = module.UniqueReference, ModuleConfigId = Guid.NewGuid(), Added = DateTime.Now, Zip = zip, Release = module.Release, Name = module.Name, Description = module.Description, Author = module.Author, AuthorEmail = module.AuthorEmail, ModuleWebsite = module.ModuleWebsite, UserEnabled = enabled }); }