This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
PluginManager.py
62 lines (53 loc) · 1.71 KB
/
PluginManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class PluginManager:
def __init__(self):
# key is plugin class, value is true if on else false
self.plugins = {}
# key is packet type, value is set of plugin classes (key for self.plugins)
self.hooks = {}
"""
initialize all plugins.
creates a dictionary with key
returns true if success, false o.w.
"""
def initializePlugins(self) -> bool:
import os
for plugin in os.listdir("Plugins"):
if plugin[-3:] == ".py":
t = plugin.replace(".py", "")
exec("from Plugins.{} import *".format(t))
try:
# by default, the plugin is not active.
if eval(t).load == True:
if eval(t).defaultState == True:
self.plugins.update({eval(t + "()") : True})
else:
self.plugins.update({eval(t + "()") : False})
except Exception as e:
print("There was an error when loading plugins. Make sure you follow the naming convention when writing your own plugins.")
print("Error:", e)
return False
print("[Initializer]: Successfully loaded {} plugins.".format(len(self.plugins)))
return True
# returns relevant class you searched for
def findClass(self, text: str):
for plugin in self.plugins:
if type(plugin).__name__ == text:
return plugin
"""
Creates a dictionary with key = PacketType, value = key of plugins
allows us to just call specific plugins on specific packets
"""
def initializeHookDictionary(self):
for plugin in self.plugins:
for hook in plugin.hooks:
# add new packettype hook
if hook not in self.hooks:
self.hooks.update({hook : {plugin}})
# already exists
else:
self.hooks[hook].add(plugin)
def initialize(self):
if not self.initializePlugins():
return False
self.initializeHookDictionary()
return True