From d85530b14bac1451ae116a0a422e860400d5a2b8 Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 20 Aug 2024 22:40:14 -0400 Subject: [PATCH] extensions cleanup --- CMakeLists.txt | 1 - src/CMakeLists.txt | 4 - src/app/editor/editor_manager.cc | 108 ++++-------------- src/app/editor/editor_manager.h | 2 +- src/app/editor/system/extension_manager.cc | 44 ++++++- src/app/editor/system/extension_manager.h | 3 +- src/{ext => base}/extension.h | 20 ---- src/{ext/sample.c => base/extension_sample.c} | 2 +- src/ext/ext.cmake | 35 ------ src/ext/extension.cc | 55 --------- src/ext/sample.py | 46 -------- 11 files changed, 70 insertions(+), 250 deletions(-) rename src/{ext => base}/extension.h (85%) rename src/{ext/sample.c => base/extension_sample.c} (98%) delete mode 100644 src/ext/ext.cmake delete mode 100644 src/ext/extension.cc delete mode 100644 src/ext/sample.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 3242602a..ade74ac9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,6 @@ set(YAZE_BUILD_PYTHON ON) set(YAZE_BUILD_CLIB ON) set(YAZE_INSTALL_CLIB OFF) set(YAZE_BUILD_TESTS OFF) -set(YAZE_BUILD_EXTENSIONS ON) # libpng features in bitmap.cc add_definitions("-DYAZE_LIB_PNG=1") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7c23b8ab..1bf4f065 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -80,10 +80,6 @@ if (YAZE_BUILD_Z3ED) include(cli/z3ed.cmake) endif() -if (YAZE_BUILD_EXTENSIONS) - include(ext/ext.cmake) -endif() - if (YAZE_BUILD_PYTHON) include(py/yaze_py.cmake) endif() diff --git a/src/app/editor/editor_manager.cc b/src/app/editor/editor_manager.cc index acd5dc8f..f17a5fa4 100644 --- a/src/app/editor/editor_manager.cc +++ b/src/app/editor/editor_manager.cc @@ -2,9 +2,8 @@ #include "ImGuiColorTextEdit/TextEditor.h" #include "ImGuiFileDialog/ImGuiFileDialog.h" -#include "abseil-cpp/absl/strings/match.h" #include "absl/status/status.h" - +#include "absl/strings/match.h" #include "app/core/constants.h" #include "app/core/platform/file_dialog.h" #include "app/core/platform/renderer.h" @@ -26,7 +25,7 @@ #include "app/gui/input.h" #include "app/gui/style.h" #include "app/rom.h" -#include "ext/extension.h" +#include "base/extension.h" #include "imgui/backends/imgui_impl_sdl2.h" #include "imgui/backends/imgui_impl_sdlrenderer2.h" #include "imgui/imgui.h" @@ -71,7 +70,6 @@ absl::Status EditorManager::Update() { ManageKeyboardShortcuts(); DrawYazeMenu(); - DrawFileDialog(); DrawStatusPopup(); DrawAboutPopup(); DrawInfoPopup(); @@ -309,22 +307,7 @@ void EditorManager::ManageKeyboardShortcuts() { } } -void EditorManager::DrawFileDialog() { - gui::FileDialogPipeline("ChooseFileDlgKey", ".sfc,.smc", std::nullopt, [&]() { - std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName(); - status_ = rom()->LoadFromFile(filePathName); - static RecentFilesManager manager("recent_files.txt"); - - // Load existing recent files - manager.Load(); - - // Add a new file - manager.AddFile(filePathName); - - // Save the updated list - manager.Save(); - }); -} +void EditorManager::DrawFileDialog() {} void EditorManager::DrawStatusPopup() { if (!status_.ok()) { @@ -389,26 +372,17 @@ void EditorManager::DrawInfoPopup() { void EditorManager::DrawYazeMenu() { static bool show_display_settings = false; - static bool show_command_line_interface = false; if (BeginMenuBar()) { DrawYazeMenuBar(); - SameLine(GetWindowWidth() - GetStyle().ItemSpacing.x - - CalcTextSize(ICON_MD_DISPLAY_SETTINGS).x - 150); - // Modify the style of the button to have no background color + CalcTextSize(ICON_MD_DISPLAY_SETTINGS).x - 110); PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); if (Button(ICON_MD_DISPLAY_SETTINGS)) { show_display_settings = !show_display_settings; } - - if (Button(ICON_MD_TERMINAL)) { - show_command_line_interface = !show_command_line_interface; - } PopStyleColor(); - Text("yaze v%s", core::kYazeVersion.data()); - EndMenuBar(); } @@ -417,13 +391,6 @@ void EditorManager::DrawYazeMenu() { gui::DrawDisplaySettings(); End(); } - - if (show_command_line_interface) { - Begin("Command Line Interface", &show_command_line_interface, - ImGuiWindowFlags_None); - Text("Enter a command:"); - End(); - } } void EditorManager::DrawYazeMenuBar() { @@ -572,7 +539,7 @@ void EditorManager::DrawYazeMenuBar() { MENU_ITEM2("Copy", "Ctrl+C") { status_ = current_editor_->Copy(); } MENU_ITEM2("Paste", "Ctrl+V") { status_ = current_editor_->Paste(); } Separator(); - MENU_ITEM2("Find", "Ctrl+F") {} + MENU_ITEM2("Find", "Ctrl+F") { status_ = current_editor_->Find(); } EndMenu(); } @@ -583,28 +550,17 @@ void EditorManager::DrawYazeMenuBar() { static bool show_palette_editor = false; static bool show_emulator = false; + if (show_imgui_demo) ShowDemoWindow(); + if (show_imgui_metrics) ShowMetricsWindow(&show_imgui_metrics); + if (show_memory_editor) memory_editor_.Update(show_memory_editor); + if (show_asm_editor) assembly_editor_.Update(show_asm_editor); + if (show_emulator) { Begin("Emulator", &show_emulator, ImGuiWindowFlags_MenuBar); emulator_.Run(); End(); } - if (show_imgui_metrics) { - ShowMetricsWindow(&show_imgui_metrics); - } - - if (show_memory_editor) { - memory_editor_.Update(show_memory_editor); - } - - if (show_imgui_demo) { - ShowDemoWindow(); - } - - if (show_asm_editor) { - assembly_editor_.Update(show_asm_editor); - } - if (show_palette_editor) { Begin("Palette Editor", &show_palette_editor); status_ = palette_editor_.Update(); @@ -625,22 +581,7 @@ void EditorManager::DrawYazeMenuBar() { EndMenu(); } - if (BeginMenu("Extensions")) { - if (MenuItem("Load Extension", nullptr, nullptr)) { - // Load the extension - } - if (MenuItem("Unload Extension")) { - // Unload the extension - } - if (BeginMenu("Extensions")) { - // List all loaded extensions - EndMenu(); - } - EndMenu(); - } - static bool show_resource_label_manager = false; - if (current_project_.project_opened_) { // Project Menu if (BeginMenu("Project")) { @@ -653,13 +594,6 @@ void EditorManager::DrawYazeMenuBar() { EndMenu(); } } - if (show_resource_label_manager) { - rom()->resource_label()->DisplayLabels(&show_resource_label_manager); - if (current_project_.project_opened_ && - !current_project_.labels_filename_.empty()) { - current_project_.labels_filename_ = rom()->resource_label()->filename_; - } - } static bool open_rom_help = false; static bool open_supported_features = false; @@ -729,11 +663,10 @@ void EditorManager::DrawYazeMenuBar() { Text("Project Menu"); Text("Create a new project or open an existing one."); Text("Save the project to save the current state of the project."); - Text( + TextWrapped( "To save a project, you need to first open a ROM and initialize your " "code path and labels file. Label resource manager can be found in " - "the " - "View menu. Code path is set in the Code editor after opening a " + "the View menu. Code path is set in the Code editor after opening a " "folder."); if (Button("Close", gui::kDefaultModalSize)) { @@ -742,19 +675,24 @@ void EditorManager::DrawYazeMenuBar() { } EndPopup(); } + + if (show_resource_label_manager) { + rom()->resource_label()->DisplayLabels(&show_resource_label_manager); + if (current_project_.project_opened_ && + !current_project_.labels_filename_.empty()) { + current_project_.labels_filename_ = rom()->resource_label()->filename_; + } + } } void EditorManager::LoadRom() { - if (flags()->kNewFileDialogWrapper) { - auto file_name = FileDialogWrapper::ShowOpenFileDialog(); - PRINT_IF_ERROR(rom()->LoadFromFile(file_name)); + auto file_name = FileDialogWrapper::ShowOpenFileDialog(); + auto load_rom = rom()->LoadFromFile(file_name); + if (load_rom.ok()) { static RecentFilesManager manager("recent_files.txt"); manager.Load(); manager.AddFile(file_name); manager.Save(); - } else { - ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Open ROM", - ".sfc,.smc", "."); } } diff --git a/src/app/editor/editor_manager.h b/src/app/editor/editor_manager.h index e0afefc7..4ef14728 100644 --- a/src/app/editor/editor_manager.h +++ b/src/app/editor/editor_manager.h @@ -30,7 +30,7 @@ #include "app/gui/icons.h" #include "app/gui/input.h" #include "app/rom.h" -#include "ext/extension.h" +#include "base/extension.h" #include "imgui/imgui.h" #include "imgui/misc/cpp/imgui_stdlib.h" #include "imgui_memory_editor.h" diff --git a/src/app/editor/system/extension_manager.cc b/src/app/editor/system/extension_manager.cc index f70cf7d3..f48fdecf 100644 --- a/src/app/editor/system/extension_manager.cc +++ b/src/app/editor/system/extension_manager.cc @@ -1,13 +1,49 @@ #include "extension_manager.h" +#include + +#include #include -#include "ext/extension.h" +#include "base/extension.h" namespace yaze { namespace app { namespace editor { +void ExtensionManager::LoadExtension(const std::string& filename, + yaze_editor_context* context) { + auto extension_path = filename.c_str(); + void* handle = dlopen(extension_path, RTLD_LAZY); + if (!handle) { + std::cerr << "Cannot open extension: " << dlerror() << std::endl; + return; + } + dlerror(); // Clear any existing error + + // Load the symbol to retrieve the extension + auto get_extension = reinterpret_cast( + dlsym(handle, "get_yaze_extension")); + const char* dlsym_error = dlerror(); + if (dlsym_error) { + std::cerr << "Cannot load symbol 'get_yaze_extension': " << dlsym_error + << std::endl; + dlclose(handle); + return; + } + + yaze_extension* extension = get_extension(); + if (extension && extension->initialize) { + extension->initialize(context); + } else { + std::cerr << "Failed to initialize the extension." << std::endl; + dlclose(handle); + return; + } + + extensions_.push_back(extension); +} + void ExtensionManager::RegisterExtension(yaze_extension* extension) { extensions_.push_back(extension); } @@ -22,6 +58,12 @@ void ExtensionManager::ShutdownExtensions() { for (auto& extension : extensions_) { extension->cleanup(); } + + // if (handle) { + // dlclose(handle); + // handle = nullptr; + // extension = nullptr; + // } } void ExtensionManager::ExecuteExtensionUI(yaze_editor_context* context) { diff --git a/src/app/editor/system/extension_manager.h b/src/app/editor/system/extension_manager.h index 94e64352..54ec0cf3 100644 --- a/src/app/editor/system/extension_manager.h +++ b/src/app/editor/system/extension_manager.h @@ -3,7 +3,7 @@ #include -#include "ext/extension.h" +#include "base/extension.h" namespace yaze { namespace app { @@ -11,6 +11,7 @@ namespace editor { class ExtensionManager { public: + void LoadExtension(const std::string& filename, yaze_editor_context* context); void RegisterExtension(yaze_extension* extension); void InitializeExtensions(yaze_editor_context* context); void ShutdownExtensions(); diff --git a/src/ext/extension.h b/src/base/extension.h similarity index 85% rename from src/ext/extension.h rename to src/base/extension.h index 62ce06d4..2599906c 100644 --- a/src/ext/extension.h +++ b/src/base/extension.h @@ -84,26 +84,6 @@ typedef struct yaze_extension { } yaze_extension; -/** - * @brief Get the extension interface. - */ -yaze_extension* get_yaze_extension(); - -/** - * @brief Load a C extension. - */ -void yaze_load_c_extension(const char* extension_path, yaze_editor_context* context); - -/** - * @brief Load a Python extension. - */ -void yaze_load_py_extension(const char* script_path); - -/** - * @brief Clean up the extension. - */ -void yaze_cleanup_extension(); - #ifdef __cplusplus } #endif diff --git a/src/ext/sample.c b/src/base/extension_sample.c similarity index 98% rename from src/ext/sample.c rename to src/base/extension_sample.c index 62a6933f..1abb6265 100644 --- a/src/ext/sample.c +++ b/src/base/extension_sample.c @@ -1,6 +1,6 @@ #include -#include "ext/extension.h" +#include "base/extension.h" #include "yaze.h" void my_extension_initialize(yaze_editor_context* context) { diff --git a/src/ext/ext.cmake b/src/ext/ext.cmake deleted file mode 100644 index 7ea54090..00000000 --- a/src/ext/ext.cmake +++ /dev/null @@ -1,35 +0,0 @@ -add_library( - yaze_ext - ext/extension.cc -) - -target_include_directories( - yaze_ext PUBLIC - lib/ - app/ - ${CMAKE_SOURCE_DIR}/src/ - ${Boost_INCLUDE_DIRS} -) - -target_link_libraries( - yaze_ext PUBLIC - ${PYTHON_LIBRARIES} - Boost::python3 -) - -# C Sample -add_library( - yaze_ext_c SHARED - ext/sample.c -) - -target_include_directories( - yaze_ext_c PUBLIC - lib/ - ${CMAKE_SOURCE_DIR}/src/ -) - -target_link_libraries( - yaze_ext_c PUBLIC - yaze_ext -) \ No newline at end of file diff --git a/src/ext/extension.cc b/src/ext/extension.cc deleted file mode 100644 index 82519fbe..00000000 --- a/src/ext/extension.cc +++ /dev/null @@ -1,55 +0,0 @@ -#include "extension.h" - -#include - -#include - -static void* handle = nullptr; -static yaze_extension* extension = nullptr; - -yaze_extension* get_yaze_extension() { return extension; } - -void yaze_load_c_extension(const char* extension_path, yaze_editor_context* context) { - handle = dlopen(extension_path, RTLD_LAZY); - if (!handle) { - std::cerr << "Cannot open extension: " << dlerror() << std::endl; - return; - } - dlerror(); // Clear any existing error - - // Load the symbol to retrieve the extension - auto get_extension = reinterpret_cast( - dlsym(handle, "get_yaze_extension")); - const char* dlsym_error = dlerror(); - if (dlsym_error) { - std::cerr << "Cannot load symbol 'get_yaze_extension': " << dlsym_error - << std::endl; - dlclose(handle); - return; - } - - extension = get_extension(); - if (extension && extension->initialize) { - extension->initialize(context); - } else { - std::cerr << "Failed to initialize the extension." << std::endl; - dlclose(handle); - handle = nullptr; - extension = nullptr; - } -} - -void yaze_load_py_extension(const char* script_path) { - // TODO: Use Boost.Python to load Python extensions -} - -void yaze_cleanup_extension() { - if (extension && extension->cleanup) { - extension->cleanup(); - } - if (handle) { - dlclose(handle); - handle = nullptr; - extension = nullptr; - } -} \ No newline at end of file diff --git a/src/ext/sample.py b/src/ext/sample.py deleted file mode 100644 index 11aad62e..00000000 --- a/src/ext/sample.py +++ /dev/null @@ -1,46 +0,0 @@ -import yaze - -class YazePyExtension: - def __init__(self): - self.name = "My Python Extension" - self.version = "1.0" - - def initialize(self, context): - print(f"{self.name} Initialized with context: {context}") - self.context = context - self.register_event_hooks() - - def cleanup(self): - print(f"{self.name} Cleaned Up") - - def manipulate_rom(self, rom): - if rom and rom.data: - print(f"First byte of ROM: 0x{rom.data[0]:02X}") - else: - print("ROM data is not loaded.") - - def extend_ui(self, context): - import imgui - if imgui.begin("My Python Extension Window"): - imgui.text("Hello from My Python Extension!") - imgui.end() - - def register_commands(self): - # Register custom commands here - print(f"{self.name} Commands Registered") - - def register_custom_tools(self): - # Register custom tools here - print(f"{self.name} Custom Tools Registered") - - def on_rom_loaded(self): - print("ROM has been loaded!") - - def register_event_hooks(self): - # Register event hooks, like for ROM loaded - self.context.register_event_hooks( - YAZE_EVENT_ROM_LOADED, self.on_rom_loaded) - - -def get_yaze_extension(): - return YazePyExtension()