feat: add version checking

This commit is contained in:
2021-11-14 15:11:44 +01:00
parent fde3a247d5
commit 11a517a41a
2 changed files with 13 additions and 36 deletions

View File

@@ -4,6 +4,8 @@
#include <ctime> #include <ctime>
#define TD_VERSION "alpha-0.0.0"
namespace td { namespace td {
namespace utils { namespace utils {
@@ -32,7 +34,6 @@ public:
static void removeOldFile(); static void removeOldFile();
private: private:
std::string getDownloadFileURL(); std::string getDownloadFileURL();
std::time_t getLocalFileTimeStamp();
}; };
} // namespace utils } // namespace utils

View File

@@ -18,20 +18,13 @@ bool Updater::checkUpdate() {
if (newFileUrl.empty()) return false; if (newFileUrl.empty()) return false;
httplib::Client client("thesims.freeboxos.fr", 30000); httplib::Client client("thesims.freeboxos.fr", 30000);
if (auto res = client.Head(newFileUrl.c_str())) {
std::string distantFileCreation = res.value().get_header_value("Last-Modified");
char formatedTime[100]; if (auto res = client.Get("/Tower%20Defense/version")) {
std::time_t localFileTileStamp = getLocalFileTimeStamp();
std::strftime(formatedTime, sizeof(formatedTime), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&localFileTileStamp)); std::string currentVersion = TD_VERSION;
std::string lastVersion = res.value().body;
std::string localFileCreation = formatedTime; if (currentVersion != lastVersion) {
std::cout << "Local file : " << localFileCreation << std::endl;
std::cout << "Distant file : " << distantFileCreation << std::endl;
if (distantFileCreation != localFileCreation) {
return true; return true;
} }
return false; return false;
@@ -39,25 +32,8 @@ bool Updater::checkUpdate() {
std::cerr << "Error : " << res.error() << std::endl; std::cerr << "Error : " << res.error() << std::endl;
return false; return false;
} }
}
std::time_t Updater::getLocalFileTimeStamp(){ return false;
#ifdef _WIN32
std::string localFile = getLocalFilePath();
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
if(GetFileAttributesEx(localFile.c_str(), GetFileExInfoStandard, &fileInfo) > 0){
ULARGE_INTEGER ull;
ull.LowPart = fileInfo.ftCreationTime.dwLowDateTime;
ull.HighPart = fileInfo.ftCreationTime.dwHighDateTime;
time_t lastModified = ull.QuadPart / 10000000ULL - 11644473600ULL;
return lastModified;
}
#endif
return 0;
} }
std::string Updater::getDownloadFileURL() { std::string Updater::getDownloadFileURL() {
@@ -122,20 +98,20 @@ void Updater::downloadUpdate() {
} }
} }
void Updater::removeOldFile(){ void Updater::removeOldFile() {
std::remove(std::string(getLocalFilePath() + "_old").c_str()); std::remove(std::string(getLocalFilePath() + "_old").c_str());
} }
bool Updater::writeFile() { bool Updater::writeFile() {
if (m_FileWrited || !m_DownloadComplete) return false; if (m_FileWrited || !m_DownloadComplete) return false;
if(!m_FileBuffer.WriteFile(getLocalFilePath() + "_recent")) if (!m_FileBuffer.WriteFile(getLocalFilePath() + "_recent"))
return false; return false;
#ifdef _WIN32 #ifdef _WIN32
std::rename(getLocalFilePath().c_str(), std::string(getLocalFilePath() + "_old").c_str()); std::rename(getLocalFilePath().c_str(), std::string(getLocalFilePath() + "_old").c_str());
std::rename(std::string(getLocalFilePath() + "_recent").c_str(), getLocalFilePath().c_str()); std::rename(std::string(getLocalFilePath() + "_recent").c_str(), getLocalFilePath().c_str());
#endif #endif
m_FileWrited = true; m_FileWrited = true;
m_DownloadComplete = false; m_DownloadComplete = false;