Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Downloader window, settings, stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Oct 27, 2023
1 parent 256fe67 commit a458e02
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 8 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set(HYDRA_QT_FILES
qt/aboutwindow.cxx
qt/keypicker.cxx
qt/terminalwindow.cxx
qt/downloaderwindow.cxx
vendored/miniaudio.c
vendored/stb_image_write.c
vendored/miniz/miniz.c
Expand Down
15 changes: 12 additions & 3 deletions include/download.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace hydra

inline std::pair<std::string, std::string> split_url(const std::string& url)
{
std::regex path_regex("https://(.*?)(/.*)");
std::regex path_regex("http[s]?://(.*?)(/.*)");
std::string host = "https://", query;
std::smatch match;
if (std::regex_search(url, match, path_regex) && match.size() == 3)
Expand All @@ -21,21 +21,30 @@ namespace hydra
}
else
{
printf("[rcheevos]: failed to parse URL: %s\n", url.c_str());
printf("Failed to parse URL: %s\n", url.c_str());
}
return std::make_pair(host, query);
}

struct Downloader
{
static HydraBufferWrapper Download(const std::string& url)
{
return DownloadProgress(url);
}

static HydraBufferWrapper
DownloadProgress(const std::string& url,
std::function<bool(uint64_t current, uint64_t total)> progress = nullptr)
{
try
{
auto [host, query] = split_url(url);
httplib::Client client(host);
client.set_follow_location(true);
auto response = client.Get(query);
httplib::Result response =
progress ? client.Get(query, progress) : client.Get(query);
printf("Finished: %s\n", url.c_str());

if (!response)
{
Expand Down
1 change: 1 addition & 0 deletions include/settings.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Settings
public:
static void Open(const std::filesystem::path& path)
{
map_.clear();
save_path_ = path;
std::ifstream ifs(save_path_);
if (ifs.good())
Expand Down
60 changes: 60 additions & 0 deletions qt/downloaderwindow.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "downloaderwindow.hxx"
#include "download.hxx"
#include <fmt/format.h>
#include <future>
#include <QLabel>
#include <QProgressBar>
#include <QTextEdit>
#include <QVBoxLayout>

DownloaderWindow::DownloaderWindow(const std::vector<std::string>& download_queue)
: QWidget(nullptr, Qt::Window), download_queue_(download_queue)
{
setMinimumSize(500, 400);
setAttribute(Qt::WA_DeleteOnClose);
QVBoxLayout* layout = new QVBoxLayout;
log_ = new QTextEdit;
log_->setReadOnly(true);
log_->setLineWrapMode(QTextEdit::NoWrap);
log_->setMinimumSize(500, 400);
log_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

progress_bar_ = new QProgressBar;
progress_bar_->setMinimum(0);
progress_bar_->setMaximum(100);
progress_bar_->setValue(0);

byte_progress_label_ = new QLabel("");
byte_progress_label_->setAlignment(Qt::AlignCenter);

layout->addWidget(log_);
layout->addWidget(byte_progress_label_);
layout->addWidget(progress_bar_);
setLayout(layout);

setWindowFlags(Qt::WindowStaysOnTopHint);

downloading_ = true;
std::thread t([this]() {
std::future<hydra::HydraBufferWrapper> f = std::async(std::launch::async, [this]() {
auto ret = hydra::Downloader::DownloadProgress(
download_queue_[0], [this](uint64_t current, uint64_t total) {
if (current == 0 && total == 0)
{
printf("Unknown size\n");
}
else
{
int percent = (int)(current * 100 / total);
printf("Progress: %d%%\n", percent);
}
return downloading_;
});
return ret;
});
f.wait();
});
t.detach();
}

DownloaderWindow::~DownloaderWindow() {}
26 changes: 26 additions & 0 deletions qt/downloaderwindow.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <QWidget>
#include <string>
#include <vector>

class QProgressBar;
class QTextEdit;
class QLabel;

class DownloaderWindow : public QWidget
{
Q_OBJECT

public:
DownloaderWindow(const std::vector<std::string>& download_queue);
~DownloaderWindow();

private:
std::vector<std::string> download_queue_;
bool downloading_ = false;

QProgressBar* progress_bar_;
QTextEdit* log_;
QLabel* byte_progress_label_;
};
6 changes: 2 additions & 4 deletions qt/mainwindow.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "mainwindow.hxx"
#include "aboutwindow.hxx"
#include "downloaderwindow.hxx"
#include "input.hxx"
#include "qthelper.hxx"
#include "scripteditor.hxx"
#include "settingswindow.hxx"
Expand Down Expand Up @@ -139,10 +141,6 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
msg->show();
watcher->deleteLater();
}
else
{
printf("Result: %d\n", static_cast<int>(watcher->result()));
}
});
watcher->setFuture(future);
}
Expand Down
4 changes: 3 additions & 1 deletion qt/mainwindow.hxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "input.hxx"
#include "screenwidget.hxx"
#include "settings.hxx"
#include <array>
Expand All @@ -20,6 +19,8 @@
#include <thread>
#include <unordered_map>

class DownloaderWindow;

class MainWindow : public QMainWindow
{
Q_OBJECT
Expand Down Expand Up @@ -92,6 +93,7 @@ private:
QAction* recent_act_;
QTimer* emulator_timer_;
ScreenWidget* screen_;
DownloaderWindow* downloader_;
ma_device sound_device_{};
bool frontend_driven_ = false;
std::unique_ptr<hydra::EmulatorWrapper> emulator_;
Expand Down
21 changes: 21 additions & 0 deletions qt/settingswindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ void SettingsWindow::create_tabs()
use_cwd->setCheckState(Settings::Get("screenshot_path").empty() ? Qt::Checked
: Qt::Unchecked);
general_layout->addWidget(use_cwd, 1, 0, 1, 2);

QSpacerItem* spacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
general_layout->addItem(spacer, 2, 0);

QPushButton* reset_settings = new QPushButton("Reset settings");
connect(reset_settings, &QPushButton::clicked, this, [this]() {
auto reply = QMessageBox::question(this, "Reset settings",
"Are you sure you want to reset your settings?",
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes)
{
std::filesystem::copy(Settings::GetSavePath() / "settings.json",
Settings::GetSavePath() / "settings.json.bak",
std::filesystem::copy_options::overwrite_existing);
std::filesystem::remove(Settings::GetSavePath() / "settings.json");
Settings::Open(Settings::GetSavePath() / "settings.json");
}
});
general_layout->addWidget(reset_settings, 3, 0, 1, 3);
}
{
QGridLayout* cores_layout = new QGridLayout;
Expand Down Expand Up @@ -232,6 +251,7 @@ void SettingsWindow::create_tabs()
msg.exec();
});
cores_layout->addWidget(core_list, 0, 0, 1, 0);
// TODO: become a non-lazy developer
add_filepicker(cores_layout, "Core directory", "core_path", "", 1, 0, true,
[](const std::string&) {
Settings::ReinitCoreInfo();
Expand Down Expand Up @@ -273,6 +293,7 @@ void SettingsWindow::create_tabs()
const auto& core = Settings::CoreInfo[i];
QGridLayout* core_layout = new QGridLayout;
core_layout->setAlignment(Qt::AlignTop);
core_layout->setSpacing(12);
core_layout->setColumnStretch(0, 1);
core_layout->setColumnStretch(1, 2);
const std::vector<std::string>& firmware_files = core.firmware_files;
Expand Down
2 changes: 2 additions & 0 deletions vendored/httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3897,6 +3897,8 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
auto exceed_payload_max_length = false;

if (is_chunked_transfer_encoding(x.headers)) {
// Hack: signal that we won't know the progress
progress(0, 0);
ret = read_content_chunked(strm, x, out);
} else if (!has_header(x.headers, "Content-Length")) {
ret = read_content_without_length(strm, out);
Expand Down

0 comments on commit a458e02

Please sign in to comment.