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

@@ -18,20 +18,13 @@ bool Updater::checkUpdate() {
if (newFileUrl.empty()) return false;
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];
std::time_t localFileTileStamp = getLocalFileTimeStamp();
if (auto res = client.Get("/Tower%20Defense/version")) {
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;
std::cout << "Local file : " << localFileCreation << std::endl;
std::cout << "Distant file : " << distantFileCreation << std::endl;
if (distantFileCreation != localFileCreation) {
if (currentVersion != lastVersion) {
return true;
}
return false;
@@ -39,25 +32,8 @@ bool Updater::checkUpdate() {
std::cerr << "Error : " << res.error() << std::endl;
return false;
}
}
std::time_t Updater::getLocalFileTimeStamp(){
#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;
return false;
}
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());
}
bool Updater::writeFile() {
if (m_FileWrited || !m_DownloadComplete) return false;
if(!m_FileBuffer.WriteFile(getLocalFilePath() + "_recent"))
if (!m_FileBuffer.WriteFile(getLocalFilePath() + "_recent"))
return false;
#ifdef _WIN32
std::rename(getLocalFilePath().c_str(), std::string(getLocalFilePath() + "_old").c_str());
std::rename(std::string(getLocalFilePath() + "_recent").c_str(), getLocalFilePath().c_str());
#endif
#ifdef _WIN32
std::rename(getLocalFilePath().c_str(), std::string(getLocalFilePath() + "_old").c_str());
std::rename(std::string(getLocalFilePath() + "_recent").c_str(), getLocalFilePath().c_str());
#endif
m_FileWrited = true;
m_DownloadComplete = false;