Skip to content

Commit 0d2e89a

Browse files
committed
updated hook on discord updating process (once more)
1 parent 559f479 commit 0d2e89a

File tree

1 file changed

+87
-53
lines changed

1 file changed

+87
-53
lines changed

installer.py

+87-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import time
4+
import json
45
import logging
56
import platform
67
import subprocess
@@ -19,68 +20,121 @@
1920
logging.basicConfig(format='(%(asctime)s) %(message)s')
2021
logger.setLevel(logging.INFO)
2122

23+
CURRENT_SETTINGS_VERSION = 1
24+
SETTINGS_PATH = 'settings.json'
25+
2226
APPDATA = os.getenv('appdata')
2327
LOCALAPPDATA = os.getenv('localappdata')
2428
BD_ASAR_URL = 'https://github.com/rauenzi/BetterDiscordApp/releases/latest/download/betterdiscord.asar'
2529
BD_ASAR_SAVE_PATH = os.path.join(APPDATA, 'BetterDiscord/data/betterdiscord.asar').replace('\\', '/')
2630

27-
if not os.path.exists(f'{LOCALAPPDATA}/Discord/Update.exe'):
28-
logger.info(f'Discord was not found ({LOCALAPPDATA}/Discord).')
29-
input('ENTER to exit...')
30-
sys.exit(0)
3131

32-
#
32+
def start_discord():
33+
# running discord from c:/ for prevent locking the script's working dir
34+
script_working_dir = os.path.dirname(os.path.abspath(__file__))
35+
36+
os.chdir('c:/')
37+
subprocess.Popen(f'{os.path.join(DISCORD_PARENT_PATH, "Update.exe")} --processStart Discord.exe')
38+
os.chdir(script_working_dir)
39+
3340

34-
logger.info('Killing discord...')
41+
# load settings
42+
if os.path.exists(SETTINGS_PATH):
43+
settings: dict = json.load(open(SETTINGS_PATH))
44+
45+
DISCORD_PARENT_PATH = settings.get('discord_installed_path')
46+
else:
47+
DISCORD_PARENT_PATH = f'{LOCALAPPDATA}/Discord'
48+
49+
# get discord location from user if it is not valid
50+
while True:
51+
if not os.path.exists(os.path.join(DISCORD_PARENT_PATH, 'update.exe')):
52+
logger.info(f'Discord was not found at "{DISCORD_PARENT_PATH}". Enter the dir to folder with "Update.exe":')
53+
DISCORD_PARENT_PATH = input('\n=> ')
54+
55+
json.dump({'discord_installed_path': DISCORD_PARENT_PATH}, open(SETTINGS_PATH, 'w'))
56+
else:
57+
break
58+
59+
# The "--service-sandbox-type=audio" argument will only be in the
60+
# updated discord instance, so it won't be in the update module
61+
62+
is_discord_running = False
63+
is_discord_updating = True
64+
65+
# checking for currently updating discord
3566

36-
# killing discord to prevent any errors
3767
for process in psutil.process_iter(['name']):
38-
if process.info['name'] == 'Discord.exe':
39-
process.kill()
68+
if process.info.get('name') == 'Discord.exe':
69+
is_discord_running = True
70+
71+
try:
72+
for arg in process.cmdline():
73+
if '--service-sandbox-type=audio' in arg:
74+
is_discord_updating = False
75+
except psutil.NoSuchProcess:
76+
pass
77+
78+
if is_discord_running and not is_discord_updating:
79+
logger.info('Discord is started and not updating. Killing discord...')
4080

41-
# # installing the latest version of discord
42-
logger.info('Updating discord to latest version...')
81+
for process in psutil.process_iter(['name']):
82+
if process.info['name'] == 'Discord.exe' and process.is_running():
83+
process.kill()
84+
time.sleep(2) # discord may not close instantly, so we need to wait for a while
85+
is_discord_running = False
86+
87+
# installing the latest version of discord
88+
if not is_discord_running:
89+
discord_path = [i for i in os.listdir(DISCORD_PARENT_PATH) if i.startswith('app-')] # remove all not 'app-' items
90+
discord_path.sort() # the oldest version will be the last of list
91+
discord_path = os.path.join(DISCORD_PARENT_PATH, discord_path[-1])
4392

44-
subprocess.Popen(f'{os.path.join(LOCALAPPDATA, "Discord/Update.exe")} --processStart Discord.exe')
93+
start_discord()
94+
logger.info('Discord updater started')
4595

