Пример #1
0
 /// <summary>
 ///     Creates new <see cref="SafeTimer"/> timer.
 /// </summary>
 /// <param name="plugin"></param>
 /// <param name="interval"></param>
 /// <param name="game"></param>
 /// <param name="action"></param>
 /// <returns></returns>
 public static SafeTimer CreateTimer(this IPlugin3 plugin, int interval, GameInterface game, Action action)
 {
     return(new SafeTimer(interval, game, action)
     {
         PluginName = plugin.Name
     });
 }
Пример #2
0
        public static T LoadSettingsJSON <T>(this IPlugin3 plugin, string path = null) where T : class, new()
        {
            var mySettingsFile = path ?? $"{AppFolders.PluginsSettingsDir}\\{plugin.Name}\\settings.json";

            if (File.Exists(mySettingsFile))
            {
                var rawText = File.ReadAllText(mySettingsFile, Encoding.UTF8);
                return(JsonConvert.DeserializeObject <T>(rawText));
            }
            return(new T());
        }
Пример #3
0
        /// <summary>
        /// DO NOT USE INSIDE <see cref="IPlugin3.OnStop"/> or <see cref="IPlugin3.OnStart"/> METHODS!
        /// </summary>
        /// <param name="callerPlugin"></param>
        /// <param name="name"></param>
        public static void AddPluginToRunning(this IPlugin3 callerPlugin, string name)
        {
            var plugin = PluginManagerEx.LoadedPlugins.FirstOrDefault(l => l.Name == name);

            if (plugin != null)
            {
                var activePlugin = PluginManagerEx.RunningPlugins.FirstOrDefault(l => l.Name == name);
                if (activePlugin == null && WoWProcessManager.Processes.TryGetValue(PluginManagerEx.PluginWoW[callerPlugin.Name], out WowProcess wow))
                {
                    PluginManagerEx.AddPluginToRunning(plugin, wow);
                }
            }
        }
Пример #4
0
 internal static void RemovePluginFromRunning(IPlugin3 plugin)
 {
     lock (AddRemoveLock)
     {
         if (RunningPlugins.Contains(plugin))
         {
             try
             {
                 plugin.OnStop();
                 PluginWoW.Remove(plugin.Name);
                 log.Info($"[{plugin.Name}] Plug-in is stopped");
             }
             catch (Exception ex)
             {
                 log.Error($"Can't shutdown plug-in [{plugin.Name}]: {ex.Message}");
             }
             Notify.TrayPopup(nameof(AxTools), "Plug-in <" + plugin.Name + "> is stopped", NotifyUserType.Info, false, plugin.TrayIcon);
             _pluginContainers.First(l => l.Plugin.GetType() == plugin.GetType()).IsRunning = false;
             PluginStateChanged?.Invoke(plugin);
         }
     }
 }
Пример #5
0
        public static void SaveSettingsJSON <T>(this IPlugin3 plugin, T data, string path = null) where T : class
        {
            var sb = new StringBuilder(1024);

            using (StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture))
            {
                using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
                {
                    var js = new JsonSerializer();
                    js.Converters.Add(new StringEnumConverter());
                    jsonWriter.Formatting  = Formatting.Indented;
                    jsonWriter.IndentChar  = ' ';
                    jsonWriter.Indentation = 4;
                    js.Serialize(jsonWriter, data);
                }
            }
            var mySettingsDir = AppFolders.PluginsSettingsDir + "\\" + plugin.Name;

            if (!Directory.Exists(mySettingsDir))
            {
                Directory.CreateDirectory(mySettingsDir);
            }
            File.WriteAllText(path ?? mySettingsDir + "\\settings.json", sb.ToString(), Encoding.UTF8);
        }
Пример #6
0
 internal static void AddPluginToRunning(IPlugin3 plugin, WowProcess process)
 {
     lock (AddRemoveLock)
     {
         if (!RunningPlugins.Contains(plugin))
         {
             _pluginContainers.First(l => l.Plugin.GetType() == plugin.GetType()).IsRunning = true;
             try
             {
                 plugin.OnStart(new GameInterface(process));
                 PluginWoW[plugin.Name] = process.ProcessID;
                 PluginsUsageStats[plugin.Name].Add(DateTime.UtcNow);
                 SavePluginUsageStats();
                 log.Info($"{process} [{plugin.Name}] Plug-in is started");
             }
             catch (Exception ex)
             {
                 log.Error($"Plug-in OnStart error [{plugin.Name}]: {ex.Message}");
             }
             Notify.TrayPopup(nameof(AxTools), "Plug-in <" + plugin.Name + "> is started", NotifyUserType.Info, false, plugin.TrayIcon);
             PluginStateChanged?.Invoke(plugin);
         }
     }
 }
Пример #7
0
 internal PluginContainer(IPlugin3 plugin)
 {
     Plugin    = plugin;
     IsRunning = false;
 }
