Exemplo n.º 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.GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism.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.GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism.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)))
                {
                    try
                    {
                        IndexPlugin.IIndex i = (IndexPlugin.IIndex)Activator.CreateInstance(type);

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

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

                        if (IndexInfo.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.GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger().Log("IndexMechanism", string.Format("Added new index plugin assembly: {0}", newIndexFileName),
                                                                                          MessageLevel.Info);
                    }
                }
                catch (Exception ex)
                {
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger().Log("IndexMechanism", string.Format("Unable to add new index plugin assebly {0}", dllPath),
                                                                                          MessageLevel.Error);
                    }
                    ret = false;
                }
            }

            AppDomain.Unload(tempDomain);
            return(ret);
        }
Exemplo n.º 2
0
        internal IIndex GetIndex(int indexId)
        {
            IndexInfo index;

            var i = _indexes.Where(p => p.IndexID == indexId);

            if (i.Count() != 1)
            {
                throw new WronxIndexIdException(indexId);
            }
            else
            {
                index = i.Single();
            }

            if (_indexesObjects.ContainsKey(index))
            {
                return(_indexesObjects[index]);
            }

            Monitor.Enter(_instance);
            try
            {
                Assembly assembly =
                    _pluginsDomain.Load(
                        AssemblyName.GetAssemblyName(string.Format("{0}\\{1}", this._indexesStoragePath,
                                                                   index.IndexFileName)));

                Type[] assemblyTypes;
                try
                {
                    assemblyTypes = assembly.GetTypes();
                }
                catch (Exception ex)
                {
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger().Log("IndexMechanism",
                                                                                          string.Format("Iterating types in index plugin {0} failed\n{1}", index.IndexFileName, ex.Message),
                                                                                          MessageLevel.Error);
                    }
                    Monitor.Exit(_instance);
                    return(null);
                }

                var t = assemblyTypes
                        .Where(p => p.IsClass)
                        .Where(p => p.FullName == index.IndexClassName);

                if (t.Count() == 1)
                {
                    Type type = t.Single();
                    IndexPlugin.IIndex ixi = (IndexPlugin.IIndex)Activator.CreateInstance(type);
                    _indexesObjects.Add(index, ixi);
                    ixi.SettingsChanged += new settingsChangedHandler(IIndexSettingsChanged);
                    ixi.SetSettings(index.IndexSettings);
                }
            }
            catch (Exception ex)
            {
                if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism.GetLoger().Log("IndexMechanism",
                                                                                      string.Format("Unable to read index assembly {0} from file {1}\n{2}", index.IndexClassName,
                                                                                                    index.IndexFileName, ex), MessageLevel.Error);
                }

                Monitor.Exit(_instance);
                return(null);
            }

            Monitor.Exit(_instance);
            return(_indexesObjects[index]);
        }