summaryrefslogtreecommitdiff
path: root/LeagueStop.py
blob: e5de3b2869c09523d5bf03981100c3a8fd02407e (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import threading
import functools
import playsound
import keyboard
import win32gui
import win32con
import win32api
import platform
import winreg
import ctypes
import time
import sys
import os

PRODUCTION = getattr(sys, 'frozen', False)
ROOT = os.path.abspath(__file__)

if PRODUCTION:
    head, _ = os.path.splitext(ROOT)
    ROOT = f'{head}.exe'

NAME, _ = os.path.splitext(os.path.basename(__file__))
ROOT_DIR = os.path.dirname(ROOT)

class Wrap:

    @staticmethod
    def error_alert(err_title, err_desc):
        def decorator(callback):
            @functools.wraps(callback)
            def wrapper(*args, **kwargs):
                try:
                    return callback(*args, **kwargs)
                except Exception:
                    win32api.MessageBox(None,
                                        err_desc,
                                        err_title,
                                        win32con.MB_ICONERROR)
                    sys.exit(1)

            return wrapper

        return decorator

    @staticmethod
    def thread_kill(callback):
        def wrapper(*args, **kwargs):
            try:
                return callback(*args, **kwargs)
            except Exception:
                sys.exit(1)
        
        return wrapper

    @staticmethod
    def thread_call(callback):
        def wrapper(*args, **kwargs):
            threading.Thread(target=callback,
                             args=args,
                             kwargs=kwargs,
                             daemon=True).start()

        return wrapper

class Check:

    @staticmethod
    def is_windows():
        os = platform.system()

        if os != 'Windows':
            sys.exit(1)

    @staticmethod
    @Wrap.error_alert('Administrator Privileges Required',
                      'Please try running the program again and\n'
                      'make sure to have administrator privileges.')
    def is_admin():
        admin = ctypes.windll.shell32.IsUserAnAdmin()

        if admin != 1:
            if PRODUCTION:
                ctypes.windll.shell32.ShellExecuteW(None,
                                                    'runas',
                                                    sys.executable,
                                                    ' '.join(sys.argv),
                                                    None,
                                                    None)
                sys.exit(0)
            else:
                raise OSError

class Persistence:

    _REG_KEY = r'Software\Microsoft\Windows\CurrentVersion\Run'

    @staticmethod
    def registry():
        reg = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)

        with winreg.OpenKey(reg,
                            Persistence._REG_KEY,
                            0,
                            winreg.KEY_ALL_ACCESS) as key:
            winreg.SetValueEx(key, NAME, 0, winreg.REG_EXPAND_SZ, ROOT)

class Window:

    _TASKMGR = r'C:\Windows\System32\Taskmgr.exe'
    _SUB_BLOCK_LOCALE = r'\VarFileInfo\Translation'
    _SUB_BLOCK_TITLE = r'\StringFileInfo\{:04X}{:04X}\FileDescription'

    def __init__(self, *, interval=1):
        self.interval = interval

    @Wrap.thread_call
    def hide(self, window_title):
        while True:
            window = win32gui.FindWindow(None, window_title)
            win32gui.ShowWindow(window, win32con.SW_HIDE)
            time.sleep(self.interval)

    @Wrap.error_alert('Invalid Task Manager Filepath',
                      'Please try running the program again and make\n'
                      'sure to specify a valid executable filepath.')
    def taskmgr_title(self):
        (language, code), *_ = win32api.GetFileVersionInfo(self._TASKMGR, self._SUB_BLOCK_LOCALE)
        sub_block_title = self._SUB_BLOCK_TITLE.format(language, code)
        title = win32api.GetFileVersionInfo(self._TASKMGR, sub_block_title)
        return title

class Supress:

    _BLOCKED_WINDOWS = ('League of Legends (TM) Client',)
    _SUPRESSED_KEYS = ('enter',)
    _SOUND_FILENAME = (lambda filename:
                          os.path.join(sys._MEIPASS, filename) if PRODUCTION
                          else os.path.join(ROOT_DIR, filename))('KEKW.mp3')

    def run(self):
        self._event_loop()
        keyboard.wait()

    @Wrap.thread_call
    def _event_loop(self):
        hooked = False

        while True:
            if self._active_window() in self._BLOCKED_WINDOWS:
                if not hooked:
                    self._register_hooks()
                    hooked = True
            else:
                if hooked:
                    self._unregister_hooks()
                    hooked = False

            time.sleep(0.01)

    def _active_window(self):
        hwnd = win32gui.GetForegroundWindow()
        title = win32gui.GetWindowText(hwnd)
        return title

    def _register_hooks(self):
        for key in self._SUPRESSED_KEYS:
            keyboard.hook_key(key, self._hook_event, suppress=True)

    def _unregister_hooks(self):
        keyboard.unhook_all()

    def _hook_event(self, event):
        if event.event_type == 'down':
            self._play_sound()

    @Wrap.thread_call
    @Wrap.thread_kill
    def _play_sound(self):
        playsound.playsound(self._SOUND_FILENAME)

if __name__ == '__main__':
    Check.is_windows()
    Check.is_admin()
    Persistence.registry()
    window = Window()
    window_title = window.taskmgr_title()
    window.hide(window_title)
    Supress().run()