-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathusdx.py
123 lines (94 loc) · 3.26 KB
/
usdx.py
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
import logging
import os.path
import re
import hashlib
import shutil
import threading
import pyautogui
import keyboard
import config
colors = [
"blue",
"red",
"green",
"yellow",
"orange",
"pink",
"violet",
"brown",
"gray",
"dark_blue",
"sky",
"cyan",
"flame",
"orchid",
"harlequin",
"green_yellow"
]
def calculate_md5(file_path):
"""
Calculate and return the MD5 hash of a file.
:param file_path: path to the file for which the MD5 hash needs to be calculated.
:return: a string representing the MD5 hash.
"""
hash_md5 = hashlib.md5()
# Open the file in binary mode and read chunks
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
# Return the hex representation of the digest
return hash_md5.hexdigest().upper()
def replace_in_config(file_path, replace):
with open(file_path, "r") as file:
content = file.read()
for header, settings in replace.items():
for key, value in settings.items():
def replacer(match):
return f"{match.group(1)}{value}"
content = re.sub(rf'(\[{header}\][^\[]*{key}=)[^\n]*', replacer, content, count=1)
with open(file_path, "w") as file:
file.write(content)
_enter_names_lock = threading.Lock()
def enter_names(players):
with _enter_names_lock:
logging.info("Entering player names")
max_len_to_delete = max(*list(len(p) for p in players), 10)
# set number of players
pyautogui.press('left', presses=6)
pyautogui.press('right', presses=len(players) - 1)
pyautogui.press('down')
for player in players:
pyautogui.press('down', presses=2)
for i in range(max_len_to_delete):
keyboard.press_and_release('backspace')
# pyautogui.press('backspace', presses=max_len_to_delete)
pyautogui.write(player)
pyautogui.press('up', presses=2)
pyautogui.press('right')
pyautogui.press('up')
def change_config(colors):
logging.info("Changing USDX config")
settings = {
"PlayerColor": {},
"PlayerAvatar": {},
"Advanced": {
"OnSongClick": "Select Players"
}
}
# copy avatars into USDX directory
from main import SCRIPT_BASE_PATH
for avatar in os.listdir(os.path.join(SCRIPT_BASE_PATH, "./avatars")):
file_path = os.path.join(SCRIPT_BASE_PATH, "./avatars", avatar)
# Check if it's a file and not a directory
if os.path.isfile(file_path):
# Copy each file to the destination directory
shutil.copy(file_path, config.usdx_avatars_dir)
for i, color in enumerate(colors):
if color in colors:
# TODO: colors do not fit
settings["PlayerColor"][f"P{i + 1}"] = colors.index(color) + 1
settings["PlayerAvatar"][f"P{i + 1}"] = calculate_md5(os.path.join(config.usdx_avatars_dir, f'cat_{color}.jpg'))
else:
settings["PlayerColor"][f"P{i + 1}"] = 14
settings["PlayerAvatar"][f"P{i + 1}"] = calculate_md5(os.path.join(config.usdx_avatars_dir, 'cat_rainbow.jpg'))
replace_in_config(config.usdx_config_file, settings)