ctai/src/main.cpp
2025-03-29 17:57:47 +08:00

154 lines
4.8 KiB
C++

#include <windows.h>
#include <iostream>
#include <QTranslator>
#include "sui_base_config.h"
#include "sui_base_ex.h"
#include "ctai.h"
#include <QApplication>
#include <QStandardPaths>
#include <QCefContext.h>
#include "QCefWidget/CefWidget.h"
#define DEBUG_PRINT true
void debug_print_argv(int argc, char *argv[])
{
if (!argc)
{
return;
}
std::lock_guard<std::mutex> lock(m_mutex);
DWORD process_id = GetCurrentProcessId();
std::cout << "CurrentProcessId Argvs" << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "Process ID:" << process_id << " Argv:" << i << ": " << argv[i] << std::endl;
}
}
QString curExeDir()
{
return QDir::currentPath();
}
void exsMkDir(QString Dir)
{
if (!QDir(Dir).exists())
{
QDir().mkdir(Dir);
}
}
bool exeCinfog(int argc, char *argv[])
{
std::string ex_1 = "renderer";
std::string ex_2 = "utility";
for (int i = 0; i < argc; i++)
{
std::string arg = argv[i];
if (arg.find(ex_1) == std::string::npos || arg.find(ex_2) == std::string::npos)
{
return true;
}
}
return false;
}
void initConfig(int argc, char *argv[], QCefConfig &config, QString currentDir)
{
// 检测自定义缓存路径
exsMkDir(currentDir + "/user");
exsMkDir(currentDir + "/user/userCache");
// 子进程路径和名字,可以利用本进程复用,
// Mingw编译下多进程情况需要设置否则程序会退出因为无法拉起子进程
if (exeCinfog(argc, argv))
{
config.setBrowserSubProcessPath(currentDir + "/ctai.exe");
}
config.setRootCachePath(currentDir + "/user");
config.setCachePath(currentDir + "/user/userCache");
config.setResourceDirectoryPath(currentDir + "/cef_binary");
config.setLocalesDirectoryPath(currentDir + "/cef_binary/locales");
// 浏览器标识
config.setUserAgent("Mozilla/5.0 (Windows NT 10.0; CEF/3.2272.2035) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 SoftwareInformer/1.6.1400");
// 日志输出级别
config.setLogLevel(QCefConfig::LOGSEVERITY_VERBOSE);
// 桥名字
config.setBridgeObjectName("CtaiClient");
// 内置的协议方案名称
config.setBuiltinSchemeName("Ctai");
// 无窗口渲染模式
// config.setWindowlessRenderingEnabled(true);
// 远程Debug端口
// config.setRemoteDebuggingPort(9000);
// 请求头的语言
config.setAcceptLanguageList("zh-CN");
// 允许忽略 localhost 上的 TLS/SSL 错误
config.addCommandLineSwitch("allow-insecure-localhost");
config.addCommandLineSwitch("ignore-certificate-errors-spki-list");
config.addCommandLineSwitch("ignore-ssl-errors");
// 单进程模式
// config.addCommandLineSwitch("single-process");
// 启用GPU加速
config.addCommandLineSwitch("enable-gpu");
// GPU加速合成
config.addCommandLineSwitch("enable-gpu-compositing");
// 在进程中使用GPU渲染-开启后只有一个进程
config.addCommandLineSwitch("in-process-gpu");
// 无头模式
// config.addCommandLineSwitch("headless");
// 密钥链
config.addCommandLineSwitch("use-mock-keychain");
// chrome运行时环境
config.addCommandLineSwitch("enable-chrome-runtime");
// Alloy 风格
config.addCommandLineSwitch("use-alloy-style");
// 离屏渲染
// config.addCommandLineSwitch("off-screen-rendering-enabled");
// 网络服务In进程
config.addCommandLineSwitchWithValue("enable-features", "NetworkServiceInProcess");
// 语言
config.addCommandLineSwitchWithValue("lang", "zh-CN");
// 跨域进行远程
// config.addCommandLineSwitchWithValue("remote-allow-origins", "*");
// 禁用沙盒
config.addCommandLineSwitchWithValue("no-sandbox", "ture");
// 渲染进程限制
config.addCommandLineSwitchWithValue("renderer-process-limit", "1");
// 外部消息循环
config.addCommandLineSwitchWithValue("external-message-pump", "false");
// 多线程消息循环
config.addCommandLineSwitchWithValue("multi-threaded-message-loop", "true");
}
int main(int argc, char *argv[])
{
if (DEBUG_PRINT)
{
debug_print_argv(argc, argv);
}
QApplication a(argc, argv);
QCefConfig config;
// 初始化上下文的配置
initConfig(argc, argv, config, curExeDir());
// 初始化浏览器上下文
QCefContext cefContext(&a, argc, argv, &config);
CefWidget cw;
cw.show();
qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype");
// 加载语言包
QTranslator qtBaseTranslator;
if (!qtBaseTranslator.load(QStringLiteral(":/res/translator/qtbase_zh_CN.qm")))
{
// 处理加载翻译文件失败的情况
qDebug() << "Failed to load translation file.";
return -1;
}
a.installTranslator(&qtBaseTranslator);
// 读取窗体的配置并初始化
sui_init_config();
ctai x;
x.init_layout();
// 仅限于windows平台
#if defined(__WIN32__)
x.title()->set_type(QD_TYPE::QD_EXIT);
#endif
x.show();
a.exec();
qDebug() << "EXIT";
return 0;
}