2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-30 22:25:12 +00:00

Option to set API ID and hash from start parameter

This commit is contained in:
RadRussianRus
2020-07-23 17:52:26 +03:00
parent 866f83aa17
commit d3935259d7
10 changed files with 107 additions and 4 deletions

View File

@@ -363,9 +363,12 @@ int main(int argc, char *argv[]) {
bool testmode = false;
bool externalupdater = false;
bool customWorkingDir = false;
bool useEnvApi = true;
char *key = 0;
char *workdir = 0;
char *customApiId = 0;
char *customApiHash = 0;
for (int i = 1; i < argc; ++i) {
if (equal(argv[i], "-noupdate")) {
needupdate = false;
@@ -391,6 +394,12 @@ int main(int argc, char *argv[]) {
exeName = argv[i];
} else if (equal(argv[i], "-exepath") && ++i < argc) {
exePath = argv[i];
} else if (equal(argv[i], "-no-env-api")) {
useEnvApi = false;
} else if (equal(argv[i], "-api-id") && ++i < argc) {
customApiId = argv[i];
} else if (equal(argv[i], "-api-hash") && ++i < argc) {
customApiHash = argv[i];
}
}
if (exeName.empty() || exeName.find('/') != string::npos) {
@@ -488,6 +497,14 @@ int main(int argc, char *argv[]) {
push(workdir);
}
if (!useEnvApi) push("-no-env-api");
if (customApiId && customApiHash) {
push("-api-id");
push(customApiId);
push("-api-hash");
push(customApiHash);
}
auto args = vector<char*>();
for (auto &arg : values) {
args.push_back(arg.data());

View File

@@ -91,8 +91,10 @@ int main(int argc, const char * argv[]) {
openLog();
pid_t procId = 0;
BOOL update = YES, toSettings = NO, autoStart = NO, startInTray = NO, testMode = NO, freeType = NO, externalUpdater = NO;
BOOL customWorkingDir = NO;
BOOL customWorkingDir = NO, useEnvApi = YES;
NSString *key = nil;
NSString *customApiId = nil;
NSString *customApiHash = nil;
for (int i = 0; i < argc; ++i) {
if ([@"-workpath" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) {
if (++i < argc) {
@@ -124,6 +126,12 @@ int main(int argc, const char * argv[]) {
customWorkingDir = YES;
} else if ([@"-key" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) {
if (++i < argc) key = [NSString stringWithUTF8String:argv[i]];
} else if ([@"-no-env-api" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) {
useEnvApi = NO;
} else if ([@"-api-id" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) {
if (++i < argc) customApiId = [NSString stringWithUTF8String:argv[i]];
} else if ([@"-api-hash" isEqualToString:[NSString stringWithUTF8String:argv[i]]]) {
if (++i < argc) customApiHash = [NSString stringWithUTF8String:argv[i]];
}
}
if (!workDir) {
@@ -268,6 +276,13 @@ int main(int argc, const char * argv[]) {
[args addObject:@"-workdir"];
[args addObject:workDir];
}
if (!useEnvApi) [args addObject:@"-no-env-api"];
if (customApiId && customApiHash) {
[args addObject:@"-api-id"];
[args addObject:customApiId];
[args addObject:@"-api-hash"];
[args addObject:customApiHash];
}
writeLog([[NSArray arrayWithObjects:@"Running application '", appPath, @"' with args '", [args componentsJoinedByString:@"' '"], @"'..", nil] componentsJoinedByString:@""]);
for (int i = 0; i < 5; ++i) {

View File

@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
bool _debug = false;
wstring updaterName, updaterDir, updateTo, exeName, customWorkingDir, customKeyFile;
wstring customApiId, customApiHash;
bool equal(const wstring &a, const wstring &b) {
return !_wcsicmp(a.c_str(), b.c_str());
@@ -340,6 +341,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara
int argsCount;
bool needupdate = false, autostart = false, debug = false, writeprotected = false, startintray = false, testmode = false, freetype = false, externalupdater = false;
bool useEnvApi = true;
args = CommandLineToArgvW(GetCommandLine(), &argsCount);
if (args) {
for (int i = 1; i < argsCount; ++i) {
@@ -383,6 +385,14 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara
break;
}
}
} else if (equal(args[i], L"-no-env-api")) {
useEnvApi = false;
} else if (equal(args[i], L"-api-id") && ++i < argsCount) {
writeLog(std::wstring(L"Argument: ") + args[i]);
customApiId = args[i];
} else if (equal(args[i], L"-api-hash") && ++i < argsCount) {
writeLog(std::wstring(L"Argument: ") + args[i]);
customApiHash = args[i];
}
}
if (exeName.empty()) {
@@ -437,6 +447,11 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdPara
if (!customKeyFile.empty()) {
targs += L" -key \"" + customKeyFile + L"\"";
}
if (!useEnvApi) targs += L" -no-env-api";
if (!customApiId.empty() && !customApiHash.empty()) {
targs += L" -api-id \"" + customApiId + L"\"";
targs += L" -api-hash \"" + customApiHash + L"\"";
}
writeLog(L"Result arguments: " + targs);
bool executed = false;

View File

@@ -335,10 +335,12 @@ int Launcher::exec() {
Platform::start();
Ui::DisableCustomScaling();
if (qEnvironmentVariableIsSet(kApiIdVarName.utf8())
if (cUseEnvApi()
&& qEnvironmentVariableIsSet(kApiIdVarName.utf8())
&& qEnvironmentVariableIsSet(kApiHashVarName.utf8())) {
cSetApiId(qgetenv(kApiIdVarName.utf8()).toInt());
cSetApiHash(QString::fromLatin1(qgetenv(kApiHashVarName.utf8())));
cSetApiFromStartParams(false);
}
auto result = executeApplication();
@@ -463,6 +465,9 @@ void Launcher::processArguments() {
{ "-workdir" , KeyFormat::OneValue },
{ "--" , KeyFormat::OneValue },
{ "-scale" , KeyFormat::OneValue },
{ "-no-env-api" , KeyFormat::NoValues },
{ "-api-id" , KeyFormat::OneValue },
{ "-api-hash" , KeyFormat::OneValue },
};
auto parseResult = QMap<QByteArray, QStringList>();
auto parsingKey = QByteArray();
@@ -520,6 +525,15 @@ void Launcher::processArguments() {
? style::kScaleAuto
: value;
}
gUseEnvApi = !parseResult.contains("-no-env-api");
auto customApiId = parseResult.value("-api-id", {}).join(QString()).toInt();
auto customApiHash = parseResult.value("-api-hash", {}).join(QString());
if (customApiId > 0 && !customApiHash.isEmpty()) {
gApiId = customApiId;
gApiHash = customApiHash;
gApiFromStartParams = true;
}
}
int Launcher::executeApplication() {

View File

@@ -216,3 +216,6 @@ int gApiId = 0;
QString gApiHash;
#endif // TDESKTOP_API_ID && TDESKTOP_API_HASH
bool gUseEnvApi = true;
bool gApiFromStartParams = false;

View File

@@ -127,3 +127,5 @@ DeclareSetting(bool, ForwardChatOnClick);
DeclareSetting(int, ApiId);
DeclareSetting(QString, ApiHash);
DeclareSetting(bool, UseEnvApi);
DeclareSetting(bool, ApiFromStartParams);

View File

@@ -96,6 +96,17 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
}
}
if (!cUseEnvApi()) {
argumentsList.push("-no-env-api");
}
if (cApiFromStartParams()) {
argumentsList.push("-api-id");
argumentsList.push(QFile::encodeName(QString::number(cApiId())));
argumentsList.push("-api-hash");
argumentsList.push(QFile::encodeName(cApiHash()));
}
Logs::closeMain();
CrashReports::Finish();

View File

@@ -74,6 +74,13 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
if (customWorkingDir()) {
[args addObject:@"-workdir_custom"];
}
if (!cUseEnvApi()) [args addObject:@"-no-env-api"];
if (cApiFromStartParams()) {
[args addObject:@"-api-id"];
[args addObject:Q2NSString(QString::number(cApiId()))];
[args addObject:@"-api-hash"];
[args addObject:Q2NSString(cApiHash())];
}
DEBUG_LOG(("Application Info: executing %1 %2").arg(NS2QString(path)).arg(NS2QString([args componentsJoinedByString:@" "])));
Logs::closeMain();

View File

@@ -99,6 +99,16 @@ bool Launcher::launchUpdater(UpdaterLaunch action) {
pushArgument('"' + cExeDir() + '"');
}
}
if (!cUseEnvApi()) {
pushArgument(qsl("-no-env-api"));
}
if (cApiFromStartParams()) {
pushArgument(qsl("-api-id"));
pushArgument('"' + QString::number(cApiId()) + '"');
pushArgument(qsl("-api-hash"));
pushArgument('"' + cApiHash() + '"');
}
return launch(operation, binaryPath, argumentsList);
}

View File

@@ -458,6 +458,15 @@ void RegisterCustomScheme(bool force) {
HKEY rkey;
QString exe = QDir::toNativeSeparators(cExeDir() + cExeName());
QString possibleParams;
if (!cUseEnvApi()) {
possibleParams += " -no-env-api";
}
if (cApiFromStartParams()) {
possibleParams += " -api-id \"" + QString::number(cApiId()) + "\"";
possibleParams += " -api-hash \"" + cApiHash() + "\"";
}
// Legacy URI scheme registration
if (!_psOpenRegKey(L"Software\\Classes\\tg", &rkey)) return;
@@ -470,7 +479,7 @@ void RegisterCustomScheme(bool force) {
if (!_psOpenRegKey(L"Software\\Classes\\tg\\shell", &rkey)) return;
if (!_psOpenRegKey(L"Software\\Classes\\tg\\shell\\open", &rkey)) return;
if (!_psOpenRegKey(L"Software\\Classes\\tg\\shell\\open\\command", &rkey)) return;
if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\" -- \"%1\""))) return;
if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\"") + possibleParams + qsl(" -- \"%1\""))) return;
// URI scheme registration as Default Program - Windows Vista and above
if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg", &rkey)) return;
@@ -480,7 +489,7 @@ void RegisterCustomScheme(bool force) {
if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg\\shell", &rkey)) return;
if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg\\shell\\open", &rkey)) return;
if (!_psOpenRegKey(L"Software\\Classes\\ktgdesktop.tg\\shell\\open\\command", &rkey)) return;
if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\" -- \"%1\""))) return;
if (!_psSetKeyValue(rkey, 0, '"' + exe + qsl("\" -workdir \"") + cWorkingDir() + qsl("\"") + possibleParams + qsl(" -- \"%1\""))) return;
if (!_psOpenRegKey(L"Software\\KotatogramDesktop", &rkey)) return;
if (!_psOpenRegKey(L"Software\\KotatogramDesktop\\Capabilities", &rkey)) return;