Exemplo n.º 1
0
        public void InitAddon()
        {
            if (m_addon == null)
            {
                Type[] types;
                try
                {
                    types = m_assembly.GetTypes();
                }
                catch (Exception e)
                {
                    throw new Exception(string.Format("Unable to load Addon {0} - " +
                                                      "please make sure that it and it's dependencies were built against the current build and all it's dependencies are available.", this), e);
                }
                foreach (var type in types)
                {
                    var interfaces = type.GetInterfaces();
                    if (interfaces.Contains(typeof(IWCellAddon)))
                    {
                        var addonAttributes = type.GetCustomAttributes <WCellAddonAttribute>();
                        m_attr  = addonAttributes.Length > 0 ? addonAttributes[0] : null;
                        m_addon = (IWCellAddon)Activator.CreateInstance(type);

                        if (m_addon != null)
                        {
                            WCellAddonMgr.RegisterAddon(this);
                        }
                        break;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static WCellAddonContext LoadAddon(FileInfo file)
        {
            string            fullName = file.FullName;
            WCellAddonContext context;

            if (WCellAddonMgr.ContextsByFile.TryGetValue(fullName, out context))
            {
                if (!WCellAddonMgr.Unload(context))
                {
                    return((WCellAddonContext)null);
                }
            }

            Assembly asm;

            try
            {
                asm = Assembly.LoadFrom(fullName);
            }
            catch (BadImageFormatException ex)
            {
                LogManager.GetCurrentClassLogger().Error(
                    "Failed to load Assembly \"{0}\" because it has the wrong format - Make sure that you only load .NET assemblies that are compiled for the correct target platform: {1}",
                    (object)file.Name, Environment.Is64BitProcess ? (object)"xx64" : (object)"x86");
                return((WCellAddonContext)null);
            }

            WCellAddonContext wcellAddonContext = new WCellAddonContext(file, asm);

            WCellAddonMgr.Contexts.Add(wcellAddonContext);
            WCellAddonMgr.ContextsByFile.Add(wcellAddonContext.File.FullName, wcellAddonContext);
            return(wcellAddonContext);
        }
Exemplo n.º 3
0
        private static void RecurseLoadAddons(DirectoryInfo folder, string[] ignoredNames)
        {
            foreach (FileSystemInfo fileSystemInfo in folder.GetFileSystemInfos())
            {
                if (!((IEnumerable <string>)ignoredNames).Contains <string>(fileSystemInfo.Name.ToLower()
                                                                            .Replace(".dll", "")))
                {
                    if (fileSystemInfo is DirectoryInfo)
                    {
                        WCellAddonMgr.LoadAddons((DirectoryInfo)fileSystemInfo, ignoredNames);
                    }
                    else if (fileSystemInfo.Name.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
                    {
                        bool flag = true;
                        foreach (Assembly coreLib in WCellAddonMgr.CoreLibs)
                        {
                            if (coreLib.FullName.Equals(fileSystemInfo.Name.Replace(".dll", ""),
                                                        StringComparison.CurrentCultureIgnoreCase))
                            {
                                LogManager.GetCurrentClassLogger().Warn(
                                    "The core Assembly \"" + fileSystemInfo.FullName +
                                    "\" has been found in the Addon folder where it does not belong.- When compiling custom Addons, please make sure to set 'Copy Local' of all core-references to 'False'!");
                                flag = false;
                                break;
                            }
                        }

                        if (flag)
                        {
                            WCellAddonMgr.LoadAddon((FileInfo)fileSystemInfo);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Automatically loads all Addons from the given folder, ignoring any sub-folders or files
 /// that are in ignoreString, seperated by semicolon.
 /// </summary>
 /// <param name="folderName">The dir to look in for the Addon-Assemblies.</param>
 /// <param name="ignoreString">eg.: MyDllFile; My2ndFileIsJustALib; AnotherAddonFile</param>
 public static void LoadAddons(string folderName, string ignoreString)
 {
     WCellAddonMgr.LoadAddons(new DirectoryInfo(folderName), ((IEnumerable <string>)ignoreString.Split(
                                                                  new char[1]
     {
         ';'
     }, StringSplitOptions.RemoveEmptyEntries))
                              .TransformArray <string, string>((Func <string, string>)(s => s.ToLower().Trim().Replace(".dll", ""))));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Loads an Addon from the given file.
        /// Returns null if file does not exist.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public WCellAddonContext LoadAndInitAddon(FileInfo file)
        {
            WCellAddonContext wcellAddonContext = WCellAddonMgr.LoadAddon(file);

            if (wcellAddonContext != null)
            {
                wcellAddonContext.InitAddon();
            }
            return(wcellAddonContext);
        }
Exemplo n.º 6
0
        public WCellAddonContext TryLoadAddon(string libName)
        {
            WCellAddonContext contextByName = WCellAddonMgr.GetContextByName(libName);

            if (contextByName != null)
            {
                return(this.LoadAndInitAddon(contextByName.File));
            }
            if (!libName.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
            {
                libName += ".dll";
            }
            return(this.LoadAndInitAddon(libName));
        }
Exemplo n.º 7
0
 public static void LoadAddons(DirectoryInfo folder, string[] ignoredNames)
 {
     if (WCellAddonMgr.CoreLibs == null)
     {
         WCellAddonMgr.CoreLibs = AppDomain.CurrentDomain.GetAssemblies();
     }
     if (!folder.Exists)
     {
         return;
     }
     WCellAddonMgr.RecurseLoadAddons(folder, ignoredNames);
     foreach (WCellAddonContext context in (IEnumerable <WCellAddonContext>)WCellAddonMgr.Contexts)
     {
         context.InitAddon();
     }
 }
Exemplo n.º 8
0
        public static bool Unload(WCellAddonContext context)
        {
            IWCellAddon addon = context.Addon;

            if (addon == null)
            {
                return(false);
            }
            Logger currentClassLogger = LogManager.GetCurrentClassLogger();

            currentClassLogger.Info("Unloading Addon: " + (object)context + " ...");
            WCellAddonMgr.TearDown(addon);
            WCellAddonMgr.Contexts.Remove(context);
            WCellAddonMgr.ContextsByFile.Remove(context.File.FullName);
            WCellAddonMgr.ContextsByName.Remove(context.ShortName);
            WCellAddonMgr.ContextsByTypeName.Remove(addon.GetType().FullName);
            currentClassLogger.Info("Done. - Unloaded Addon: " + (object)context);
            return(true);
        }
Exemplo n.º 9
0
        internal static void RegisterAddon(WCellAddonContext context)
        {
            string fullName = context.Addon.GetType().FullName;

            if (context.ShortName.Length == 0)
            {
                LogManager.GetCurrentClassLogger().Warn("Addon of Type \"{0}\" did not specify a ShortName.",
                                                        context.Addon.GetType().FullName);
            }
            else
            {
                if (context.ShortName.ContainsIgnoreCase("addon"))
                {
                    LogManager.GetCurrentClassLogger()
                    .Warn(
                        "The Addon ShortName \"{0}\" contains the word \"Addon\" - The name should be short and not contain unnecessary information.",
                        context.ShortName);
                }
                if (WCellAddonMgr.ContextsByName.ContainsKey(context.ShortName))
                {
                    throw new ArgumentException(string.Format(
                                                    "Found more than one addon with ShortName \"{0}\": {1} and {2}", (object)context.ShortName,
                                                    (object)WCellAddonMgr.GetAddon(context.ShortName), (object)context.Addon));
                }
                WCellAddonMgr.ContextsByName.Add(context.ShortName, context);
                if (fullName.Equals(context.ShortName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return;
                }
            }

            if (WCellAddonMgr.ContextsByTypeName.ContainsKey(fullName))
            {
                throw new InvalidProgramException("Tried to register two Addons with the same TypeName: " + fullName);
            }
            WCellAddonMgr.ContextsByTypeName.Add(fullName, context);
        }
Exemplo n.º 10
0
        public void InitAddon()
        {
            if (this.m_addon != null)
            {
                return;
            }
            Type[] types;
            try
            {
                types = this.m_assembly.GetTypes();
            }
            catch (Exception ex)
            {
                throw new Exception(
                          string.Format(
                              "Unable to load Addon {0} - please make sure that it and it's dependencies were built against the current build and all it's dependencies are available.",
                              (object)this), ex);
            }

            foreach (Type type in types)
            {
                if (((IEnumerable <Type>)type.GetInterfaces()).Contains <Type>(typeof(IWCellAddon)))
                {
                    WCellAddonAttribute[] customAttributes = type.GetCustomAttributes <WCellAddonAttribute>();
                    this.m_attr  = customAttributes.Length > 0 ? customAttributes[0] : (WCellAddonAttribute)null;
                    this.m_addon = (IWCellAddon)Activator.CreateInstance(type);
                    if (this.m_addon != null)
                    {
                        WCellAddonMgr.RegisterAddon(this);
                        break;
                    }

                    break;
                }
            }
        }
Exemplo n.º 11
0
 public static WCellAddonContext GetContext <A>() where A : IWCellAddon
 {
     return(WCellAddonMgr.GetContextByTypeName(typeof(A).FullName));
 }
Exemplo n.º 12
0
 public static WCellAddonContext GetContext(Type addonType)
 {
     return(WCellAddonMgr.GetContextByTypeName(addonType.FullName));
 }
Exemplo n.º 13
0
 public static IWCellAddon GetAddon(string shortName)
 {
     return(WCellAddonMgr.GetContextByName(shortName)?.Addon);
 }