96+
logger.info('Waiting for finish of discord updating...')
4697
quit_from_loop = False
98+
4799
while not quit_from_loop:
48100
for process in psutil.process_iter(['name']):
49101
if quit_from_loop:
50102
break
51103

52104
if process.info['name'] == 'Discord.exe':
53-
if not process.is_running():
54-
continue
55-
56-
for arg in process.cmdline():
57-
# this arg will be only in updater, so if it is true, wait for terminating this process
58-
if '--standard-schemes' in arg:
59-
quit_from_loop = True
60-
break
105+
try:
106+
for arg in process.cmdline():
107+
if '--service-sandbox-type=audio' in arg:
108+
time.sleep(5) # wait 5 seconds to avoid any problematic shit
109+
quit_from_loop = True
110+
break
111+
except psutil.NoSuchProcess:
112+
pass
61113

62114
logger.info('Update finished. Patching...')
63115
print()
64116
time.sleep(0.1)
65117

66118
# patching
119+
for process in psutil.process_iter(['name']):
120+
if process.info['name'] == 'Discord.exe' and process.is_running():
121+
process.kill()
122+
time.sleep(2)
67123

68124
# determining the latest installed version of discord
69-
discord_parent_path = f'{LOCALAPPDATA}/Discord/'
70-
discord_path = [i for i in os.listdir(discord_parent_path) if i.startswith('app-')] # remove all not 'app-' items
125+
discord_path = [i for i in os.listdir(DISCORD_PARENT_PATH) if i.startswith('app-')] # remove all not 'app-' items
71126
discord_path.sort() # the oldest version will be the last of list
72-
discord_path = os.path.join(discord_parent_path, discord_path[-1])
127+
discord_path = os.path.join(DISCORD_PARENT_PATH, discord_path[-1])
73128
index_js_path = os.path.join(discord_path, 'modules/discord_desktop_core-1/discord_desktop_core/index.js')
74-
index_js_default_content = b"module.exports = require('./core.asar');"
75129
bd_required_folders = [
76-
f'{APPDATA}/BetterDiscord',
77-
f'{APPDATA}/BetterDiscord/data',
78-
f'{APPDATA}/BetterDiscord/themes',
79-
f'{APPDATA}/BetterDiscord/plugins'
130+
os.path.join(APPDATA, 'BetterDiscord'),
131+
os.path.join(APPDATA, 'BetterDiscord/data'),
132+
os.path.join(APPDATA, 'BetterDiscord/themes'),
133+
os.path.join(APPDATA, 'BetterDiscord/plugins')
80134
]
81135

82136
# making folders
83-
logger.info('Making required folders...')
137+
logger.info('Making folders...')
84138

85139
for folder in bd_required_folders:
86140
if not os.path.exists(folder):
@@ -97,7 +151,7 @@
97151
try:
98152
response = requests.get(BD_ASAR_URL)
99153
except requests.exceptions.ConnectionError:
100-
print(f'Failed to download asar. Retrying in 3 seconds...')
154+
logger.info(f'Failed to download asar. Retrying in 3 seconds...')
101155
time.sleep(3)
102156
else:
103157
with open(BD_ASAR_SAVE_PATH, 'wb') as file:
@@ -111,12 +165,6 @@
111165
# patching index.js
112166
logger.info('Trying to patch discord startup script...')
113167

114-
if not os.path.exists(index_js_path):
115-
os.makedirs(os.path.join(discord_path, 'modules/discord_desktop_core-1/discord_desktop_core'))
116-
117-
with open(index_js_path, 'wb') as file:
118-
file.write(index_js_default_content)
119-
120168
with open(index_js_path, 'rb') as file:
121169
content = file.readlines()
122170

@@ -135,23 +183,9 @@
135183
print()
136184
time.sleep(0.1)
137185

138-
# restarting discord
139-
logger.info('Trying restart discord...')
140-
141-
for process in psutil.process_iter(['name']):
142-
if process.info['name'] == 'Discord.exe':
143-
process.kill()
144-
145-
time.sleep(1)
146-
147-
# running discord from c:/ for prevent locking the script's working dir
148-
script_env_path = os.path.dirname(os.path.abspath(__file__))
149-
150-
os.chdir('c:/')
151-
subprocess.Popen(f'cmd /c start {os.path.join(discord_path, "discord.exe")}')
152-
os.chdir(script_env_path)
153-
154-
logger.info('Discord has been restarted!')
186+
# start discord
187+
start_discord()
188+
logger.info('Discord has been started!')
155189
print()
156190
time.sleep(0.1)
157191

0 commit comments

Comments
 (0)