コード例 #1
0
ファイル: ModuleLoader.cs プロジェクト: pezipink/FarNet
        /// <summary>
        /// Reads the module from the cache.
        /// </summary>
        /// <param name="fileInfo">Module file information.</param>
        /// <returns>True if the module has been loaded from the cache.</returns>
        bool ReadCache(FileInfo fileInfo)
        {
            Log.Source.TraceInformation("Read cache {0}", fileInfo);

            string path = fileInfo.FullName;
            var data = _Cache.Get(path);
            if (data == null)
                return false;

            ++_Cache.CountLoaded;
            bool done = false;
            ModuleManager manager = null;
            try
            {
                // read data
                EnumerableReader reader = new EnumerableReader((IEnumerable)data);

                // >> Stamp
                var assemblyStamp = (long)reader.Read();
                if (assemblyStamp != fileInfo.LastWriteTime.Ticks)
                    return false;

                // new manager, add it now, remove later on errors
                manager = new ModuleManager(path);
                _Managers.Add(manager.ModuleName, manager);

                // read and load data
                var settings = manager.ReadSettings();
                manager.LoadData(settings);

                // >> Culture of cached resources
                var savedCulture = (string)reader.Read();

                // check the culture
                if (savedCulture.Length > 0)
                {
                    // the culture changed, ignore the cache
                    if (savedCulture != manager.CurrentUICulture.Name)
                        return false;

                    // restore the flag
                    manager.CachedResources = true;
                }

                // >> Settings
                manager.HasSettings = (bool)reader.Read();

                object kind;
                while (null != (kind = reader.TryRead()))
                {
                    ProxyAction action = null;
                    switch ((ModuleItemKind)kind)
                    {
                        case ModuleItemKind.Host:
                            {
                                manager.SetModuleHost((string)reader.Read());
                            }
                            break;
                        case ModuleItemKind.Command:
                            {
                                var it = new ProxyCommand(manager, reader);
                                Host.Instance.RegisterProxyCommand(it);
                                action = it;
                            }
                            break;
                        case ModuleItemKind.Editor:
                            {
                                var it = new ProxyEditor(manager, reader);
                                Host.Instance.RegisterProxyEditor(it);
                                action = it;
                            }
                            break;
                        case ModuleItemKind.Drawer:
                            {
                                var it = new ProxyDrawer(manager, reader);
                                Host.Instance.RegisterProxyDrawer(it);
                                action = it;
                            }
                            break;
                        case ModuleItemKind.Tool:
                            {
                                var it = new ProxyTool(manager, reader);
                                Host.Instance.RegisterProxyTool(it);
                                action = it;
                            }
                            break;
                        default:
                            throw new ModuleException();
                    }

                    if (action != null)
                        action.LoadData((Hashtable)settings[action.Id]);
                }

                done = true;
            }
            catch (ModuleException)
            {
                // ignore known
            }
            catch (Exception ex)
            {
                throw new ModuleException(string.Format(null, "Error on reading the cache for '{0}'.", path), ex);
            }
            finally
            {
                if (!done)
                {
                    // remove cached data
                    _Cache.Remove(path);

                    // remove the manager
                    if (manager != null)
                        RemoveModuleManager(manager);
                }
            }

            return done;
        }
