|
1 | 1 | import os
|
2 | 2 | import sys
|
3 | 3 | import time
|
| 4 | +import json |
4 | 5 | import logging
|
5 | 6 | import platform
|
6 | 7 | import subprocess
|
|
19 | 20 | logging.basicConfig(format='(%(asctime)s) %(message)s')
|
20 | 21 | logger.setLevel(logging.INFO)
|
21 | 22 |
|
| 23 | +CURRENT_SETTINGS_VERSION = 1 |
| 24 | +SETTINGS_PATH = 'settings.json' |
| 25 | + |
22 | 26 | APPDATA = os.getenv('appdata')
|
23 | 27 | LOCALAPPDATA = os.getenv('localappdata')
|
24 | 28 | BD_ASAR_URL = 'https://github.com/rauenzi/BetterDiscordApp/releases/latest/download/betterdiscord.asar'
|
25 | 29 | BD_ASAR_SAVE_PATH = os.path.join(APPDATA, 'BetterDiscord/data/betterdiscord.asar').replace('\\', '/')
|
26 | 30 |
|
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) |
31 | 31 |
|
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 | + |
33 | 40 |
|
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 |
35 | 66 |
|
36 |
| -# killing discord to prevent any errors |
37 | 67 | 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...') |
40 | 80 |
|
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]) |
43 | 92 |
|
44 |
| -subprocess.Popen(f'{os.path.join(LOCALAPPDATA, "Discord/Update.exe")} --processStart Discord.exe') |
| 93 | + start_discord() |
| 94 | + logger.info('Discord updater started') |
45 | 95 |
|
| 96 | +logger.info('Waiting for finish of discord updating...') |
46 | 97 | quit_from_loop = False
|
| 98 | + |
47 | 99 | while not quit_from_loop:
|
48 | 100 | for process in psutil.process_iter(['name']):
|
49 | 101 | if quit_from_loop:
|
50 | 102 | break
|
51 | 103 |
|
52 | 104 | 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 |
61 | 113 |
|
62 | 114 | logger.info('Update finished. Patching...')
|
63 | 115 | print()
|
64 | 116 | time.sleep(0.1)
|
65 | 117 |
|
66 | 118 | # 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) |
67 | 123 |
|
68 | 124 | # 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 |
71 | 126 | 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]) |
73 | 128 | 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');" |
75 | 129 | 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') |
80 | 134 | ]
|
81 | 135 |
|
82 | 136 | # making folders
|
83 |
| -logger.info('Making required folders...') |
| 137 | +logger.info('Making folders...') |
84 | 138 |
|
85 | 139 | for folder in bd_required_folders:
|
86 | 140 | if not os.path.exists(folder):
|
|
97 | 151 | try:
|
98 | 152 | response = requests.get(BD_ASAR_URL)
|
99 | 153 | 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...') |
101 | 155 | time.sleep(3)
|
102 | 156 | else:
|
103 | 157 | with open(BD_ASAR_SAVE_PATH, 'wb') as file:
|
|
111 | 165 | # patching index.js
|
112 | 166 | logger.info('Trying to patch discord startup script...')
|
113 | 167 |
|
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 |
| - |
120 | 168 | with open(index_js_path, 'rb') as file:
|
121 | 169 | content = file.readlines()
|
122 | 170 |
|
|
135 | 183 | print()
|
136 | 184 | time.sleep(0.1)
|
137 | 185 |
|
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!') |
155 | 189 | print()
|
156 | 190 | time.sleep(0.1)
|
157 | 191 |
|
|
0 commit comments