feat: add version checking
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#define TD_VERSION "alpha-0.0.0"
|
||||
|
||||
namespace td {
|
||||
namespace utils {
|
||||
|
||||
@@ -32,7 +34,6 @@ public:
|
||||
static void removeOldFile();
|
||||
private:
|
||||
std::string getDownloadFileURL();
|
||||
std::time_t getLocalFileTimeStamp();
|
||||
};
|
||||
|
||||
} // namespace utils
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user