extensions cleanup

This commit is contained in:
scawful
2024-08-20 22:40:14 -04:00
parent 7b33313281
commit d85530b14b
11 changed files with 70 additions and 250 deletions

View File

@@ -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()

View File

@@ -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", ".");
}
}

View File

@@ -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"

View File

@@ -1,13 +1,49 @@
#include "extension_manager.h"
#include <dlfcn.h>
#include <iostream>
#include <vector>
#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<yaze_extension* (*)()>(
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) {

View File

@@ -3,7 +3,7 @@
#include <vector>
#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();

View File

@@ -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

View File

@@ -1,6 +1,6 @@
#include <stdio.h>
#include "ext/extension.h"
#include "base/extension.h"
#include "yaze.h"
void my_extension_initialize(yaze_editor_context* context) {

View File

@@ -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
)

View File

@@ -1,55 +0,0 @@
#include "extension.h"
#include <dlfcn.h>
#include <iostream>
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<yaze_extension* (*)()>(
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;
}
}

View File

@@ -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()