Skip to content

Commit

Permalink
Improve SimpleControllerItem to support additional controller directo…
Browse files Browse the repository at this point in the history
…ries in the plugin path
  • Loading branch information
s-nakaoka committed Mar 21, 2024
1 parent 2fbf12a commit 051c10f
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions src/BodyPlugin/SimpleControllerItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cnoid/FileUtil>
#include <cnoid/UTF8>
#include <cnoid/ConnectionSet>
#include <cnoid/PluginManager>
#include <cnoid/ProjectManager>
#include <cnoid/ItemManager>
#include <QLibrary>
Expand Down Expand Up @@ -90,7 +91,6 @@ class SimpleControllerItem::Impl : public SimulationSimpleControllerIO

std::string controllerModuleName;
std::string controllerModuleFilename;
filesystem::path controllerDirPath;
QLibrary controllerModule;
bool doReloading;
bool isSymbolExportEnabled;
Expand All @@ -109,6 +109,9 @@ class SimpleControllerItem::Impl : public SimulationSimpleControllerIO
Impl(SimpleControllerItem* self, const Impl& org);
~Impl();
void doCommonInitializationInConstructor();
bool checkIfControllerModuleInControllerDirectory(
const filesystem::path& modulePath, filesystem::path& out_controllerDirPath);
bool checkIfControllerModulePathExists(const filesystem::path& modulePath);
void setController(const std::string& name);
bool loadController();
bool configureController(BodyItem* bodyItem);
Expand Down Expand Up @@ -164,8 +167,6 @@ class SimpleControllerItem::Impl : public SimulationSimpleControllerIO
}


namespace {

MySimpleControllerConfig::MySimpleControllerConfig(SimpleControllerItem::Impl* impl)
: SimpleControllerConfig(impl),
impl(impl)
Expand All @@ -179,8 +180,6 @@ Referenced* MySimpleControllerConfig::bodyItem()
return impl->self->targetBodyItem();
}

}


void SimpleControllerItem::initializeClass(ExtensionManager* ext)
{
Expand Down Expand Up @@ -208,8 +207,6 @@ SimpleControllerItem::Impl::Impl(SimpleControllerItem* self)
doReloading = false;
isSymbolExportEnabled = false;

controllerDirPath = pluginDirPath() / "simplecontroller";

baseDirectoryType.setSymbol(NO_BASE_DIRECTORY, N_("None"));
baseDirectoryType.setSymbol(CONTROLLER_DIRECTORY, N_("Controller directory"));
baseDirectoryType.setSymbol(PROJECT_DIRECTORY, N_("Project directory"));
Expand All @@ -228,7 +225,6 @@ SimpleControllerItem::Impl::Impl(SimpleControllerItem* self, const Impl& org)
: self(self),
config(this),
controllerModuleName(org.controllerModuleName),
controllerDirPath(org.controllerDirPath),
baseDirectoryType(org.baseDirectoryType)
{
doCommonInitializationInConstructor();
Expand Down Expand Up @@ -296,6 +292,42 @@ SimpleController* SimpleControllerItem::controller()
}


bool SimpleControllerItem::Impl::checkIfControllerModuleInControllerDirectory
(const filesystem::path& modulePath, filesystem::path& out_controllerDirPath)
{
bool isInControllerDirectories = false;

for(auto& pluginDir : PluginManager::instance()->pluginDirectories()){
out_controllerDirPath = filesystem::path(fromUTF8(pluginDir)) / "simplecontroller";
if(modulePath.is_absolute()){
if(modulePath.parent_path() == out_controllerDirPath){
isInControllerDirectories = true;
break;
}
} else {
if(checkIfControllerModulePathExists(out_controllerDirPath / modulePath)){
isInControllerDirectories = true;
break;
}
}
}

return isInControllerDirectories;
}


bool SimpleControllerItem::Impl::checkIfControllerModulePathExists(const filesystem::path& modulePath)
{
if(filesystem::exists(modulePath)){
return true;
}
filesystem::path pathWithExtension = modulePath;
pathWithExtension += ".";
pathWithExtension += DLL_EXTENSION;
return filesystem::exists(pathWithExtension);
}


void SimpleControllerItem::setController(const std::string& name)
{
impl->setController(name);
Expand All @@ -312,7 +344,8 @@ void SimpleControllerItem::Impl::setController(const std::string& name)
filesystem::path modulePath(fromUTF8(name));
if(modulePath.is_absolute()){
baseDirectoryType.select(NO_BASE_DIRECTORY);
if(modulePath.parent_path() == controllerDirPath){
filesystem::path controllerDirPath;
if(checkIfControllerModuleInControllerDirectory(modulePath, controllerDirPath)){
baseDirectoryType.select(CONTROLLER_DIRECTORY);
modulePath = modulePath.filename();
} else {
Expand Down Expand Up @@ -347,7 +380,14 @@ bool SimpleControllerItem::Impl::loadController()
filesystem::path modulePath(fromUTF8(controllerModuleName));
if(!modulePath.is_absolute()){
if(baseDirectoryType.is(CONTROLLER_DIRECTORY)){
modulePath = controllerDirPath / modulePath;
for(auto& pluginDir : PluginManager::instance()->pluginDirectories()){
filesystem::path controllerDirPath = filesystem::path(fromUTF8(pluginDir)) / "simplecontroller";
filesystem::path moduleFullPath = controllerDirPath / modulePath;
if(checkIfControllerModulePathExists(moduleFullPath)){
modulePath = moduleFullPath;
break;
}
}
} else if(baseDirectoryType.is(PROJECT_DIRECTORY)){
string projectDir = ProjectManager::instance()->currentProjectDirectory();
if(!projectDir.empty()){
Expand Down Expand Up @@ -1080,7 +1120,13 @@ void SimpleControllerItem::Impl::doPutProperties(PutPropertyFunction& putPropert
{
FilePathProperty moduleProperty(controllerModuleName);
if(baseDirectoryType.is(CONTROLLER_DIRECTORY)){
moduleProperty.setBaseDirectory(toUTF8(controllerDirPath.string()));
filesystem::path modulePath(fromUTF8(controllerModuleName));
filesystem::path dirPath;
if(checkIfControllerModuleInControllerDirectory(modulePath, dirPath)){
moduleProperty.setBaseDirectory(toUTF8(dirPath.string()));
} else {
moduleProperty.setBaseDirectory(toUTF8((pluginDirPath() / "simplecontroller").string()));
}
} else if(baseDirectoryType.is(PROJECT_DIRECTORY)){
moduleProperty.setBaseDirectory(ProjectManager::instance()->currentProjectDirectory());
}
Expand Down

0 comments on commit 051c10f

Please sign in to comment.