diff --git a/Release/include/XNCore/XNModelObject.h b/Release/include/XNCore/XNModelObject.h index 2f7153f..b35b4cb 100644 --- a/Release/include/XNCore/XNModelObject.h +++ b/Release/include/XNCore/XNModelObject.h @@ -169,6 +169,18 @@ public: */ void SetSetFreq(double setFreq); + /** + * @brief 获取模型动态库路径 + * @return const std::string&: 模型动态库路径 + */ + const std::string &GetLibPath(); + + /** + * @brief 设置模型动态库路径 + * @param sLibPath: std::string类型,模型动态库路径 + */ + void SetLibPath(const std::string &sLibPath); + /** * @brief 单步执行函数 * @details 模型默认的周期性执行函数 diff --git a/XNCore/XNModelManager.cpp b/XNCore/XNModelManager.cpp index b4008fd..6c1401e 100755 --- a/XNCore/XNModelManager.cpp +++ b/XNCore/XNModelManager.cpp @@ -78,11 +78,12 @@ void XNModelManager::LoadModel(const std::string &modelPath, const std::string & model->SetFramework(GetFramework()); model->SetInitializeType(initialType); model->SetThreadID(threadID); + std::filesystem::path parentPath = std::filesystem::path(modelPath).parent_path(); + model->SetLibPath(parentPath.string()); if (initialType == 0) { // 使用std::filesystem处理路径 std::filesystem::path configPath = - std::filesystem::path(modelPath).parent_path() - / (className + "_V" + modelVersion + ".mcfg"); + parentPath / (className + "_V" + modelVersion + ".mcfg"); model->SetXmlPath(configPath.string()); } else if (initialType == 1) { model->SetXmlPath(planeName + "," + className + "," + modelVersion); diff --git a/XNCore/XNModelObject.cpp b/XNCore/XNModelObject.cpp index e697210..c744f86 100755 --- a/XNCore/XNModelObject.cpp +++ b/XNCore/XNModelObject.cpp @@ -192,6 +192,18 @@ void XNModelObject::SetSetFreq(double setFreq) d->_setFreq = setFreq; } +const std::string &XNModelObject::GetLibPath() +{ + T_D(); + return d->_sLibPath; +} + +void XNModelObject::SetLibPath(const std::string &sLibPath) +{ + T_D(); + d->_sLibPath = sLibPath; +} + // 初始化函数 void XNModelObject::Initialize() { diff --git a/XNCore/XNModelObject.h b/XNCore/XNModelObject.h index 2f7153f..b35b4cb 100755 --- a/XNCore/XNModelObject.h +++ b/XNCore/XNModelObject.h @@ -169,6 +169,18 @@ public: */ void SetSetFreq(double setFreq); + /** + * @brief 获取模型动态库路径 + * @return const std::string&: 模型动态库路径 + */ + const std::string &GetLibPath(); + + /** + * @brief 设置模型动态库路径 + * @param sLibPath: std::string类型,模型动态库路径 + */ + void SetLibPath(const std::string &sLibPath); + /** * @brief 单步执行函数 * @details 模型默认的周期性执行函数 diff --git a/XNCore/XNScenarioManager.cpp b/XNCore/XNScenarioManager.cpp index 468fdbd..6f0a422 100755 --- a/XNCore/XNScenarioManager.cpp +++ b/XNCore/XNScenarioManager.cpp @@ -124,7 +124,7 @@ bool XNScenarioManager::ParseScenarioXml(const std::string &XmlPath) std::string libName = service->Attribute("ClassName"); std::string serviceVersion = service->Attribute("Version"); libName = XNSim::getFileNameWithoutExt(libName); - std::string dynamicLibName = servicePath + "lib" + libName + ".so." + serviceVersion; + std::string dynamicLibName = servicePath + "/lib" + libName + ".so." + serviceVersion; // 加载动态库 GetFramework()->GetServiceManager()->LoadService(dynamicLibName, libName, serviceVersion, 0); @@ -177,7 +177,7 @@ bool XNScenarioManager::ParseScenarioXml(const std::string &XmlPath) std::string libName = model->Attribute("ClassName"); std::string modelVersion = model->Attribute("Version"); libName = XNSim::getFileNameWithoutExt(libName); - std::string dynamicLibName = modelPath + "lib" + libName + ".so." + modelVersion; + std::string dynamicLibName = modelPath + "/lib" + libName + ".so." + modelVersion; // 加载动态库 GetFramework()->GetModelManager()->LoadModel(dynamicLibName, libName, modelVersion, planeName, 0, threadID); @@ -195,12 +195,12 @@ bool XNScenarioManager::ParseConfig(const std::string &ConfigID) { T_D(); // 获取数据库路径 - std::string dbPath = std::getenv("XNCore"); - if (dbPath.empty()) { + std::string XNCorePath = std::getenv("XNCore"); + if (XNCorePath.empty()) { LOG_ERROR("0x1015 未设置XNCore环境变量, 引擎将退出!"); return false; } - dbPath += "/database/XNSim.db"; + std::string dbPath = XNCorePath + "/database/XNSim.db"; // 打开数据库 sqlite3 *db; @@ -239,15 +239,31 @@ bool XNScenarioManager::ParseConfig(const std::string &ConfigID) std::string version = XNSim::getStringFromSqlite3(stmt, 4); std::string kernel = XNSim::getStringFromSqlite3(stmt, 5); std::string rootPath = XNSim::getStringFromSqlite3(stmt, 7); + if (rootPath.empty()) { + LOG_WARNING("0x1020 未设置工作目录,使用默认工作目录: %1", XNCorePath); + rootPath = XNCorePath; + } GetFramework()->SetWorkPath(rootPath); // 设置模型库目录 std::string modelPath = rootPath + XNSim::getStringFromSqlite3(stmt, 8); + if (modelPath.empty()) { + LOG_WARNING("0x1020 未设置模型库目录,使用默认模型库目录: %1/Models", XNCorePath); + modelPath = XNCorePath + "/Models"; + } GetFramework()->SetModelPath(modelPath); // 设置服务库目录 std::string servicePath = rootPath + XNSim::getStringFromSqlite3(stmt, 9); + if (servicePath.empty()) { + LOG_WARNING("0x1020 未设置服务库目录,使用默认服务库目录: %1/Services", XNCorePath); + servicePath = XNCorePath + "/Services"; + } GetFramework()->SetServicePath(servicePath); // 设置域ID uint32_t domainID = std::stoul(XNSim::getStringFromSqlite3(stmt, 10)); + if (domainID == 0 || domainID > 225) { + LOG_WARNING("0x1020 域ID设置错误,使用默认域ID: 10"); + domainID = 10; + } GetFramework()->GetDDSManager()->SetDomainID(domainID); // 读取CPU亲和性 std::string cpuAff = XNSim::getStringFromSqlite3(stmt, 6); @@ -279,7 +295,7 @@ bool XNScenarioManager::ParseConfig(const std::string &ConfigID) std::string ServiceName = XNSim::getStringFromSqlite3(servicesStmt, 3); ClassName = XNSim::getFileNameWithoutExt(ClassName); - std::string dynamicLibName = servicePath + "lib" + ClassName + ".so." + ServiceVersion; + std::string dynamicLibName = servicePath + "/lib" + ClassName + ".so." + ServiceVersion; // 加载动态库 GetFramework()->GetServiceManager()->LoadService(dynamicLibName, ClassName, ServiceVersion, 1); @@ -373,7 +389,7 @@ bool XNScenarioManager::ParseConfig(const std::string &ConfigID) std::string ModelName = XNSim::getStringFromSqlite3(modelsStmt, 3); ClassName = XNSim::getFileNameWithoutExt(ClassName); - std::string dynamicLibName = modelPath + "lib" + ClassName + ".so." + ModelVersion; + std::string dynamicLibName = modelPath + "/lib" + ClassName + ".so." + ModelVersion; // 加载动态库 LOG_INFO("0x1021 加载模型: %1", dynamicLibName); GetFramework()->GetModelManager()->LoadModel(dynamicLibName, ClassName, ModelVersion,