コード例 #2
0
        /// <summary>
        /// Reads the module from the cache.
        /// </summary>
        /// <param name="fileInfo">Module file information.</param>
        /// <returns>True if the module has been loaded from the cache.</returns>
        bool ReadCache(FileInfo fileInfo)
        {
            Log.Source.TraceInformation("Read cache {0}", fileInfo);

            string path = fileInfo.FullName;
            var    data = _Cache.Get(path);

            if (data == null)
            {
                return(false);
            }

            ++_Cache.CountLoaded;
            bool          done    = false;
            ModuleManager manager = null;

            try
            {
                // read data
                EnumerableReader reader = new EnumerableReader((IEnumerable)data);

                // >> Stamp
                var assemblyStamp = (long)reader.Read();
                if (assemblyStamp != fileInfo.LastWriteTime.Ticks)
                {
                    return(false);
                }

                // new manager, add it now, remove later on errors
                manager = new ModuleManager(path);
                _Managers.Add(manager.ModuleName, manager);

                // read and load data
                var settings = manager.ReadSettings();
                manager.LoadData(settings);

                // >> Culture of cached resources
                var savedCulture = (string)reader.Read();

                // check the culture
                if (savedCulture.Length > 0)
                {
                    // the culture changed, ignore the cache
                    if (savedCulture != manager.CurrentUICulture.Name)
                    {
                        return(false);
                    }

                    // restore the flag
                    manager.CachedResources = true;
                }

                // >> Settings
                manager.HasSettings = (bool)reader.Read();

                object kind;
                while (null != (kind = reader.TryRead()))
                {
                    ProxyAction action = null;
                    switch ((ModuleItemKind)kind)
                    {
                    case ModuleItemKind.Host:
                    {
                        manager.SetModuleHost((string)reader.Read());
                    }
                    break;

                    case ModuleItemKind.Command:
                    {
                        var it = new ProxyCommand(manager, reader);
                        Host.Instance.RegisterProxyCommand(it);
                        action = it;
                    }
                    break;

                    case ModuleItemKind.Editor:
                    {
                        var it = new ProxyEditor(manager, reader);
                        Host.Instance.RegisterProxyEditor(it);
                        action = it;
                    }
                    break;

                    case ModuleItemKind.Drawer:
                    {
                        var it = new ProxyDrawer(manager, reader);
                        Host.Instance.RegisterProxyDrawer(it);
                        action = it;
                    }
                    break;

                    case ModuleItemKind.Tool:
                    {
                        var it = new ProxyTool(manager, reader);
                        Host.Instance.RegisterProxyTool(it);
                        action = it;
                    }
                    break;

                    default:
                        throw new ModuleException();
                    }

                    if (action != null)
                    {
                        action.LoadData((Hashtable)settings[action.Id]);
                    }
                }

                done = true;
            }
            catch (ModuleException)
            {
                // ignore known
            }
            catch (Exception ex)
            {
                throw new ModuleException(string.Format(null, "Error on reading the cache for '{0}'.", path), ex);
            }
            finally
            {
                if (!done)
                {
                    // remove cached data
                    _Cache.Remove(path);

                    // remove the manager
                    if (manager != null)
                    {
                        RemoveModuleManager(manager);
                    }
                }
            }

            return(done);
        }
コード例 #3
0
ファイル: ModuleLoader.cs プロジェクト: pezipink/FarNet
        /// <summary>
        /// Loads the module item by type.
        /// </summary>
        static int LoadType(ModuleManager manager, Hashtable settings, Type type)
        {
            Log.Source.TraceInformation("Load class {0}", type);

            // case: host
            if (typeof(ModuleHost).IsAssignableFrom(type))
            {
                manager.SetModuleHost(type);
                return 0;
            }

            // case: settings
            if (typeof(ApplicationSettingsBase).IsAssignableFrom(type))
            {
                manager.HasSettings = true;
                return 0;
            }

            // command
            ProxyAction action;
            if (typeof(ModuleCommand).IsAssignableFrom(type))
            {
                var it = new ProxyCommand(manager, type);
                Host.Instance.RegisterProxyCommand(it);
                action = it;
            }
            // tool
            else if (typeof(ModuleTool).IsAssignableFrom(type))
            {
                var it = new ProxyTool(manager, type);
                Host.Instance.RegisterProxyTool(it);
                action = it;
            }
            // editor
            else if (typeof(ModuleEditor).IsAssignableFrom(type))
            {
                var it = new ProxyEditor(manager, type);
                Host.Instance.RegisterProxyEditor(it);
                action = it;
            }
            // drawer
            else if (typeof(ModuleDrawer).IsAssignableFrom(type))
            {
                var it = new ProxyDrawer(manager, type);
                Host.Instance.RegisterProxyDrawer(it);
                action = it;
            }
            else throw new ModuleException("Unknown module class type.");

            // set settings
            action.LoadData((Hashtable)settings[action.Id]);

            return 1;
        }