Skip to content

Commit

Permalink
feat: improve functions and ui on module
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed Apr 30, 2024
1 parent a891f5f commit 3d2ab7c
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,13 @@ if (BUILD_APPLICATION)
)

add_custom_command(TARGET ${AppName} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AppDir/usr/lib"
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AppDir/usr/plugins"
COMMENT "Complement to build the required architecture"
)

add_custom_command(TARGET ${AppName} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AppDir/usr/lib/mods"
COMMAND ${CMAKE_COMMAND} -E rename "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/mods" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AppDir/usr/lib/mods"
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AppDir/usr/plugins/mods"
COMMAND ${CMAKE_COMMAND} -E rename "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/mods" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/AppDir/usr/plugins/mods"
COMMENT "Copying Mods into App Image"
)

Expand Down
18 changes: 15 additions & 3 deletions src/core/module/GlobalModuleContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GlobalModuleContext::Impl {
return default_task_runner_;
}

auto RegisterModule(const ModulePtr& module) -> bool {
auto RegisterModule(const ModulePtr& module, bool integrated_module) -> bool {
GF_CORE_LOG_DEBUG("attempting to register module: {}",
module->GetModuleIdentifier());
// Check if the module is null or already registered.
Expand All @@ -120,6 +120,7 @@ class GlobalModuleContext::Impl {
GpgFrontend::SecureCreateSharedObject<ModuleRegisterInfo>();
register_info->module = module;
register_info->channel = acquire_new_unique_channel();
register_info->integrated = integrated_module;

// move module to its task runner' thread
register_info->module->setParent(nullptr);
Expand Down Expand Up @@ -322,6 +323,11 @@ class GlobalModuleContext::Impl {
return m.has_value() && m->get()->activate;
}

auto IsIntegratedModule(ModuleIdentifier m_id) -> bool {
auto m = search_module_register_table(m_id);
return m.has_value() && m->get()->integrated;
}

auto ListAllRegisteredModuleID() -> QList<ModuleIdentifier> {
QList<ModuleIdentifier> module_ids;
for (const auto& module : module_register_table_) {
Expand All @@ -343,6 +349,7 @@ class GlobalModuleContext::Impl {
int channel;
ModulePtr module;
bool activate;
bool integrated;
QList<QString> listening_event_ids;
};

Expand Down Expand Up @@ -411,8 +418,9 @@ auto GlobalModuleContext::GetGlobalTaskRunner()
return p_->GetGlobalTaskRunner();
}

auto GlobalModuleContext::RegisterModule(ModulePtr module) -> bool {
return p_->RegisterModule(module);
auto GlobalModuleContext::RegisterModule(ModulePtr module,
bool integrated_module) -> bool {
return p_->RegisterModule(module, integrated_module);
}

auto GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) -> bool {
Expand Down Expand Up @@ -449,6 +457,10 @@ auto GlobalModuleContext::IsModuleActivated(ModuleIdentifier m_id) -> bool {
return p_->IsModuleActivated(m_id);
}

auto GlobalModuleContext::IsIntegratedModule(ModuleIdentifier m_id) -> bool {
return p_->IsIntegratedModule(m_id);
}

auto GlobalModuleContext::ListAllRegisteredModuleID()
-> QList<ModuleIdentifier> {
return p_->ListAllRegisteredModuleID();
Expand Down
4 changes: 3 additions & 1 deletion src/core/module/GlobalModuleContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject {

auto GetGlobalTaskRunner() -> std::optional<TaskRunnerPtr>;

auto RegisterModule(ModulePtr) -> bool;
auto RegisterModule(ModulePtr, bool) -> bool;

auto ActiveModule(ModuleIdentifier) -> bool;

Expand All @@ -86,6 +86,8 @@ class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject {

auto IsModuleActivated(ModuleIdentifier) -> bool;

auto IsIntegratedModule(ModuleIdentifier) -> bool;

auto ListAllRegisteredModuleID() -> QList<ModuleIdentifier>;

private:
Expand Down
24 changes: 18 additions & 6 deletions src/core/module/ModuleInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,34 @@

namespace GpgFrontend::Module {

void LoadModuleFromPath(const QString& mods_path) {
void LoadModuleFromPath(const QString& mods_path, bool integrated) {
for (const auto& module_library_name :
QDir(mods_path).entryList(QStringList() << "*.so"
<< "*.dll"
<< "*.dylib",
QDir::Files)) {
ModuleManager::GetInstance().LoadModule(mods_path + "/" +
module_library_name);
ModuleManager::GetInstance().LoadModule(
mods_path + "/" + module_library_name, integrated);
}
}

auto LoadIntegratedMods() -> bool {
auto exec_binary_path = QCoreApplication::applicationDirPath();

#if defined(MACOS) && defined(RELEASE)
auto mods_path = exec_binary_path + "../PlugIns/mods";
#else
auto mods_path = exec_binary_path + "/mods";
#endif

// AppImage
if (!qEnvironmentVariable("APPIMAGE").isEmpty()) {
mods_path = qEnvironmentVariable("APPDIR") + "/usr/lib/mods";
mods_path = qEnvironmentVariable("APPDIR") + "/usr/plugins/mods";
}

// Flatpak
if (!qEnvironmentVariable("container").isEmpty()) {
mods_path = "/app/lib/mods";
}

GF_CORE_LOG_DEBUG("try loading integrated modules at path: {} ...",
Expand All @@ -65,7 +77,7 @@ auto LoadIntegratedMods() -> bool {
return false;
}

LoadModuleFromPath(mods_path);
LoadModuleFromPath(mods_path, true);

GF_CORE_LOG_DEBUG("load integrated modules done.");
return true;
Expand All @@ -82,7 +94,7 @@ auto LoadExternalMods() -> bool {
return false;
}

LoadModuleFromPath(mods_path);
LoadModuleFromPath(mods_path, false);

GF_CORE_LOG_DEBUG("load integrated modules done.");
return true;
Expand Down
50 changes: 39 additions & 11 deletions src/core/module/ModuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class ModuleManager::Impl {

~Impl() = default;

auto LoadAndRegisterModule(const QString& module_library_path) -> void {
auto LoadAndRegisterModule(const QString& module_library_path,
bool integrated_module) -> void {
Thread::TaskRunnerGetter::GetInstance()
.GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Default)
->PostTask(new Thread::Task(
Expand All @@ -77,21 +78,39 @@ class ModuleManager::Impl {
}

module->SetGPC(gmc_.get());
if (!gmc_->RegisterModule(module)) return -1;
if (!gmc_->RegisterModule(module, integrated_module)) return -1;

SettingsObject so(
QString("module.%1.so").arg(module->GetModuleIdentifier()));
const auto module_id = module->GetModuleIdentifier();
const auto module_hash = module->GetModuleHash();

SettingsObject so(QString("module.%1.so").arg(module_id));
ModuleSO module_so(so);

// if user has set auto active enable
if (module_so.module_id == module->GetModuleIdentifier() &&
module_so.module_hash == module->GetModuleHash() &&
module_so.auto_activate) {
if (gmc_->ActiveModule(module->GetModuleIdentifier())) {
if ((module_so.module_id == module_id &&
module_so.module_hash == module_hash &&
module_so.auto_activate) ||
// integrated modules activate by default
((module_so.module_id.isEmpty() ||
module_so.module_hash.isEmpty()) &&
integrated_module)) {
if (!gmc_->ActiveModule(module_id)) {
return -1;
}
}

// reset module settings after change
if ((module_so.module_id.isEmpty() ||
module_so.module_id != module_id) ||
(module_so.module_hash.isEmpty() ||
module_so.module_hash != module_hash)) {
module_so.module_id = module_id;
module_so.module_hash = module_hash;
module_so.auto_activate = integrated_module;

so.Store(module_so.ToJson());
}

return 0;
},
__func__, nullptr));
Expand All @@ -111,7 +130,7 @@ class ModuleManager::Impl {
->PostTask(new Thread::Task(
[=](GpgFrontend::DataObjectPtr) -> int {
module->SetGPC(gmc_.get());
return gmc_->RegisterModule(module) ? 0 : -1;
return gmc_->RegisterModule(module, false) ? 0 : -1;
},
__func__, nullptr));
}
Expand Down Expand Up @@ -196,6 +215,10 @@ class ModuleManager::Impl {
return gmc_->IsModuleActivated(id);
}

auto IsIntegratedModule(ModuleIdentifier id) -> bool {
return gmc_->IsIntegratedModule(id);
}

auto GRT() -> GlobalRegisterTable* { return grt_.get(); }

private:
Expand Down Expand Up @@ -231,8 +254,9 @@ ModuleManager::ModuleManager(int channel)

ModuleManager::~ModuleManager() = default;

void ModuleManager::LoadModule(QString module_library_path) {
return p_->LoadAndRegisterModule(module_library_path);
void ModuleManager::LoadModule(QString module_library_path,
bool integrated_module) {
return p_->LoadAndRegisterModule(module_library_path, integrated_module);
}

auto ModuleManager::SearchModule(ModuleIdentifier module_id) -> ModulePtr {
Expand Down Expand Up @@ -298,6 +322,10 @@ auto ModuleManager::IsModuleActivated(ModuleIdentifier id) -> bool {
return p_->IsModuleActivated(id);
}

auto ModuleManager::IsIntegratedModule(ModuleIdentifier id) -> bool {
return p_->IsIntegratedModule(id);
}

auto ModuleManager::ListAllRegisteredModuleID() -> QList<ModuleIdentifier> {
return p_->ListAllRegisteredModuleID();
};
Expand Down
4 changes: 3 additions & 1 deletion src/core/module/ModuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GPGFRONTEND_CORE_EXPORT ModuleManager

virtual ~ModuleManager() override;

auto LoadModule(QString) -> void;
auto LoadModule(QString, bool) -> void;

auto SearchModule(ModuleIdentifier) -> ModulePtr;

Expand All @@ -76,6 +76,8 @@ class GPGFRONTEND_CORE_EXPORT ModuleManager

auto IsModuleActivated(ModuleIdentifier) -> bool;

auto IsIntegratedModule(ModuleIdentifier) -> bool;

void ListenEvent(ModuleIdentifier, EventIdentifier);

void TriggerEvent(EventRefrernce);
Expand Down
14 changes: 14 additions & 0 deletions src/ui/dialog/controller/ModuleControllerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "ModuleControllerDialog.h"

#include "core/function/GlobalSettingStation.h"
#include "core/model/SettingsObject.h"
#include "core/struct/settings_object/ModuleSO.h"
#include "ui_ModuleControllerDialog.h"
Expand All @@ -43,6 +44,7 @@ ModuleControllerDialog::ModuleControllerDialog(QWidget* parent)
ui_(std::make_shared<Ui_ModuleControllerDialog>()),
module_manager_(&Module::ModuleManager::GetInstance()) {
ui_->setupUi(this);
ui_->actionsGroupBox->hide();

connect(ui_->moduleListView, &ModuleListView::SignalSelectModule, this,
&ModuleControllerDialog::slot_load_module_details);
Expand Down Expand Up @@ -77,6 +79,11 @@ ModuleControllerDialog::ModuleControllerDialog(QWidget* parent)
QInputDialog::getText(this, "Please provide an Event ID", "Event ID");
Module::TriggerEvent(event_id);
});

connect(ui_->showModsDirButton, &QPushButton::clicked, this, [=]() {
QDesktopServices::openUrl(QUrl::fromLocalFile(
GlobalSettingStation::GetInstance().GetModulesDir()));
});
}

void ModuleControllerDialog::slot_load_module_details(
Expand All @@ -86,6 +93,13 @@ void ModuleControllerDialog::slot_load_module_details(
SettingsObject so(QString("module.%1.so").arg(module_id));
ModuleSO module_so(so);

if (module_id.isEmpty() || module == nullptr) {
ui_->actionsGroupBox->hide();
return;
}

ui_->actionsGroupBox->show();

if (module_so.module_id != module_id ||
module_so.module_hash != module->GetModuleHash()) {
module_so.module_id = module_id;
Expand Down
6 changes: 5 additions & 1 deletion src/ui/widgets/ModuleListView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ void ModuleListView::load_module_informations() {
model_->clear();
for (const auto &module_id : module_ids) {
auto module = module_manager.SearchModule(module_id);
auto integrated_module = module_manager.IsIntegratedModule(module_id);
auto meta_data = module->GetModuleMetaData();
auto *item = new QStandardItem(meta_data.value("Name", module_id));

auto *item = new QStandardItem((integrated_module ? "*" : "") +
meta_data.value("Name", module_id));

item->setData(module_id, Qt::UserRole + 1);
model_->appendRow(item);
}
Expand Down
38 changes: 32 additions & 6 deletions ui/ModuleControllerDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="showModsDirButton">
<property name="text">
<string>Show Mods Directory</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -91,17 +98,36 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="activateOrDeactiveButton">
<property name="text">
<string>Activate</string>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="autoActivateButton">
<property name="text">
<string>Auto Activate</string>
<widget class="QGroupBox" name="actionsGroupBox">
<property name="title">
<string>Actions</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QPushButton" name="activateOrDeactiveButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Activate</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="autoActivateButton">
<property name="text">
<string>Auto Activate</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
Expand Down

0 comments on commit 3d2ab7c

Please sign in to comment.