Repository navigation
Expand file tree
/
Copy pathgeneral.py
More file actions
107 lines (75 loc) · 2.79 KB
/
Copy pathgeneral.py
File metadata and controls
107 lines (75 loc) · 2.79 KB
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
# coding=utf-8
#
from .internal import SetClient, SetServer, Runtime as _Runtime
# TYPE_CHECKING
if 0>1:
import typing
from .mod_server import ToolDeltaModServer as Server
from .mod_client import ToolDeltaModClient as Client
FuncT = typing.TypeVar("FuncT", bound="typing.Callable[[], None]")
# TYPE_CHECKING END
extra_server_init_register_funcs = {} # type: dict[int, set[typing.Callable[[], None]]]
extra_client_init_register_funcs = {} # type: dict[int, set[typing.Callable[[], None]]]
extra_server_uninit_register_funcs = {} # type: dict[int, set[typing.Callable[[], None]]]
extra_client_uninit_register_funcs = {} # type: dict[int, set[typing.Callable[[], None]]]
def run_by_priority(funcs):
# type: (dict[int, set[typing.Callable[[], None]]]) -> None
for _, fns in sorted(funcs.items(), key=lambda x: x[0], reverse=True):
for func in fns:
func()
def InitClient(client):
# type: (Client) -> None
"在客户端类初始化时调用"
SetClient(client)
run_by_priority(extra_client_init_register_funcs)
def UninitClient(client):
# type: (Client) -> None
"在客户端类销毁时调用"
run_by_priority(extra_client_uninit_register_funcs)
def InitServer(server):
# type: (Server) -> None
"在服务端类初始化时调用"
SetServer(server)
run_by_priority(extra_server_init_register_funcs)
def UninitServer(server):
# type: (Server) -> None
"在服务端类销毁时调用"
run_by_priority(extra_server_uninit_register_funcs)
def ServerInitCallback(priority=0):
"添加服务端初始化时注册的函数"
def decorator(func):
# type: (FuncT) -> FuncT
extra_server_init_register_funcs.setdefault(priority, set()).add(func)
if _Runtime.server is not None:
func()
return func
return decorator
def ClientInitCallback(priority=0):
"添加客户端初始化时注册的函数"
def decorator(func):
# type: (FuncT) -> FuncT
extra_client_init_register_funcs.setdefault(priority, set()).add(func)
if _Runtime.client is not None:
func()
return func
return decorator
def ServerUninitCallback(priority=0):
"添加服务端销毁时注册的函数"
def decorator(func):
# type: (FuncT) -> FuncT
extra_server_uninit_register_funcs.setdefault(priority, set()).add(func)
return func
return decorator
def ClientUninitCallback(priority=0):
"添加客户端销毁时注册的函数"
def decorator(func):
# type: (FuncT) -> FuncT
extra_client_uninit_register_funcs.setdefault(priority, set()).add(func)
return func
return decorator
__all__ = [
"ClientInitCallback",
"ClientUninitCallback",
"ServerInitCallback",
"ServerUninitCallback",
]