mirror of
https://github.com/huiyiruciduojiao/FuckScreenCap.git
synced 2026-01-28 03:54:37 +08:00
386 lines
12 KiB
C++
386 lines
12 KiB
C++
// ConfigManager.cpp : 定义静态库的函数。
|
||
//
|
||
|
||
#include "pch.h"
|
||
#include "framework.h"
|
||
#include "ConfigManager.h"
|
||
#include <fstream>
|
||
#include <sstream>
|
||
#include <algorithm>
|
||
#include <iostream>
|
||
#include <Windows.h>
|
||
#include <Shlwapi.h>
|
||
|
||
#pragma comment(lib,"shlwapi.lib")
|
||
|
||
// TODO: 这是一个库函数示例
|
||
namespace ConfigManager {
|
||
|
||
// ProcessFilterConfig 实现
|
||
void ProcessFilterConfig::clear() {
|
||
processList.clear();
|
||
mode = WHITELIST;
|
||
caseSensitive = false;
|
||
}
|
||
|
||
// Config 类实现
|
||
Config::Config() {
|
||
m_configFilePath = getDefaultConfigPath();
|
||
}
|
||
|
||
Config::Config(const std::string& configPath) : m_configFilePath(configPath) {
|
||
}
|
||
|
||
std::string Config::toLowerCase(const std::string& str) const {
|
||
std::string result = str;
|
||
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
|
||
return result;
|
||
}
|
||
|
||
std::string Config::trim(const std::string& str) const {
|
||
size_t start = str.find_first_not_of(" \t\r\n");
|
||
if (start == std::string::npos) return "";
|
||
size_t end = str.find_last_not_of(" \t\r\n");
|
||
return str.substr(start, end - start + 1);
|
||
}
|
||
|
||
bool Config::fileExists(const std::string& filePath) const {
|
||
return GetFileAttributesA(filePath.c_str()) != INVALID_FILE_ATTRIBUTES;
|
||
}
|
||
|
||
void Config::setConfigPath(const std::string& configPath) {
|
||
m_configFilePath = configPath;
|
||
}
|
||
|
||
std::string Config::getConfigPath() const {
|
||
return m_configFilePath;
|
||
}
|
||
|
||
std::string Config::getDefaultConfigPath() const {
|
||
char exePath[MAX_PATH] = { 0 };
|
||
GetModuleFileNameA(NULL, exePath, MAX_PATH);
|
||
PathRemoveFileSpecA(exePath);
|
||
PathAppendA(exePath, "process_filter.ini");
|
||
return std::string(exePath);
|
||
}
|
||
|
||
bool Config::loadConfig() {
|
||
return loadConfig(m_configFilePath);
|
||
}
|
||
|
||
bool Config::loadConfig(const std::string& configPath) {
|
||
m_filterConfig.clear();
|
||
|
||
std::ifstream configFile(configPath);
|
||
if (!configFile.is_open()) {
|
||
std::cerr << "Warning: 无法打开配置文件: " << configPath << std::endl;
|
||
return false;
|
||
}
|
||
|
||
std::string line;
|
||
bool inProcessSection = false;
|
||
|
||
std::cout << "Info: 开始读取配置文件: " << configPath << std::endl;
|
||
|
||
while (std::getline(configFile, line)) {
|
||
line = trim(line);
|
||
|
||
// 跳过空行和注释
|
||
if (line.empty() || line.front() == '#' || line.front() == ';') {
|
||
continue;
|
||
}
|
||
|
||
// 检查是否是节标题
|
||
if (line.front() == '[' && line.back() == ']') {
|
||
std::string section = line.substr(1, line.length() - 2);
|
||
section = trim(section);
|
||
inProcessSection = (toLowerCase(section) == "process");
|
||
continue;
|
||
}
|
||
|
||
if (!inProcessSection) {
|
||
continue;
|
||
}
|
||
|
||
// 解析配置项
|
||
size_t equalPos = line.find('=');
|
||
if (equalPos == std::string::npos) {
|
||
continue;
|
||
}
|
||
|
||
std::string key = trim(line.substr(0, equalPos));
|
||
std::string value = trim(line.substr(equalPos + 1));
|
||
|
||
key = toLowerCase(key);
|
||
|
||
if (key == "mode") {
|
||
value = toLowerCase(value);
|
||
if (value == "whitelist" || value == "white") {
|
||
m_filterConfig.mode = ProcessFilterConfig::WHITELIST;
|
||
std::cout << "Info: 设置为白名单模式" << std::endl;
|
||
}
|
||
else if (value == "blacklist" || value == "black") {
|
||
m_filterConfig.mode = ProcessFilterConfig::BLACKLIST;
|
||
std::cout << "Info: 设置为黑名单模式" << std::endl;
|
||
}
|
||
}
|
||
else if (key == "casesensitive") {
|
||
value = toLowerCase(value);
|
||
m_filterConfig.caseSensitive = (value == "true" || value == "1" || value == "yes");
|
||
std::cout << "Info: 大小写敏感: " << (m_filterConfig.caseSensitive ? "开启" : "关闭") << std::endl;
|
||
}
|
||
else if (key == "list") {
|
||
// 解析进程列表,支持逗号分隔
|
||
std::stringstream ss(value);
|
||
std::string processName;
|
||
while (std::getline(ss, processName, ',')) {
|
||
processName = trim(processName);
|
||
if (!processName.empty()) {
|
||
if (!m_filterConfig.caseSensitive) {
|
||
processName = toLowerCase(processName);
|
||
}
|
||
m_filterConfig.processList.insert(processName);
|
||
std::cout << "Info: 添加进程: " << processName << std::endl;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
configFile.close();
|
||
|
||
std::cout << "Info: 配置文件加载完成,共加载 " << m_filterConfig.processList.size() << " 个进程名" << std::endl;
|
||
return true;
|
||
}
|
||
|
||
bool Config::saveDefaultConfig() const {
|
||
return saveDefaultConfig(m_configFilePath);
|
||
}
|
||
|
||
bool Config::saveDefaultConfig(const std::string& configPath) const {
|
||
std::ofstream configFile(configPath);
|
||
if (!configFile.is_open()) {
|
||
std::cerr << "Error: 无法创建配置文件: " << configPath << std::endl;
|
||
return false;
|
||
}
|
||
|
||
configFile << "# 进程过滤配置文件\n";
|
||
configFile << "# 支持的配置项:\n";
|
||
configFile << "# mode: whitelist(白名单) 或 blacklist(黑名单)\n";
|
||
configFile << "# casesensitive: true(大小写敏感) 或 false(大小写不敏感)\n";
|
||
configFile << "# list: 进程名列表,用逗号分隔\n\n";
|
||
|
||
configFile << "[Process]\n";
|
||
configFile << "# 过滤模式: whitelist(只处理列表中的进程) 或 blacklist(处理除列表外的进程)\n";
|
||
configFile << "mode = whitelist\n\n";
|
||
|
||
configFile << "# 是否大小写敏感\n";
|
||
configFile << "casesensitive = false\n\n";
|
||
|
||
configFile << "# 进程名列表(包含.exe后缀),用逗号分隔\n";
|
||
configFile << "list = chrome.exe, msedge.exe, mstsc.exe, notepad.exe, calc.exe\n\n";
|
||
|
||
configFile << "# 示例黑名单配置:\n";
|
||
configFile << "# mode = blacklist\n";
|
||
configFile << "# list = explorer.exe, winlogon.exe, csrss.exe, smss.exe\n";
|
||
|
||
configFile.close();
|
||
|
||
std::cout << "Info: 默认配置文件已创建: " << configPath << std::endl;
|
||
return true;
|
||
}
|
||
|
||
bool Config::reloadConfig() {
|
||
// 如果配置文件不存在,创建默认配置
|
||
if (!fileExists(m_configFilePath)) {
|
||
std::cout << "Warning: 配置文件不存在,创建默认配置" << std::endl;
|
||
if (!saveDefaultConfig()) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return loadConfig();
|
||
}
|
||
|
||
const ProcessFilterConfig& Config::getProcessFilterConfig() const {
|
||
return m_filterConfig;
|
||
}
|
||
|
||
ProcessFilterConfig& Config::getProcessFilterConfig() {
|
||
return m_filterConfig;
|
||
}
|
||
|
||
bool Config::shouldProcessBeHooked(const std::string& processName) const {
|
||
if (m_filterConfig.processList.empty()) {
|
||
// 如果没有配置,使用默认行为
|
||
return true;
|
||
}
|
||
|
||
std::string nameToCheck = processName;
|
||
if (!m_filterConfig.caseSensitive) {
|
||
nameToCheck = toLowerCase(processName);
|
||
}
|
||
|
||
bool inList = m_filterConfig.processList.find(nameToCheck) != m_filterConfig.processList.end();
|
||
|
||
if (m_filterConfig.mode == ProcessFilterConfig::WHITELIST) {
|
||
return inList; // 白名单模式:只处理列表中的进程
|
||
}
|
||
else {
|
||
return !inList; // 黑名单模式:处理列表外的进程
|
||
}
|
||
}
|
||
|
||
void Config::setFilterMode(ProcessFilterConfig::FilterMode mode) {
|
||
m_filterConfig.mode = mode;
|
||
}
|
||
|
||
ProcessFilterConfig::FilterMode Config::getFilterMode() const {
|
||
return m_filterConfig.mode;
|
||
}
|
||
|
||
void Config::setCaseSensitive(bool sensitive) {
|
||
m_filterConfig.caseSensitive = sensitive;
|
||
}
|
||
|
||
bool Config::isCaseSensitive() const {
|
||
return m_filterConfig.caseSensitive;
|
||
}
|
||
|
||
void Config::addProcess(const std::string& processName) {
|
||
std::string nameToAdd = processName;
|
||
if (!m_filterConfig.caseSensitive) {
|
||
nameToAdd = toLowerCase(processName);
|
||
}
|
||
m_filterConfig.processList.insert(nameToAdd);
|
||
}
|
||
|
||
void Config::removeProcess(const std::string& processName) {
|
||
std::string nameToRemove = processName;
|
||
if (!m_filterConfig.caseSensitive) {
|
||
nameToRemove = toLowerCase(processName);
|
||
}
|
||
m_filterConfig.processList.erase(nameToRemove);
|
||
}
|
||
|
||
void Config::clearProcessList() {
|
||
m_filterConfig.processList.clear();
|
||
}
|
||
|
||
std::vector<std::string> Config::getProcessList() const {
|
||
std::vector<std::string> result;
|
||
result.reserve(m_filterConfig.processList.size());
|
||
for (const auto& process : m_filterConfig.processList) {
|
||
result.push_back(process);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
size_t Config::getProcessCount() const {
|
||
return m_filterConfig.processList.size();
|
||
}
|
||
|
||
bool Config::isConfigValid() const {
|
||
return !m_configFilePath.empty();
|
||
}
|
||
|
||
void Config::printConfig() const {
|
||
std::cout << "=== 配置信息 ===" << std::endl;
|
||
std::cout << "配置文件路径: " << m_configFilePath << std::endl;
|
||
std::cout << "过滤模式: " << (m_filterConfig.mode == ProcessFilterConfig::WHITELIST ? "白名单" : "黑名单") << std::endl;
|
||
std::cout << "大小写敏感: " << (m_filterConfig.caseSensitive ? "是" : "否") << std::endl;
|
||
std::cout << "进程列表 (" << m_filterConfig.processList.size() << "个):" << std::endl;
|
||
for (const auto& process : m_filterConfig.processList) {
|
||
std::cout << " - " << process << std::endl;
|
||
}
|
||
std::cout << "================" << std::endl;
|
||
}
|
||
|
||
// GlobalConfig 静态成员初始化
|
||
Config* GlobalConfig::s_instance = nullptr;
|
||
bool GlobalConfig::s_initialized = false;
|
||
|
||
bool GlobalConfig::initialize() {
|
||
if (s_initialized) {
|
||
return true;
|
||
}
|
||
|
||
s_instance = new Config();
|
||
s_initialized = s_instance->reloadConfig();
|
||
|
||
if (!s_initialized) {
|
||
delete s_instance;
|
||
s_instance = nullptr;
|
||
}
|
||
|
||
return s_initialized;
|
||
}
|
||
|
||
bool GlobalConfig::initialize(const std::string& configPath) {
|
||
if (s_initialized) {
|
||
return true;
|
||
}
|
||
|
||
s_instance = new Config(configPath);
|
||
s_initialized = s_instance->reloadConfig();
|
||
|
||
if (!s_initialized) {
|
||
delete s_instance;
|
||
s_instance = nullptr;
|
||
}
|
||
|
||
return s_initialized;
|
||
}
|
||
|
||
void GlobalConfig::cleanup() {
|
||
if (s_instance) {
|
||
delete s_instance;
|
||
s_instance = nullptr;
|
||
}
|
||
s_initialized = false;
|
||
}
|
||
|
||
Config* GlobalConfig::getInstance() {
|
||
return s_instance;
|
||
}
|
||
|
||
bool GlobalConfig::isInitialized() {
|
||
return s_initialized;
|
||
}
|
||
|
||
// 便捷函数实现
|
||
bool initializeGlobalConfig() {
|
||
return GlobalConfig::initialize();
|
||
}
|
||
|
||
bool initializeGlobalConfig(const std::string& configPath) {
|
||
return GlobalConfig::initialize(configPath);
|
||
}
|
||
|
||
Config* getGlobalConfig() {
|
||
return GlobalConfig::getInstance();
|
||
}
|
||
|
||
void cleanupGlobalConfig() {
|
||
GlobalConfig::cleanup();
|
||
}
|
||
|
||
bool shouldProcessBeHooked(const std::string& processName) {
|
||
|
||
Config* config = GlobalConfig::getInstance();
|
||
if (!config) {
|
||
initializeGlobalConfig();
|
||
config = GlobalConfig::getInstance();
|
||
}
|
||
return config->shouldProcessBeHooked(processName);
|
||
}
|
||
|
||
bool reloadGlobalConfig() {
|
||
Config* config = GlobalConfig::getInstance();
|
||
if (!config) {
|
||
return false;
|
||
}
|
||
return config->reloadConfig();
|
||
}
|
||
|
||
} // namespace ConfigManager
|