Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/svg/download.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions include/utils/update_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <string>

namespace Updater {
void checkForUpdates();
inline bool hasUpdate;
inline std::string url;
} // namespace Updater
3 changes: 3 additions & 0 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#define APP_VERSION "0.1.0"
6 changes: 6 additions & 0 deletions src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "storage/storage.h"
#include "utils/banner_manager.h"
#include "utils/tray_manager.h"
#include "utils/update_manager.h"

#ifdef _WIN32
#define _WIN32_WINNT 0x0A00 // Windows 10
Expand All @@ -28,6 +29,7 @@
GLFWcursor *WindowCallbacks::cursor_nwse = nullptr;
GLFWcursor *WindowCallbacks::cursor_nesw = nullptr;
GLFWcursor *WindowCallbacks::cursor_h = nullptr;

GLFWcursor *WindowCallbacks::cursor_v = nullptr;
bool WindowCallbacks::hovered_;
#endif
Expand Down Expand Up @@ -147,12 +149,14 @@ void Application::run() {
storage.initTOML();

bool discordRpc = true;
bool checkForUpdates = true;
bool systemTray = false;
auto toml = storage.loadTOML();
auto *settingsTable = toml["settings"].as_table();
if (settingsTable) {
discordRpc = settingsTable->get("discord_rpc")->value_or(false);
systemTray = settingsTable->get("quit_tray_min")->value_or(false);
checkForUpdates = settingsTable->get("check_updates")->value_or(false);
}

std::vector<std::unique_ptr<Client>> clients;
Expand Down Expand Up @@ -193,6 +197,8 @@ void Application::run() {
discord::RichPresence::UpdatePresence("Lazap", "In Main Menu");
}

if (checkForUpdates) Updater::checkForUpdates();

printf("Startup took: %ld ms\n",
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - startupBegin)
Expand Down
2 changes: 2 additions & 0 deletions src/core/utils/image_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "battery/embed.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include <format>

#include "stb_image.h"

std::unordered_map<std::string, GLuint> ImageManager::cache;
Expand Down
79 changes: 79 additions & 0 deletions src/core/utils/update_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "utils/update_manager.h"

#include <cpr/cpr.h>

#include <glaze/glaze.hpp>
#include <iostream>

#include "string.h"
#include "version.h"

struct UpdateRes {
std::string tag_name;
std::string html_url;
};

template <>
struct glz::meta<UpdateRes> {
static constexpr auto value = object("tag_name", &UpdateRes::tag_name,
"html_url", &UpdateRes::html_url);
};

struct Version {
int major;
int minor;
int patch;
};

Version parseVersion(std::string version) {
Version result{};

if (!version.empty() && version[0] == 'v') version.erase(0, 1);

sscanf(version.data(), "%d.%d.%d", &result.major, &result.minor,
&result.patch);

return result;
}

bool isNewer(const Version& remote, const Version& local) {
if (remote.major != local.major) return remote.major > local.major;

if (remote.minor != local.minor) return remote.minor > local.minor;

return remote.patch > local.patch;
}

namespace Updater {
void checkForUpdates() {
cpr::Response res = cpr::Get(cpr::Url{
"https://api.github.com/repos/Lazap-Development/Lazap/releases/latest"});

if (res.status_code != 200) {
std::cerr << "HTTP Error: " << res.status_code << std::endl;
return;
}

UpdateRes obj;

if (auto ec =
glz::read<glz::opts{.error_on_unknown_keys = false}>(obj, res.text);
ec) {
std::cerr << glz::format_error(ec, res.text) << std::endl;
return;
}

glz::read_json(obj, res.text);

auto remoteVersion = parseVersion(obj.tag_name);
auto localVersion = parseVersion(APP_VERSION);

if (isNewer(remoteVersion, localVersion)) {
std::cout << "Remote Version: " << obj.tag_name << std::endl;
std::cout << "Local Version: " << APP_VERSION << std::endl;

hasUpdate = true;
url = obj.html_url;
}
}
} // namespace Updater
22 changes: 19 additions & 3 deletions src/ui/panels/left_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#endif
#include "ui/theme.h"
#include "utils/image_manager.h"
#include "utils/update_manager.h"

using namespace ui;

Expand All @@ -32,6 +33,8 @@ void LeftPanel::init() {
0xFF000000);
ImageManager::loadSVG(b::embed<"assets/svg/github.svg">(), "github",
0xFFFFFFFF);
ImageManager::loadSVG(b::embed<"assets/svg/download.svg">(), "download",
0x99FF66B2);
ImageManager::loadSVG(b::embed<"assets/svg/settings.svg">(), "settings",
0xFFFFFFFF);
ImageManager::loadSVG(b::embed<"assets/svg/settings.svg">(), "settings-black",
Expand Down Expand Up @@ -112,10 +115,23 @@ void LeftPanel::render() {
view_->view == ViewType::Library))
if (view_->view != ViewType::Library) view_->Library();

ImGui::Dummy(
ImVec2(0, ImGui::GetContentRegionAvail().y - (125.0f * scale_.y)));

// Bottom icons
if (Updater::hasUpdate) {
ImGui::Dummy(
ImVec2(0, ImGui::GetContentRegionAvail().y - (180.0f * scale_.y)));

if (drawIconButton("Update", ImageManager::get("download"),
ImageManager::get("download"),
ImVec2(30 * scale_.x, 30 * scale_.x), false))
openURL(Updater::url);

ImGui::Dummy(ImVec2(0, 30.0f * scale_.y));
}
else {
ImGui::Dummy(
ImVec2(0, ImGui::GetContentRegionAvail().y - (125.0f * scale_.y)));
}

if (drawIconButton("GitHub", ImageManager::get("github"),
ImageManager::get("github"),
ImVec2(30 * scale_.x, 30 * scale_.x), false))
Expand Down
7 changes: 3 additions & 4 deletions src/ui/panels/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ void SettingsPanel::render() {
else
Autostart::deleteShortcut();
}
if (addOption("Check for Updates", InputType::Toggle, &checkUpdates_,
true)) {
// saveSettings();
ImGui::OpenPopup("Coming Soon");
if (addOption("Check for Updates", InputType::Toggle, &checkUpdates_)) {
saveSettings();
ImGui::OpenPopup("Restart");
}

ImGui::EndGroup();
Expand Down
Loading