-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
211 lines (184 loc) · 5.34 KB
/
utilities.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
from os import path as op, getenv, listdir, makedirs, remove, rmdir, rename
import sys
from psutil import process_iter as pi
from pickle import load, dump
import winreg
from configparser import ConfigParser as CfgP
from requests import get
VERSION = "v1.0"
GITHUBURL = "https://github.com/developers192/Adaptive"
RELEASEURL = GITHUBURL + "/releases/latest"
ISSUESURL = GITHUBURL + "/issues/new"
APPID = f'com.ria.adaptive.{VERSION[1:].replace(".", "")}'
PROFILESDIR = op.join(getenv("APPDATA"), "Adaptive", "Profiles")
DEFAULTCONFIG = {
"leaguePath": "",
"showWindowOnStartup": True,
"updateOnStartup": True,
"modeConfigs": {}
}
CONFIGDIR = op.join(getenv("APPDATA"), "Adaptive", "config.adapt")
def resourcePath(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = op.abspath(".")
return op.join(base_path, relative_path)
def procPath(name):
for proc in pi():
if proc.name() == name:
return proc.exe()
return False
def checkLeaguePath(path):
try:
for p in listdir(path):
if p == "LeagueClient.exe":
return True
except:
return False
return False
def getLeaguePath():
path = procPath("LeagueClient.exe")
if path:
path = op.dirname(path)
return path
return ""
def fetchConfig(entry):
makedirs(op.dirname(CONFIGDIR), exist_ok = True)
try:
with open(CONFIGDIR, "rb") as f:
data = load(f)
except FileNotFoundError:
with open(CONFIGDIR, "wb") as f:
dump(DEFAULTCONFIG, f)
data = DEFAULTCONFIG
try: data = data[entry]
except KeyError:
editConfig(entry, DEFAULTCONFIG[entry])
data = DEFAULTCONFIG[entry]
return data
def editConfig(entry, value):
makedirs(op.dirname(CONFIGDIR), exist_ok = True)
with open(CONFIGDIR, "rb") as f:
data = load(f)
data[entry] = value
with open(CONFIGDIR, "wb") as f:
dump(data, f)
return
def currentPath():
if hasattr(sys, 'frozen'):
basis = sys.executable
else:
basis = sys.argv[0]
return basis
def toggleAutostart():
with winreg.OpenKey(
key=winreg.HKEY_CURRENT_USER,
sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
reserved=0,
access=winreg.KEY_ALL_ACCESS,
) as key:
try:
if not checkAutostart():
winreg.SetValueEx(key, "RiaAdaptive", 0, winreg.REG_SZ, currentPath())
else:
winreg.DeleteValue(key, "RiaAdaptive")
except OSError:
return False
return True
def checkAutostart():
with winreg.OpenKey(
key=winreg.HKEY_CURRENT_USER,
sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
reserved=0,
access=winreg.KEY_ALL_ACCESS,
) as key:
idx = 0
while idx < 1_000: # Max 1.000 values
try:
key_name, _, _ = winreg.EnumValue(key, idx)
if key_name == "RiaAdaptive":
return True
idx += 1
except OSError:
break
return False
def profileList():
makedirs(PROFILESDIR, exist_ok = True)
return list(listdir(PROFILESDIR))
def addCurrentProfile(name):
if name in profileList() or name == "No changes":
return -1
if len(set(("\\", "/", ":", "*", "?", '"', "<", ">", "|")).intersection(name)) > 0:
return -4
makedirs(op.join(PROFILESDIR, name), exist_ok = True)
leaguePath = fetchConfig("leaguePath")
if leaguePath == "": return -2
dat = {}
for t in ("game.cfg", "input.ini"):
try:
with open(op.join(fetchConfig("leaguePath"), "Config", t), "r") as f:
dat[t] = f.read()
except FileNotFoundError:
return -3
for t in dat:
with open(op.join(PROFILESDIR, name, t), "w") as f:
f.write(dat[t])
return 0
def removeProfile(name):
for t in ("game.cfg", "input.ini"):
try: remove(op.join(PROFILESDIR, name, t))
except OSError: pass
try: rmdir(op.join(PROFILESDIR, name))
except OSError: pass
current = fetchConfig("modeConfigs")
for k in current.copy():
if current[k] == name:
del current[k]
editConfig("modeConfigs", current)
def renameProfile(name, newName):
if newName in profileList() or newName == "No changes":
return -1
if len(set(("\\", "/", ":", "*", "?", '"', "<", ">", "|")).intersection(newName)) > 0:
return -4
try:
rename(op.join(PROFILESDIR, name), op.join(PROFILESDIR, newName))
except: return -2
current = fetchConfig("modeConfigs")
for k in current:
if current[k] == name:
current[k] = newName
editConfig("modeConfigs", current)
return 0
def fetchModeConfig(modeId):
current = fetchConfig("modeConfigs")
try: return current[modeId]
except KeyError:
return -1
def editModeConfig(modeId, config):
current = fetchConfig("modeConfigs")
if config == -1:
if modeId in current:
del current[modeId]
else:
current[modeId] = config
editConfig("modeConfigs", current)
print(fetchConfig("modeConfigs"))
def replaceConfig(config):
for t in ("game.cfg", "input.ini"):
cfg = CfgP()
cfg.optionxform = str
cfg.read(op.join(PROFILESDIR, config, t))
cfgL = CfgP()
cfgL.optionxform = str
cfgL.read(op.join(fetchConfig("leaguePath"), "Config", t))
for section in cfg:
for option in cfg[section]:
cfgL[section][option] = cfg[section][option]
with open(op.join(fetchConfig("leaguePath"), "Config", t), "w") as f:
cfgL.write(f, space_around_delimiters = False)
def isOutdated():
latestver = get(RELEASEURL).url.split(r"/")[-1]
if latestver != VERSION:
return latestver
return False