-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate games script with logs.py
More file actions
201 lines (157 loc) · 7.56 KB
/
Update games script with logs.py
File metadata and controls
201 lines (157 loc) · 7.56 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
# Combined epic games and steam update games and then saves logs to onedrive
import subprocess
import time
import psutil
import json
import os
from datetime import datetime
from filelock import FileLock, Timeout
valid_choices = ["steam", "epic", "bnet", "all"]
while True:
direction = input("What games would you like to update? [steam, epic, all] ").lower()
if direction in valid_choices:
break
else:
print("Please type a valid input: 'steam', 'epic', or 'all'")
steam_game_dict = {
"paladins": [r"D:\SteamLibrary\steamapps\common\Paladins\Binaries\Win64\Paladins.exe" ,
"D:\SteamLibrary\steamapps\common\Paladins\Binaries\Win64\Paladins.url"],
"overwach": r"C:\Program Files (x86)\Steam\steamapps\common\Overwatch\Overwatch.exe",
"halo": r"C:\Program Files (x86)\Steam\steamapps\common\Halo Infinite\game\HaloInfinite.exe",
#"the finals": r"C:\Program Files (x86)\Steam\steamapps\common\The Finals\Discovery\Binaries\Win64\Discovery.exe", # I can't get popen to launch the finales, running the desktop link via powershell works through
"overcooked": r"D:\SteamLibrary\steamapps\common\Overcooked! 2\Overcooked2.exe",
"apex": r"C:\Program Files (x86)\Steam\steamapps\common\Apex Legends\r5apex.exe", # apex/steam prompted me to click okay to get the game to launch
"destiny": r"D:\SteamLibrary\steamapps\common\Destiny 2\destiny2.exe",
"dota2": r"D:\SteamLibrary\steamapps\common\dota 2 beta\game\bin\win64\dota2.exe"
}
epic_game_dict = {
'fortnite': [r'C:\Program Files\Epic Games\Fortnite\FortniteGame\Binaries\Win64\FortniteClient-Win64-Shipping.exe',
r'C:\Users\Ryan\Desktop\Fortnite.url'],
#'r6': r'C:\Program Files\Epic Games\RainbowSixSiege\RainbowSix.exe',
'rl': r'C:\Program Files\Epic Games\rocketleague\Binaries\Win64\RocketLeague.exe',
'civ': r"D:\Games\SidMeiersCivilizationVI\Base\Binaries\Win64EOS\CivilizationVI_DX12.exe",
'totalwar': r'D:\Games\TotalWarSagaTROY\Troy.exe'
}
bnet_game_dict = {
'hearthstone': r"D:\Games\Hearthstone\Hearthstone.exe", # Hearthstone launches initally then quits out to battle.net to check for updates even if there aren't any
'Heroes of the Storm': r"D:\Games\Heroes of the Storm\Heroes of the Storm.exe", # while heros of the storm doesn't open when you click on exe it does still prompt battle.net to check for updates
'StarCraft II': r"D:\Games\StarCraft II\Versions\Base92174\SC2_x64.exe" # while StarCraft II doesn't open when you click on exe it does still prompt battle.net to check for updates
}
game_dict = {}
epic_launcher = "C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
steam_launcher = "C:\Program Files (x86)\Steam\steam.exe"
bnet_launcher = "C:\Program Files (x86)\Battle.net\Battle.net.exe"
# Steam
if direction == "steam":
game_dict.update(steam_game_dict)
subprocess.Popen(steam_launcher)
print("Launched Steam")
# Epic
elif direction == "epic":
game_dict.update(epic_game_dict)
subprocess.Popen(epic_launcher)
print("Launched Epic Games")
# Epic
elif direction == "bnet":
game_dict.update(bnet_game_dict)
subprocess.Popen(bnet_launcher)
print("Launched Battle Net")
# All
elif direction == "all":
game_dict.update(epic_game_dict)
game_dict.update(steam_game_dict)
#game_dict.update(bnet_game_dict)
subprocess.Popen(steam_launcher)
time.sleep(3)
subprocess.Popen(epic_launcher)
#time.sleep(3)
#subprocess.Popen(bnet_launcher)
print("Launched Steam, Epic, and Battle Net!")
print(f"{direction} directory choosen with the following games:")
for key in game_dict.keys():
print(key)
# Sleep statement to break up between starting game launchers and launching games
time.sleep(10)
# kill game with exceptions
def kill_game(game_exe, game_name, status_dict):
for proc in psutil.process_iter(): # closing a game with psutil works great because if a game is updating, the exe file doesn't show up in task manager details
try:
if game_exe in proc.name(): #checks if game exe file is anywhere in proc.name, makes searching way easier
proc.kill()
#updating the dictionary with the successful game launch
status_dict.update({game_name : "No update"})
print(f"{game_name} process found and has been terminated.\n")
return status_dict
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
print(f"{game_name} process not found.")
print(f"{game_name} either never launched or is queuing update :)\n")
status_dict.update({game_name : "Update found or never launched"})
return status_dict
def loop_games(game_dict):
total_games = len(game_dict)
current_game = 0
status_dict = {}
for game_name, game_path in game_dict.items():
# launch the game
if isinstance(game_path, list):
# Starting games with url link
subprocess.Popen(['start', game_path[1]], shell=True)
# Setting the proper game path so that it works with the kill funciton
game_path = game_path[0]
else:
# Starting games with exe link
subprocess.Popen(game_path)
print(f"\n{game_name} is now running.")
# Wait for game to start/load into main menu
time.sleep(60)
# get the game exe file from the game_path
game_exe = game_path.split('\\')[-1]
# Terminating the game client
status_dict = kill_game(game_exe, game_name, status_dict)
current_game +=1
print(f"{current_game} out of {total_games} games tested for updates\n")
# Adding another sleep statement after one game closed and before we open another one
time.sleep(7)
print(f"\nAll {total_games} games have been launched and terminated.")
return status_dict
time.sleep(10)
status_dict = loop_games(game_dict)
# Updating the results of the status_dict with computer info and time for more complete logging info
def time_name(status_dict):
# Retrieve current time for logging usage
c_time = datetime.now()
c_time_formatted = c_time.strftime("%m/%d/%y %H:%M:%S") # 6/27/24 HH:MM:SS
# comptuer name
machine_name = os.environ.get('COMPUTERNAME')
added_dict = {"computer_name": machine_name, "date": c_time_formatted}
status_dict.update(added_dict)
return status_dict
status_dict = time_name(status_dict)
# Setting static varialbes for the saved db location
save_folder = r"C:\Users\Ryan\Coding Projects\KDL Project\Esports PT\Logs"
file_path = os.path.join(save_folder, 'update_logs.json')
lock_file_path = file_path + '.lock'
# Ensure the save folder exists
os.makedirs(save_folder, exist_ok=True)
# Function to save the updated address database
def resave_json(dict):
lock = FileLock(lock_file_path, timeout=10)
try:
with lock:
# Load the address database within the lock context
if os.path.exists(file_path):
with open(file_path, 'r') as f:
update_db = json.load(f)
else:
update_db = []
# Append the new entry
update_db.append(dict)
# Save the updated address database
with open(file_path, 'w') as f:
json.dump(update_db, f, indent=4)
print("Results have been saved to the JSON file.")
except Timeout:
print("Another process is currently accessing the file. Please try again later.")
resave_json(status_dict)
time.sleep(4)