public void createConfig(string uniqueID, string uri = Constants.FALLBACK_URI, string retry = Constants.FALLBACK_HOTKEY)
 {
     Debugger.Error("Config.json for DiscordHelper not found!");
     Debugger.Module("Creating Config.json.", Name);
     File.WriteAllText(
         configPath, "{\n\t\"uri\": \"" + uri + "\",\n\t\"id\": \"" + uniqueID + "\",\n\t\"retry\": \"" + retry + "\"\n}");
 }
        private int SetupWsClient(string uri, string id)
        {
            WsClient = new WebSocket(uri + "?uniqueid=" + HttpUtility.UrlEncode(id));

            WsClient.OnOpen += (sender, e) =>
            {
                //Send confirmation message to HunterPie
                Debugger.Module("Connected to discord server.", Name);
            };

            WsClient.OnClose += (sender, e) =>
            {
                Debugger.Module("Close code: " + e.Code + " Close reason: " + e.Reason, Name);
                Debugger.Module("Disconnected from discord server. Press " + config.retry + " to try reconnect.", Name);
            };

            WsClient.OnMessage += (sender, e) =>
            {
                handleMessage(sender, e);
            };

            WsClient.Connect();

            return(1);
        }
Ejemplo n.º 3
0
        public void PreloadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            Debugger.Module("Pre loading modules");
            string[] modules = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
            foreach (string module in modules)
            {
                // Skip modules without Module.json
                if (!File.Exists(Path.Combine(module, "module.json")))
                {
                    continue;
                }

                try
                {
                    string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);
                    PluginSettings    modSettings      = GetPluginSettings(module);

                    if (File.Exists(Path.Combine(module, modInformation.EntryPoint)))
                    {
                        Debugger.Module($"Compiling plugin: {modInformation.Name}");
                        if (CompilePlugin(module, modInformation))
                        {
                            Debugger.Module($"{modInformation.Name} compiled successfully.");
                        }
                        else
                        {
                            continue;
                        }
                    }
                    Stopwatch s = Stopwatch.StartNew();
                    foreach (string required in modInformation.Dependencies)
                    {
                        AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
                    }
                    Assembly           plugin  = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll")));
                    IEnumerable <Type> entries = plugin.ExportedTypes.Where(exp => exp.GetMethod("Initialize") != null);
                    if (entries.Count() > 0)
                    {
                        dynamic mod = plugin.CreateInstance(entries.First().ToString());
                        packages.Add(new PluginPackage {
                            plugin = mod, information = modInformation, settings = modSettings, path = module
                        });
                    }
                } catch (Exception err)
                {
                    Debugger.Error(err);
                    continue;
                }
            }
            IsReady = true;
            benchmark.Stop();
            Debugger.Module($"Pre loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
            if (QueueLoad)
            {
                LoadPlugins();
            }
        }
Ejemplo n.º 4
0
        public void LoadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            if (packages.Count > 0)
            {
                // Quick load
                foreach (PluginPackage package in packages)
                {
                    if (!package.settings.IsEnabled)
                    {
                        continue;
                    }

                    try
                    {
                        package.plugin.Initialize(ctx);
                    }
                    catch (Exception err)
                    {
                        Debugger.Error(err);
                    }
                }
            }
            benchmark.Stop();
            Debugger.Module($"Loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
        }
Ejemplo n.º 5
0
 public void UnloadPlugins()
 {
     foreach (PluginPackage package in packages)
     {
         UnloadPlugin(package.plugin);
     }
     Debugger.Module("Unloaded all modules.");
 }
Ejemplo n.º 6
0
 public void UnloadPlugins()
 {
     foreach (IPlugin plugin in plugins)
     {
         plugin.Unload();
     }
     Debugger.Module("Unloaded all modules.");
 }
Ejemplo n.º 7
0
        public void LoadPlugins(Game ctx)
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            if (plugins.Count > 0)
            {
                // Quick load
                foreach (IPlugin plugin in plugins)
                {
                    plugin.Initialize(ctx);
                }
            }
            else
            {
                string[] modules = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
                foreach (string module in modules)
                {
                    try
                    {
                        string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                        PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                        if (File.Exists(Path.Combine(module, modInformation.EntryPoint)))
                        {
                            Debugger.Module($"Compiling plugin: {modInformation.Name}");
                            if (CompilePlugin(module, modInformation))
                            {
                                Debugger.Module($"{modInformation.Name} compiled successfully.");
                            }
                            else
                            {
                                continue;
                            }
                        }

                        foreach (string required in modInformation.Dependencies)
                        {
                            AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
                        }
                        var plugin = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll")));
                        IEnumerable <Type> entries = plugin.ExportedTypes.Where(exp => exp.GetMethod("Initialize") != null);
                        if (entries.Count() > 0)
                        {
                            dynamic mod = plugin.CreateInstance(entries.First().ToString());
                            mod.Initialize(ctx);
                            plugins.Add(mod);
                        }
                    } catch (Exception err)
                    {
                        Debugger.Error(err);
                        continue;
                    }
                }
            }
            benchmark.Stop();
            Debugger.Module($"Loaded {plugins.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
        }
