From 30183f1eb6536994ee270a0bde7ccda38c882ed7 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Thu, 30 Jun 2022 20:24:33 +0200 Subject: [PATCH 1/5] feat: Implement module manager --- lib/src/internal/entities/module_manager.dart | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lib/src/internal/entities/module_manager.dart diff --git a/lib/src/internal/entities/module_manager.dart b/lib/src/internal/entities/module_manager.dart new file mode 100644 index 000000000..d20cecb9f --- /dev/null +++ b/lib/src/internal/entities/module_manager.dart @@ -0,0 +1,81 @@ +import 'dart:io'; +import 'dart:mirrors'; + +import 'package:mineral/console.dart'; +import 'package:mineral/core.dart'; +import 'package:mineral/src/exceptions/already_exist.dart'; +import 'package:mineral/src/internal/entities/command_manager.dart'; +import 'package:mineral/src/internal/entities/store_manager.dart'; + +import 'event_manager.dart'; + +class Module { + final String identifier; + final String label; + final String? description; + + const Module({ required this.identifier, required this.label, this.description }); +} + +abstract class MineralModule { + late final String identifier; + late final String label; + late final String? description; + + abstract List events; + abstract List commands; + abstract List stores; + + Future init () async {} +} + +class ModuleManager { + final Map _modules = {}; + Map get modules => _modules; + + ModuleManager add (MineralModule module, { String? overrideIdentifier }) { + Module moduleDecorator = reflect(module).type.metadata.first.reflectee; + module..identifier = moduleDecorator.identifier + ..label = moduleDecorator.label + ..description = moduleDecorator.description; + + if (_modules.containsKey(moduleDecorator.identifier)) { + if (overrideIdentifier != null) { + _modules.putIfAbsent(overrideIdentifier, () => module); + } else { + throw AlreadyExist(cause: 'Module ${moduleDecorator.identifier} is already registered, perhaps this is an error. If not, please change the name of the module when you register it.'); + } + } else { + _modules.putIfAbsent(moduleDecorator.identifier, () => module); + } + + return this; + } + + Future load () async { + EventManager eventManager = ioc.singleton(ioc.services.event); + CommandManager commandManager = ioc.singleton(ioc.services.command); + StoreManager storeManager = ioc.singleton(ioc.services.store); + + _modules.forEach((key, module) async { + await module.init(); + + for (MineralEvent event in module.events) { + eventManager.add(event); + } + + for (MineralCommand command in module.commands) { + commandManager.add(command); + } + + for (MineralStore event in module.stores) { + storeManager.add(event); + } + + Console.debug( + prefix: 'Loading module', + message: '"${module.label}" with ${module.events.length} events, ${module.commands.length} commands and ${module.stores.length} stores.' + ); + }); + } +} From 74f204d8674698e5d91a60104f3f4d9486d70663 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Thu, 30 Jun 2022 20:24:41 +0200 Subject: [PATCH 2/5] feat: Implement module manager --- lib/src/internal/entities/module_manager.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/internal/entities/module_manager.dart b/lib/src/internal/entities/module_manager.dart index d20cecb9f..b0152f523 100644 --- a/lib/src/internal/entities/module_manager.dart +++ b/lib/src/internal/entities/module_manager.dart @@ -1,4 +1,3 @@ -import 'dart:io'; import 'dart:mirrors'; import 'package:mineral/console.dart'; From 192c3108f63c958241bfabdf98c852ca7f607091 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Thu, 30 Jun 2022 20:25:05 +0200 Subject: [PATCH 3/5] feat: Add ModuleManager to available services --- lib/src/internal/ioc.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/src/internal/ioc.dart b/lib/src/internal/ioc.dart index 2cf5786af..b70611ac5 100644 --- a/lib/src/internal/ioc.dart +++ b/lib/src/internal/ioc.dart @@ -5,6 +5,7 @@ class Service { final event = 'Mineral/Core/Event'; final command = 'Mineral/Core/Command'; final store = 'Mineral/Core/Store'; + final modules = 'Mineral/Core/Modules'; final shards = 'Mineral/Core/Shards'; } From 85b396b747f189c0b6d51d989c760adf6ae13d89 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Thu, 30 Jun 2022 20:25:30 +0200 Subject: [PATCH 4/5] feat: Externalization of ModuleManager classes --- lib/core.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/core.dart b/lib/core.dart index 0a05108e2..d25630525 100644 --- a/lib/core.dart +++ b/lib/core.dart @@ -10,3 +10,4 @@ export 'src/internal/http.dart'; export 'src/internal/entities/event_manager.dart' show Event, Events, MineralEvent; export 'src/internal/entities/command_manager.dart' show Command, MineralCommand, Option, OptionType, OptionChoice, Subcommand, CommandGroup; export 'src/internal/entities/store_manager.dart' show Store, MineralStore; +export 'src/internal/entities/module_manager.dart' show Module, MineralModule; From 5ced1c8a5eac8943b51b7b6bb59f20694d72b720 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Thu, 30 Jun 2022 20:26:05 +0200 Subject: [PATCH 5/5] feat: Implement ModuleManager into kernel --- lib/src/internal/kernel.dart | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/src/internal/kernel.dart b/lib/src/internal/kernel.dart index 1a7ee53db..2d953d397 100644 --- a/lib/src/internal/kernel.dart +++ b/lib/src/internal/kernel.dart @@ -3,8 +3,8 @@ import 'package:mineral/exception.dart'; import 'package:mineral/src/internal/entities/command_manager.dart'; import 'package:mineral/src/internal/entities/event_manager.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/src/internal/entities/module_manager.dart'; import 'package:mineral/src/internal/websockets/sharding/shard_manager.dart'; -import 'package:mineral/src/internal/websockets/websocket_manager.dart'; import 'entities/store_manager.dart'; @@ -12,21 +12,23 @@ class Kernel { EventManager events = EventManager(); CommandManager commands = CommandManager(); StoreManager stores = StoreManager(); + ModuleManager modules = ModuleManager(); List intents = []; Kernel() { ioc.bind(namespace: ioc.services.event, service: events); ioc.bind(namespace: ioc.services.command, service: commands); ioc.bind(namespace: ioc.services.store, service: stores); + ioc.bind(namespace: ioc.services.modules, service: modules); } Future init () async { + Environment environment = await _loadEnvironment(); + Http http = Http(baseUrl: 'https://discord.com/api'); http.defineHeader(header: 'Content-Type', value: 'application/json'); ioc.bind(namespace: ioc.services.http, service: http); - Environment environment = await _loadEnvironment(); - String? token = environment.get('APP_TOKEN'); if (token == null) { throw TokenException( @@ -35,6 +37,8 @@ class Kernel { ); } + await modules.load(); + final String? shardsCount = environment.get('SHARDS_COUNT'); ShardManager manager = ShardManager(http, token, intents);