-
Notifications
You must be signed in to change notification settings - Fork 7
/
LinuxED.py
executable file
·244 lines (212 loc) · 12.6 KB
/
LinuxED.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# written by Creatable
# credits:
# - Tcll5850: https://gitlab.com/Tcll
# for cleaning up the code, making it more maintainable, and extending it's functionality, as well as fixing issues with older python versions.
import os
import sys
import urllib.request
import zipfile
import distutils.core
import shutil
import getpass
import tempfile
from stat import S_IREAD, S_IRGRP, S_IROTH, S_IWUSR
if __name__ == "__main__":
"""Comment out old Windows notice
if os.name == 'nt': print('WARNING: it appears you are running the Linux installer on Windows.\n'
'If you are unaware of what you\'re doing, it\'s recommended you close this installer.\n'
'Otherwise you may continue at your own risk.\n')"""
if os.name == 'nt': print('WARNING: it appears you are running LinuxED on Windows.\n'
'LinuxED was not originally made for Windows and Windows compatibility is not maintained.\n'
'Continue at your own risk.')
# Define the starting variables, these are all their own thing.
username = getpass.getuser()
dirpath = os.path.realpath('')
currentdir = True
if 'XDG_DATA_HOME' in os.environ:
if os.path.exists(os.path.join(os.environ['XDG_DATA_HOME'], "EnhancedDiscord")):
dirpath = os.environ['XDG_DATA_HOME']
currentdir = False
elif 'HOME' in os.environ:
if os.path.exists(os.path.join(os.environ['HOME'], '.local', 'share', "EnhancedDiscord")):
dirpath = os.path.join(os.environ['HOME'], '.local', 'share')
currentdir = False
filepath = os.path.dirname(os.path.realpath(__file__))
tempdir = tempfile.gettempdir()
scriptname = os.path.basename(__file__)
detect_versions = lambda discordpath,idxsubpath: [
(discordpath+vsn+idxsubpath, vsn) for vsn in (os.listdir(discordpath) if os.path.exists(discordpath) else []) if os.path.isdir(discordpath+vsn) and len(vsn.split('.')) == 3 ]
print('Welcome to the LinuxED installation script.')
baseclients = {
"STABLE" : detect_versions('/home/%s/.config/discord/'%username, '/modules/discord_desktop_core/index.js'),
"CANARY" : detect_versions('/home/%s/.config/discordcanary/'%username, '/modules/discord_desktop_core/index.js'),
"PTB" : detect_versions('/home/%s/.config/discordptb/'%username, '/modules/discord_desktop_core/index.js'),
"SNAP" : detect_versions('/home/%s/snap/discord/current/.config/discord/'%username, '/modules/discord_desktop_core/index.js'),
"FLATPAK": detect_versions('/home/%s/.var/app/com.discordapp.Discord/config/discord/'%username, '/modules/discord_desktop_core/index.js')
}
if sys.platform == 'darwin':
baseclients = {
"STABLE" : detect_versions('/Users/%s/Library/Application Support/discord/'%username, '/modules/discord_desktop_core/index.js'),
"CANARY" : detect_versions('/Users/%s/Library/Application Support/discordcanary/'%username, '/modules/discord_desktop_core/index.js'),
"PTB" : detect_versions('/Users/%s/Library/Application Support/discordptb/'%username, '/modules/discord_desktop_core/index.js')
}
elif os.name == 'nt':
baseclients = {
"STABLE" : detect_versions('C:/Users/%s/AppData/Roaming/Discord/'%username, '/modules/discord_desktop_core/index.js'),
"CANARY" : detect_versions('C:/Users/%s/AppData/Roaming/discordcanary/'%username, '/modules/discord_desktop_core/index.js'),
"PTB" : detect_versions('C:/Users/%s/AppData/Roaming/Discord PTB/'%username, '/modules/discord_desktop_core/index.js')
}
clients = [ (str(i+1),cpv) for i,cpv in enumerate( (c,p,v) for c in baseclients if baseclients[c] for p,v in baseclients[c] ) ]
clients.append( (str(len(clients)+1), ("CUSTOM",'', '')) )
getclient = dict(clients).get
def validate_custom_client():
while True:
print("\nPlease enter the location of your client's index.js file.")
jspath = input('> ')
if os.path.exists(jspath) and os.path.isfile(jspath): return 'CUSTOM', jspath, ''
elif not jspath:
print("\nOperation cancelled...")
return 'CUSTOM', jspath, ''
else:
print("\nError: The specified location was not valid.")
print("Please enter a valid location or press Enter to cancel.")
def select_client(allow_custom=False):
if len(clients) > 2 or allow_custom:
while True:
print('\nPlease enter the number for the client you wish to patch, or press Enter to exit:')
result = input('%s\n> '%('\n'.join('%s. %s: %s'%(i,o,v) for i,(o,p,v) in clients)) )
client, jspath, version = getclient( result, (None,'','') )
if client=='CUSTOM':
client, jspath, version = validate_custom_client()
if not jspath: continue
if jspath: return client, jspath, version
if not result:
print("\nOperation cancelled...")
#input('Press Enter to Exit...')
return 'CUSTOM', jspath, ''
print("\nError: The specified option was not valid.")
elif len(clients) == 1:
print('\nThe installer could not detect any known Discord clients.')
print('Do you have Discord installed in a custom location? (y/n)')
if input("> ").upper() in {"Y","YES"}: return validate_custom_client()
else:
print('\nNo Discord client could be found.')
print('Please install Discord and re-run this installer.')
#input('Press Enter to Exit...')
return 'CUSTOM', '', ''
else: return getclient('1')
client, jspath, version = select_client()
if jspath:
backuppath = "%s.backup"%jspath
while True:
print('\nOperating on client: %s %s\n'%(client,version))
print('Please type the number for your desired option:')
enhanceddir = os.path.join(dirpath, "EnhancedDiscord")
if os.name == 'nt':
injdir = 'process.env.injDir = "%s"' % enhanceddir.encode('unicode_escape').decode("utf-8")
else:
injdir = 'process.env.injDir = "%s"' % enhanceddir
# this is not my code but it's what I put at the end of index.js
patch = """%s
require(`${process.env.injDir}/injection.js`);
module.exports = require('./core.asar');"""%injdir
# room for expansion (other params can be provided here)
optionsdict = [('Install ED',),('Uninstall ED',),('Update ED',),('Update LinuxED',),('Select Client',)]
if currentdir == False:
optionsdict.append(('Use current directory',))
else:
if 'XDG_DATA_HOME' in os.environ:
optionsdict.append(('Use $XDG_DATA_HOME',))
if 'HOME' in os.environ:
optionsdict.append(('Use $HOME',))
optionsdict.append(('Exit',))
options = [ (str(i+1),o) for i,o in enumerate(optionsdict)]
getoption = dict(options).get
option,*params = getoption( input( '%s\n> '%('\n'.join('%s. %s'%(i,o) for i,(o,*p) in options) ) ), (None,) )
print()
if option == 'Exit':
print("Exiting...")
exit()
break # shouldn't get here, but just in case.
elif option == 'Update LinuxED':
print("Updating LinuxED...")
urllib.request.urlretrieve('https://github.com/Cr3atable/LinuxED/archive/master.zip', '%s/LinuxEDUpdate.zip' % tempdir)
with zipfile.ZipFile("%s/LinuxEDUpdate.zip" % tempdir,"r") as zip_ref:
zip_ref.extractall(tempdir)
os.remove("%s/LinuxED-master/LICENSE" % tempdir)
os.remove("%s/LinuxED-master/README.md" % tempdir)
os.remove("%s/LinuxED-master/.gitignore" % tempdir)
os.remove("%s/LinuxED-master/plugins/style.css" % tempdir)
shutil.move("%s/LinuxED-master/LinuxED.py" % tempdir, "%s/LinuxED-master/%s" % (tempdir, scriptname))
distutils.dir_util.copy_tree('%s/LinuxED-master/' % tempdir, filepath)
shutil.rmtree("%s/LinuxED-master" % tempdir)
os.remove("%s/LinuxEDUpdate.zip" % tempdir)
if os.name != "nt":
try:
os.system("chmod +x %s" % os.path.join(filepath, scriptname))
except:
print("Couldn't make script executable...\nYou may experience problems when trying to use it again")
print("Update complete!\nPlease restart LinuxED for the update to take effect.")
elif option == 'Uninstall ED':
print('Uninstalling EnhancedDiscord...')
if os.path.exists(backuppath):
print("Making sure index.js is no longer read only before removing it...")
os.chmod(jspath, S_IWUSR|S_IREAD)
os.remove(jspath)
shutil.move(backuppath, jspath)
print("Successfully uninstalled EnhancedDiscord!")
else:
print("Error: Couldn't find index.js backup, did you use the installer to install ED?\n")
elif option == 'Install ED':
if not os.path.exists(enhanceddir):
print("Downloading ED...")
urllib.request.urlretrieve('https://github.com/joe27g/EnhancedDiscord/archive/master.zip', '%s/EnhancedDiscord.zip' % tempdir)
with zipfile.ZipFile("%s/EnhancedDiscord.zip" % tempdir,"r") as zip_ref:
zip_ref.extractall(tempdir)
shutil.move("%s/EnhancedDiscord-master" % tempdir, "%s/EnhancedDiscord" % dirpath)
os.remove("%s/EnhancedDiscord.zip" % tempdir)
if not os.path.exists(backuppath):
print("Creating index.js.backup...")
with open(jspath,'r') as original:
with open(backuppath,'w') as backup: backup.write(original.read())
print("Patching index.js...")
with open(jspath,"w") as idx: idx.write(patch)
print("Making index.js read only so Discord can't tamper with it...")
os.chmod(jspath, S_IREAD|S_IRGRP|S_IROTH)
cfgpath = "%s/config.json"%enhanceddir
if not os.path.exists(cfgpath):
print("Creating config.json...")
with open(cfgpath,"w") as cfg: cfg.write("{}")
print("EnhancedDiscord installation complete!\n")
elif option == 'Update ED':
if os.path.exists(enhanceddir):
print("Updating ED...")
urllib.request.urlretrieve('https://github.com/joe27g/EnhancedDiscord/archive/master.zip', '%s/EDUpdate.zip' % tempdir)
with zipfile.ZipFile("%s/EDUpdate.zip" % tempdir,"r") as zip_ref:
zip_ref.extractall(tempdir)
distutils.dir_util.copy_tree('%s/EnhancedDiscord-master' % tempdir, '%s/EnhancedDiscord' % dirpath)
shutil.rmtree("%s/EnhancedDiscord-master" % tempdir)
os.remove("%s/EDUpdate.zip" % tempdir)
print("Update complete!")
else:
print("It seems EnhancedDiscord is not installed in the current directory so it was unable to be updated.")
elif option == 'Select Client':
print("Selecting new Discord client...")
backup = (client, jspath, version)
client, jspath, version = select_client(True)
if not jspath: client, jspath, version = backup
print('\nOperating on client: %s %s\n'%(client,version))
elif option == 'Use current directory':
dirpath = os.path.realpath('')
currentdir = True
elif option == 'Use $XDG_DATA_HOME':
dirpath = os.environ['XDG_DATA_HOME']
currentdir = False
elif option == 'Use $HOME':
dirpath = os.path.join(os.environ['HOME'], '.local', 'share')
currentdir = False
else:
print('Error: The specified option was not valid.\n')
print('Please type the number for your desired option:')