From 25cc02431b61097b2adfc2fbc493b648b0300c3b Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Mon, 10 Mar 2025 11:05:40 -0400 Subject: [SharedCache] Refactor Shared Cache In absence of a better name, this commit refactors the shared cache code. --- view/sharedcache/ui/dscpicker.cpp | 77 ++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 29 deletions(-) (limited to 'view/sharedcache/ui/dscpicker.cpp') diff --git a/view/sharedcache/ui/dscpicker.cpp b/view/sharedcache/ui/dscpicker.cpp index a82da4fc..c6448ff4 100644 --- a/view/sharedcache/ui/dscpicker.cpp +++ b/view/sharedcache/ui/dscpicker.cpp @@ -6,37 +6,56 @@ #include #include "progresstask.h" -#include - using namespace BinaryNinja; +using namespace SharedCacheAPI; void DisplayDSCPicker(UIContext* ctx, Ref dscView) { - BackgroundThread::create(ctx ? ctx->mainWindow() : nullptr)->thenBackground( - [dscView=dscView](QVariant var) { - QStringList entries; - Ref cache = new SharedCacheAPI::SharedCache(dscView); - - for (const auto& img : cache->GetAvailableImages()) - entries.push_back(QString::fromStdString(img)); - - return entries; - })->thenMainThread([ctx](QVariant var){ - QStringList entries = var.toStringList(); - - auto choiceDialog = new MetadataChoiceDialog(ctx ? ctx->mainWindow() : nullptr, "Pick Image", "Select", entries); - choiceDialog->AddWidthRequiredByItem(ctx, 300); - choiceDialog->AddHeightRequiredByItem(ctx, 150); - choiceDialog->exec(); - - if (choiceDialog->GetChosenEntry().has_value()) - return QVariant(QString::fromStdString(entries.at((qsizetype)choiceDialog->GetChosenEntry().value().idx).toStdString())); - else - return QVariant(""); - })->thenBackground([dscView=dscView](QVariant var){ - if (var.toString().isEmpty()) - return; - Ref cache = new SharedCacheAPI::SharedCache(dscView); - cache->LoadImageWithInstallName(var.toString().toStdString()); - })->start(); + static auto getImageNames = [dscView](QVariant var) { + auto controller = SharedCacheController::GetController(*dscView); + if (!controller) + return QStringList(); + + QStringList entries = {}; + for (const auto& img : controller->GetImages()) + entries.push_back(QString::fromStdString(img.name)); + return entries; + }; + + static auto getChosenImage = [ctx](QVariant var) { + QStringList entries = var.toStringList(); + + auto choiceDialog = new MetadataChoiceDialog(ctx ? ctx->mainWindow() : nullptr, "Pick Image", "Select", entries); + choiceDialog->AddWidthRequiredByItem(ctx, 300); + choiceDialog->AddHeightRequiredByItem(ctx, 150); + choiceDialog->exec(); + + if (!choiceDialog->GetChosenEntry().has_value()) + return QVariant(""); + + return QVariant(QString::fromStdString(entries.at((qsizetype)choiceDialog->GetChosenEntry().value().idx).toStdString())); + }; + + static auto loadSelectedImage = [dscView](QVariant var) { + auto selectedImageName = var.toString().toStdString(); + if (selectedImageName.empty()) + return; + + if (auto controller = SharedCacheController::GetController(*dscView)) + { + if (const auto selectedImage = controller->GetImageWithName( selectedImageName)) + { + controller->ApplyImage(*dscView, *selectedImage); + dscView->AddAnalysisOption("linearsweep"); + dscView->UpdateAnalysis(); + } + } + }; + + + BackgroundThread::create(ctx ? ctx->mainWindow() : nullptr) + ->thenBackground([](QVariant var){ return getImageNames(var); }) + ->thenMainThread([](QVariant var){ return getChosenImage(var); }) + ->thenBackground([](QVariant var){ return loadSelectedImage(var); }) + ->start(); } -- cgit v1.3.1