BagFix and auto apdate
This commit is contained in:
parent
c8d5ee1675
commit
f6cfb6297c
BIN
bin/Xenith
BIN
bin/Xenith
Binary file not shown.
161
main.cpp
161
main.cpp
@ -14,6 +14,7 @@
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
namespace std::filesystem::__cxx11
|
||||
{
|
||||
class filesystem_error;
|
||||
@ -247,75 +248,111 @@ Version parse_version(const std::string& str) {
|
||||
Version v;
|
||||
std::stringstream ss(str);
|
||||
std::string segment;
|
||||
if (std::getline(ss, segment, '.')) v.major = std::stoi(segment);
|
||||
if (std::getline(ss, segment, '.')) v.minor = std::stoi(segment);
|
||||
if (std::getline(ss, segment, '.')) v.patch = std::stoi(segment);
|
||||
|
||||
try {
|
||||
if (std::getline(ss, segment, '.')) v.major = std::stoi(segment);
|
||||
else return v;
|
||||
|
||||
if (std::getline(ss, segment, '.')) v.minor = std::stoi(segment);
|
||||
else return v;
|
||||
|
||||
if (std::getline(ss, segment, '.')) v.patch = std::stoi(segment);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Ошибка парсинга версии '" << str << "': " << e.what() << std::endl;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static bool available_new_version()
|
||||
{
|
||||
std::string cmd = "curl -s https://git.bipfr.ru/BIPfR/Xenith/raw/branch/master/version.txt -o ./tmp/remote_version.txt";
|
||||
system(cmd.c_str());
|
||||
std::ifstream tmp_file("./tmp/remote_version.txt");
|
||||
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;
|
||||
// Правильное сравнение версий
|
||||
bool is_newer(const Version& remote, const Version& current) {
|
||||
if (remote.major != current.major) return remote.major > current.major;
|
||||
if (remote.minor != current.minor) return remote.minor > current.minor;
|
||||
return remote.patch > current.patch;
|
||||
}
|
||||
|
||||
static int cmd_update(const Args& a)
|
||||
{
|
||||
if (available_new_version())
|
||||
{
|
||||
namespace fs = std::filesystem;
|
||||
std::cout << "Удаляем старые файлы..." << std::endl;
|
||||
for (const auto& entry : fs::directory_iterator(".")) {
|
||||
std::string name = entry.path().filename().string();
|
||||
if (name == "models") {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (fs::is_directory(entry.status())) {
|
||||
fs::remove_all(entry.path());
|
||||
} else {
|
||||
fs::remove(entry.path());
|
||||
}
|
||||
} catch (const std::filesystem::__cxx11::filesystem_error& e) {
|
||||
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");
|
||||
static bool available_new_version() {
|
||||
const std::string REMOTE_URL = "https://git.bipfr.ru/BIPfR/Xenith/raw/branch/master/version.txt";
|
||||
const std::string TMP_FILE = "./tmp/remote_version.txt";
|
||||
|
||||
if (result != 0) {
|
||||
std::cerr << "Ошибка при клонировании репозитория" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
result = system("cp -r /tmp/xenith_update/* .");
|
||||
if (result != 0) {
|
||||
std::cerr << "Ошибка при копировании файлов" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
system("rm -rf /tmp/xenith_update");
|
||||
std::cout << "Обновление завершено! Умница Зайка!!!" << std::endl;
|
||||
} else
|
||||
{
|
||||
std::cout << "Уже установлена последняя версия! " << std::endl;
|
||||
fs::create_directories("./tmp");
|
||||
|
||||
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(".")) {
|
||||
std::string name = entry.path().filename().string();
|
||||
if (name == "models") continue;
|
||||
|
||||
try {
|
||||
if (fs::is_directory(entry.status())) {
|
||||
fs::remove_all(entry.path());
|
||||
} else {
|
||||
fs::remove(entry.path());
|
||||
}
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
std::cerr << "Ошибка удаления " << name << ": " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
std::cerr << "Ошибка клонирования репозитория" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
std::cout << "Копирование файлов..." << std::endl;
|
||||
result = system(("cp -ra " + TMP_CLONE + "/. .").c_str());
|
||||
if (result != 0) {
|
||||
std::cerr << "Ошибка копирования файлов" << std::endl;
|
||||
system(("rm -rf " + TMP_CLONE).c_str());
|
||||
return -1;
|
||||
}
|
||||
system(("rm -rf " + TMP_CLONE).c_str());
|
||||
std::cout << "Обновление завершено! Умница Зайка!!!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
0.0.2
|
||||
0.0.3
|
||||
Loading…
x
Reference in New Issue
Block a user