private EltraPluginCacheItem FindPluginInFileSystem(DeviceToolPayload payload)
        {
            EltraPluginCacheItem result = null;
            var pluginFilePath          = GetPluginFilePath(GetPluginFileName(payload.Id));

            try
            {
                if (File.Exists(pluginFilePath))
                {
                    if (payload.Mode == DeviceToolPayloadMode.Development)
                    {
                        result = UpdateCache(payload, pluginFilePath);
                    }
                    else
                    {
                        if (GetHashCodeFromFile(pluginFilePath, out var hashCode) && hashCode == payload.HashCode)
                        {
                            result = UpdateCache(payload, pluginFilePath);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - FindPluginInFileSystem", e);
            }

            return(result);
        }
        private EltraPluginCacheItem FindPluginInCache(DeviceToolPayload payload)
        {
            EltraPluginCacheItem result = null;

            foreach (var pluginCacheItem in PluginCache)
            {
                if (payload.Mode == DeviceToolPayloadMode.Development)
                {
                    if (pluginCacheItem.PayloadId == payload.Id)
                    {
                        result = pluginCacheItem;
                        break;
                    }
                }
                else
                {
                    if (pluginCacheItem.HashCode == payload.HashCode)
                    {
                        result = pluginCacheItem;
                        break;
                    }
                }
            }

            return(result);
        }
        private EltraPluginCacheItem UpdateCache(DeviceToolPayload payload, string fullPath)
        {
            EltraPluginCacheItem result = null;

            if (UpdatePluginCache(fullPath, payload))
            {
                var cacheItem = FindPluginInCache(payload);

                if (cacheItem != null && cacheItem.Plugin != null)
                {
                    var viewModels = cacheItem.Plugin.GetViewModels();

                    OnPluginAdded(payload, viewModels);

                    result = cacheItem;
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        private EltraPluginCacheItem FindPluginInFileSystem(DeviceToolPayload payload)
        {
            EltraPluginCacheItem result = null;

            try
            {
                var pluginFilePath = GetPluginFilePath(payload.FileName);

                if (Debugging)
                {
                    var currentPathFileName = Path.GetFileName(pluginFilePath);
                    var binFolder           = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    var currentPathFullPath = Path.Combine(binFolder, currentPathFileName);

                    if (File.Exists(currentPathFullPath))
                    {
                        if (payload.Mode == DeviceToolPayloadMode.Development)
                        {
                            result = UpdateCache(payload, currentPathFullPath);
                        }
                    }
                    else if (File.Exists(pluginFilePath))
                    {
                        if (payload.Mode == DeviceToolPayloadMode.Development)
                        {
                            result = UpdateCache(payload, pluginFilePath);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - FindPluginInFileSystem", e);
            }

            return(result);
        }
        private bool UpdatePluginCache(string assemblyPath, DeviceToolPayload payload)
        {
            bool result = false;

            try
            {
                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"load assembly - path = {assemblyPath}");

                var theAssembly = Assembly.LoadFrom(assemblyPath);

                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", "load assembly - get types");

                Type[] types = theAssembly.GetTypes();

                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"load assembly - get types - count = {types.Length}");

                foreach (Type t in types)
                {
                    try
                    {
                        var type = t.GetInterface("EltraXamCommon.Plugins.IEltraNavigoPlugin");

                        MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"type full name = {t.FullName}");

                        if (type != null && !string.IsNullOrEmpty(t.FullName))
                        {
                            MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"create instance $ {t.FullName}");

                            var assemblyInstace = theAssembly.CreateInstance(t.FullName, false);

                            if (assemblyInstace is IEltraNavigoPlugin pluginInterface)
                            {
                                pluginInterface.DialogService = _dialogService;

                                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"find payload {payload.FileName}");

                                if (FindPluginInCache(payload) == null)
                                {
                                    var pluginCacheItem = new EltraPluginCacheItem()
                                    {
                                        FullPath = assemblyPath, HashCode = payload.HashCode, PayloadId = payload.Id, Plugin = pluginInterface
                                    };

                                    PluginCache.Add(pluginCacheItem);

                                    MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"add payload {payload.FileName} cache item");
                                }
                                else
                                {
                                    MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"payload {payload.FileName} already added");
                                }

                                result = true;

                                break;
                            }
                            else
                            {
                                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"error: create instance $ {t.FullName} failed!");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MsgLogger.Exception($"{GetType().Name} - UpdatePluginCache [1]", e);
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - UpdatePluginCache [2]", e);
            }

            return(result);
        }
Exemplo n.º 6
0
        private bool UpdatePluginCache(string assemblyPath, DeviceToolPayload payload)
        {
            bool result = false;

            try
            {
                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"load assembly - path = {assemblyPath}");

                var payloadPath = PluginStore.GetAssemblyFile(payload.Id);

                if (!string.IsNullOrEmpty(payloadPath))
                {
                    assemblyPath = payloadPath;
                }

                if (!string.IsNullOrEmpty(assemblyPath))
                {
                    var pluginAssembly = Assembly.LoadFrom(assemblyPath);

                    MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", "load assembly - get types");

                    Type[] types = pluginAssembly.GetTypes();

                    MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"load assembly - get types - count = {types.Length}");

                    foreach (Type t in types)
                    {
                        try
                        {
                            var type = t.GetInterface("EltraXamCommon.Plugins.IEltraNavigoPluginService");

                            MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"type full name = {t.FullName}");

                            if (type != null && !string.IsNullOrEmpty(t.FullName))
                            {
                                MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"create instance $ {t.FullName}");

                                var    assemblyInstace = pluginAssembly.CreateInstance(t.FullName, false);
                                string name            = pluginAssembly.GetName().Name;
                                string version         = pluginAssembly.GetName().Version.ToString();

                                if (assemblyInstace is IEltraNavigoPluginService pluginService)
                                {
                                    pluginService.DialogRequested += OnPluginInterfaceDialogRequested;

                                    MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"find payload {payload.FileName}");

                                    if (FindPluginInCache(payload) == null)
                                    {
                                        var pluginCacheItem = new EltraPluginCacheItem()
                                        {
                                            Name          = name, FullPath = assemblyPath,
                                            HashCode      = payload.HashCode, PayloadId = payload.Id,
                                            PluginService = pluginService, Version = version
                                        };

                                        PluginCache.Add(pluginCacheItem);

                                        MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"add payload {payload.FileName} cache item");
                                    }
                                    else
                                    {
                                        MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"payload {payload.FileName} already added");
                                    }

                                    result = true;

                                    break;
                                }
                                else
                                {
                                    MsgLogger.WriteDebug($"{GetType().Name} - UpdatePluginCache", $"error: create instance $ {t.FullName} failed!");
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            GC.Collect();
                            GC.WaitForPendingFinalizers();

                            MsgLogger.Exception($"{GetType().Name} - UpdatePluginCache [1]", e);
                        }
                    }
                }
                else
                {
                    MsgLogger.WriteError($"{GetType().Name} - UpdatePluginCache", "assembly store returns empty element");
                }
            }
            catch (Exception e)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();

                MsgLogger.Exception($"{GetType().Name} - UpdatePluginCache [2]", e);
            }

            return(result);
        }