-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_plugin.py
152 lines (117 loc) · 3.64 KB
/
python_plugin.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
from Action import Action
from Command import Command
from Event import Event
from Event import EventMode
from py4j.java_gateway import JavaGateway, CallbackServerParameters
from onEnable import OnEnable, OnDisable
import importlib
PluginFolder = "./plugins"
MainModule = "__init__"
InfoFile = "/info.txt"
server = None
try:
os.mkdir(PluginFolder)
except:
pass
# noinspection PyMethodMayBeStatic
class PythonListener(object):
def refreshPlugins(self):
Plugin.refresh()
for plugin in Plugin.plugins:
Plugin.plugins[plugin].load()
def loadPlugin(self, plugin: str):
try:
Plugin.plugins[plugin].load()
except:
Plugin.refresh()
if Plugin.plugins[plugin].load() == 0:
raise PythonPlugin("{} is not a directory or does not exist".format(plugin))
def unloadPlugin(self, plugin: str):
Plugin.plugins[plugin].unload()
def reloadPlugin(self, plugin: str):
Plugin.plugins[plugin].reload()
def callEvent(self, name: str, mode: int, obj):
event_mode = None
if mode == 1:
event_mode = EventMode.NormalHookMode
if mode == 0:
event_mode = EventMode.PreHookMode
Event.call(name, event_mode, obj)
def commandExecutor(self, name: str, args, player):
Command.call(name, args, player)
def getCommands(self):
a = []
for command in Command.instances:
a.append(command.name)
return a
def getActions(self):
a = []
for action in Action.instances:
a.append(action.name)
return a
def getServer(self, server_):
global server
server = server_
def setPluginsDir(self, str):
os.chdir(str)
class Java:
implements = ["cn.textwar.langs.python.PyPluginLoader"]
from py4j.java_gateway import JavaGateway, CallbackServerParameters
listener = PythonListener()
gateway = JavaGateway(
callback_server_parameters=CallbackServerParameters(),
python_server_entry_point=listener)
gateway.start_callback_server()
class PythonPlugin(Exception):
pass
class Plugin:
plugins = {}
def __init__(self, path, info):
self.__class__.plugins[path] = self
self.path = path
self.info = info
self.enable = 0
self.module = None
def load(self):
self.enable = 1
if self.module is not None:
self.module = importlib.reload(self.module)
else:
self.module = importlib.import_module(self.path)
# except:
# return 0
# finally:
self.module.getServer(server)
OnEnable.call(self.path)
return self
def unload(self):
try:
self.enable = 0
OnDisable.call(self.path)
Event.unregister(self.path)
except:
pass
return self
def info(self):
return self.info
def reload(self):
# try:
self.unload()
self.module = importlib.reload(self.module)
self.load()
self.enable = 1
# except:
# self.enable = 0
return self
def __repr__(self):
return "".join(self.path.split(".")[1:])
@classmethod
def refresh(cls):
possible_plugins = os.listdir(PluginFolder)
for i in possible_plugins:
location = os.path.join(PluginFolder, i)
if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
continue
info = open(location + InfoFile, "r").read()
cls(path=".".join(location.split("/")[1:]), info=info)