🔌插件系统实现
PART 1:PluginManager插件管理器
# 加载类
class Load:
def __init__(self,Register) -> None:
self.Register=Register
pass
# 检测插件
def check(self):
# 读取 Plugins.json 文件
with open("myproject\PluginSystem\Plugins.json", 'r') as f:
registered_plugins = [i["name"] for i in json.load(f)['PluginList']]
# print(registered_plugins)
# 扫描子文件夹 Plugins 下的所有文件夹,找到其中的插件
plugins = []
for folder_name in os.listdir('myproject\PluginSystem\Plugins'):
folder_path = os.path.join('myproject\PluginSystem\Plugins', folder_name)
if os.path.isdir(folder_path) and os.path.isfile(os.path.join(folder_path, 'info.json')):
plugins.append(folder_name)
# print(plugins)
# 检查每个插件是否都已经在 Plugins.json 中注册,为注册则注册该插件
for plugin in plugins:
if plugin not in registered_plugins:
# print(f'Error: Plugin {plugin} is not registered in Plugins.json')
self.Register.registe(plugin)
print("所有插件加载完成")PART 2:MessageManager插件消息管理器
PART 3:plugins.Interface插件接口
最后更新于