code/change.cpp

125 lines
3.6 KiB
C++
Raw Normal View History

2024-10-04 16:03:58 +08:00
#include <iostream>
2024-10-04 17:27:26 +08:00
#include <string>
#include <windows.h>
#include <cstdlib>
#include <fstream> // 用于文件存在性检查
#include "SimpleIni.h"
2024-10-04 16:03:58 +08:00
using namespace std;
2024-10-04 17:27:26 +08:00
// 函数声明
bool fileExists(const std::string& filename);
bool createDefaultConfig(const std::string& filename);
2024-10-04 16:42:46 +08:00
int main() {
2024-10-04 17:27:26 +08:00
const std::string configFile = "config.ini";
// 检查 config.ini 是否存在
if (!fileExists(configFile)) {
cout << "未检测到 " << configFile << " 文件,正在创建默认配置..." << endl;
if (createDefaultConfig(configFile)) {
cout << "默认配置文件创建成功。" << endl;
} else {
cout << "创建默认配置文件失败。" << endl;
return 1; // 退出程序,因为无法创建配置文件
}
}
// 初始化INI文件
2024-10-04 16:42:46 +08:00
CSimpleIniA ini;
ini.SetUnicode();
ini.SetMultiKey(true);
2024-10-04 17:27:26 +08:00
SI_Error rc = ini.LoadFile(configFile.c_str());
if (rc < 0) {
cout << "无法加载配置文件 " << configFile << endl;
return 1;
}
const char* name = ini.GetValue("User", "name", "0");
cout << "欢迎使用CSSO与CS起源切换程序" << endl;
cout << "当前模式: " << (strcmp(name, "0") == 1 ? "CSSO" : "CS起源") << endl;
2024-10-04 17:27:26 +08:00
cout << "请选择操作:" << endl;
cout << "1. CSSO" << endl;
cout << "2. CS起源" << endl;
cout << "3. 退出" << endl;
int choice;
cin >> choice;
if (choice == 1) {
if (strcmp(name, "0") == 0) {
cout << "你已是CSSO模式!" << endl;
} else {
// 切换到CSSO模式
int result1 = system("ren bin bin_backup");
2024-10-04 16:42:46 +08:00
int result2 = system("ren bin2 bin");
2024-10-04 17:27:26 +08:00
if (result1 == 0 && result2 == 0) {
ini.SetValue("User", "name", "0");
SI_Error saveRc = ini.SaveFile(configFile.c_str());
if (saveRc >= 0) {
cout << "成功切换到CSSO模式!" << endl;
} else {
cout << "保存配置文件失败!" << endl;
}
} else {
cout << "切换失败!" << endl;
2024-10-04 16:42:46 +08:00
}
2024-10-04 17:27:26 +08:00
Sleep(100);
2024-10-04 16:42:46 +08:00
}
2024-10-04 17:27:26 +08:00
}
else if (choice == 2) {
if (strcmp(name, "1") == 0) {
cout << "你已是CS起源模式!" << endl;
} else {
// 切换到CS起源模式
int result1 = system("ren bin bin2");
int result2 = system("ren bin_backup bin");
if (result1 == 0 && result2 == 0) {
ini.SetValue("User", "name", "1");
SI_Error saveRc = ini.SaveFile(configFile.c_str());
if (saveRc >= 0) {
cout << "成功切换到CS起源模式!" << endl;
} else {
cout << "保存配置文件失败!" << endl;
}
} else {
cout << "切换失败!" << endl;
}
Sleep(100);
2024-10-04 16:42:46 +08:00
}
2024-10-04 16:03:58 +08:00
}
2024-10-04 17:27:26 +08:00
else if (choice == 3) {
cout << "退出程序." << endl;
2024-10-04 17:38:10 +08:00
Sleep(100);
2024-10-04 17:27:26 +08:00
return 0;
}
else {
cout << "无效的选项,请重新运行程序并选择正确的选项。" << endl;
}
return 0;
}
// 检查文件是否存在
bool fileExists(const std::string& filename) {
std::ifstream infile(filename);
return infile.good();
}
// 创建默认的config.ini文件
bool createDefaultConfig(const std::string& filename) {
CSimpleIniA ini;
ini.SetUnicode();
ini.SetMultiKey(true);
// 设置默认值
ini.SetValue("User", "name", "0");
// 保存INI文件
SI_Error rc = ini.SaveFile(filename.c_str());
return (rc >= 0);
2024-10-04 16:03:58 +08:00
}