Replace binary servers.db with text-based servers.txt (name:ip:port)

This commit is contained in:
daoge_cmd
2026-03-09 05:44:27 +08:00
parent 88798b501d
commit 88b2f831a0
7 changed files with 143 additions and 248 deletions

View File

@@ -1581,67 +1581,50 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
MultiByteToWideChar(CP_ACP, 0, g_Win64Username, -1, g_Win64UsernameW, 17);
// convert servers.txt to servers.db
if (GetFileAttributesA("servers.txt") != INVALID_FILE_ATTRIBUTES &&
GetFileAttributesA("servers.db") == INVALID_FILE_ATTRIBUTES)
// convert servers.db to servers.txt
if (GetFileAttributesA("servers.db") != INVALID_FILE_ATTRIBUTES &&
GetFileAttributesA("servers.txt") == INVALID_FILE_ATTRIBUTES)
{
FILE* txtFile = nullptr;
if (fopen_s(&txtFile, "servers.txt", "r") == 0 && txtFile)
FILE* dbFile = nullptr;
if (fopen_s(&dbFile, "servers.db", "rb") == 0 && dbFile)
{
struct MigEntry { std::string ip; uint16_t port; std::string name; };
std::vector<MigEntry> migEntries;
char line[512];
while (fgets(line, sizeof(line), txtFile))
char magic[4] = {};
if (fread(magic, 1, 4, dbFile) == 4 && memcmp(magic, "MCSV", 4) == 0)
{
int l = (int)strlen(line);
while (l > 0 && (line[l - 1] == '\n' || line[l - 1] == '\r' || line[l - 1] == ' '))
line[--l] = '\0';
if (l == 0) continue;
std::string srvIP = line;
if (!fgets(line, sizeof(line), txtFile)) break;
l = (int)strlen(line);
while (l > 0 && (line[l - 1] == '\n' || line[l - 1] == '\r' || line[l - 1] == ' '))
line[--l] = '\0';
uint16_t srvPort = (uint16_t)atoi(line);
std::string srvName;
if (fgets(line, sizeof(line), txtFile))
uint32_t version = 0, count = 0;
fread(&version, sizeof(uint32_t), 1, dbFile);
fread(&count, sizeof(uint32_t), 1, dbFile);
if (version == 1)
{
l = (int)strlen(line);
while (l > 0 && (line[l - 1] == '\n' || line[l - 1] == '\r' || line[l - 1] == ' '))
line[--l] = '\0';
srvName = line;
for (uint32_t s = 0; s < count; s++)
{
uint16_t ipLen = 0, p = 0, nameLen = 0;
if (fread(&ipLen, sizeof(uint16_t), 1, dbFile) != 1) break;
if (ipLen == 0 || ipLen > 256) break;
char ipBuf[257] = {};
if (fread(ipBuf, 1, ipLen, dbFile) != ipLen) break;
if (fread(&p, sizeof(uint16_t), 1, dbFile) != 1) break;
if (fread(&nameLen, sizeof(uint16_t), 1, dbFile) != 1) break;
if (nameLen > 256) break;
char nameBuf[257] = {};
if (nameLen > 0 && fread(nameBuf, 1, nameLen, dbFile) != nameLen) break;
migEntries.push_back({std::string(ipBuf), p, std::string(nameBuf)});
}
}
if (!srvIP.empty() && srvPort > 0)
migEntries.push_back({srvIP, srvPort, srvName});
}
fclose(txtFile);
fclose(dbFile);
if (!migEntries.empty())
{
FILE* dbFile = nullptr;
if (fopen_s(&dbFile, "servers.db", "wb") == 0 && dbFile)
FILE* txtFile = nullptr;
if (fopen_s(&txtFile, "servers.txt", "w") == 0 && txtFile)
{
fwrite("MCSV", 1, 4, dbFile);
uint32_t ver = 1;
uint32_t cnt = (uint32_t)migEntries.size();
fwrite(&ver, sizeof(uint32_t), 1, dbFile);
fwrite(&cnt, sizeof(uint32_t), 1, dbFile);
for (size_t i = 0; i < migEntries.size(); i++)
{
uint16_t ipLen = (uint16_t)migEntries[i].ip.length();
fwrite(&ipLen, sizeof(uint16_t), 1, dbFile);
fwrite(migEntries[i].ip.c_str(), 1, ipLen, dbFile);
fwrite(&migEntries[i].port, sizeof(uint16_t), 1, dbFile);
uint16_t nameLen = (uint16_t)migEntries[i].name.length();
fwrite(&nameLen, sizeof(uint16_t), 1, dbFile);
fwrite(migEntries[i].name.c_str(), 1, nameLen, dbFile);
}
fclose(dbFile);
fprintf(txtFile, "%s:%s:%u\n", migEntries[i].name.c_str(), migEntries[i].ip.c_str(), migEntries[i].port);
fclose(txtFile);
}
}
}