From a24c1436afc0b9b12bf9b0e8cf7b61e39ae03e4f Mon Sep 17 00:00:00 2001 From: erwincoumans Date: Thu, 11 Oct 2018 14:39:31 -0700 Subject: [PATCH] state 2 of FileIO plugin: adding/removing FileIO types, search through all registered FileIO types. (not enabled by default yet) Example: fileIO = p.loadPlugin("fileIOPlugin") print("fileIO=",fileIO) p.executePluginCommand(fileIO,"e:/develop/bullet3/data/plane.zip", [p.AddFileIOAction,p.ZipFileIO]) p.executePluginCommand(fileIO,"e:/develop/bullet3/data/test2.zip", [p.AddFileIOAction,p.ZipFileIO]) planeId = p.loadURDF("plane.urdf") duckId = p.loadURDF("duck_vhacd.urdf",[0,0,1]) --- .../ImportURDFDemo/BulletUrdfImporter.cpp | 7 +- examples/SharedMemory/SharedMemoryPublic.h | 14 + .../plugins/fileIOPlugin/fileIOPlugin.cpp | 311 +++++++++++++++--- .../plugins/fileIOPlugin/zipFileIO.h | 8 + examples/pybullet/pybullet.c | 7 + 5 files changed, 306 insertions(+), 41 deletions(-) diff --git a/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp b/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp index e1190bb40..80f86b5e4 100644 --- a/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp +++ b/examples/Importers/ImportURDFDemo/BulletUrdfImporter.cpp @@ -189,7 +189,12 @@ bool BulletURDFImporter::loadURDF(const char* fileName, bool forceFixedBase) BulletErrorLogger loggie; m_data->m_urdfParser.setParseSDF(false); - bool result = m_data->m_urdfParser.loadUrdf(xml_string.c_str(), &loggie, forceFixedBase, (m_data->m_flags & CUF_PARSE_SENSORS)); + bool result = false; + + if (xml_string.length()) + { + result = m_data->m_urdfParser.loadUrdf(xml_string.c_str(), &loggie, forceFixedBase, (m_data->m_flags & CUF_PARSE_SENSORS)); + } return result; } diff --git a/examples/SharedMemory/SharedMemoryPublic.h b/examples/SharedMemory/SharedMemoryPublic.h index e2c27ede1..1cab59bf8 100644 --- a/examples/SharedMemory/SharedMemoryPublic.h +++ b/examples/SharedMemory/SharedMemoryPublic.h @@ -909,4 +909,18 @@ enum eConstraintSolverTypes eConstraintSolverLCP_BLOCK_PGS, }; +enum eFileIOActions +{ + eAddFileIOAction = 1024,//avoid collision with eFileIOTypes + eRemoveFileIOAction, +}; + + +enum eFileIOTypes +{ + ePosixFileIO = 1, + eZipFileIO, + eCNSFileIO, +}; + #endif //SHARED_MEMORY_PUBLIC_H diff --git a/examples/SharedMemory/plugins/fileIOPlugin/fileIOPlugin.cpp b/examples/SharedMemory/plugins/fileIOPlugin/fileIOPlugin.cpp index 9b39ae469..d592ce48e 100644 --- a/examples/SharedMemory/plugins/fileIOPlugin/fileIOPlugin.cpp +++ b/examples/SharedMemory/plugins/fileIOPlugin/fileIOPlugin.cpp @@ -6,27 +6,209 @@ #include #include "../../../CommonInterfaces/CommonFileIOInterface.h" #include "../../../Utils/b3ResourcePath.h" + + +#ifndef B3_EXCLUDE_DEFAULT_FILEIO #include "../../../Utils/b3BulletDefaultFileIO.h" +#endif //B3_EXCLUDE_DEFAULT_FILEIO +#ifdef B3_USE_ZIPFILE_FILEIO #include "zipFileIO.h" +#endif //B3_USE_ZIPFILE_FILEIO + + +#ifdef B3_USE_CNS_FILEIO +#include "CNSFileIO.h" +#endif //B3_USE_CNS_FILEIO + +#define B3_MAX_FILEIO_INTERFACES 1024 + +struct WrapperFileHandle +{ + CommonFileIOInterface* childFileIO; + int m_childFileHandle; +}; + +struct WrapperFileIO : public CommonFileIOInterface +{ + CommonFileIOInterface* m_availableFileIOInterfaces[B3_MAX_FILEIO_INTERFACES]; + int m_numWrapperInterfaces; + + WrapperFileHandle m_wrapperFileHandles[B3_MAX_FILEIO_INTERFACES]; + + + WrapperFileIO() + :m_numWrapperInterfaces(0) + { + for (int i=0;i=0 && fileIOIndex=0) + { + //figure out what wrapper interface to use + //use the first one that can open the file + for (int i=0;ifileOpen(fileName, mode); + if (childHandle>=0) + { + m_wrapperFileHandles[wrapperFileHandle].childFileIO = childFileIO; + m_wrapperFileHandles[wrapperFileHandle].m_childFileHandle = childHandle; + break; + } + } + } + } + return wrapperFileHandle; + } + + virtual int fileRead(int fileHandle, char* destBuffer, int numBytes) + { + int fileReadResult=-1; + if (fileHandle>=0 && fileHandlefileRead( + m_wrapperFileHandles[fileHandle].m_childFileHandle, destBuffer, numBytes); + } + } + return fileReadResult; + } + + virtual int fileWrite(int fileHandle,const char* sourceBuffer, int numBytes) + { + //todo + return -1; + } + virtual void fileClose(int fileHandle) + { + int fileReadResult=-1; + if (fileHandle>=0 && fileHandlefileClose( + m_wrapperFileHandles[fileHandle].m_childFileHandle); + m_wrapperFileHandles[fileHandle].childFileIO = 0; + m_wrapperFileHandles[fileHandle].m_childFileHandle = -1; + } + } + } + virtual bool findResourcePath(const char* fileName, char* resourcePathOut, int resourcePathMaxNumBytes) + { + bool found = false; + for (int i=0;ifindResourcePath(fileName, resourcePathOut, resourcePathMaxNumBytes); + } + if (found) + break; + } + return found; + } + virtual char* readLine(int fileHandle, char* destBuffer, int numBytes) + { + char* result = 0; + + int fileReadResult=-1; + if (fileHandle>=0 && fileHandlereadLine( + m_wrapperFileHandles[fileHandle].m_childFileHandle, + destBuffer, numBytes); + } + } + return result; + } + virtual int getFileSize(int fileHandle) + { + int numBytes = 0; + + int fileReadResult=-1; + if (fileHandle>=0 && fileHandlegetFileSize( + m_wrapperFileHandles[fileHandle].m_childFileHandle); + } + } + return numBytes; + } + +}; -//#define B3_USE_ZLIB -#ifdef B3_USE_ZLIB -typedef ZipFileIO MyFileIO; -#else -typedef b3BulletDefaultFileIO MyFileIO; -#endif struct FileIOClass { int m_testData; - MyFileIO m_fileIO; + WrapperFileIO m_fileIO; FileIOClass() : m_testData(42), - m_fileIO()//"e:/develop/bullet3/data/plane.zip") + m_fileIO() { } virtual ~FileIOClass() @@ -38,49 +220,98 @@ B3_SHARED_API int initPlugin_fileIOPlugin(struct b3PluginContext* context) { FileIOClass* obj = new FileIOClass(); context->m_userPointer = obj; + +#ifndef B3_EXCLUDE_DEFAULT_FILEIO + obj->m_fileIO.addFileIOInterface(new b3BulletDefaultFileIO()); +#endif //B3_EXCLUDE_DEFAULT_FILEIO + + return SHARED_MEMORY_MAGIC_NUMBER; } B3_SHARED_API int executePluginCommand_fileIOPlugin(struct b3PluginContext* context, const struct b3PluginArguments* arguments) { - printf("text argument:%s\n", arguments->m_text); - printf("int args: ["); - for (int i = 0; i < arguments->m_numInts; i++) - { - printf("%d", arguments->m_ints[i]); - if ((i + 1) < arguments->m_numInts) - { - printf(","); - } - } - printf("]\nfloat args: ["); - for (int i = 0; i < arguments->m_numFloats; i++) - { - printf("%f", arguments->m_floats[i]); - if ((i + 1) < arguments->m_numFloats) - { - printf(","); - } - } - printf("]\n"); + int result=-1; FileIOClass* obj = (FileIOClass*)context->m_userPointer; - b3SharedMemoryStatusHandle statusHandle; - int statusType = -1; - int bodyUniqueId = -1; - - b3SharedMemoryCommandHandle command = - b3LoadUrdfCommandInit(context->m_physClient, arguments->m_text); - - statusHandle = b3SubmitClientCommandAndWaitStatus(context->m_physClient, command); - statusType = b3GetStatusType(statusHandle); - if (statusType == CMD_URDF_LOADING_COMPLETED) + printf("text argument:%s\n", arguments->m_text); + printf("int args: ["); + + //remove a fileIO type + if (arguments->m_numInts==1) { - bodyUniqueId = b3GetStatusBodyIndex(statusHandle); + int fileIOIndex = arguments->m_ints[0]; + obj->m_fileIO.removeFileIOInterface(fileIOIndex); } - return bodyUniqueId; + + if (arguments->m_numInts==2) + { + int action = arguments->m_ints[0]; + switch (action) + { + case eAddFileIOAction: + { + //create new fileIO interface + int fileIOType = arguments->m_ints[1]; + switch (fileIOType) + { + case ePosixFileIO: + { +#ifdef B3_EXCLUDE_DEFAULT_FILEIO + printf("ePosixFileIO is not enabled in this build.\n"); +#else + obj->m_fileIO.addFileIOInterface(new b3BulletDefaultFileIO()); +#endif + break; + } + case eZipFileIO: + { +#ifdef B3_USE_ZIPFILE_FILEIO + if (arguments->m_text) + { + obj->m_fileIO.addFileIOInterface(new ZipFileIO(arguments->m_text)); + } +#else + printf("eZipFileIO is not enabled in this build.\n"); +#endif + break; + } + case eCNSFileIO: + { +#ifdef B3_USE_CNS_FILEIO + B3_USE_ZIPFILE_FILEIO + if (arguments->m_text) + { + obj->m_fileIO.addFileIOInterface(new CNSFileIO(arguments->m_text)); + } +#else//B3_USE_CNS_FILEIO + printf("CNSFileIO is not enabled in this build.\n"); +#endif //B3_USE_CNS_FILEIO + break; + } + default: + { + } + } + break; + } + case eRemoveFileIOAction: + + { + //remove fileIO interface + int fileIOIndex = arguments->m_ints[1]; + obj->m_fileIO.removeFileIOInterface(fileIOIndex); + break; + } + default: + { + printf("executePluginCommand_fileIOPlugin: unknown action\n"); + } + } + } + return result; } B3_SHARED_API struct CommonFileIOInterface* getFileIOFunc_fileIOPlugin(struct b3PluginContext* context) diff --git a/examples/SharedMemory/plugins/fileIOPlugin/zipFileIO.h b/examples/SharedMemory/plugins/fileIOPlugin/zipFileIO.h index e83c06c63..0df577052 100644 --- a/examples/SharedMemory/plugins/fileIOPlugin/zipFileIO.h +++ b/examples/SharedMemory/plugins/fileIOPlugin/zipFileIO.h @@ -29,7 +29,12 @@ struct ZipFileIO : public CommonFileIOInterface virtual ~ZipFileIO() { + for (int i=0;i