Пример #8
0
 public static void LogError(this IPlugin3 plugin, object text)
 {
     log.Error($"[Plug-in: {plugin.Name}] {text}");
 }
Пример #9
0
 /// <summary>
 /// Makes a record to the log. WoW process name, process id and plug-in name are included
 /// </summary>
 /// <param name="plugin"></param>
 /// <param name="text"></param>
 public static void LogPrint(this IPlugin3 plugin, object text)
 {
     log.Info($"[Plug-in: {plugin.Name}] {text}");
 }
Пример #10
0
 public static string GetPluginSourceFolder(this IPlugin3 plugin)
 {
     return($"{Settings2.Instance.PluginSourceFolder}\\{plugin.Name}");
 }
Пример #11
0
 public static string GetPluginSettingsDir(this IPlugin3 plugin)
 {
     return($"{AppFolders.PluginsSettingsDir}\\{plugin.Name}");
 }
Пример #12
0
 ///  <summary>
 ///
 ///  </summary>
 ///  <param name="plugin"></param>
 ///  <param name="text">Any text you want</param>
 ///  <param name="warning">Is it warning or info</param>
 ///  <param name="sound">Play sound</param>
 ///  <param name="timeout">Time in seconds before pop-up disappears</param>
 /// <param name="onClick"></param>
 public static void ShowNotify(this IPlugin3 plugin, string text, bool warning, bool sound, int timeout = 10, MouseEventHandler onClick = null)
 {
     Notify.TrayPopup("[" + plugin.Name + "]", text, warning ? NotifyUserType.Warn : NotifyUserType.Info, sound, plugin.TrayIcon, timeout, onClick);
 }
Пример #13
0
        private static void LoadPluginsFromDisk()
        {
#pragma warning disable S3885 // "Assembly.Load" should be used
            Assembly.LoadFile(Path.Combine(Application.StartupPath, "KeraLua.dll"));
            Assembly.LoadFile(Path.Combine(Application.StartupPath, "NLua.dll"));
            Assembly.LoadFile(Path.Combine(Application.StartupPath, "ICSharpCode.TextEditor.dll"));

            var directories = Directory.GetDirectories(Settings2.Instance.PluginSourceFolder);
            foreach (string directory in directories)
            {
                try
                {
                    log.Info($"Processing directory <{directory}>");
                    var      md5ForFolder = Utils.CreateMd5ForFolder(directory);
                    var      dllPath      = $"{AppFolders.PluginsBinariesDir}\\{md5ForFolder}.dll";
                    Assembly dll;
                    if (!File.Exists(dllPath))
                    {
                        dll = CompilePlugin(directory);
                        log.Info($"Plug-in from directory <{directory}> is compiled");
                    }
                    else
                    {
                        dll = Assembly.LoadFile(dllPath);
                        log.Info($"Plug-in from directory <{directory}> with hash {md5ForFolder} is already compiled");
                    }
                    if (dll != null)
                    {
                        var type = dll.GetTypes().FirstOrDefault(k => k.IsClass && typeof(IPlugin3).IsAssignableFrom(k));
                        if (type != default(Type))
                        {
                            IPlugin3 temp = null;
                            if (typeof(Control).IsAssignableFrom(type))
                            {
                                log.Info($"Plug-in from directory <{directory}> creates control(s) in it's constructor!");
                                MainWindow.Instance.Invoke((MethodInvoker) delegate { temp = (IPlugin3)Activator.CreateInstance(type); });
                            }
                            else
                            {
                                temp = (IPlugin3)Activator.CreateInstance(type);
                            }
                            if (_pluginContainers.Select(l => l.Plugin).All(l => l.Name != temp.Name))
                            {
                                if (!string.IsNullOrWhiteSpace(temp.Name))
                                {
                                    _pluginContainers.Add(new PluginContainer(temp));
                                    log.Info($"Plug-in loaded: {temp.Name} {temp.Version}");
                                    PluginLoaded?.Invoke(temp);
                                }
                                else
                                {
                                    throw new MissingFieldException("<IPlugin2.Name> is empty");
                                }
                            }
                            else
                            {
                                log.Info($"Can't load plug-in [{temp.Name}]: already loaded");
                            }
                        }
                        else
                        {
                            throw new BadImageFormatException("Can't find IPlugin2 interface in plug-in image!");
                        }
                    }
                    else
                    {
                        throw new BadImageFormatException("Plug-in image is null!");
                    }
                }
                catch (Exception ex)
                {
                    log.Info($"Can't load plug-in <{directory}>: {ex.Message}");
                    Notify.TrayPopup("[PluginManager] Plug-in error", $"Plug-in from folder '{directory}' cannot be loaded.\r\nClick here to open the website with updated versions of plug-ins",
                                     NotifyUserType.Warn, true, null, 10, (sender, args) => Process.Start(Globals.PluginsURL));
                }
            }
#pragma warning restore S3885 // "Assembly.Load" should be used
        }