Example #1
0
        internal bool AddIndex(string dllPath)
        {
            if (!File.Exists(dllPath))
            {
                return(false);
            }

            string newIndexFileName = dllPath.Split('\\').Last();
            int    tries            = 0;

            while (File.Exists(string.Format("{0}\\{1}", this._indexesStoragePath, newIndexFileName)))
            {
                newIndexFileName = string.Format("{0}({1}).{2}", dllPath.Split('\\').Last().Split('.').First(), ++tries,
                                                 dllPath.Split('\\').Last().Split('.').Last());
            }


            AppDomain tempDomain = AppDomain.CreateDomain("tempDomain");
            Assembly  assembly   = tempDomain.Load(AssemblyName.GetAssemblyName(dllPath));
            //Assembly assembly = Assembly.LoadFile(dllPath);
            bool ret = false;

            Type[] assemblyTypes;
            try
            {
                assemblyTypes = assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException ex)
            {
                if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Iterating types in new index plugin assembly failed\n{0}", ex.LoaderExceptions.First().Message),
                                                                                           MessageLevel.Warning);
                }
                return(false);
            }
            catch (Exception ex)
            {
                if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Iterating types in new index plugin assembly failed\n{0}", ex.Message),
                                                                                           MessageLevel.Warning);
                }
                return(false);
            }

            foreach (Type type in assemblyTypes)
            {
                if (type.IsClass && type.GetInterfaces().Contains(typeof(IndexPlugin.IIndex <object>)))
                {
                    try
                    {
                        IndexPlugin.IIndex <object> i = (IndexPlugin.IIndex <object>)Activator.CreateInstance(type);

                        if (!i.EmptyIndexData.GetType().IsSerializable)
                        {
                            throw new Exception();
                        }

                        IndexInfo <T> ixi = new IndexInfo <T>
                        {
                            IndexFileName  = newIndexFileName,
                            IndexName      = i.Name,
                            IndexClassName = type.FullName,
                            IndexID        = CORE.Settings <T> .GetInstance().NextIndexesIdentity
                        };

                        if (IndexInfo <T> .Save(ixi,
                                                string.Format("{0}\\{1}-{2}.xml", this._indexesStoragePath, type.GUID,
                                                              newIndexFileName)))
                        {
                            _indexes.Add(ixi);
                            ret = true;
                        }
                        i.Dispose();
                    }
                    catch (Exception e)
                    {
                    }
                }
            }

            if (ret)
            {
                try
                {
                    File.Copy(dllPath, string.Format("{0}\\{1}", this._indexesStoragePath, newIndexFileName));
                    File.SetAttributes(string.Format("{0}\\{1}", this._indexesStoragePath, newIndexFileName),
                                       FileAttributes.Normal);
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Added new index plugin assembly: {0}", newIndexFileName),
                                                                                               MessageLevel.Info);
                    }
                }
                catch (Exception ex)
                {
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Unable to add new index plugin assebly {0}", dllPath),
                                                                                               MessageLevel.Error);
                    }
                    ret = false;
                }
            }

            AppDomain.Unload(tempDomain);
            return(ret);
        }