Magpie/Runtime/DllMain.cpp

118 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (c) 2021 - present, Liu Xu
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "pch.h"
#include "App.h"
#include "Utils.h"
static HINSTANCE hInst = NULL;
std::shared_ptr<spdlog::logger> logger = nullptr;
// DLL 入口
BOOL APIENTRY DllMain(
HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
hInst = hModule;
break;
case DLL_PROCESS_DETACH:
App::GetInstance().~App();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
// 日志级别:
// 0TRACE1DEBUG...6OFF
API_DECLSPEC void WINAPI SetLogLevel(UINT logLevel) {
assert(logger);
logger->flush();
logger->set_level((spdlog::level::level_enum)logLevel);
static const char* LOG_LEVELS[7] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "OFF" };
SPDLOG_LOGGER_INFO(logger, fmt::format("当前日志级别:{}", LOG_LEVELS[logLevel]));
}
API_DECLSPEC BOOL WINAPI Initialize(UINT logLevel) {
// 初始化日志
try {
logger = spdlog::rotating_logger_mt(".", "logs/Runtime.log", 100000, 1);
logger->set_level((spdlog::level::level_enum)logLevel);
logger->set_pattern("%Y-%m-%d %H:%M:%S.%e|%l|%s:%!|%v");
logger->flush_on(spdlog::level::warn);
spdlog::flush_every(std::chrono::seconds(5));
} catch (const spdlog::spdlog_ex&) {
return FALSE;
}
SetLogLevel(logLevel);
// 初始化 App
if (!App::GetInstance().Initialize(hInst)) {
SPDLOG_LOGGER_CRITICAL(logger, "初始化 App 失败");
return FALSE;
}
// 初始化 Renderer
if (!Renderer::GetInstance().Initialize()) {
SPDLOG_LOGGER_CRITICAL(logger, "初始化 Renderer 失败");
return FALSE;
}
// 初始化 Hasher
if (!Utils::Hasher::GetInstance().Initialize()) {
SPDLOG_LOGGER_CRITICAL(logger, "初始化 Utils::Hasher 失败");
return FALSE;
}
return TRUE;
}
API_DECLSPEC const char* WINAPI Run(
HWND hwndSrc,
const char* effectsJson,
UINT captureMode,
int frameRate, // 0垂直同步负数不限帧率正数限制的帧率
float cursorZoomFactor, // 负数和 0和源原窗口相同正数缩放比例
UINT cursorInterpolationMode, // 0最近邻1双线性
UINT flags
) {
const auto& version = Utils::GetOSVersion();
SPDLOG_LOGGER_INFO(logger, fmt::format("OS 版本:{}.{}.{}",
version.dwMajorVersion, version.dwMinorVersion, version.dwBuildNumber));
App& app = App::GetInstance();
if (!app.Run(hwndSrc, effectsJson, captureMode, frameRate, cursorZoomFactor, cursorInterpolationMode, flags)
) {
// 初始化失败
SPDLOG_LOGGER_INFO(logger, "App.Run 失败");
return app.GetErrorMsg();
}
SPDLOG_LOGGER_INFO(logger, "即将退出");
logger->flush();
return nullptr;
}