Skip to content

Commit

Permalink
Fix updater crash (#469)
Browse files Browse the repository at this point in the history
* fix updater crash

* lint

* win32 wchar log

* patch libobs for header file

* lint

* remove logs leftovers
  • Loading branch information
royshil authored Oct 10, 2023
1 parent b8fcfd3 commit 0ac64a8
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 92 deletions.
7 changes: 7 additions & 0 deletions cmake/common/buildspec_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ function(_setup_obs_studio)
set(_cmake_version "3.0.0")
endif()

message(STATUS "Patch libobs")
execute_process(
COMMAND patch --forward "libobs/CMakeLists.txt" "${CMAKE_CURRENT_SOURCE_DIR}/patch_libobs.diff"
RESULT_VARIABLE _process_result
WORKING_DIRECTORY "${dependencies_dir}/${_obs_destination}")
message(STATUS "Patch - done")

message(STATUS "Configure ${label} (${arch})")
execute_process(
COMMAND
Expand Down
12 changes: 12 additions & 0 deletions patch_libobs.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/libobs/CMakeLists.txt b/libobs/CMakeLists.txt
index d2e2671..a08d948 100644
--- a/libobs/CMakeLists.txt
+++ b/libobs/CMakeLists.txt
@@ -287,6 +287,7 @@ set(public_headers
util/base.h
util/bmem.h
util/c99defs.h
+ util/config-file.h
util/darray.h
util/profiler.h
util/sse-intrin.h
76 changes: 52 additions & 24 deletions src/obs-utils/obs-config-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,77 @@
#include "plugin-support.h"

#include <obs-module.h>
#include <util/config-file.h>
#include <filesystem>

int getFlagFromConfig(const char *name, bool *returnValue)
void create_config_folder()
{
// Check configuration to see if update checks are disabled
char *config_file = obs_module_file("config.json");
if (!config_file) {
obs_log(LOG_INFO, "Unable to find config file");
return OBS_BGREMOVAL_CONFIG_FAIL;
char *config_folder_path = obs_module_config_path("");
if (config_folder_path == nullptr) {
obs_log(LOG_ERROR, "Failed to get config folder path");
return;
}
std::filesystem::path config_folder_std_path(config_folder_path);
bfree(config_folder_path);

// create the folder if it doesn't exist
if (!std::filesystem::exists(config_folder_std_path)) {
#ifdef _WIN32
obs_log(LOG_INFO, "Config folder does not exist, creating: %S",
config_folder_std_path.c_str());
#else
obs_log(LOG_INFO, "Config folder does not exist, creating: %s",
config_folder_std_path.c_str());
#endif
// Create the config folder
std::filesystem::create_directories(config_folder_std_path);
}
}

int getFlagFromConfig(const char *name, bool *returnValue, bool defaultValue)
{
create_config_folder(); // ensure the config folder exists

obs_data_t *data = obs_data_create_from_json_file(config_file);
if (!data) {
obs_log(LOG_INFO, "Failed to parse config file");
// Get the config file
char *config_file_path = obs_module_config_path("config.ini");

config_t *config;
int ret = config_open(&config, config_file_path, CONFIG_OPEN_EXISTING);
if (ret != CONFIG_SUCCESS) {
obs_log(LOG_INFO, "Failed to open config file %s",
config_file_path);
*returnValue = defaultValue;
return OBS_BGREMOVAL_CONFIG_FAIL;
}

*returnValue = obs_data_get_bool(data, name);
obs_data_release(data);
*returnValue = config_get_bool(config, "config", name);
config_close(config);

bfree(config_file_path);

return OBS_BGREMOVAL_CONFIG_SUCCESS;
}

int setFlagFromConfig(const char *name, const bool value)
{
create_config_folder(); // ensure the config folder exists

// Get the config file
char *config_file = obs_module_file("config.json");
if (!config_file) {
obs_log(LOG_INFO, "Unable to find config file");
return OBS_BGREMOVAL_CONFIG_FAIL;
}
char *config_file_path = obs_module_config_path("config.ini");

// Parse the config file
obs_data_t *json_data = obs_data_create_from_json_file(config_file);
if (!json_data) {
obs_log(LOG_INFO, "Failed to parse config file");
config_t *config;
int ret = config_open(&config, config_file_path, CONFIG_OPEN_ALWAYS);
if (ret != CONFIG_SUCCESS) {
obs_log(LOG_INFO, "Failed to open config file %s",
config_file_path);
return OBS_BGREMOVAL_CONFIG_FAIL;
}

// Update the config
obs_data_set_bool(json_data, name, value);
obs_data_save_json(json_data, config_file);
config_set_bool(config, "config", name, value);
config_save(config);
config_close(config);

obs_data_release(json_data);
bfree(config_file_path);

return OBS_BGREMOVAL_CONFIG_SUCCESS;
}
3 changes: 2 additions & 1 deletion src/obs-utils/obs-config-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ enum {
*
* @param name The name of the config item.
* @param returnValue The value of the config item.
* @param defaultValue The default value of the config item.
* @return OBS_BGREMOVAL_CONFIG_SUCCESS if the config item was found,
* OBS_BGREMOVAL_CONFIG_FAIL otherwise.
*/
int getFlagFromConfig(const char *name, bool *returnValue);
int getFlagFromConfig(const char *name, bool *returnValue, bool defaultValue);

/**
* Set a boolean flag in the module configuration file.
Expand Down
12 changes: 7 additions & 5 deletions src/update-checker/UpdateDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UpdateDialog.hpp"
#include "obs-utils/obs-config-utils.h"
#include "plugin-support.h"

#include <obs.h>
#include <obs-module.h>
Expand All @@ -11,15 +12,15 @@
static QString dialogContent =
"<h1>Background Removal - Update available! 🚀</h1>"
"<p>A new version of the Background Removal plugin (<a "
"href=\"https://github.com/royshil/obs-backgroundremoval/releases\">v{version}</a>) is "
"href=\"https://github.com/obs-ai/obs-backgroundremoval/releases\">v{version}</a>) is "
"now available for download. We've made some exciting updates and improvements that we think "
"you'll love. To get the latest features and enhancements, please follow the link below:</p>"
"<p>Download the latest version from GitHub: <a "
"href=\"https://github.com/royshil/obs-backgroundremoval/releases\">v{version}</a></p>"
"href=\"https://github.com/obs-ai/obs-backgroundremoval/releases\">v{version}</a></p>"
"<p>Once you've downloaded the new version, install the update as usual, there's no need to "
"uninstall the previous version.</p>"
"<p>If you have any questions or need assistance during the update process, feel free to reach out"
" to our <a href=\"https://github.com/royshil/obs-backgroundremoval/issues\">support team</a>.</p>"
" to our <a href=\"https://github.com/obs-ai/obs-backgroundremoval/issues\">support team</a>.</p>"
"<p>Thank you for using our plugin and we hope you enjoy the latest release! 🙏</p>"
"<h2>Changelog</h2>";

Expand All @@ -30,7 +31,8 @@ UpdateDialog::UpdateDialog(
setWindowTitle("Background Removal - Update available! 🚀");
setLayout(layout);
QLabel *label = new QLabel(dialogContent.replace(
QString("{version}"), QString(latestVersion.version)));
QString("{version}"),
QString::fromStdString(latestVersion.version)));
label->setOpenExternalLinks(true);
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
label->setTextFormat(Qt::RichText);
Expand All @@ -39,7 +41,7 @@ UpdateDialog::UpdateDialog(

QScrollArea *scrollArea = new QScrollArea;
QLabel *scrollAreaLabel =
new QLabel(QString(latestVersion.responseBody));
new QLabel(QString::fromStdString(latestVersion.responseBody));
scrollAreaLabel->setOpenExternalLinks(true);
scrollAreaLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
scrollAreaLabel->setTextFormat(Qt::MarkdownText);
Expand Down
24 changes: 5 additions & 19 deletions src/update-checker/github-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "plugin-support.h"

static const std::string GITHUB_LATEST_RELEASE_URL =
"https://api.github.com/repos/royshil/obs-backgroundremoval/releases/latest";
"https://api.github.com/repos/obs-ai/obs-backgroundremoval/releases/latest";

void github_utils_get_release_information(
std::function<void(github_utils_release_information)> callback)
Expand All @@ -25,34 +25,20 @@ void github_utils_get_release_information(
if (!data) {
obs_log(LOG_INFO,
"Failed to parse latest release info");
callback(
{OBS_BGREMOVAL_GITHUB_UTILS_ERROR, NULL, NULL});
callback({OBS_BGREMOVAL_GITHUB_UTILS_ERROR, "", ""});
return;
}

// The version is in the "tag_name" property
char *version = strdup(obs_data_get_string(data, "tag_name"));
char *body = strdup(obs_data_get_string(data, "body"));
std::string version = obs_data_get_string(data, "tag_name");
std::string body = obs_data_get_string(data, "body");
obs_data_release(data);

// remove the "v" prefix in version, if it exists
if (version[0] == 'v') {
char *newVersion = (char *)malloc(strlen(version) - 1);
strcpy(newVersion, version + 1);
free(version);
version = newVersion;
version = version.substr(1);
}

callback({OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS, body, version});
});
}

void github_utils_release_information_free(
struct github_utils_release_information info)
{
if (info.responseBody != NULL)
free(info.responseBody);
if (info.version != NULL)
free(info.version);
info.responseCode = OBS_BGREMOVAL_GITHUB_UTILS_ERROR;
}
8 changes: 3 additions & 5 deletions src/update-checker/github-utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <string>

enum {
OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS = 0,
Expand All @@ -9,12 +10,9 @@ enum {

struct github_utils_release_information {
int responseCode;
char *responseBody;
char *version;
std::string responseBody;
std::string version;
};

void github_utils_get_release_information(
std::function<void(github_utils_release_information)> callback);

void github_utils_release_information_free(
struct github_utils_release_information info);
83 changes: 45 additions & 38 deletions src/update-checker/update-checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,56 @@

#include <QTimer>

UpdateDialog *update_dialog;

extern "C" const char *PLUGIN_VERSION;

UpdateDialog *update_dialog = nullptr;

void check_update(void)
{
github_utils_get_release_information([](github_utils_release_information
info) {
if (info.responseCode == OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS) {
obs_log(LOG_INFO, "Latest release is %s", info.version);
bool shouldCheckForUpdates = false;
if (getFlagFromConfig("check_for_updates",
&shouldCheckForUpdates) !=
OBS_BGREMOVAL_CONFIG_SUCCESS) {
// Failed to get the config value, assume it's enabled
shouldCheckForUpdates = true;
}

if (!shouldCheckForUpdates) {
// Update checks are disabled
return;
}

if (strcmp(info.version, PLUGIN_VERSION) == 0) {
// No update available, latest version is the same as the current version
return;
}

try {
update_dialog = new UpdateDialog(
info,
(QWidget *)
obs_frontend_get_main_window());
QTimer::singleShot(2000, update_dialog,
&UpdateDialog::exec);
} catch (...) {
obs_log(LOG_ERROR,
"Failed to construct UpdateDialog");
}
} else {
bool shouldCheckForUpdates = false;
if (getFlagFromConfig("check_for_updates", &shouldCheckForUpdates,
true) != OBS_BGREMOVAL_CONFIG_SUCCESS) {
// Failed to get the config value, assume it's enabled
shouldCheckForUpdates = true;
// store the default value
setFlagFromConfig("check_for_updates", shouldCheckForUpdates);
}

if (!shouldCheckForUpdates) {
// Update checks are disabled
return;
}

const auto callback = [](github_utils_release_information info) {
if (info.responseCode != OBS_BGREMOVAL_GITHUB_UTILS_SUCCESS) {
obs_log(LOG_INFO,
"failed to get latest release information");
return;
}
github_utils_release_information_free(info);
});
obs_log(LOG_INFO, "Latest release is %s", info.version.c_str());

if (info.version == PLUGIN_VERSION) {
// No update available, latest version is the same as the current version
return;
}

try {
QTimer::singleShot(2000, [=]() {
QWidget *main_window = (QWidget *)
obs_frontend_get_main_window();
if (main_window == nullptr) {
obs_log(LOG_ERROR,
"Update Checker failed to get main window");
return;
}
update_dialog =
new UpdateDialog(info, main_window);
update_dialog->exec();
});
} catch (...) {
obs_log(LOG_ERROR, "Failed to construct UpdateDialog");
}
};

github_utils_get_release_information(callback);
}

0 comments on commit 0ac64a8

Please sign in to comment.