-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
242 lines (231 loc) · 10.3 KB
/
index.js
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
const fs = require("fs")
let data = {};
let Language = {};
const VALORANT = require("./valorant.js");
const zlib = require("zlib");
const languageFile = require("./language.json")
let playerName = null;
const { ConfigManager } = require("./utils/ConfigManager.js")
const color = require("./utils/ColorManager");
process.title = "Valorant Settings Applier"
async function main() {
await createData();
await reloadData().then(() => {
ConfigManager.reloadData(data, Language)
});
let username, password
if (!fs.existsSync("./cookies")) fs.mkdirSync("./cookies");
if (data.hasOwnProperty("account") && data.hasOwnProperty("cookies")) {
if (data["account"].hasOwnProperty("username") && data["account"].hasOwnProperty("password")) {
if ((data["account"]["username"] == null || data["account"]["password"] == null) && data["cookies"] == null) {
await ConfigManager.Setup()
}
}
username = ConfigManager.getUsername()
password = ConfigManager.getPassword()
// mainProcess
const valorant = new VALORANT.API();
await valorant.getClientVersion();
if (data["cookies"] == null) {
await valorant.authorize(username, password).then(async () => {
data["cookies"] = valorant.cookies;
if (valorant.multifactor == true) {
await ConfigManager.askForMfa()
const code = await ConfigManager.getMfa()
await valorant.mfa(code, valorant.cookies)
data["cookies"] = valorant.cookies;
}
if (valorant.user_id) {
data["account"]["username"] = username;
data["account"]["password"] = password;
fs.writeFileSync(`./cookies/${valorant.username}.json`, JSON.stringify(valorant.cookies, null, "\t"));
fs.writeFileSync("./data.json", JSON.stringify(data, null, "\t"));
}
})
.catch((err) => {
let message;
switch (err.message) {
case "auth_failure":
message = Language["WRONG_USERNAMEORPASSWORD"]
break;
case "multifactor_attempt_failed":
message = Language["WRONG_MFA"]
default:
message = Language["ANOTHER_ERROR"] + " " + err.toString()
}
process.stdout.write(message.toString())
process.stdin.resume();
});
} else if (data["cookies"]) {
await valorant.reAuthorize(data["cookies"]).catch(async (error) => {
data["cookies"] = null
fs.writeFileSync("./data.json", JSON.stringify(data, null, "\t"));
});
}
if (valorant.user_id) {
const playerData = (await valorant.getPlayers([valorant.user_id])).data;
playerName = `${playerData[0]["GameName"]}#${playerData[0]["TagLine"]}`;
askForSelection(valorant);
}
} else {
createData(true);
main();
}
}
async function applySettings(profileList = [], valorant = new VALORANT.API()) {
await ConfigManager.askForProfile(profileList);
const profileName = ConfigManager.getProfile();
if (fs.existsSync(`./profiles/${profileName}.json`)) {
let profileData = fs.readFileSync(`./profiles/${profileName}.json`, "utf8");
profileData = zlib.deflateRawSync(profileData, { windowBits: 15 }).toString('base64');
await valorant.savePreference({ type: "Ares.PlayerSettings", data: profileData });
} else {
console.log(`${color.Red}${Language["profileNotExist"]}${color.Reset}`);
await applySettings(profileList);
}
}
async function nameSettings(settings) {
await ConfigManager.askForNameProfile();
const name = ConfigManager.getName();
if (fs.existsSync(`./profiles/${name}.json`)) {
await ConfigManager.askForReplace()
const replace = ConfigManager.getReplace();
if (replace.toLowerCase() == "y" || replace.toLowerCase() == "yes") {
fs.writeFileSync(`./profiles/${name}.json`, JSON.stringify(settings, null, "\t"));
} else {
await nameSettings(settings);
}
} else {
fs.writeFileSync(`./profiles/${name}.json`, JSON.stringify(settings, null, "\t"));
}
}
async function askForSelection(valorant = new VALORANT.API()) {
console.clear();
await ConfigManager.askForSelection(playerName, data["account"]["username"]);
const options = ConfigManager.getOptions();
switch (options.toString()) {
case "1":
console.log(`${color.FgYellow}${Language["changeAccount"]}`)
data["cookies"] = null;
data["account"]["username"] = null;
data["account"]["password"] = null;
await ConfigManager.Setup();
if (fs.existsSync(`./cookies/${data["account"]["username"]}.json`)) {
let cookie = fs.readFileSync(`./cookies/${data["account"]["username"]}.json`, "utf8");
cookie = JSON.parse(cookie);
data["cookies"] = cookie;
fs.writeFileSync("./data.json", JSON.stringify(data, null, "\t"));
await valorant.reAuthorize(data["cookies"]).catch(async (error) => {
data["cookies"] = null
fs.writeFileSync("./data.json", JSON.stringify(data, null, "\t"));
});
} else {
username = ConfigManager.getUsername()
password = ConfigManager.getPassword()
await valorant.authorize(username, password).then(async (error) => {
data["cookies"] = valorant.cookies;
if (valorant.multifactor == true) {
await ConfigManager.askForMfa()
const code = await ConfigManager.getMfa()
await valorant.mfa(code, valorant.cookies)
data["cookies"] = valorant.cookies;
}
if (valorant.user_id) {
data["account"]["username"] = username;
data["account"]["password"] = password;
fs.writeFileSync(`./cookies/${valorant.username}.json`, JSON.stringify(valorant.cookies, null, "\t"));
fs.writeFileSync("./data.json", JSON.stringify(data, null, "\t"));
}
})
.catch((err) => {
let message;
switch (err.stack) {
case "auth_failure":
message = Language["WRONG_USERNAMEORPASSWORD"]
break;
case "multifactor_attempt_failed":
message = Language["WRONG_MFA"]
default:
message = Language["ANOTHER_ERROR"] + " " + err.message.toString()
}
process.stdout.write(message.toString())
process.stdin.resume();
});
}
if (valorant.user_id) {
const playerData = (await valorant.getPlayers([valorant.user_id])).data;
playerName = `${playerData[0]["GameName"]}#${playerData[0]["TagLine"]}`;
}
break;
case "2":
console.log(`${color.FgYellow}${Language["saveSettings"]}`)
await valorant.getPlayerSettings().then(async (response) => {
let settings = zlib.inflateRawSync(new Buffer.from(response.data.data, 'base64'), { windowBits: 15 }).toString();
settings = JSON.parse(settings);
if (!fs.existsSync("./profiles")) {
fs.mkdirSync("./profiles");
}
await nameSettings(settings);
})
break;
case "3":
console.log(`${color.FgYellow}${Language["applySettings"]}`)
console.log(color.BgWhite)
let profileList = [];
if (!fs.existsSync("./profiles")) {
fs.mkdirSync("./profiles");
}
const profiles = fs.readdirSync("./profiles/");
profiles.filter((profile) => profile.split(".").pop() === "json").forEach((profile) => {
profileList.push(profile.replace(".json", ""));
});
await applySettings(profileList, valorant);
break;
default:
break;
}
askForSelection(valorant);
}
async function askForLanguage() {
const defaultData = {
account: {
username: null,
password: null
},
cookies: null,
language: "en-US",
}
await ConfigManager.askForLanguage(playerName)
const language = ConfigManager.getLanguags()
switch (language.toString()) {
case "1":
defaultData["language"] = "en-US"
Language = languageFile["en-US"];
fs.writeFileSync("./data.json", JSON.stringify(defaultData, null, "\t"));
break;
case "2":
defaultData["language"] = "zh-TW"
Language = languageFile["zh-TW"];
fs.writeFileSync("./data.json", JSON.stringify(defaultData, null, "\t"));
break;
case "3":
defaultData["language"] = "zh-CN"
Language = languageFile["zh-CN"];
fs.writeFileSync("./data.json", JSON.stringify(defaultData, null, "\t"));
break;
default:
await askForLanguage();
break;
}
}
async function createData(force = false) {
if (!fs.existsSync("./data.json") || force == true) {
await askForLanguage();
}
}
async function reloadData() {
let dataUnparsed = fs.readFileSync("./data.json", "utf8");
data = JSON.parse(dataUnparsed);
Language = languageFile[data["language"]];
}
main()