feat: Update client build and add remote update feature

This commit is contained in:
yuanyuanxiang
2024-12-28 18:35:34 +08:00
parent 48260b367f
commit 8aa42d5db2
17 changed files with 346 additions and 53 deletions

View File

@@ -11,6 +11,8 @@ typedef void (*StopRun)();
typedef bool (*IsStoped)();
typedef BOOL (*IsExit)();
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
StopRun stop = NULL;
@@ -18,7 +20,7 @@ StopRun stop = NULL;
IsStoped bStop = NULL;
// <20>Ƿ<EFBFBD><C7B7>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ض<EFBFBD>
IsStoped bExit = NULL;
IsExit bExit = NULL;
BOOL status = 0;
@@ -90,6 +92,9 @@ BOOL CALLBACK callback(DWORD CtrlType)
return TRUE;
}
// <20><><EFBFBD>г<EFBFBD><D0B3><EFBFBD>.
BOOL Run(const char* argv1, int argv2);
// @brief <20><><EFBFBD>ȶ<EFBFBD>ȡsettings.ini<6E><69><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ȡIP<49>Ͷ˿<CDB6>.
// [settings]
// localIp=XXX
@@ -103,44 +108,100 @@ int main(int argc, const char *argv[])
}
status = 0;
SetConsoleCtrlHandler(&callback, TRUE);
char path[_MAX_PATH], *p = path;
do {
BOOL ret = Run(argc > 1 ? argv[1] : (strlen(g_ConnectAddress.szServerIP) == 0 ? "127.0.0.1" : g_ConnectAddress.szServerIP),
argc > 2 ? atoi(argv[2]) : (g_ConnectAddress.iPort == 0 ? 6543 : g_ConnectAddress.iPort));
if (ret == 1) {
return -1;
}
} while (status == 0);
status = 0;
return -1;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>: IP <20><> <20>˿<EFBFBD>.
BOOL Run(const char* argv1, int argv2) {
BOOL result = FALSE;
char path[_MAX_PATH], * p = path;
GetModuleFileNameA(NULL, path, sizeof(path));
while (*p) ++p;
while ('\\' != *p) --p;
strcpy(p+1, "ServerDll.dll");
*(p + 1) = 0;
std::string folder = path;
std::string oldFile = folder + "ServerDll.old";
std::string newFile = folder + "ServerDll.new";
strcpy(p + 1, "ServerDll.dll");
BOOL ok = TRUE;
if (_access(newFile.c_str(), 0) != -1) {
if (_access(oldFile.c_str(), 0) != -1)
{
if (!DeleteFileA(oldFile.c_str()))
{
std::cerr << "Error deleting file. Error code: " << GetLastError() << std::endl;
ok = FALSE;
}
}
if (ok && !MoveFileA(path, oldFile.c_str())) {
std::cerr << "Error removing file. Error code: " << GetLastError() << std::endl;
ok = FALSE;
}else {
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
if (SetFileAttributesA(oldFile.c_str(), FILE_ATTRIBUTE_HIDDEN))
{
std::cout << "File created and set to hidden: " << oldFile << std::endl;
}
}
if (ok && !MoveFileA(newFile.c_str(), path)) {
std::cerr << "Error removing file. Error code: " << GetLastError() << std::endl;
MoveFileA(oldFile.c_str(), path);// recover
}else if (ok){
std::cout << "Using new file: " << newFile << std::endl;
}
}
HMODULE hDll = LoadLibraryA(path);
typedef void (*TestRun)(char* strHost,int nPort);
typedef void (*TestRun)(char* strHost, int nPort);
TestRun run = hDll ? TestRun(GetProcAddress(hDll, "TestRun")) : NULL;
stop = hDll ? StopRun(GetProcAddress(hDll, "StopRun")) : NULL;
bStop = hDll ? IsStoped(GetProcAddress(hDll, "IsStoped")) : NULL;
bExit = hDll ? IsStoped(GetProcAddress(hDll, "IsExit")) : NULL;
bExit = hDll ? IsExit(GetProcAddress(hDll, "IsExit")) : NULL;
if (run)
{
char *ip = g_ConnectAddress.szServerIP;
int &port = g_ConnectAddress.iPort;
char* ip = g_ConnectAddress.szServerIP;
int& port = g_ConnectAddress.iPort;
strcpy(p + 1, "settings.ini");
if (_access(path, 0) == -1) { // <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD>ȴӲ<C8B4><D3B2><EFBFBD><EFBFBD><EFBFBD>ȡֵ<C8A1><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD>g_ConnectAddressȡֵ.
ip = argc > 1 ? argv[1] :(strlen(ip)==0 ? "127.0.0.1" : ip);
port = argc > 2 ? atoi(argv[2]) : (port==0 ? 6543: port);
} else {
strcpy(ip, argv1);
port = argv2;
}
else {
GetPrivateProfileStringA("settings", "localIp", g_ConnectAddress.szServerIP, ip, _MAX_PATH, path);
port = GetPrivateProfileIntA("settings", "ghost", g_ConnectAddress.iPort, path);
}
printf("[server] %s:%d\n", ip, port);
do
do
{
run(ip, port);
while(bStop && !bStop() && 0 == status)
while (bStop && !bStop() && 0 == status)
Sleep(20);
} while (bExit && !bExit() && 0 == status);
while(bStop && !bStop() && 1 == status)
while (bStop && !bStop() && 1 == status)
Sleep(20);
if (bExit) {
result = bExit();
}
if (!FreeLibrary(hDll)) {
printf("<EFBFBD>ͷŶ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>\"ServerDll.dll\"ʧ<EFBFBD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", GetLastError());
}
else {
printf("<EFBFBD>ͷŶ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>\"ServerDll.dll\"<EFBFBD>ɹ<EFBFBD>!\n");
}
}
else {
printf("<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>\"ServerDll.dll\"ʧ<EFBFBD><EFBFBD>.\n");
printf("<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>\"ServerDll.dll\"ʧ<EFBFBD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %d\n", GetLastError());
Sleep(3000);
}
status = 0;
return -1;
}
return result;
}