-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_instance.py
More file actions
206 lines (167 loc) · 5.93 KB
/
Copy pathmulti_instance.py
File metadata and controls
206 lines (167 loc) · 5.93 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Minimal Roblox multi-instance helper for Windows.
The process keeps Roblox's singleton mutex open until the user exits.
No third-party package or administrator permission is required.
"""
from __future__ import annotations
import ctypes
import os
import sys
from ctypes import wintypes
MUTEX_NAME = "ROBLOX_singletonEvent"
ROBLOX_PROCESS_NAMES = frozenset(
{
"robloxplayerbeta.exe",
"robloxplayer.exe",
"windows10universal.exe",
}
)
ERROR_ACCESS_DENIED = 5
ERROR_INVALID_HANDLE = 6
ERROR_ALREADY_EXISTS = 183
TH32CS_SNAPPROCESS = 0x00000002
INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
class PROCESSENTRY32W(ctypes.Structure):
_fields_ = [
("dwSize", wintypes.DWORD),
("cntUsage", wintypes.DWORD),
("th32ProcessID", wintypes.DWORD),
("th32DefaultHeapID", ctypes.c_void_p),
("th32ModuleID", wintypes.DWORD),
("cntThreads", wintypes.DWORD),
("th32ParentProcessID", wintypes.DWORD),
("pcPriClassBase", ctypes.c_long),
("dwFlags", wintypes.DWORD),
("szExeFile", ctypes.c_wchar * 260),
]
def configure_console() -> None:
"""Use Unicode where supported without failing on older consoles."""
for stream in (sys.stdout, sys.stderr):
reconfigure = getattr(stream, "reconfigure", None)
if reconfigure is not None:
try:
reconfigure(errors="replace")
except (AttributeError, OSError):
pass
def configure_kernel32():
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.CreateMutexW.argtypes = (
wintypes.LPVOID,
wintypes.BOOL,
wintypes.LPCWSTR,
)
kernel32.CreateMutexW.restype = wintypes.HANDLE
kernel32.CloseHandle.argtypes = (wintypes.HANDLE,)
kernel32.CloseHandle.restype = wintypes.BOOL
kernel32.CreateToolhelp32Snapshot.argtypes = (
wintypes.DWORD,
wintypes.DWORD,
)
kernel32.CreateToolhelp32Snapshot.restype = wintypes.HANDLE
kernel32.Process32FirstW.argtypes = (
wintypes.HANDLE,
ctypes.POINTER(PROCESSENTRY32W),
)
kernel32.Process32FirstW.restype = wintypes.BOOL
kernel32.Process32NextW.argtypes = (
wintypes.HANDLE,
ctypes.POINTER(PROCESSENTRY32W),
)
kernel32.Process32NextW.restype = wintypes.BOOL
return kernel32
def find_roblox_processes(kernel32):
"""Return the PIDs of currently running Roblox player processes."""
pids = set()
snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if not snapshot or snapshot == INVALID_HANDLE_VALUE:
return ()
try:
entry = PROCESSENTRY32W()
entry.dwSize = ctypes.sizeof(PROCESSENTRY32W)
found = kernel32.Process32FirstW(snapshot, ctypes.byref(entry))
while found:
if entry.szExeFile.lower() in ROBLOX_PROCESS_NAMES:
pids.add(int(entry.th32ProcessID))
found = kernel32.Process32NextW(snapshot, ctypes.byref(entry))
finally:
kernel32.CloseHandle(snapshot)
return tuple(sorted(pids))
def retry_or_leave() -> bool:
"""Return True for retry and False for a clean exit."""
while True:
try:
answer = input("Close it, then choose [R]etry or [L]eave: ")
except (EOFError, KeyboardInterrupt):
print()
return False
answer = answer.strip().lower()
if answer in ("", "r", "retry"):
return True
if answer in ("l", "leave", "q", "quit", "exit"):
return False
print("Please enter R to retry or L to leave.")
def claim_mutex(kernel32):
"""Create the singleton mutex, or return None after the user leaves."""
while True:
pids = find_roblox_processes(kernel32)
if pids:
joined_pids = ", ".join(str(pid) for pid in pids)
print()
print("Roblox is already running.")
print(f"Detected Roblox process ID(s): {joined_pids}")
print("Multi-instance must be enabled before Roblox starts.")
if retry_or_leave():
continue
return None
ctypes.set_last_error(0)
handle = kernel32.CreateMutexW(None, False, MUTEX_NAME)
error = ctypes.get_last_error()
if handle and error != ERROR_ALREADY_EXISTS:
return handle
if handle:
kernel32.CloseHandle(handle)
print()
if error in (
ERROR_ACCESS_DENIED,
ERROR_INVALID_HANDLE,
ERROR_ALREADY_EXISTS,
):
print("Roblox or another process already owns the singleton mutex.")
else:
message = ctypes.FormatError(error).strip() if error else "Unknown error"
print(f"Windows could not create the mutex: {message} (code {error})")
if not retry_or_leave():
return None
def main() -> int:
configure_console()
print("=" * 62)
print(" Multi-Roblox Manager - Lightweight Multi-Instance Utility")
print("=" * 62)
if os.name != "nt":
print("\nThis utility only works on Windows.")
return 1
try:
kernel32 = configure_kernel32()
handle = claim_mutex(kernel32)
except KeyboardInterrupt:
print("\n\nCancelled. Nothing was changed.")
return 0
except OSError as exc:
print(f"\nWindows API error: {exc}")
return 1
if handle is None:
print("\nNothing was changed. Goodbye.")
return 0
try:
print()
print("SUCCESS: Roblox multi-instance is now active.")
print("Keep this window open, then launch your Roblox clients.")
try:
input("\nPress Enter or Ctrl+C to stop multi-instance...")
except (EOFError, KeyboardInterrupt):
print()
finally:
kernel32.CloseHandle(handle)
print("Multi-instance stopped. The singleton mutex was released.")
return 0
if __name__ == "__main__":
raise SystemExit(main())