Ejemplo n.º 8
0
        public async Task <bool> PreloadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            Debugger.Module("Pre loading modules");
            foreach (string module in IterateModules())
            {
                try
                {
                    string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                    try
                    {
                        await PreloadPlugin(module, modInformation);

                        failedPlugins.Remove(modInformation.Name);
                    }
                    catch
                    {
                        failedPlugins.Add(modInformation.Name);
                        throw;
                    }
                }
                catch (Exception err)
                {
                    Debugger.Error(err);
                }
            }

            benchmark.Stop();
            Debugger.Module($"Pre loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");

            if (!tsc.Task.IsCompleted)
            {
                tsc.SetResult(null);
            }
            return(true);
        }
Ejemplo n.º 9
0
        public async Task LoadPlugins()
        {
            await PreloadTask;

            Stopwatch benchmark = Stopwatch.StartNew();

            if (packages.Count > 0)
            {
                // Quick load
                foreach (PluginPackage package in packages.Where(p => p.settings.IsEnabled))
                {
                    try
                    {
                        package.plugin.Initialize(ctx);
                    }
                    catch (Exception err)
                    {
                        Debugger.Error(err);
                    }
                }
            }
            benchmark.Stop();
            Debugger.Module($"Loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
        }
Ejemplo n.º 10
0
        private async Task PreloadPlugin(string module, PluginInformation modInformation)
        {
            if (File.Exists(Path.Combine(module, ".remove")))
            {
                Directory.Delete(module, true);
                Debugger.Module($"Plugin {modInformation.Name} removed.");
                return;
            }

            if (modInformation.Update.MinimumVersion is null)
            {
                Debugger.Error($"{modInformation.Name.ToUpper()} MIGHT BE OUTDATED! CONSIDER UPDATING IT.");
            }

            if (PluginUpdate.PluginSupportsUpdate(modInformation))
            {
                switch (await PluginUpdate.UpdateAllFiles(modInformation, module))
                {
                case UpdateResult.Updated:
                    var serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    modInformation = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                    Debugger.Module($"Updated plugin: {modInformation.Name} (ver {modInformation.Version})");
                    break;

                case UpdateResult.Skipped:
                    break;

                case UpdateResult.Failed:
                    Debugger.Error($"Failed to update plugin: {modInformation.Name}");
                    break;

                case UpdateResult.UpToDate:
                    Debugger.Module($"Plugin {modInformation.Name} is up-to-date (ver {modInformation.Version})");
                    break;
                }
            }

            PluginSettings modSettings = GetPluginSettings(module);

            if (!string.IsNullOrEmpty(modInformation.EntryPoint) &&
                File.Exists(Path.Combine(module, modInformation.EntryPoint)))
            {
                Debugger.Module($"Compiling plugin: {modInformation.Name}");
                if (CompilePlugin(module, modInformation))
                {
                    Debugger.Module($"{modInformation.Name} compiled successfully.");
                }
                else
                {
                    return;
                }
            }

            foreach (string required in modInformation.Dependencies)
            {
                AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
            }

            Assembly plugin =
                AppDomain.CurrentDomain.Load(
                    AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll")));
            IEnumerable <Type> entries = plugin.ExportedTypes.Where(exp => exp.GetMethod("Initialize") != null);

            if (entries.Any())
            {
                dynamic mod = plugin.CreateInstance(entries.First().ToString());
                packages.Add(new PluginPackage
                {
                    plugin = mod, information = modInformation, settings = modSettings, path = module
                });
            }
        }
Ejemplo n.º 11
0
        private async Task PreloadPlugin(string module, PluginInformation modInformation)
        {
            if (File.Exists(Path.Combine(module, ".remove")))
            {
                Directory.Delete(module, true);
                Debugger.Module($"Plugin {modInformation.Name} removed.");
                return;
            }

            if (modInformation.Update.MinimumVersion is null)
            {
                Debugger.Error($"{modInformation.Name.ToUpper()} MIGHT BE OUTDATED! CONSIDER UPDATING IT.");
            }

            if (PluginUpdate.PluginSupportsUpdate(modInformation))
            {
                switch (await PluginUpdate.UpdateAllFiles(modInformation, module))
                {
                case UpdateResult.Updated:
                    var serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    modInformation = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                    Debugger.Module($"Updated plugin: {modInformation.Name} (ver {modInformation.Version})");
                    break;

                case UpdateResult.Skipped:
                    break;

                case UpdateResult.Failed:
                    Debugger.Error($"Failed to update plugin: {modInformation.Name}");
                    break;

                case UpdateResult.UpToDate:
                    Debugger.Module($"Plugin {modInformation.Name} is up-to-date (ver {modInformation.Version})");
                    break;
                }
            }

            PluginSettings modSettings = GetPluginSettings(module);

            if (!string.IsNullOrEmpty(modInformation.EntryPoint) &&
                File.Exists(Path.Combine(module, modInformation.EntryPoint)))
            {
                Debugger.Module($"Compiling plugin: {modInformation.Name}");
                if (CompilePlugin(module, modInformation))
                {
                    Debugger.Module($"{modInformation.Name} compiled successfully.");
                }
                else
                {
                    return;
                }
            }

            foreach (string required in modInformation.Dependencies)
            {
                AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
            }

            var moduleAssembly = AppDomain.CurrentDomain.Load(
                AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll"))
                );
            var pluginType = moduleAssembly.ExportedTypes.FirstOrDefault(exp => exp.GetMethod("Initialize") != null);

            if (pluginType != null)
            {
                var plugin = (IPlugin)moduleAssembly.CreateInstance(pluginType.FullName);
                // making sure that name is matching modInformation, e.g. if plugin dev forgot to populate this value
                plugin.Name = modInformation.Name;

                var package = new PluginPackage
                {
                    plugin = plugin, information = modInformation, settings = modSettings, path = module
                };
                packages.Add(package);

                // if plugin is enabled, adding it's settings
                if (modSettings.IsEnabled)
                {
                    AddPackageSettings(package);
                }
            }
        }
Ejemplo n.º 12
0
        public async Task <bool> PreloadPlugins()
        {
            Stopwatch benchmark = Stopwatch.StartNew();

            Debugger.Module("Pre loading modules");
            string[] modules = Directory.GetDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules"));
            foreach (string module in modules)
            {
                // Skip modules without Module.json
                if (!File.Exists(Path.Combine(module, "module.json")))
                {
                    continue;
                }

                try
                {
                    string            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                    PluginInformation modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                    if (modInformation.Update.MinimumVersion is null)
                    {
                        Debugger.Error($"{modInformation.Name.ToUpper()} MIGHT BE OUTDATED! CONSIDER UPDATING IT.");
                    }

                    if (PluginUpdate.PluginSupportsUpdate(modInformation))
                    {
                        switch (await PluginUpdate.UpdateAllFiles(modInformation, module))
                        {
                        case UpdateResult.Updated:
                            serializedModule = File.ReadAllText(Path.Combine(module, "module.json"));
                            modInformation   = JsonConvert.DeserializeObject <PluginInformation>(serializedModule);

                            Debugger.Module($"Updated plugin: {modInformation.Name} (ver {modInformation.Version})");
                            break;

                        case UpdateResult.Skipped:
                            break;

                        case UpdateResult.Failed:
                            Debugger.Error($"Failed to update plugin: {modInformation.Name}");
                            continue;

                        case UpdateResult.UpToDate:
                            Debugger.Module($"Plugin {modInformation.Name} is up-to-date (ver {modInformation.Version})");
                            break;
                        }
                    }

                    PluginSettings modSettings = GetPluginSettings(module);

                    if (!string.IsNullOrEmpty(modInformation.EntryPoint) && File.Exists(Path.Combine(module, modInformation.EntryPoint)))
                    {
                        Debugger.Module($"Compiling plugin: {modInformation.Name}");
                        if (CompilePlugin(module, modInformation))
                        {
                            Debugger.Module($"{modInformation.Name} compiled successfully.");
                        }
                        else
                        {
                            continue;
                        }
                    }
                    Stopwatch s = Stopwatch.StartNew();
                    foreach (string required in modInformation.Dependencies)
                    {
                        AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, required)));
                    }
                    Assembly           plugin  = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(Path.Combine(module, $"{modInformation.Name}.dll")));
                    IEnumerable <Type> entries = plugin.ExportedTypes.Where(exp => exp.GetMethod("Initialize") != null);
                    if (entries.Count() > 0)
                    {
                        dynamic mod = plugin.CreateInstance(entries.First().ToString());
                        packages.Add(new PluginPackage {
                            plugin = mod, information = modInformation, settings = modSettings, path = module
                        });
                    }
                }
                catch (Exception err)
                {
                    Debugger.Error(err);
                    continue;
                }
            }

            IsReady = true;
            benchmark.Stop();
            Debugger.Module($"Pre loaded {packages.Count} module(s) in {benchmark.ElapsedMilliseconds}ms");
            if (QueueLoad)
            {
                LoadPlugins();
            }
            return(true);
        }