| 233 | //------------------------------------------- simulation ------------------------------------------- |
| 234 | |
| 235 | mjModel *LoadModel(const char *file, mj::Simulate &sim) |
| 236 | { |
| 237 | // this copy is needed so that the mju::strlen call below compiles |
| 238 | char filename[mj::Simulate::kMaxFilenameLength]; |
| 239 | mju::strcpy_arr(filename, file); |
| 240 | |
| 241 | // make sure filename is not empty |
| 242 | if (!filename[0]) |
| 243 | { |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
| 247 | // load and compile |
| 248 | char loadError[kErrorLength] = ""; |
| 249 | mjModel *mnew = 0; |
| 250 | if (mju::strlen_arr(filename) > 4 && |
| 251 | !std::strncmp(filename + mju::strlen_arr(filename) - 4, ".mjb", |
| 252 | mju::sizeof_arr(filename) - mju::strlen_arr(filename) + 4)) |
| 253 | { |
| 254 | mnew = mj_loadModel(filename, nullptr); |
| 255 | if (!mnew) |
| 256 | { |
| 257 | mju::strcpy_arr(loadError, "could not load binary model"); |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | mnew = mj_loadXML(filename, nullptr, loadError, kErrorLength); |
| 263 | // remove trailing newline character from loadError |
| 264 | if (loadError[0]) |
| 265 | { |
| 266 | int error_length = mju::strlen_arr(loadError); |
| 267 | if (loadError[error_length - 1] == '\n') |
| 268 | { |
| 269 | loadError[error_length - 1] = '\0'; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | mju::strcpy_arr(sim.load_error, loadError); |
| 275 | |
| 276 | if (!mnew) |
| 277 | { |
| 278 | std::printf("%s\n", loadError); |
| 279 | return nullptr; |
| 280 | } |
| 281 | |
| 282 | // compiler warning: print and pause |
| 283 | if (loadError[0]) |
| 284 | { |
| 285 | // mj_forward() below will print the warning message |
| 286 | std::printf("Model compiled, but simulation warning (paused):\n %s\n", loadError); |
| 287 | sim.run = 0; |
| 288 | } |
| 289 | |
| 290 | return mnew; |
| 291 | } |
| 292 |
no test coverage detected