|
7 | 7 | # ============================================================================= |
8 | 8 | # Python |
9 | 9 | import bisect |
| 10 | +import math |
| 11 | +import time |
| 12 | + |
10 | 13 | from contextlib import suppress |
11 | 14 | from enum import IntEnum |
12 | | -import math |
13 | 15 | from threading import Thread |
14 | | -import time |
| 16 | +from warnings import warn |
15 | 17 |
|
16 | 18 | # Source.Python |
17 | | -from core import AutoUnload, WeakAutoUnload |
| 19 | +from core import AutoUnload |
| 20 | +from core import WeakAutoUnload |
18 | 21 | from hooks.exceptions import except_hooks |
19 | 22 | from listeners import ( |
20 | 23 | listeners_logger, on_tick_listener_manager, OnLevelEnd, |
|
42 | 45 | # ============================================================================= |
43 | 46 | # >> THREAD WORKAROUND |
44 | 47 | # ============================================================================= |
45 | | -class GameThread(Thread): |
46 | | - """Workaround for :class:`threading.Thread`.""" |
47 | | - |
48 | | - # Since _delay_manager now always registers a tick listener, we probably |
49 | | - # don't need this anymore. |
50 | | - #def __init__(self, *args, **kwargs): |
51 | | - # super().__init__(*args, **kwargs) |
52 | | - # on_tick_listener_manager.register_listener(self._tick) |
53 | | - # |
54 | | - #def __del__(self): |
55 | | - # on_tick_listener_manager.unregister_listener(self._tick) |
56 | | - # |
57 | | - #def _bootstrap_inner(self): |
58 | | - # try: |
59 | | - # super()._bootstrap_inner() |
60 | | - # finally: |
61 | | - # on_tick_listener_manager.unregister_listener(self._tick) |
62 | | - # |
63 | | - #def _tick(self): |
64 | | - # pass |
| 48 | +class GameThread(WeakAutoUnload, Thread): |
| 49 | + """A subclass of :class:`threading.Thread` that throws a warning if the |
| 50 | + plugin that created the thread has been unloaded while the thread is still |
| 51 | + running. |
| 52 | + """ |
| 53 | + |
| 54 | + def _add_instance(self, caller): |
| 55 | + super()._add_instance(caller) |
| 56 | + self._caller = caller |
| 57 | + |
| 58 | + def _unload_instance(self): |
| 59 | + if self.is_alive(): |
| 60 | + warn( |
| 61 | + f'Thread "{self.name}" ({self.ident}) from "{self._caller}" ' |
| 62 | + f'is running even though its plugin has been unloaded!') |
65 | 63 |
|
66 | 64 |
|
67 | 65 | # ============================================================================= |
|
0 commit comments