Skip to content

Commit 4941570

Browse files
committed
app: fix windows build issues
1 parent 2bff3bc commit 4941570

File tree

9 files changed

+61
-47
lines changed

9 files changed

+61
-47
lines changed

app/src/app/DevAssistant/AppLoader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -42,7 +42,7 @@ void InitAppConfig(const char *bin_path) {
4242
#ifdef OSX
4343
CKAppConf::GetInstance()->SetRootDir(app_path + "/Contents");
4444
#else
45-
CU::String::LastNCharSubStr(app_dir, '/', 2, root_dir);
45+
CU::String::LastNCharSubStr(app_path, '\\', 2, root_dir);
4646
CKAppConf::GetInstance()->SetRootDir(root_dir);
4747
CKAppConf::GetInstance()->SetRelativePath("app_icon", "data/Resource/AppIcon.ico");
4848
#endif

app/src/app/DevAssistant/QDAMainWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//

app/src/app/DevAssistant/main.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -121,17 +121,17 @@ int main(int argc, char *argv[])
121121
return FixBuildScript(argv[1]);
122122
}
123123

124-
#ifdef WIN
125-
// Run InstallReg.exe as Root Authority
126-
auto app_bin = GetBinRelativePath(argv[0]);
127-
auto root_flag = CKAppConf::GetInstance()->GetRelativePath("app_bin", app_bin + "/root_flag");
128-
if (CU::File::IsFileExist(root_flag) != 0) {
129-
auto bin_install = CKAppConf::GetInstance()->GetRelativePath("app_bin", app_bin + "/InstallReg.exe");
130-
if (CE::AppLoader::RunAsRoot(bin_install)) {
131-
CU::File::SaveFileString(root_flag, "AutoStart");
132-
}
133-
}
134-
#endif
124+
//#ifdef WIN
125+
// // Run InstallReg.exe as Root Authority
126+
// auto app_bin = GetBinRelativePath(argv[0]);
127+
// auto root_flag = CKAppConf::GetInstance()->GetRelativePath("app_bin", app_bin + "/root_flag");
128+
// if (CU::File::IsFileExist(root_flag) != 0) {
129+
// auto bin_install = CKAppConf::GetInstance()->GetRelativePath("app_bin", app_bin + "/InstallReg.exe");
130+
// if (CE::AppLoader::RunAsRoot(bin_install)) {
131+
// CU::File::SaveFileString(root_flag, "AutoStart");
132+
// }
133+
// }
134+
//#endif
135135

136136
return LoadApp(argc, argv);
137137
}

