Change File Finder (#873)

This commit is contained in:
David Markowitz 2022-12-05 07:04:59 -08:00 committed by GitHub
parent 2ba3103a0c
commit 0a616f891f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 45 deletions

View File

@ -4,6 +4,8 @@
#include <cstdint> #include <cstdint>
#include <cassert> #include <cassert>
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <map>
template <typename T> template <typename T>
inline size_t MinSize(size_t size, const std::basic_string_view<T>& string) { inline size_t MinSize(size_t size, const std::basic_string_view<T>& string) {
@ -290,51 +292,30 @@ std::u16string GeneralUtils::ReadWString(RakNet::BitStream* inStream) {
return string; return string;
} }
#ifdef _WIN32 std::vector<std::string> GeneralUtils::GetSqlFileNamesFromFolder(const std::string& folder) {
#define WIN32_LEAN_AND_MEAN // Because we dont know how large the initial number before the first _ is we need to make it a map like so.
#include <Windows.h> std::map<uint32_t, std::string> filenames{};
for (auto& t : std::filesystem::directory_iterator(folder)) {
std::vector<std::string> GeneralUtils::GetFileNamesFromFolder(const std::string& folder) { auto filename = t.path().filename().string();
std::vector<std::string> names; auto index = std::stoi(GeneralUtils::SplitString(filename, '_').at(0));
std::string search_path = folder + "/*.*"; filenames.insert(std::make_pair(index, filename));
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
names.push_back(fd.cFileName);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
#else
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <iostream>
#include <vector>
#include <cstring>
std::vector<std::string> GeneralUtils::GetFileNamesFromFolder(const std::string& folder) {
std::vector<std::string> names;
struct dirent* entry;
DIR* dir = opendir(folder.c_str());
if (dir == NULL) {
return names;
} }
while ((entry = readdir(dir)) != NULL) { // Now sort the map by the oldest migration.
std::string value(entry->d_name, strlen(entry->d_name)); std::vector<std::string> sortedFiles{};
if (value == "." || value == "..") { auto fileIterator = filenames.begin();
continue; std::map<uint32_t, std::string>::iterator oldest = filenames.begin();
while (!filenames.empty()) {
if (fileIterator == filenames.end()) {
sortedFiles.push_back(oldest->second);
filenames.erase(oldest);
fileIterator = filenames.begin();
oldest = filenames.begin();
continue;
} }
names.push_back(value); if (oldest->first > fileIterator->first) oldest = fileIterator;
fileIterator++;
} }
closedir(dir); return sortedFiles;
return names;
} }
#endif

View File

@ -12,6 +12,7 @@
#include <BitStream.h> #include <BitStream.h>
#include "Game.h" #include "Game.h"
#include "dLogger.h"
/*! /*!
\file GeneralUtils.hpp \file GeneralUtils.hpp
@ -138,7 +139,7 @@ namespace GeneralUtils {
std::vector<std::string> SplitString(const std::string& str, char delimiter); std::vector<std::string> SplitString(const std::string& str, char delimiter);
std::vector<std::string> GetFileNamesFromFolder(const std::string& folder); std::vector<std::string> GetSqlFileNamesFromFolder(const std::string& folder);
template <typename T> template <typename T>
T Parse(const char* value); T Parse(const char* value);

View File

@ -38,7 +38,7 @@ void MigrationRunner::RunMigrations() {
sql::SQLString finalSQL = ""; sql::SQLString finalSQL = "";
bool runSd0Migrations = false; bool runSd0Migrations = false;
for (const auto& entry : GeneralUtils::GetFileNamesFromFolder((BinaryPathFinder::GetBinaryDir() / "./migrations/dlu/").string())) { for (const auto& entry : GeneralUtils::GetSqlFileNamesFromFolder((BinaryPathFinder::GetBinaryDir() / "./migrations/dlu/").string())) {
auto migration = LoadMigration("dlu/" + entry); auto migration = LoadMigration("dlu/" + entry);
if (migration.data.empty()) { if (migration.data.empty()) {
@ -102,7 +102,7 @@ void MigrationRunner::RunSQLiteMigrations() {
stmt->execute(); stmt->execute();
delete stmt; delete stmt;
for (const auto& entry : GeneralUtils::GetFileNamesFromFolder((BinaryPathFinder::GetBinaryDir() / "migrations/cdserver/").string())) { for (const auto& entry : GeneralUtils::GetSqlFileNamesFromFolder((BinaryPathFinder::GetBinaryDir() / "migrations/cdserver/").string())) {
auto migration = LoadMigration("cdserver/" + entry); auto migration = LoadMigration("cdserver/" + entry);
if (migration.data.empty()) continue; if (migration.data.empty()) continue;