| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "engine/engine_plugin.h" |
| |
|
| | #include <cctype> |
| | #include <cstdio> |
| | #include <cstring> |
| | #include <memory> |
| | #include <new> |
| | #include <string> |
| | #include <string_view> |
| |
|
| | extern "C" { |
| | #if defined(_WIN32) || defined(__CYGWIN__) |
| | #include <windows.h> |
| | #else |
| | #include <dirent.h> |
| | #include <dlfcn.h> |
| | #endif |
| | } |
| |
|
| | #include <mujoco/mjplugin.h> |
| | #include "engine/engine_global_table.h" |
| | #include "engine/engine_util_errmem.h" |
| |
|
| | |
| | void mjp_defaultPlugin(mjpPlugin* plugin) { |
| | std::memset(plugin, 0, sizeof(*plugin)); |
| | } |
| |
|
| | namespace { |
| | using mujoco::GlobalTable; |
| |
|
| | constexpr int kMaxNameLength = 1024; |
| | constexpr int kMaxAttributes = 255; |
| |
|
| | |
| | int strklen(const char* s) { |
| | for (int i = 0; i < kMaxNameLength; ++i) { |
| | if (!s[i]) { |
| | return i; |
| | } |
| | } |
| | return -1; |
| | } |
| |
|
| | |
| | std::unique_ptr<char[]> CopyName(const char* s) { |
| | int len = strklen(s); |
| | if (len == -1) { |
| | return nullptr; |
| | } |
| | std::unique_ptr<char[]> out(new(std::nothrow) char[len + 1]); |
| | if (!out) { |
| | return nullptr; |
| | } |
| | std::strncpy(out.get(), s, len); |
| | out.get()[len] = '\0'; |
| | return out; |
| | } |
| |
|
| | |
| | bool IsValidURISchemeFormat(const char* prefix) { |
| | int len; |
| |
|
| | |
| | if (prefix == nullptr || !(len = std::strlen(prefix))) { |
| | return false; |
| | } |
| |
|
| | |
| | if (!std::isalpha(prefix[0])) { |
| | return false; |
| | } |
| |
|
| | for (int i = 1; i < len; i++) { |
| | |
| | if (!std::isalnum(prefix[i]) && |
| | (prefix[i] != '+') && |
| | (prefix[i] != '.') && |
| | (prefix[i] != '-')) { |
| | return false; |
| | } |
| | } |
| | return true; |
| | } |
| |
|
| | |
| | const char* PluginAttrSeek(const mjModel* m, int plugin_id, int attrib_id) { |
| | const char* ptr = m->plugin_attr + m->plugin_attradr[plugin_id]; |
| | for (int i = 0; i < attrib_id; ++i) { |
| | while (*ptr) { |
| | ++ptr; |
| | } |
| | ++ptr; |
| | } |
| | return ptr; |
| | } |
| | } |
| |
|
| | template <> |
| | const char* GlobalTable<mjpPlugin>::HumanReadableTypeName() { |
| | return "plugin"; |
| | } |
| |
|
| | template <> |
| | std::string_view GlobalTable<mjpPlugin>::ObjectKey(const mjpPlugin& plugin) { |
| | return std::string_view(plugin.name, strklen(plugin.name)); |
| | } |
| |
|
| | |
| | template <> |
| | bool GlobalTable<mjpPlugin>::ObjectEqual(const mjpPlugin& plugin1, const mjpPlugin& plugin2) { |
| | if (plugin1.name && !plugin2.name) { |
| | return false; |
| | } |
| | if (plugin2.name && !plugin1.name) { |
| | return false; |
| | } |
| | if (plugin1.name && plugin2.name && |
| | std::strncmp(plugin1.name, plugin2.name, kMaxNameLength)) { |
| | return false; |
| | } |
| |
|
| | if (plugin1.nattribute != plugin2.nattribute) { |
| | return false; |
| | } |
| | for (int i = 0; i < plugin1.nattribute; ++i) { |
| | if (plugin1.attributes[i] && !plugin2.attributes[i]) { |
| | return false; |
| | } |
| | if (plugin1.attributes[i] && !plugin2.attributes[i]) { |
| | return false; |
| | } |
| | if (plugin1.attributes[i] && plugin2.attributes[i] && |
| | std::strncmp(plugin1.attributes[i], plugin2.attributes[i], |
| | kMaxNameLength)) { |
| | return false; |
| | } |
| | } |
| |
|
| | const char* ptr1 = reinterpret_cast<const char*>(&plugin1.attributes) + |
| | sizeof(plugin1.attributes); |
| | const char* ptr2 = reinterpret_cast<const char*>(&plugin2.attributes) + |
| | sizeof(plugin2.attributes); |
| | std::size_t remaining_size = |
| | sizeof(mjpPlugin) - (ptr1 - reinterpret_cast<const char*>(&plugin1)); |
| | return !std::memcmp(ptr1, ptr2, remaining_size); |
| | } |
| |
|
| | template <> |
| | bool GlobalTable<mjpPlugin>::CopyObject(mjpPlugin& dst, const mjpPlugin& src, ErrorMessage& err) { |
| | |
| | std::unique_ptr<char[]> name = CopyName(src.name); |
| | if (!name) { |
| | if (strklen(src.name) == -1) { |
| | std::snprintf(err, sizeof(err), |
| | "plugin->name length exceeds the maximum limit of %d", kMaxNameLength); |
| | } else { |
| | std::snprintf(err, sizeof(err), "failed to allocate memory for plugin name"); |
| | } |
| | return false; |
| | } |
| |
|
| | |
| | std::unique_ptr<std::unique_ptr<char[]>[]> attributes_list; |
| | if (src.nattribute) { |
| | attributes_list.reset(new(std::nothrow) std::unique_ptr<char[]>[src.nattribute]); |
| | if (!attributes_list) { |
| | std::snprintf(err, sizeof(err), "failed to allocate memory for plugin attribute list"); |
| | return false; |
| | } |
| | for (int i = 0; i < src.nattribute; ++i) { |
| | std::unique_ptr<char[]> attr = CopyName(src.attributes[i]); |
| | if (!attr) { |
| | if (strklen(src.attributes[i]) == -1) { |
| | std::snprintf( |
| | err, sizeof(err), |
| | "length of plugin attribute %d exceeds the maximum limit of %d", i, kMaxAttributes); |
| | } else { |
| | std::snprintf(err, sizeof(err), "failed to allocate memory for plugin attribute %d", i); |
| | } |
| | return false; |
| | } |
| | attributes_list[i].swap(attr); |
| | } |
| | } |
| |
|
| | |
| | const char** attributes = nullptr; |
| | if (src.nattribute) { |
| | attributes = new(std::nothrow) const char*[src.nattribute]; |
| | if (!attributes) { |
| | std::snprintf(err, sizeof(err), "failed to allocate memory for plugin attribute array"); |
| | return -1; |
| | } |
| | for (int i = 0; i < src.nattribute; ++i) { |
| | attributes[i] = attributes_list[i].release(); |
| | } |
| | } |
| |
|
| | dst = src; |
| | dst.name = name.release(); |
| | dst.attributes = attributes; |
| |
|
| | return true; |
| | } |
| |
|
| | template <> |
| | const char* GlobalTable<mjpResourceProvider>::HumanReadableTypeName() { |
| | return "resource provider"; |
| | } |
| |
|
| | template <> |
| | std::string_view GlobalTable<mjpResourceProvider>::ObjectKey(const mjpResourceProvider& plugin) { |
| | return std::string_view(plugin.prefix, strklen(plugin.prefix)); |
| | } |
| |
|
| | |
| | template <> |
| | bool GlobalTable<mjpResourceProvider>::ObjectEqual(const mjpResourceProvider& p1, const mjpResourceProvider& p2) { |
| | return (CaseInsensitiveEqual(p1.prefix, p2.prefix) && |
| | p1.open == p2.open && |
| | p1.read == p2.read && |
| | p1.close == p2.close && |
| | p1.getdir == p2.getdir && |
| | p1.modified == p2.modified && |
| | p1.data == p2.data); |
| | } |
| |
|
| | template <> |
| | bool GlobalTable<mjpResourceProvider>::CopyObject(mjpResourceProvider& dst, const mjpResourceProvider& src, ErrorMessage& err) { |
| | |
| | std::unique_ptr<char[]> prefix = CopyName(src.prefix); |
| | if (!prefix) { |
| | if (strklen(src.prefix) == -1) { |
| | std::snprintf(err, sizeof(err), |
| | "provider->prefix length exceeds the maximum limit of %d", kMaxNameLength); |
| | } else { |
| | std::snprintf(err, sizeof(err), "failed to allocate memory for resource provider prefix"); |
| | } |
| | return false; |
| | } |
| |
|
| | dst = src; |
| | dst.prefix = prefix.release(); |
| | return true; |
| | } |
| |
|
| | |
| | int mjp_registerPlugin(const mjpPlugin* plugin) { |
| | if (!plugin->name) { |
| | mju_error("plugin->name is a null pointer"); |
| | } else if (plugin->name[0] == '\0') { |
| | mju_error("plugin->name is an empty string"); |
| | } else if (plugin->nattribute < 0) { |
| | mju_error("plugin->nattribute is negative"); |
| | } else if (plugin->nattribute > kMaxAttributes) { |
| | mju_error("plugin->nattribute exceeds the maximum limit of %i", |
| | kMaxAttributes); |
| | } |
| |
|
| | return GlobalTable<mjpPlugin>::GetSingleton().AppendIfUnique(*plugin); |
| | } |
| |
|
| | |
| | const mjpPlugin* mjp_getPluginAtSlotUnsafe(int slot, int nslot) { |
| | return GlobalTable<mjpPlugin>::GetSingleton().GetAtSlotUnsafe(slot, nslot); |
| | } |
| |
|
| | |
| | const mjpPlugin* mjp_getPluginUnsafe(const char* name, int* slot, int nslot) { |
| | return GlobalTable<mjpPlugin>::GetSingleton().GetByKeyUnsafe(name, slot, nslot); |
| | } |
| |
|
| | |
| | int mjp_pluginCount() { |
| | return GlobalTable<mjpPlugin>::GetSingleton().count(); |
| | } |
| |
|
| | |
| | const mjpPlugin* mjp_getPluginAtSlot(int slot) { |
| | return GlobalTable<mjpPlugin>::GetSingleton().GetAtSlot(slot); |
| | } |
| |
|
| | |
| | const mjpPlugin* mjp_getPlugin(const char* name, int* slot) { |
| | return GlobalTable<mjpPlugin>::GetSingleton().GetByKey(name, slot); |
| | } |
| |
|
| | |
| | |
| | const char* mj_getPluginConfig(const mjModel* m, int plugin_id, const char* attrib) { |
| | if (plugin_id < 0 || plugin_id >= m->nplugin || attrib == nullptr) { |
| | return nullptr; |
| | } |
| |
|
| | const mjpPlugin* plugin = mjp_getPluginAtSlot(m->plugin[plugin_id]); |
| | if (!plugin) { |
| | return nullptr; |
| | } |
| |
|
| | for (int i = 0; i < plugin->nattribute; ++i) { |
| | if (std::strcmp(plugin->attributes[i], attrib) == 0) { |
| | return PluginAttrSeek(m, plugin_id, i); |
| | } |
| | } |
| |
|
| | return nullptr; |
| | } |
| |
|
| | |
| | void mjp_defaultResourceProvider(mjpResourceProvider* provider) { |
| | std::memset(provider, 0, sizeof(*provider)); |
| | } |
| |
|
| | |
| | int mjp_registerResourceProvider(const mjpResourceProvider* provider) { |
| | |
| | if (!IsValidURISchemeFormat(provider->prefix)) { |
| | mju_warning("provider->prefix is '%s' which is not a valid URI scheme format", |
| | provider->prefix); |
| | return -1; |
| | } |
| |
|
| | if (!provider->open || !provider->read || !provider->close) { |
| | mju_warning("provider must have the open, read, and close callbacks defined"); |
| | return -1; |
| | } |
| |
|
| | return GlobalTable<mjpResourceProvider>::GetSingleton().AppendIfUnique(*provider) + 1; |
| | } |
| |
|
| | |
| | int mjp_resourceProviderCount() { |
| | return GlobalTable<mjpResourceProvider>::GetSingleton().count(); |
| | } |
| |
|
| | |
| | const mjpResourceProvider* mjp_getResourceProvider(const char* resource_name) { |
| | if (!resource_name || !resource_name[0]) { |
| | return nullptr; |
| | } |
| |
|
| | const char* ch = std::strchr(resource_name, ':'); |
| | if (ch == nullptr) { |
| | return nullptr; |
| | } |
| |
|
| | int n = ch - resource_name; |
| | std::string file_prefix = std::string(resource_name, n); |
| |
|
| | |
| | if (!IsValidURISchemeFormat(file_prefix.c_str())) { |
| | return nullptr; |
| | } |
| |
|
| | return GlobalTable<mjpResourceProvider>::GetSingleton().GetByKey(file_prefix.c_str(), nullptr); |
| | } |
| |
|
| | |
| | const mjpResourceProvider* mjp_getResourceProviderAtSlot(int slot) { |
| | |
| | return GlobalTable<mjpResourceProvider>::GetSingleton().GetAtSlot(slot - 1); |
| | } |
| |
|
| | |
| | void mj_loadPluginLibrary(const char* path) { |
| | #if defined(_WIN32) || defined(__CYGWIN__) |
| | LoadLibraryA(path); |
| | #else |
| | void* handle = dlopen(path, RTLD_NOW | RTLD_LOCAL); |
| | if (!handle) { |
| | const char* error = dlerror(); |
| | if (error) { |
| | mju_error("Error loading plugin library '%s': %s\n", path, error); |
| | } else { |
| | mju_error("Unknown error loading plugin library '%s'\n", path); |
| | } |
| | } |
| | #endif |
| | } |
| |
|
| | |
| | void mj_loadAllPluginLibraries(const char* directory, |
| | mjfPluginLibraryLoadCallback callback) { |
| | auto load_dso_and_call_callback = [&](const std::string& filename, |
| | const std::string& dso_path) { |
| | int nplugin_before; |
| | int nplugin_after; |
| |
|
| | auto& plugin_table = GlobalTable<mjpPlugin>::GetSingleton(); |
| | { |
| | auto lock = plugin_table.LockExclusively(); |
| | nplugin_before = mjp_pluginCount(); |
| | mj_loadPluginLibrary(dso_path.c_str()); |
| | nplugin_after = mjp_pluginCount(); |
| | } |
| |
|
| | if (callback) { |
| | int count = nplugin_after - nplugin_before; |
| | int first = count ? nplugin_before : -1; |
| | callback(filename.c_str(), first, count); |
| | } |
| | }; |
| |
|
| | |
| | #if defined(_WIN32) || defined(__CYGWIN__) |
| | const std::string sep = "\\"; |
| | WIN32_FIND_DATAA find_data; |
| | HANDLE hfile = FindFirstFileA( |
| | (directory + sep + "*.dll").c_str(), &find_data); |
| | if (!hfile) { |
| | return; |
| | } |
| |
|
| | |
| | bool keep_going = true; |
| | while (keep_going) { |
| | const std::string name(find_data.cFileName); |
| | |
| | const std::string dso_path = directory + sep + name; |
| | load_dso_and_call_callback(name.c_str(), dso_path.c_str()); |
| | keep_going = FindNextFileA(hfile, &find_data); |
| | } |
| | FindClose(hfile); |
| | #else |
| | const std::string sep = "/"; |
| | #if defined(__APPLE__) |
| | const std::string dso_suffix = ".dylib"; |
| | #else |
| | const std::string dso_suffix = ".so"; |
| | #endif |
| |
|
| | DIR* dirp = opendir(directory); |
| | if (!dirp) { |
| | return; |
| | } |
| |
|
| | |
| | for (struct dirent* dp; (dp = readdir(dirp));) { |
| | |
| | if (dp->d_type == DT_REG) { |
| | const std::string name(dp->d_name); |
| | if (name.size() > dso_suffix.size() && |
| | name.substr(name.size() - dso_suffix.size()) == dso_suffix) { |
| | |
| | const std::string dso_path = directory + sep + name; |
| | load_dso_and_call_callback(name.c_str(), dso_path.c_str()); |
| | } |
| | } |
| | } |
| | closedir(dirp); |
| | #endif |
| | } |
| |
|