1010from mcdreforged .api .utils import serializer
1111from ruamel import yaml
1212
13+ from hooks import utils
14+ from hooks .utils import process_arg_server
15+
1316scripts_folder : str = ''
1417
1518
@@ -40,6 +43,8 @@ class TaskType(Enum):
4043 shell_command = 'shell_command'
4144 server_command = 'server_command'
4245 mcdr_command = 'mcdr_command'
46+
47+ python_code = 'python_code'
4348
4449
4550class Task (Serializable ):
@@ -56,6 +61,12 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
5661
5762 start_time = time .time ()
5863
64+ if self .command is None :
65+ server .logger .error (
66+ f'Task state is not correct! Task: { self .name } Hooks: { hook } TaskType: { self .task_type } '
67+ f'command: { self .command } ' )
68+ return
69+
5970 if self .task_type == TaskType .undefined :
6071 server .logger .error (
6172 f'Task state is not correct! Task: { self .name } Hooks: { hook } TaskType: { self .task_type } '
@@ -94,7 +105,32 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
94105 for key in var_dict .keys ():
95106 command = command .replace ('{$' + key + '}' , str (var_dict .get (key )))
96107
97- server .execute_command (self .command )
108+ server .execute_command (command )
109+
110+ elif self .task_type == TaskType .python_code :
111+ # 注入变量
112+ finally_command = self .command
113+
114+ # 要被注入的赋值语句
115+ command_input = StringIO ()
116+
117+ if var_dict is not None :
118+ for key in var_dict .keys ():
119+ command_input .write (key )
120+ command_input .write ('=' )
121+
122+ var_value = var_dict .get (key )
123+
124+ if (not utils .is_int_var (var_value )) and (not utils .is_list_var (var_value )) and (not utils .is_dict_var (var_value )):
125+ var_value = '"' + str (var_value ) + '"'
126+
127+ command_input .write (str (var_value ))
128+ command_input .write (';' )
129+ command_input .write ('\n ' )
130+
131+ finally_command = finally_command .replace ('# variable injection here' , command_input .getvalue ())
132+
133+ exec (finally_command )
98134
99135 server .logger .debug (f'Task finished, name: { self .name } , task_type: { self .task_type } , command: { self .command } , '
100136 f'costs { time .time () - start_time } seconds.' )
@@ -370,23 +406,6 @@ def list_all_files(root_dir) -> list[str]:
370406 parse_and_apply_scripts (script , server )
371407
372408
373- def process_arg_server (server : PluginServerInterface ) -> PluginServerInterface :
374- server .func_is_server_running = server .is_server_running ()
375- server .func_is_server_startup = server .is_server_startup ()
376- server .func_is_rcon_running = server .is_rcon_running ()
377- server .func_get_server_pid = server .get_server_pid ()
378- server .func_get_server_pid_all = server .get_server_pid_all ()
379- server .func_get_server_information = str (serialize (server .get_server_information ()))
380- server .func_get_data_folder = server .get_data_folder ()
381- server .func_get_plugin_file_path = server .get_plugin_file_path ('hooks' )
382- server .func_get_plugin_list = str (server .get_plugin_list ())
383- server .func_get_unloaded_plugin_list = str (server .get_unloaded_plugin_list ())
384- server .func_get_disabled_plugin_list = str (server .get_disabled_plugin_list ())
385- server .func_get_mcdr_language = server .get_mcdr_language ()
386- server .func_get_mcdr_config = str (server .get_mcdr_config ())
387- return server
388-
389-
390409def on_load (server : PluginServerInterface , old_module ):
391410 global config , scripts_folder , temp_config
392411
0 commit comments