Exemple #1
0
        /* And after finished the tree structor setup */
        /* We will check all the .dll file in mod directory that is in sub bot directory */
        static void ModSetup()
        {
            Debug.Log("System", "Mod Setup start...", ConsoleColor.Cyan);

            /* Loop all bot setup config */
            for (int i = 0; i < BotFolder.Length; i++)
            {
                /* This will check all the file in the .dll */
                ModArray = Directory.GetFiles(Path.Combine(BotFolder[i], Utility.BotsModFolder));

                /* Loop all mod dll */
                for (int j = 0; j < ModArray.Length; j++)
                {
                    /* If the file extension is not dll */
                    /* It mean this is not the module */
                    /* Because the module will have .dll extension */
                    if (Path.GetExtension(Path.GetFullPath(Path.Combine(ModArray[j]))).ToUpper() != ".DLL")
                    {
                        break;
                    }

                    /* Read that file in the assembly variable */
                    Assembly mod = Assembly.LoadFile(Path.GetFullPath(Path.Combine(ModArray[j])));

                    /* And check all the types in that .dll */
                    for (int k = 0; k < mod.GetTypes().Length; k++)
                    {
                        /* We create a instance object for every single types in the assembly */
                        object instance = mod.CreateInstance(mod.GetTypes()[k].Name);

                        /* Then we check if this dude have CustomModule interface in it */
                        /* Because we only want to control the interface */
                        /* We don't need to work with the class */
                        ICustomModule CM = instance as ICustomModule;
                        if (CM != null)
                        {
                            /* Sweet Jesus, we got them */
                            Console.WriteLine("Have interface custom module");

                            /* Tell the interface setup the default config */
                            /* Then initialize the mod */
                            CM.ConfigSetup();
                            CM.Initialize();
                        }
                        else
                        {
                            Console.WriteLine("Have no interface custom module");
                        }
                    }
                }
            }
            Debug.Log("System", "Mod Setup finished...", ConsoleColor.Cyan);
        }
        /* Loading the mods from the mod directory */
        public void LoadModule()
        {
            Debug.BotLog(m_BotSetting.name, "Modules", "Start loading modules...", (ConsoleColor)m_BotSetting.color);
            bool PathExist = Directory.Exists(m_Path);

            if (!PathExist)
            {
                Debug.BotLog(m_BotSetting.name, "Modules", "Path " + m_Path + " does not exist", (ConsoleColor)m_BotSetting.color);
                return;
            }

            bool ModExist = Directory.Exists(Path.Combine(m_Path, Utility.BotsModFolder));

            if (!ModExist)
            {
                Debug.BotLog(m_BotSetting.name, "Modules", "Mod path does not exist", ConsoleColor.Yellow);
                Debug.BotLog(m_BotSetting.name, "Modules", "Please check your setup", ConsoleColor.Yellow);
                return;
            }

            /* Modules */
            string[]        modules = Directory.GetFiles(Path.Combine(m_Path, Utility.BotsModFolder));
            List <Assembly> mods    = new List <Assembly>();

            /* Loading all the mod in Mod directory */
            for (int i = 0; i < modules.Length; i++)
            {
                Assembly mod = Assembly.LoadFile(Path.GetFullPath(modules[i]));
                mods.Add(mod);

                for (int k = 0; k < mod.GetTypes().Length; k++)
                {
                    /* We create a instance object for every single types in the assembly */
                    object instance = mod.CreateInstance(mod.GetTypes()[k].Name);

                    CustomModule module_base = instance as CustomModule;
                    if (module_base != null)
                    {
                        module_base.m_DiscordSocketClient = m_DiscordSocketClient;
                        module_base.SetBotRoot(m_Path);
                    }

                    ICustomModule module_interface = instance as ICustomModule;
                    if (module_interface != null)
                    {
                        Struct.ModuleInfo info = module_interface.GetInfo();
                        Debug.BotLog(m_BotSetting.name, "Modules", "Detect module: " + info.m_ModuleName, (ConsoleColor)m_BotSetting.color);
                        Debug.BotLog(m_BotSetting.name, "Modules", "Module descrption: " + info.m_ModuleDescription, (ConsoleColor)m_BotSetting.color);
                        Debug.BotLog(m_BotSetting.name, "Modules", "Module Version: " + info.m_VersionMajor + "." + info.m_VersionMinor, (ConsoleColor)m_BotSetting.color);

                        module_interface.ConfigSetup();
                        module_interface.Initialize();
                    }
                }
            }

            /* store into array */
            m_Modules = mods.ToArray();

            Debug.BotLog(m_BotSetting.name, "Modules", "Finished loading modules... this bot's modules amount: " + mods.Count, (ConsoleColor)m_BotSetting.color);
        }