@@ -164,6 +164,14 @@ void PluginCommand::HighLevelILInstructionPluginCommandActionCallback(
164164}
165165
166166
167+ void PluginCommand::ProjectPluginCommandActionCallback (void *ctxt, BNProject* project)
168+ {
169+ RegisteredProjectCommand* cmd = (RegisteredProjectCommand*)ctxt;
170+ Ref<Project> projectObject = new Project (BNNewProjectReference (project));
171+ return cmd->action (projectObject);
172+ }
173+
174+
167175bool PluginCommand::DefaultPluginCommandIsValidCallback (void * ctxt, BNBinaryView* view)
168176{
169177 RegisteredDefaultCommand* cmd = (RegisteredDefaultCommand*)ctxt;
@@ -260,6 +268,14 @@ bool PluginCommand::HighLevelILInstructionPluginCommandIsValidCallback(
260268}
261269
262270
271+ bool PluginCommand::ProjectPluginCommandIsValidCallback (void * ctxt, BNProject* project)
272+ {
273+ RegisteredProjectCommand* cmd = (RegisteredProjectCommand*)ctxt;
274+ Ref<Project> projectObject = new Project (BNNewProjectReference (project));
275+ return cmd->isValid (projectObject);
276+ }
277+
278+
263279void PluginCommand::Register (
264280 const string& name, const string& description, const function<void (BinaryView* view)>& action)
265281{
@@ -452,6 +468,22 @@ void PluginCommand::RegisterForHighLevelILInstruction(const string& name, const
452468 HighLevelILInstructionPluginCommandActionCallback, HighLevelILInstructionPluginCommandIsValidCallback, cmd);
453469}
454470
471+ void PluginCommand::RegisterForProject (const std::string &name, const std::string &description,
472+ const std::function<void (Project *project)> &action)
473+ {
474+ RegisterForProject (name, description, action, [](Project*) { return true ; });
475+ }
476+
477+ void PluginCommand::RegisterForProject (const std::string &name, const std::string &description,
478+ const std::function<void (Project* project)> &action, const std::function<bool(Project* project)> &isValid)
479+ {
480+ RegisteredProjectCommand* cmd = new RegisteredProjectCommand;
481+ cmd->action = action;
482+ cmd->isValid = isValid;
483+ BNRegisterPluginCommandForProject (name.c_str (), description.c_str (), ProjectPluginCommandActionCallback,
484+ ProjectPluginCommandIsValidCallback, cmd);
485+ }
486+
455487
456488vector<PluginCommand> PluginCommand::GetList ()
457489{
@@ -481,40 +513,41 @@ vector<PluginCommand> PluginCommand::GetValidList(const PluginCommandContext& ct
481513
482514bool PluginCommand::IsValid (const PluginCommandContext& ctxt) const
483515{
484- if (!ctxt.binaryView )
485- return false ;
486-
487516 switch (m_command.type )
488517 {
489518 case DefaultPluginCommand:
519+ if (!ctxt.binaryView )
520+ return false ;
490521 if (!m_command.defaultIsValid )
491522 return true ;
492523 return m_command.defaultIsValid (m_command.context , ctxt.binaryView ->GetObject ());
493524 case AddressPluginCommand:
525+ if (!ctxt.binaryView )
526+ return false ;
494527 if (!m_command.addressIsValid )
495528 return true ;
496529 return m_command.addressIsValid (m_command.context , ctxt.binaryView ->GetObject (), ctxt.address );
497530 case RangePluginCommand:
498- if (ctxt.length == 0 )
531+ if (ctxt.length == 0 || !ctxt. binaryView )
499532 return false ;
500533 if (!m_command.rangeIsValid )
501534 return true ;
502535 return m_command.rangeIsValid (m_command.context , ctxt.binaryView ->GetObject (), ctxt.address , ctxt.length );
503536 case FunctionPluginCommand:
504- if (!ctxt.function )
537+ if (!ctxt.function || !ctxt. binaryView )
505538 return false ;
506539 if (!m_command.functionIsValid )
507540 return true ;
508541 return m_command.functionIsValid (m_command.context , ctxt.binaryView ->GetObject (), ctxt.function ->GetObject ());
509542 case LowLevelILFunctionPluginCommand:
510- if (!ctxt.lowLevelILFunction )
543+ if (!ctxt.lowLevelILFunction || !ctxt. binaryView )
511544 return false ;
512545 if (!m_command.lowLevelILFunctionIsValid )
513546 return true ;
514547 return m_command.lowLevelILFunctionIsValid (
515548 m_command.context , ctxt.binaryView ->GetObject (), ctxt.lowLevelILFunction ->GetObject ());
516549 case LowLevelILInstructionPluginCommand:
517- if (!ctxt.lowLevelILFunction )
550+ if (!ctxt.lowLevelILFunction || !ctxt. binaryView )
518551 return false ;
519552 if (ctxt.instrIndex == BN_INVALID_EXPR)
520553 return false ;
@@ -523,14 +556,14 @@ bool PluginCommand::IsValid(const PluginCommandContext& ctxt) const
523556 return m_command.lowLevelILInstructionIsValid (
524557 m_command.context , ctxt.binaryView ->GetObject (), ctxt.lowLevelILFunction ->GetObject (), ctxt.instrIndex );
525558 case MediumLevelILFunctionPluginCommand:
526- if (!ctxt.mediumLevelILFunction )
559+ if (!ctxt.mediumLevelILFunction || !ctxt. binaryView )
527560 return false ;
528561 if (!m_command.mediumLevelILFunctionIsValid )
529562 return true ;
530563 return m_command.mediumLevelILFunctionIsValid (
531564 m_command.context , ctxt.binaryView ->GetObject (), ctxt.mediumLevelILFunction ->GetObject ());
532565 case MediumLevelILInstructionPluginCommand:
533- if (!ctxt.mediumLevelILFunction )
566+ if (!ctxt.mediumLevelILFunction || !ctxt. binaryView )
534567 return false ;
535568 if (ctxt.instrIndex == BN_INVALID_EXPR)
536569 return false ;
@@ -539,21 +572,27 @@ bool PluginCommand::IsValid(const PluginCommandContext& ctxt) const
539572 return m_command.mediumLevelILInstructionIsValid (
540573 m_command.context , ctxt.binaryView ->GetObject (), ctxt.mediumLevelILFunction ->GetObject (), ctxt.instrIndex );
541574 case HighLevelILFunctionPluginCommand:
542- if (!ctxt.highLevelILFunction )
575+ if (!ctxt.highLevelILFunction || !ctxt. binaryView )
543576 return false ;
544577 if (!m_command.highLevelILFunctionIsValid )
545578 return true ;
546579 return m_command.highLevelILFunctionIsValid (
547580 m_command.context , ctxt.binaryView ->GetObject (), ctxt.highLevelILFunction ->GetObject ());
548581 case HighLevelILInstructionPluginCommand:
549- if (!ctxt.highLevelILFunction )
582+ if (!ctxt.highLevelILFunction || !ctxt. binaryView )
550583 return false ;
551584 if (ctxt.instrIndex == BN_INVALID_EXPR)
552585 return false ;
553586 if (!m_command.highLevelILInstructionIsValid )
554587 return true ;
555588 return m_command.highLevelILInstructionIsValid (
556589 m_command.context , ctxt.binaryView ->GetObject (), ctxt.highLevelILFunction ->GetObject (), ctxt.instrIndex );
590+ case ProjectPluginCommand:
591+ if (!m_command.projectIsValid )
592+ return true ;
593+ if (!ctxt.project )
594+ return false ;
595+ return m_command.projectIsValid (m_command.context , ctxt.project ->GetObject ());
557596 default :
558597 return false ;
559598 }
@@ -603,6 +642,9 @@ void PluginCommand::Execute(const PluginCommandContext& ctxt) const
603642 m_command.highLevelILInstructionCommand (
604643 m_command.context , ctxt.binaryView ->GetObject (), ctxt.highLevelILFunction ->GetObject (), ctxt.instrIndex );
605644 break ;
645+ case ProjectPluginCommand:
646+ m_command.projectCommand (m_command.context , ctxt.project ->GetObject ());
647+ break ;
606648 default :
607649 break ;
608650 }
0 commit comments