| 370 | } |
| 371 | |
| 372 | void World::LoadFromFile(const std::string& filename) |
| 373 | { |
| 374 | m_CurrentPath = filename; |
| 375 | |
| 376 | std::ifstream file(filename); |
| 377 | if (!file.is_open()) { |
| 378 | TN_ERROR("Could not open '%s' for reading.", filename.c_str()); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | nlohmann::json root; |
| 383 | try { |
| 384 | file >> root; |
| 385 | } catch (const nlohmann::json::exception& e) { |
| 386 | TN_ERROR("JSON parse error in '%s': %s", filename.c_str(), e.what()); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | int version = root.value("version", 0); |
| 391 | if (version != 1) |
| 392 | TN_WARN("Unknown world version %d, attempting load anyway.", version); |
| 393 | |
| 394 | m_Name = root.value("name", "World"); |
| 395 | |
| 396 | // Load DLL modules before deserializing actors. |
| 397 | if (root.contains("modules")) { |
| 398 | for (const auto& mod : root["modules"]) { |
| 399 | std::string modName = mod.value("name", ""); |
| 400 | std::string modPath = mod.value("path", ""); |
| 401 | if (!modName.empty() && !modPath.empty() && !ScriptModuleManager::Get().IsLoaded(modName)) |
| 402 | ScriptModuleManager::Get().Load(modName, modPath); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | Clear(); |
| 407 | IDGenerator::Get().Clear(); |
| 408 | |
| 409 | if (!root.contains("actors")) return; |
| 410 | |
| 411 | // Pass 1: Spawn actors flat, deserialize components, inject saved IDs. |
| 412 | struct ActorEntry { |
| 413 | Actor* actor; |
| 414 | uint64 parentId; |
| 415 | }; |
| 416 | std::vector<ActorEntry> entries; |
| 417 | |
| 418 | for (const auto& actorJson : root["actors"]) { |
| 419 | uint64 savedId = std::stoull(actorJson.value("id", "0")); |
| 420 | std::string actorName = actorJson.value("name", "Actor"); |
| 421 | bool actorActive = actorJson.value("active", true); |
| 422 | |
| 423 | // Construct actor directly to avoid SpawnActor's auto-OnInit. |
| 424 | auto actorPtr = std::make_shared<Actor>(this, actorName); |
| 425 | Actor* actor = actorPtr.get(); |
| 426 | |
| 427 | // Replace the auto-generated ID with the saved one. |
| 428 | IDGenerator::Get().Release(actor->GetID()); |
| 429 | IDGenerator::Get().Reserve(savedId); |
no test coverage detected