Skip to content

Commit 7f3f114

Browse files
committed
toooooooooo many fixes
1 parent edecdfe commit 7f3f114

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

hooks/__init__.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import platform
34
import time
45
from enum import Enum
56
from io import StringIO
@@ -11,8 +12,6 @@
1112

1213
scripts_folder: str = ''
1314

14-
scripts_list: dict[str, str] = {}
15-
1615

1716
class Hooks(Enum):
1817
undefined = 'undefined'
@@ -59,7 +58,8 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
5958

6059
if self.task_type == TaskType.undefined:
6160
server.logger.error(
62-
f'Task state is not correct! Task: {self.name} Hooks: {hook} TaskType: {self.task_type} command: {self.command}')
61+
f'Task state is not correct! Task: {self.name} Hooks: {hook} TaskType: {self.task_type} '
62+
f'command: {self.command}')
6363
return
6464

6565
# shell
@@ -130,10 +130,13 @@ def __init__(self):
130130
'on_user_info': []
131131
}
132132
self.task = {}
133+
self.scripts_list = {}
133134

134135
hooks: dict[str, List[str]]
135136

136137
task: dict[str, Task]
138+
139+
scripts_list: dict[str, str]
137140

138141

139142
temp_config: TempConfig
@@ -177,10 +180,8 @@ def _trigger_hooks(hook: Hooks, server: PluginServerInterface, objects_dict: dic
177180
for var_inner_attr_key in var_inner_attr_dict.keys():
178181
# 正在遍历的对象的属性字典中的正在遍历的key的value
179182
var_inner_attr_value: Any = var_inner_attr_dict.get(var_inner_attr_key)
180-
181183
finally_var_dict[an_object_key + '_' + var_inner_attr_key] = var_inner_attr_value
182184

183-
server.logger.debug(f'Executing hook {hook.value}')
184185
# 遍历被挂载到此hook的task的key
185186
for task in temp_config.hooks.get(hook.value):
186187
if temp_config.task.get(task) is None:
@@ -192,7 +193,8 @@ def _trigger_hooks(hook: Hooks, server: PluginServerInterface, objects_dict: dic
192193
temp_config.task.get(task).execute_task(server, hook.value, finally_var_dict)
193194
except Exception as e:
194195
server.logger.exception(
195-
f'Unexpected exception when executing task {task}, hook {hook.value}, task_type {temp_config.task.get(task).task_type}, command {temp_config.task.get(task).command}',
196+
f'Unexpected exception when executing task {task}, hook {hook.value}, '
197+
f'task_type {temp_config.task.get(task).task_type}, command {temp_config.task.get(task).command}',
196198
e)
197199

198200

@@ -250,6 +252,11 @@ def delete_task(name: str, src: CommandSource, server: PluginServerInterface):
250252
src.reply(RTextMCDRTranslation('hooks.mount.task_not_exist', name))
251253
return
252254

255+
for hook in temp_config.hooks.keys():
256+
for tasks_in_hook in temp_config.hooks.get(hook):
257+
if tasks_in_hook == name:
258+
unmount_task(hook, name, src, server)
259+
253260
temp_config.task.pop(name)
254261

255262
server.logger.info(f'Successfully deleted task {name}')
@@ -308,32 +315,31 @@ def man_run_task(task: str, env_str: str, src: CommandSource, server: PluginServ
308315
src.reply(RTextMCDRTranslation('hooks.man_run.success', task))
309316
except Exception as e:
310317
server.logger.exception(
311-
f'Unexpected exception when executing task {task}, hook {Hooks.undefined.value}, task_type {temp_config.task.get(task).task_type}, command {temp_config.task.get(task).command}',
318+
f'Unexpected exception when executing task {task}, hook {Hooks.undefined.value}, '
319+
f'task_type {temp_config.task.get(task).task_type}, command {temp_config.task.get(task).command}',
312320
e)
313321

314322

315-
def register_scripts(script_path: str):
316-
# 将绝对路径添加进入script_list
317-
scripts_list[os.path.basename(script_path)] = script_path
318-
319-
320323
def parse_and_apply_scripts(script: str, server: PluginServerInterface):
321-
# 读取
322-
with open(scripts_list.get(script), 'r') as f:
323-
content: dict[str, Union[str, Union[list, dict]]] = yaml.load(f.read(), Loader=yaml.Loader)
324-
325-
for task in content.get('tasks').values():
326-
# 创建task
327-
create_task(task.get('task_type'), task.get('command'), task.get('name'), server.get_plugin_command_source(),
328-
server)
329-
for hook in task.get('hooks'):
330-
# 挂载
331-
mount_task(hook, task.get('name'), server.get_plugin_command_source(), server)
324+
try:
325+
# 读取
326+
with open(temp_config.scripts_list.get(script), 'r') as f:
327+
content: dict[str, Union[str, Union[list, dict]]] = yaml.load(f.read(), Loader=yaml.Loader)
328+
329+
for task in content.get('tasks').values():
330+
# 创建task
331+
create_task(task.get('task_type'), task.get('command'), task.get('name'),
332+
server.get_plugin_command_source(),
333+
server)
334+
for hook in task.get('hooks'):
335+
# 挂载
336+
mount_task(hook, task.get('name'), server.get_plugin_command_source(), server)
337+
except Exception as e:
338+
server.logger.exception(f'Unexpected exception when parse or apply scripts {os.path.basename(script)}! Please '
339+
f'check your scripts.', e)
332340

333341

334342
def load_scripts(server: PluginServerInterface):
335-
global scripts_list
336-
337343
if not os.path.isdir(scripts_folder):
338344
# 创建脚本目录
339345
os.makedirs(scripts_folder)
@@ -357,10 +363,10 @@ def list_all_files(root_dir) -> list[str]:
357363

358364
# 遍历所有文件
359365
for script_path in list_all_files(scripts_folder):
360-
register_scripts(script_path)
366+
temp_config.scripts_list[os.path.basename(script_path)] = script_path
361367

362368
# 遍历所有已成功注册的脚本
363-
for script in scripts_list.keys():
369+
for script in temp_config.scripts_list.keys():
364370
parse_and_apply_scripts(script, server)
365371

366372

@@ -384,6 +390,11 @@ def process_arg_server(server: PluginServerInterface) -> PluginServerInterface:
384390
def on_load(server: PluginServerInterface, old_module):
385391
global config, scripts_folder, temp_config
386392

393+
if platform.platform().__contains__('Windows') or os.name != 'posix':
394+
server.logger.warning('#####################################################################################')
395+
server.logger.warning('Some features of hooks plugin cannot be run on Windows, you have already been warned.')
396+
server.logger.warning('#####################################################################################')
397+
387398
temp_config = TempConfig()
388399
config = server.load_config_simple(target_class=Configuration)
389400

@@ -492,7 +503,7 @@ def on_server_start(server: PluginServerInterface):
492503

493504

494505
def on_server_startup(server: PluginServerInterface):
495-
trigger_hooks(Hooks.on_mcdr_started, server, {'server': process_arg_server(server)})
506+
trigger_hooks(Hooks.on_server_started, server, {'server': process_arg_server(server)})
496507

497508

498509
def on_server_stop(server: PluginServerInterface, return_code: int):

0 commit comments

Comments
 (0)