app/src/kits/COSEnv/CEAppLoader.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -53,7 +53,7 @@ bool AppLoader::GetAppPath(const std::string &bin_path, std::string &output) {
5353
auto pos = bin_path.rfind(".ipa");
5454
output = bin_path.substr(0, pos + 4);
5555
#else
56-
output = bin_path
56+
output = bin_path;
5757
#endif
5858
return true;
5959
}
@@ -64,13 +64,14 @@ bool AppLoader::GetAppDirPath(const std::string &bin_path, std::string &output)
6464
return false;
6565
}
6666
auto bin_path_tmp = bin_path;
67+
std::string::size_type pos;
6768
#ifdef WIN
6869
std::replace(bin_path_tmp.begin(), bin_path_tmp.end(), '\\', '/');
6970
#elif defined(OSX)
70-
auto pos = bin_path_tmp.rfind(".app");
71+
pos = bin_path_tmp.rfind(".app");
7172
bin_path_tmp = bin_path_tmp.substr(0, pos + 4);
7273
#elif defined(IOS)
73-
auto pos = bin_path_tmp.rfind(".ipa");
74+
pos = bin_path_tmp.rfind(".ipa");
7475
bin_path_tmp = bin_path_tmp.substr(0, pos + 4);
7576
#endif
7677
pos = bin_path_tmp.find_last_of('/');
@@ -204,8 +205,8 @@ bool AppLoader::RunAsOSStart(const std::string& app_key, const std::string& bin_
204205
auto tmp_path = bin_path;
205206
std::replace(tmp_path.begin(), tmp_path.end(), '/', '\\');
206207
//CE::Regedit::DelRegValue(regkey + app_key);
207-
auto ret2 = CE::Regedit::SetRegValue(regkey + app_key, tmp_path);
208-
return ret2;
208+
return CE::Regedit::SetRegValue(regkey + app_key, tmp_path, "REG_SZ", true);
209+
209210
#elif defined(OSX)
210211
std::string start_plist = R"(
211212
<?xml version="1.0" encoding="UTF-8"?>

app/src/kits/COSEnv/CERegedit.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -84,7 +84,7 @@ std::string Regedit::GetRegValue(const std::string& hkey, const std::string& reg
8484
HKEY hkey_root = GetRegHKeyRootHandle(hkey);
8585
HKEY hKey_return = NULL; // RegOpenKeyEx Return Value
8686
if (ERROR_SUCCESS != RegOpenKeyEx(hkey_root, reg_path.c_str(), 0, KEY_READ, &hKey_return)) {
87-
LOGE("RegOpenKeyEx failed. %s", reg_path.c_str());
87+
LOGE("RegOpenKeyEx failed. %s\\%s", hkey.c_str(), reg_path.c_str());
8888
return "";
8989
}
9090

@@ -94,29 +94,33 @@ std::string Regedit::GetRegValue(const std::string& hkey, const std::string& reg
9494
// 0 不定义值类型
9595
if (ERROR_SUCCESS != RegQueryValueEx(hKey_return, reg_key.c_str(), 0, &keySz_type, (LPBYTE)&key_value, &key_size)) {
9696
RegCloseKey(hkey_root);
97-
LOGE("RegQueryValueEx failed. %s", reg_path.c_str());
97+
LOGE("RegQueryValueEx failed. %s\\%s", hkey.c_str(), reg_path.c_str());
9898
return "";
9999
}
100100

101-
LOGI("Regedit::GetRegValue() %s", key_value);
101+
LOGI("Regedit::GetRegValue() %s\\%s key_value %s", hkey.c_str(), reg_path.c_str(), key_value);
102102
RegCloseKey(hkey_root);
103103
return std::string(key_value, key_size);
104104
}
105105

106-
bool Regedit::SetRegValue(const std::string& key, const std::string& value, const std::string& reg_type)
106+
bool Regedit::SetRegValue(const std::string& key, const std::string& value, const std::string& reg_type, bool reset_old)
107107
{
108108
bool is_dir = false;
109109
std::string old_val = GetRegValue(key, is_dir);
110-
if (old_val.size() > 0 && old_val[old_val.size() - 1] == '\0') {
111-
old_val = old_val.substr(0, old_val.size() - 1);
112-
}
110+
std::string new_val = value;
111+
if (!reset_old) {
112+
if (old_val.size() > 0 && old_val[old_val.size() - 1] == '\0') {
113+
old_val = old_val.substr(0, old_val.size() - 1);
114+
}
115+
116+
// Key Exist And value Same With Old
117+
if (value.size() == old_val.size() && value == old_val) {
118+
return true;
119+
}
113120

114-
// Key Exist And value Same With Old
115-
if (value.size() == old_val.size() && value == old_val) {
116-
return true;
121+
new_val = (old_val.size() > 0) ? old_val + ";" + value : value;
117122
}
118123

119-
const std::string new_val = (old_val.size() > 0) ? old_val + ";" + value : value;
120124
std::string cmd = "";
121125
int pos_last = key.find_last_of("\\");
122126
std::string lp_key = key.substr(0, pos_last);
@@ -173,7 +177,7 @@ std::string Regedit::GetRegValue(const std::string& hkey, const std::string& reg
173177
return "";
174178
}
175179

176-
bool Regedit::SetRegValue(const std::string& key, const std::string& value, const std::string& reg_type) {
180+
bool Regedit::SetRegValue(const std::string& key, const std::string& value, const std::string& reg_type, bool reset_old) {
177181
return false;
178182
}
179183

app/src/kits/COSEnv/CERegedit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -53,7 +53,7 @@ namespace Regedit {
5353
/// @param key dir is end with "\\", detail refer to up demo.
5454
/// @param value e.g. "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.exe"
5555
/// @param reg_type values from : REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ, REG_DWORD, REG_QWORD, REG_BINARY, REG_NONE
56-
bool SetRegValue(const std::string& key, const std::string& value, const std::string& reg_type = "REG_SZ");
56+
bool SetRegValue(const std::string& key, const std::string& value, const std::string& reg_type = "REG_SZ", bool reset_old = false);
5757

5858
/// @demo dir : DelRegValue("KEY_CURRENT_USER\\Environment\\TEST_REG\\")
5959
/// @demo item : DelRegValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\devenv.exe")

app/src/kits/CUtils/logger.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -25,10 +25,10 @@
2525

2626
#include "logger.h"
2727

28+
#include <chrono>
2829
#if _HAS_CXX17
2930
#include <filesystem> // C++17
3031
#endif
31-
//#include <iostream>
3232

3333
#ifdef WIN
3434
#include <direct.h>
@@ -37,7 +37,7 @@
3737
#include <spdlog/spdlog.h>
3838
#include <spdlog/async.h>
3939
#include <spdlog/sinks/stdout_color_sinks.h>
40-
#include <spdlog/sinks/basic_file_sink.h>
40+
// #include <spdlog/sinks/basic_file_sink.h>
4141
#include <spdlog/sinks/rotating_file_sink.h>
4242

4343
#include <CUtils/units_time.h>
@@ -200,12 +200,13 @@ static int DeleteLogFiles(const std::string& dir_path) {
200200
if (entry.is_regular_file() && entry.path().extension() == ".log") {
201201
auto fsize = std::filesystem::file_size(entry.path()) / 1024;
202202
auto last_write_time = std::filesystem::last_write_time(entry.path());
203-
auto last_write_ms = decltype(last_write_time)::clock::to_time_t(last_write_time);
203+
auto last_write_ms = std::chrono::duration_cast<std::chrono::seconds>(last_write_time.time_since_epoch()).count();
204+
//auto last_write_ms = decltype(last_write_time)::clock::to_time_t(last_write_time);
204205
// std::cout << "Last modified time: " << std::asctime(std::localtime(&last_write_ms));
205206
uint64_t current_ms = units::time::now_second();
206207
if (current_ms - last_write_ms <= units::time::day2second) {
207208
spdlog::info("log file {} file size {} KB last write time to now {} ms !",
208-
entry.path().c_str(), fsize, current_ms - last_write_ms);
209+
entry.path().string().c_str(), fsize, current_ms - last_write_ms);
209210
continue;
210211
}
211212

@@ -214,10 +215,10 @@ static int DeleteLogFiles(const std::string& dir_path) {
214215
std::filesystem::remove(entry.path(), ec);
215216
if (ec) {
216217
spdlog::error("delete log file {} error {} file size {} KB last write time {} ms !",
217-
entry.path().c_str(), ec.message(), fsize, last_write_ms);
218+
entry.path().string().c_str(), ec.message(), fsize, last_write_ms);
218219
} else {
219220
spdlog::info("delete log file {} succeed file size {} KB last write time {} ms !",
220-
entry.path().c_str(), fsize, last_write_ms);
221+
entry.path().string().c_str(), fsize, last_write_ms);
221222
}
222223
}
223224
}

app/src/kits/CUtils/path_util.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -24,14 +24,15 @@
2424

2525
#include <limits.h>
2626
#include <filesystem>
27+
#include <chrono>
2728

2829
namespace utils::path {
2930

3031
std::string absolute_path(const std::string &relative_path) {
3132
std::filesystem::path tmp_path = relative_path;
3233
#ifdef WIN
33-
char abs_path[PATH_MAX] = {0};
34-
_fullpath(absPath, relative_path.c_str(), abs_path);
34+
char abs_path[MAX_PATH] = {0};
35+
_fullpath(abs_path, relative_path.c_str(), MAX_PATH);
3536
return abs_path;
3637

3738
#else
@@ -44,4 +45,9 @@ std::string absolute_path(const std::string &relative_path) {
4445
#endif
4546
}
4647

48+
static uint64_t file_last_write_ms(std::filesystem::path file_path) {
49+
auto last_write_time = std::filesystem::last_write_time(file_path);
50+
return std::chrono::duration_cast<std::chrono::seconds>(last_write_time.time_since_epoch()).count();
51+
}
52+
4753
}

app/src/kits/CUtils/path_util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIT License
1+
// MIT License
22
//
33
// Copyright (c) 2021~2024 [coder4869](https://github.com/coder4869)
44
//
@@ -25,6 +25,8 @@
2525

2626
#include <string>
2727

28+
#define MAX_PATH 4096
29+
2830
namespace utils::path {
2931
extern std::string absolute_path(const std::string &relative_path);
3032

0 commit comments

Comments
 (0)