BagFix and auto apdate

This commit is contained in:
KoDer 2026-09-29 23:27:18 +07:00
parent c8d5ee1675
commit f6cfb6297c
3 changed files with 100 additions and 63 deletions

Binary file not shown.

121
main.cpp
View File

@ -14,6 +14,7 @@
#include <sstream> #include <sstream>
#include <sys/stat.h> #include <sys/stat.h>
namespace fs = std::filesystem;
namespace std::filesystem::__cxx11 namespace std::filesystem::__cxx11
{ {
class filesystem_error; class filesystem_error;
@ -247,75 +248,111 @@ Version parse_version(const std::string& str) {
Version v; Version v;
std::stringstream ss(str); std::stringstream ss(str);
std::string segment; std::string segment;
try {
if (std::getline(ss, segment, '.')) v.major = std::stoi(segment); if (std::getline(ss, segment, '.')) v.major = std::stoi(segment);
else return v;
if (std::getline(ss, segment, '.')) v.minor = std::stoi(segment); if (std::getline(ss, segment, '.')) v.minor = std::stoi(segment);
else return v;
if (std::getline(ss, segment, '.')) v.patch = std::stoi(segment); if (std::getline(ss, segment, '.')) v.patch = std::stoi(segment);
} catch (const std::exception& e) {
std::cerr << "Ошибка парсинга версии '" << str << "': " << e.what() << std::endl;
}
return v; return v;
} }
static bool available_new_version() // Правильное сравнение версий
{ bool is_newer(const Version& remote, const Version& current) {
std::string cmd = "curl -s https://git.bipfr.ru/BIPfR/Xenith/raw/branch/master/version.txt -o ./tmp/remote_version.txt"; if (remote.major != current.major) return remote.major > current.major;
system(cmd.c_str()); if (remote.minor != current.minor) return remote.minor > current.minor;
std::ifstream tmp_file("./tmp/remote_version.txt"); return remote.patch > current.patch;
std::string version = "0.0.0";
if (tmp_file.is_open()) {
std::getline(tmp_file, version);
tmp_file.close();
}
Version remote_version = parse_version(version);
std::ifstream file("./version.txt");
version = "0.0.0";
if (file.is_open()) {
std::getline(file, version);
file.close();
}
Version current_version = parse_version(version);
if (remote_version.major > current_version.major) return true;
if (remote_version.minor > current_version.minor) return true;
if (remote_version.patch > current_version.patch) return true;
return false;
} }
static int cmd_update(const Args& a) static bool available_new_version() {
{ const std::string REMOTE_URL = "https://git.bipfr.ru/BIPfR/Xenith/raw/branch/master/version.txt";
if (available_new_version()) const std::string TMP_FILE = "./tmp/remote_version.txt";
{
namespace fs = std::filesystem; fs::create_directories("./tmp");
std::cout << "Удаляем старые файлы..." << std::endl;
std::string cmd = "curl -s --fail \"" + REMOTE_URL + "\" -o \"" + TMP_FILE + "\"";
int curl_result = system(cmd.c_str());
if (curl_result != 0) {
std::cerr << "Не удалось скачать версию. Проверьте интернет или URL." << std::endl;
return false;
}
std::ifstream tmp_file(TMP_FILE);
std::string remote_str = "0.0.0";
if (tmp_file.is_open() && std::getline(tmp_file, remote_str)) {
tmp_file.close();
} else {
std::cerr << "Файл версии пуст или нечитаем" << std::endl;
return false;
}
std::ifstream local_file("./version.txt");
std::string local_str = "0.0.0";
if (local_file.is_open() && std::getline(local_file, local_str)) {
local_file.close();
}
Version remote_ver = parse_version(remote_str);
Version local_ver = parse_version(local_str);
std::cout << "Локальная версия: " << local_ver.major << "."
<< local_ver.minor << "." << local_ver.patch << std::endl;
std::cout << "Удалённая версия: " << remote_ver.major << "."
<< remote_ver.minor << "." << remote_ver.patch << std::endl;
return is_newer(remote_ver, local_ver);
}
static int cmd_update(const Args& a) {
if (!available_new_version()) {
std::cout << "Уже установлена последняя версия! Умница Зайка!!!" << std::endl;
return 0;
}
std::cout << "Начинаем обновление..." << std::endl;
std::cout << "Очистка старых файлов (сохраняем models/)..." << std::endl;
for (const auto& entry : fs::directory_iterator(".")) { for (const auto& entry : fs::directory_iterator(".")) {
std::string name = entry.path().filename().string(); std::string name = entry.path().filename().string();
if (name == "models") { if (name == "models") continue;
continue;
}
try { try {
if (fs::is_directory(entry.status())) { if (fs::is_directory(entry.status())) {
fs::remove_all(entry.path()); fs::remove_all(entry.path());
} else { } else {
fs::remove(entry.path()); fs::remove(entry.path());
} }
} catch (const std::filesystem::__cxx11::filesystem_error& e) { } catch (const fs::filesystem_error& e) {
std::cerr << "Ошибка удаления " << name << ": " << e.what() << std::endl; std::cerr << "Ошибка удаления " << name << ": " << e.what() << std::endl;
} }
} }
std::cout << "Скачиваем новую версию..." << std::endl;
int result = system("git clone https://git.bipfr.ru/BIPfR/Xenith /tmp/xenith_update");
const std::string REPO_URL = "https://git.bipfr.ru/BIPfR/Xenith.git";
const std::string TMP_CLONE = "/tmp/xenith_update";
system(("rm -rf " + TMP_CLONE).c_str());
std::cout << "Скачивание новой версии..." << std::endl;
int result = system(("git clone --depth 1 " + REPO_URL + " " + TMP_CLONE).c_str());
if (result != 0) { if (result != 0) {
std::cerr << "Ошибка при клонировании репозитория" << std::endl; std::cerr << "Ошибка клонирования репозитория" << std::endl;
return -1; return -1;
} }
result = system("cp -r /tmp/xenith_update/* ."); std::cout << "Копирование файлов..." << std::endl;
result = system(("cp -ra " + TMP_CLONE + "/. .").c_str());
if (result != 0) { if (result != 0) {
std::cerr << "Ошибка при копировании файлов" << std::endl; std::cerr << "Ошибка копирования файлов" << std::endl;
system(("rm -rf " + TMP_CLONE).c_str());
return -1; return -1;
} }
system("rm -rf /tmp/xenith_update"); system(("rm -rf " + TMP_CLONE).c_str());
std::cout << "Обновление завершено! Умница Зайка!!!" << std::endl; std::cout << "Обновление завершено! Умница Зайка!!!" << std::endl;
} else
{
std::cout << "Уже установлена последняя версия! " << std::endl;
}
return 0; return 0;
} }

View File

@ -1 +1 @@
0.0.2 0.0.3