public ControllerTreeElement SearchRoot(ref System.Collections.Generic.Queue <string> pathSplit)
            {
                string action = pathSplit.Count == 0 ? null : pathSplit.Dequeue().ToLower();

                if (pathSplit.Count == 0)
                {
                    return(this);
                }
                else
                {
                    if (elements.Contains(action))
                    {
                        ControllerTreeElement treeElement = (ControllerTreeElement)elements[action];
                        return(treeElement.SearchRoot(ref pathSplit));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
Exemplo n.º 2
0
        private void Watcher_Work(object sender, string path, WatcherChangeTypes changeType, string nameOld = null)
        {
            lock (sender) {
                try {
                    string name;
                    string module;
                    bool   fileAct;
                    int    intTmp = baseDirectory.FullName.Length;
                    path   = path.Remove(0, intTmp);
                    intTmp = path.IndexOf(Path.DirectorySeparatorChar);
                    module = path.Substring(0, intTmp);
                    path   = path.Remove(0, intTmp);
                    if (path.LastIndexOf('.') > path.LastIndexOf(Path.DirectorySeparatorChar))
                    {
                        intTmp  = path.LastIndexOf(Path.DirectorySeparatorChar);
                        path    = path.Remove(intTmp, path.Length - intTmp);
                        fileAct = true;
                    }
                    else
                    {
                        fileAct = false;
                    }
                    intTmp = path.LastIndexOf(Path.DirectorySeparatorChar);
                    name   = path.Substring(intTmp + 1);
                    path   = path.Remove(intTmp, path.Length - intTmp);
                    if (nameOld != null)
                    {
                        intTmp  = nameOld.LastIndexOf(Path.DirectorySeparatorChar);
                        nameOld = nameOld.Remove(0, intTmp + 1);
                    }
                    int moduleNum = -1;
                    if (module == "controllers")
                    {
                        moduleNum = 0;
                    }
                    if (module == "layouts")
                    {
                        moduleNum = 1;
                    }
                    if (module == "static")
                    {
                        moduleNum = 2;
                    }

                    path = path.Replace('\\', '/');
                    if (moduleNum == 0)
                    {
                        Queue <string>        pathSplit = new Queue <string>((path + name).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));
                        ControllerTreeElement tree      = controllerTree.SearchRoot(ref pathSplit);
                        if (fileAct)
                        {
                            tree = (ControllerTreeElement)tree.elements[name];
                            tree.Load();
                        }
                        else
                        {
                            switch (changeType)
                            {
                            case WatcherChangeTypes.Deleted: {
                                tree.RemoveTree(name);
                                break;
                            }

                            case WatcherChangeTypes.Created: {
                                tree.AddTree(/*path, */ name);
                                break;
                            }

                            case WatcherChangeTypes.Renamed: {
                                tree.RemoveTree(nameOld);
                                tree.AddTree(/*$"{path}{Path.DirectorySeparatorChar}{name}", */ name);
                                break;
                            }
                            }
                        }
                    }
                    else if (moduleNum == 1 || moduleNum == 2)
                    {
                        if (fileAct)
                        {
                            ((moduleNum == 2 ? staticPlugins : layoutsPlugins)[name] as PluginLoader).Load();
                        }
                        else
                        {
                            PluginLoader loaderTmp = null;
                            switch (changeType)
                            {
                            case WatcherChangeTypes.Deleted: {
                                loaderTmp = (PluginLoader)(moduleNum == 2 ? staticPlugins : layoutsPlugins)[name];
                                loaderTmp.RemoveWorkDir();
                                (moduleNum == 2 ? staticPlugins : layoutsPlugins).Remove(name);
                                break;
                            }

                            case WatcherChangeTypes.Created: {
                                loaderTmp = new PluginLoader(ref Modules[moduleNum], path, name);
                                loaderTmp.Load();
                                (moduleNum == 2 ? staticPlugins : layoutsPlugins).Add(name, loaderTmp);
                                break;
                            }

                            case WatcherChangeTypes.Renamed: {
                                loaderTmp = (PluginLoader)(moduleNum == 2 ? staticPlugins : layoutsPlugins)[nameOld];
                                loaderTmp.RemoveWorkDir();
                                (moduleNum == 2 ? staticPlugins : layoutsPlugins).Remove(nameOld);
                                loaderTmp = new PluginLoader(ref Modules[moduleNum], path, name);
                                loaderTmp.Load();
                                (moduleNum == 2 ? staticPlugins : layoutsPlugins).Add(name, loaderTmp);
                                break;
                            }
                            }
                        }
                    }
                }
                catch (Exception e) {
                    Log.Error("Ошибка обновления пакетов", e);
                }
            }
        }